プログラムリスト
VBAのプログラムリストは以下のようになります。
'M=制約条件の数
'N=決定変数の数
'MN=M+N
'MAT(0,J)=目的関数の係数
'MAT(I,0)=制約条件の限界値
'ITER=ステップの繰り返し回数
'PIBOT=ピボット列値
'ROW=ピボット行の値
Dim MAT(101, 201), VAR(100), ITER, N, M, MN
Sub Main()
'条件の読込
M = Sheets("Sheet1").Cells(4, 3).Value
N = Sheets("Sheet1").Cells(5, 3).Value
MN = M + N
For i = 0 To 100
For j = 0 To 200
MAT(i, j) = 0
Next j
Next i
For i = 1 To M
For j = 0 To N
MAT(i, j) = Sheets("Sheet1").Cells(13 + i, 2 + j).Value
Next j
MAT(i, N + i) = 1
VAR(i) = N + i
Next i
For j = 1 To N
MAT(0, j) = -(Sheets("Sheet1").Cells(9, 2 + j).Value)
Next j
'Step0の出力
ITER = 0
OUTPUT
Do While ITER < 50
ITER = ITER + 1
'ピボット列の選定(基底に入れる変数の決定)
Max = 0
PIVOT = 0
For j = 1 To MN
If MAT(0, j) < 0 Then
If MAT(0, j) <= Max Then
PIVOT = j
Max = MAT(0, j)
End If
End If
Next j
If PIVOT = 0 Then Exit Do
'ピボット行の選定(基底から追い出す変数の決定)
Min = 10000000000#
Row = 0
For i = 1 To M
If MAT(i, PIVOT) > 0 Then
DUMMY = MAT(i, 0) / MAT(i, PIVOT)
If DUMMY >= 0 Then
If DUMMY <= Min Then
Row = i
Min = DUMMY
End If
End If
End If
Next i
'基底変数の交換およびピボット演算
DUMMY = MAT(Row, PIVOT)
For j = 0 To MN
MAT(Row, j) = MAT(Row, j) / DUMMY
Next j
For i = 0 To M
If i <> Row Then
DUMMY = MAT(i, PIVOT)
For j = 0 To MN
MAT(i, j) = MAT(i, j) - MAT(Row, j) * DUMMY
Next j
End If
Next i
VAR(Row) = PIVOT
OUTPUT
test = 1
Loop
End Sub
Sub OUTPUT()
Sheets("Sheet1").Cells(18 + 10 * ITER, 1).Formula = "Step" & ITER
Sheets("Sheet1").Cells(19 + 10 * ITER, 1).Formula = "基底変数"
Sheets("Sheet1").Cells(19 + 10 * ITER, 2).Formula = "制限"
For j = 1 To MN
Sheets("Sheet1").Cells(19 + 10 * ITER, j + 2).Formula = "X" & j
Next j
Sheets("Sheet1").Cells(20 + 10 * ITER, 1).Formula = "-Z"
For i = 1 To M
Sheets("Sheet1").Cells(20 + 10 * ITER + i, 1).Formula = "X" & VAR(i)
Next i
For i = 0 To M
For j = 0 To MN
Sheets("Sheet1").Cells(20 + 10 * ITER + i, j + 2).Value = MAT(i, j)
Next j
Next i
End Sub