线性规划VB求解
Rem 定义动态数组 Dim a() As Single, c() As Single, b() As Single, cb() As Single Dim aa() As Single, cba() As Single, xcb() As Integer, xb() As Integer Dim m As Integer, n As Integer, l As Integer, k As Integer, cc As Integer, cm As Integer, ka As Integer Dim qq As Single, tt As Single, z As Single Private Sub Command1_Click() Show n = Val(InputBox("请输入线性规划典范型方程变量的个数 N=?", "输入数据", 0)) m = Val(InputBox("请输入线性规划典范型方程约束条件的个数 M=?", "输入数据", 0)) Rem 给数组分配空间 ReDim a(0 To m + 1, 0 To n + 2) ReDim aa(1 To m + 1, 1 To n + 2) ReDim c(n) ReDim b(m) ReDim cb(m) ReDim cba(n) ReDim xcb(n) ReDim xb(m) Rem 对线性规划约束方程增广矩阵A()进行归零计算 For i = 0 To m + 1 For j = 0 To n + 2 a(i, j) = 0 Next j Next i Rem 输入线性规划约束方程系数矩阵A() For i = 1 To m For j = 1 To n a(i, j) = Val(InputBox("请输入典范型方程约束条件矩阵的系数 a(" & Str(i) & "," & Str(j) & "):", "输入数据", 0)) Next j Next i Rem 输入线性规划约束方程右端常数B() For i = 1 To m b(i) = Val(InputBox("请输入典范型方程约束条件右端的常数 b(" & Str(i) & "):", "输入数据", 0)) Next i Rem 把右端常数写入增广矩阵A()中 For i = 1 To m a(i, n + 1) = b(i) Next i Rem 输入线性规划目标函数的系数C() For i = 1 To n c(i) = Val(InputBox("请输入典范型方程目标函数的系数 c(" & Str(i) & "):", "输入数据", 0)) Next i Rem 把目标函数的系数写入增广矩阵A()中 For i = 1 To n a(0, i) = c(i) Next i Rem 输入线性规划单纯形初始表中基变量在目标函数中的系数CB() For i = 1 To m cb(i) = Val(InputBox("请输入线性规划单纯形初始表中基变量在目标函数中的系数CB(" & Str(i) & "):", "输入数据", 0)) Next i Rem 把基变量目标函数的系数写入增广矩阵A()中 For i = 1 To m a(i, 0) = cb(i) Next i Rem 记录基变量下标值 For i = 1 To m xb(i) = Val(InputBox("请输入典范型方程第" & Str(i) & "行,基变量的下标:", "输入数据", 0)) Next i Rem 检验数的累积数归零并计算检验数 For i = 1 To n cba(i) = 0 Next i For i = 1 To n For j = 1 To m cba(i) = cba(i) + a(j, 0) * a(j, i) Next j a(m + 1, i) = a(0, i) - cba(i) Next i Rem 计算目标函数值 z = 0 For i = 1 To m z = z + a(i, 0) * a(i, n + 1) Next i a(m + 1, n + 1) = z Rem 打印增广矩阵A() For i = 0 To m + 1 For j = 0 To n + 2 Print a(i, j); Next j Print Next i Print Rem 判断所有检验数是否都小于等于零 cc = 0 For i = 1 To n If a(m + 1, i) <= 0 Then cc = cc + 1 End If Next i Rem 统计检验数为零的个数 cm = 0 For i = 1 To n If a(m + 1, i) = 0 Then cm = cm + 1 End If Next i Print "cc="; cc, "cm="; cm Rem 判断此单纯形表是否为最优单纯形表 Do While cc < n Rem 统计检验数最大值并确定进基列 qq = 0.00001 l = 0 For i = 1 To n If a(m + 1, i) > qq Then qq = a(m + 1, i) l = i End If Next i Print Print "l="; l, Rem 统计进基列上A(i,j)小于等于零的个数 ka = 0 For i = 1 To m If a(i, l) <= 0 Then ka = ka + 1 End If Next i Rem 若各进基列上A(i,j)全都小于等于零,则本线性规划有无界解 If ka = m Then Print "本线性规划有无界解!" Print Exit Do End If Rem 计算比值θ并按最小比值准则确定出基行 For i = 1 To m If a(i, l) > 0 Then a(i, n + 2) = a(i, n + 1) / a(i, l) End If Next i tt = 10000 k = 0 For i = 1 To m If a(i, l) > 0 And a(i, n + 2) < tt Then tt = a(i, n + 2) k = i End If Next i Print "k="; k Print Rem 进行初等行变换时,对临时数组归零计算 For i = 1 To m + 1 For j = 1 To n + 2 aa(i, j) = 0 Next j Next i Rem 确定枢轴元素,进行初等行变换 oo = a(k, l) For i = 1 To n + 1 aa(k, i) = a(k, i) / oo Next i For i = 1 To m If i <> k Then For j = 1 To n + 1 aa(i, j) = a(i, j) + a(k, j) * (-a(i, l)) Next j End If Next i Rem 把临时数组AA()的数据写到增广矩阵A()中去 For i = 1 To m + 1 For j = 1 To n + 2 a(i, j) = aa(i, j) Next j Next i a(k, 0) = a(0, l) xb(k) = l Rem 检验数的累积数归零并计算检验数 For i = 1 To n cba(i) = 0 Next i For i = 1 To n For j = 1 To m cba(i) = cba(i) + a(j, 0) * a(j, i) Next j a(m + 1, i) = a(0, i) - cba(i) Next i Rem 计算目标函数值 z = 0 For i = 1 To m z = z + a(i, 0) * a(i, n + 1) Next i a(m + 1, n + 1) = z Rem 判断所有检验数是否都小于等于零 cc = 0 For i = 1 To n If a(m + 1, i) <= 0 Then cc = cc + 1 End If Next i Rem 统计检验数为零的个数 cm = 0 For i = 1 To n If a(m + 1, i) = 0 Then cm = cm + 1 End If Next i Rem 打印增广矩阵A() For i = 0 To m + 1 For j = 0 To n + 2 Print a(i, j); Next j Print Next i Print Loop Rem 判断此单纯形表是否为最优单纯形表 If cc = n Then Rem 判断本线性规划有唯一最优解或者有多重最优解 If cm = m Then Print "本线性规划有唯一最优解!" Print "线性规划的最优解为:" ElseIf cm > m Then Print "本线性规划有多重最优解!" Print "线性规划的最优值为:" End If End If Rem 打印线性规划的解和目标函数值 For i = 1 To m Print "X(" & Str(xb(i)) & ")="; a(i, n + 1), Next i Print "其它变量为零。" Print "Z="; a(m + 1, n + 1) Print Rem 打印增广矩阵A() For i = 0 To m + 1 For j = 0 To n + 2 Print a(i, j); Next j Print Next i End Sub
来源:https://www.cnblogs.com/yellowhh/p/12013915.html