DEFINT I-N
CLS
PRINT
PRINT " Number of variables in E.F.: "; : INPUT "", N
PRINT " Number of <= inequalities..: "; : INPUT "", M1
PRINT " Number of >= inequalities..: "; : INPUT "", M2
PRINT " Number of = equalities.....: "; : INPUT "", M3
F$ = "#####.##"
F1$ = "#####.######"
FOR i = 1 TO M + 2
FOR j = 1 TO N + 1
A(i, j) = 0!
NEXT j
NEXT i
PRINT
PRINT " Input Table:"
FOR i = 1 TO M + 1
FOR j = 1 TO N + 1
PRINT USING F$; A(i, j);
NEXT j
PRINT
NEXT i
FOR i = 1 TO N
FOR j = 1 TO M
IF iposv(j) = i THEN
PRINT " X"; i; " = ";
PRINT USING F1$; A(j + 1, 1)
GOTO 3
END IF
NEXT j
PRINT " X"; i; " = ";
PRINT USING F1$; 0!
3 NEXT i
ELSE
PRINT " No solution (error code = "; icase; ")."
END IF
'----------------------------------------------------------------------------------
-------
'USES 2000 simp1, 2100 simp2, 2200 simp3.
'Simplex method for linear programming. Input parameters A, m, n, m1, m2, and m3,
and
'output parameters A, icase, izrov, and iposv are described above (see reference).
'Constants: MMAX is the maximum number of constraints expected; NMAX is the
maximum number
'of variables expected; EPS is the absolute precision, which should be adjusted to
the
'scale of your variables.
'----------------------------------------------------------------------------------
-------
'Labels: 1,2,10,20,30
DIM l1(MMAX), l2(MMAX), l3(MMAX)
EPS = .000001
IF M <> M1 + M2 + M3 THEN
PRINT " Bad input constraint counts in simplx."
RETURN
END IF
nl1 = N
FOR k = 1 TO N
l1(k) = k 'Initialize index list of columns admissible for exchange.
izrov(k) = k 'Initially make all variables right-hand.
NEXT k
nl2 = M
FOR i = 1 TO M
IF A(i + 1, 1) < 0! THEN
PRINT " Bad input tableau in simplx, Constants bi must be nonnegative."
RETURN
END IF
l2(i) = i
iposv(i) = N + i
'----------------------------------------------------------------------------------
---------------
'Initial left-hand variables. m1 type constraints are represented by having their
slack variable
'initially left-hand, with no artificial variable. m2 type constraints have their
slack
'variable initially left-hand, with a minus sign, and their artificial variable
handled implicitly
'during their first exchange. m3 type constraints have their artificial variable
initially
'left-hand.
'----------------------------------------------------------------------------------
---------------
NEXT i
FOR i = 1 TO M2
l3(i) = 1
NEXT i
ir = 0
IF M2 + M3 = 0 THEN GOTO 30 'The origin is a feasible starting solution. Go to
phase two.
ir = 1
FOR k = 1 TO N + 1 'Compute the auxiliary objective function.
q1 = 0!
FOR i = M1 + 1 TO M
q1 = q1 + A(i + 1, k)
NEXT i
A(M + 2, k) = -q1
NEXT k
10 : iabf = 0: mm = M + 1
GOSUB 2000 'call simp1(a,m+1,l1,nl1,0,kp,bmax) Find max. coeff. of auxiliary
objective fn.
IF bmax <= EPS AND A(M + 2, 1) < -EPS THEN
icase = -1 'Auxiliary objective function is still negative and cant be
improved,
RETURN 'hence no feasible solution exists.
ELSEIF bmax <= EPS AND A(M + 2, 1) <= EPS THEN
' Auxiliary objective function is zero and cant be improved; we have a feasible
starting vector.
' Clean out the artificial variables corresponding to any remaining equality
constraints by
' go to 1s and then move on to phase two by go to 30.
m12 = M1 + M2 + 1
IF m12 <= M THEN
FOR ip = m12 TO M
IF iposv(ip) = ip + N THEN 'Found an artificial variable for an equality
constraint.
iabf = 1: mm = ip
GOSUB 2000 'call simp1(a,ip,l1,nl1,1,kp,bmax)
IF bmax > EPS THEN GOTO 1 'Exchange with column corresponding to maximum
END IF 'pivot element in row.
NEXT ip
END IF
ir = 0
m12 = m12 - 1
IF M1 + 1 > m12 THEN GOTO 30
FOR i = M1 + 1 TO M1 + M2 'Change sign of row for any m2 constraints
IF l3(i - M1) = 1 THEN 'still present from the initial basis.
FOR k = 1 TO N + 1
A(i + 1, k) = -1! * A(i + 1, k)
NEXT k
END IF
NEXT i
GOTO 30 'Go to phase two.
END IF
' End of phase one code for finding an initial feasible solution. Now, in phase
two, optimize it.
30 : iabf = 0: mm = 0
GOSUB 2000 'call simp1(a,0,l1,nl1,0,kp,bmax) Test the z-row for doneness.
IF bmax <= EPS THEN 'Done. Solution found. Return with the good news.
icase = 0
RETURN
END IF
GOSUB 2100 'call simp2(a,m,n,l2,nl2,ip,kp,q1) Locate a pivot element (phase
two).
IF ip = 0 THEN 'Objective function is unbounded. Report and return.
icase = 1
RETURN
END IF
i1 = M: k1 = N
GOSUB 2200 'call simp3(a,m,n,ip,kp) Exchange a left- and a right-hand variable
(phase two),
GOTO 20 'update lists of left- and right-hand variables and
'return for another iteration.
RETURN