Python implementation of Simplex simplicity algorithm

Source: Internet
Author: User

The simple algorithm is a classical method to solve the linear programming, which was put forward in the 50 's, the basic idea is to traverse all the vertices along the edge in the feasible domain, find the optimal value, that is the optimal value of the algorithm.

The execution of the algorithm is as follows:

  1. Finding the initial base vector
  2. Building a simple table
  3. In all non-base vectors corresponding to the C, find a minimum c i , if the c i is greater than 0, exit, obj, or a i Into the base
  4. Linear representation using base vector Group a i , the coefficient vector of the linear representation is obtained. Λ
  5. For Λ In all components greater than 0, find out min m j = 1 bJ Λ J The corresponding J, and then the aJ Out of the base
  6. The problem matrix rotates, that is, through the Gaussian line transformation, a i Transform into < Span class= "Mrow" id= "mathjax-span-127" style= "" > a j
  7. if < Span class= "Mrow" id= "mathjax-span-132" style= "" > c i full greater than 0, exit the algorithm, output obj, otherwise, repeat step 3rd
__author__ = ' Linfuyuan ' class Problem:def __init__ (self): self.obj = 0 Self.comatrix = [] self.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]/= origin # The other lines for I in range (len (self.b)): if I! = l:self.b[i] = self.b[i]-Self . b[l] * Self.comatrix[e][i] for I in range (len (self.b)): if I! = L:origin = Self.comatri X[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] = s Elf.c[i]-self.comatrix[i][l] * origin self.obj = self.obj-self.b[l] * origin basic[l] = e def clone (s Elf, another): SELf.obj = Another.obj for i in Another.b:self.b.append (i) for I in ANOTHER.C:SELF.C.A            Ppend (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 (cons            Trait_num)] for I in range (constrait_num): line = F.readline () tokens = Line.split (") For j in Range (Var_num): matrix[i][j] = float (tokens[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 (") F or I in range (Var_num):           Problem.c.append (float (tokens[i)) def Getmincindex (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 el Se:return minindexdef Getlamdavector (evector): ld = [] for i in range (len (evector)): Ld.append (Evecto R[i]) return Lddef simplex (basic, problem): Mincindex = Getmincindex (PROBLEM.C) while mincindex! = -1:ld = Getlamdavector (Problem.comatrix[mincindex]) # Find the L line l, min =-1, 10000000000 for I in Ran                    GE (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, MI Ncindex) Mincindex = Getmincindex (PROBLEM.C) return truedef Initialsimplex (Basic, problem): min, Minindex = 1 000000000, 1 for i IN Range (len (PROBLEM.B)): if problem.b[i] < Min:min = Problem.b[i] Minindex = i for I I N Range (len (PROBLEM.B)): Basic.append (I+len (PROBLEM.B)) if min >= 0:return True Else:origi        NC = Problem.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.comatri X.append (x0) problem.pivot (Basic, Minindex,-1) res = simplex (basic, problem) if res = = False or prob            Lem.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] -= problem.comatrix[j][Counter] * origin problem.obj-= problem.b[counter] * Origin counter + 1 RET Urn Truefilename = raw_input (' Please input the problem description file: ') Readproblem (filename) if Initialsimplex (Basic, Problem): res = simplex (basic, problem) if Res:print ' the optimal obj is%.2f '% problem.obj index = [' 0.00 '] * Len (problem.comatrix) counter = 0 for i in basic:index[i] = '%.2f '% problem.b[counte R] Counter + = 1 print ' The solution is {%s} '% '. Join (index) Else:print ' no feasible soluti On ' else:print ' no feasible solution ' raw_input (' please input any key to quit. ')
We hope to help you.

Python implementation of Simplex simplicity algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.