The simple algorithm is the classical method to solve the linear programming, it is proposed in the 50 's, its basic idea is to traverse all vertices along the edge in the feasible domain, find out the optimal value, that is the optimal value of the algorithm.
The algorithm is executed as follows:
To find the initial base vector to construct the simplex table in the corresponding C of all the non-base vectors, find a minimum CI, if the CI is greater than 0, then exit, output obj, otherwise the AI into the base using the base vector group linear representation AI, the linear representation of the coefficient vector λ for all the components of λ greater than 0, to find minmj= 1bjλj corresponding J, and then the AJ out of the matrix rotation, that is, through the Gaussian line transformation, the AI transform into AJ if CI is all greater than 0, then exit the algorithm, output obj, otherwise, repeat the 3rd step
__author__ = ' Xanxus ' class Problem:def __init__ (self): self.obj = 0 Self.comatrix = [] Sel F.B = [] Self.c = [] def pivot (self, basic, L, E): # The l-th line self.b[l]/= Self.comatrix E [L] origin = self.comatrix[e][l] for i in range (len (Self.comatrix)): Self.comatrix[i][l]/= or Igin # The other lines to I in range (len (self.b)): If I!= l:self.b[i] = sel F.b[i]-self.b[l] * Self.comatrix[e][i] for I in range (len (self.b)): If I!= L:origi n = self.comatrix[e][i] for j in Range (Len (Self.comatrix)): self.comatrix[j][i] = self
. comatrix[j][i]-Origin * Self.comatrix[j][l] origin = self.c[e] for i in range (len (Self.comatrix)): Self.c[i] = Self.c[i]-self.comatrix[i][l] * origin self.obj = self.obj-self.b[l] * Origin Basi C[l] = e def cloNE (Self, another): Self.obj = Another.obj for i-another.b:self.b.append (i) for I I
n Another.c:self.c.append (i) for v. in ANOTHER.COMATRIX:NEWV = [] for i in V: Newv.append (i) self.coMatrix.append (NEWV) basic = [] problem = problem () def Readproblem (
FileName): with open (filename) as F:var_num = Int (F.readline ()) constrait_num = Int (F.readline ()) Matrix = [[0] * var_num) for I in Range (Constrait_num)] for I in range (constrait_num): line = F.R Eadline () tokens = Line.split (') for J in Range (Var_num): matrix[i][j] = float (t
OKENS[J]) problem.b.append (float (tokens[-1)) for I in Range (var_num): var = []
For j in Range (Constrait_num): Var.append (Matrix[j][i]) Problem.coMatrix.append (VAR)
line = F.readline () tokens = Line.split (') for I in Range (Var_num): Problem.c.append (float (tokens[i)) def getmin CIndex (c): min, minindex = 1, 0 for I in range (Len (c)): if C[i] < 0 and c[i] < min:min = C[i] Minindex = i if min > 0:return-1 else:return minindex def getlamdavecto R (evector): ld = [] for i in range (len (evector)): Ld.append (Evector[i]) return ld def simplex (Basic, Problem): Mincindex = Getmincindex (problem.c) while mincindex!= -1:ld = Getlamdavector (Problem.comatrix
[Mincindex])
# Find the L line l, min =-1, 10000000000 to I in range (len (PROBLEM.B)): if Ld[i] > 0:
If Problem.b[i]/ld[i] < Min:l = i min = problem.b[i]/ld[i] if L = = -1:return False problem.pivot (Basic, L, mincindex) Mincindex = Getmincindex (pr OblEM.C) return True def initialsimplex (Basic, problem): min, Minindex = 1000000000,-1 to I in range (len (prob LEM.B)): if problem.b[i] < Min:min = Problem.b[i] Minindex = i for I in range (Len ( PROBLEM.B): Basic.append (I+len (PROBLEM.B)) if min >= 0:return True else:originc = pr
OBLEM.C NEWC = [] for i in range (len (PROBLEM.C)): Newc.append (0) newc.append (1) PROBLEM.C = NEWC x0 = [] for i in range (len (PROBLEM.B)): X0.append ( -1) Problem.comat Rix.append (x0) problem.pivot (Basic, Minindex,-1) res = simplex (basic, problem) if res = = False or Problem.obj!= 0:return False else:problem.c = Originc Problem.coMatrix.pop () # Gaussian row Operation counter = 0 for i in Basic:if problem.c[i
]!= 0: Origin = Problem.c[i] for j in Range (Len (PROBLEM.C)): problem.c[j]-= Prob Lem.comatrix[j][counter] * origin problem.obj-= problem.b[counter] * Origin counter + = 1 return True filename = raw_input (' Please input the problem description file: ') Readproblem (filename) if Initialsimplex (Basic, problem): res = simplex (basic, problem) if res:print ' optimal obj is%.2f '% p Roblem.obj index = [' 0.00 '] * Len (problem.comatrix) counter = 0 for I in basic:index[
I] = '%.2f '% problem.b[counter] counter + = 1 print ' The solution is {%s} '% '. Join (index) ELSE:
print ' No feasible solution ' else:print ' no feasible solution ' raw_input (' please input any key to quit. ')
Hope to help you.