diff --git a/simplex.py b/simplex.py index 3bf112f..b51273d 100644 --- a/simplex.py +++ b/simplex.py @@ -16,7 +16,7 @@ def __init__(self, tableau ): # First Phase def phase1(self): - print 'Simplex Phase 1' + print('Simplex Phase 1') res = True if self.checkFeasibility() == False: @@ -24,30 +24,30 @@ def phase1(self): self.addArtificialVariables() self.addNewCostFunction() - print self.tableau + print(self.tableau) simplex = Simplex(self.tableau) count = 1 while simplex.canContinue(): - print '' - print 'Iteration '+str(count) - print '' + print('') + print('Iteration '+str(count)) + print('') simplex.iteration() count += 1 - print simplex.tableau + print(simplex.tableau) # In this case, the Phase 1 has finished with artificial variables if len(set(self.tableau.basis) & set(self.tableau.artificial_variable)) > 0: - print 'There still exist artifical variables' + print('There still exist artifical variables') res = False else: # The sum of the infeasibilities is greater than 0, what characterizes an unfeasible solution if self.tableau[self.tableau.cost_index][self.tableau.b_index] > 0: - print 'Sum of artificial variables is greater than 0, then the problem is not feasible' + print('Sum of artificial variables is greater than 0, then the problem is not feasible') self.solution = 'infeasible' res = False # Everything has happened fine. A feasible tableau has been found to proceed to Phase 2 else: - print 'Phase 1 has found feasible tableau' + print('Phase 1 has found feasible tableau') # Remove the artificial cost function self.tableau.removeRow(self.tableau.cost_index) self.tableau.cost_index = self.tableau.lines -1 @@ -58,31 +58,31 @@ def phase1(self): self.tableau.b_index = self.tableau.columns-1 - print 'Simplex Phase 1 End' + print('Simplex Phase 1 End') return res # Second Phase # This phase occurs only if the method has found a feasible solution in Phase 1 def phase2(self): - print '' - print 'Simplex Phase 2' + print('') + print('Simplex Phase 2') i = 1 - print '### Initial Tableau Phase 2' - print self.tableau + print('### Initial Tableau Phase 2') + print(self.tableau) b = True while self.canContinue(): - print '' - print '###### Iteration',i - print '' + print('') + print('###### Iteration',i) + print('') b = self.iteration() - print self.tableau + print(self.tableau) if b == False: break i += 1 if b == True: self.solution = self.tableau[self.tableau.cost_index][self.tableau.b_index] - print '' - print 'Simplex Phase 2 End' + print('') + print('Simplex Phase 2 End') # Exceute the method def execute(self): @@ -163,7 +163,7 @@ def canContinue(self): def checkFeasibility(self): n,c = self.requeredArtificalVariables() - print n + print(n) if n == 0: return True return False @@ -256,7 +256,7 @@ def iteration(self): pivot_index = self.getPivot() if(self.isDegenerative(pivot_index)): - print 'Problem is degenerative and it needs to use another method' + print('Problem is degenerative and it needs to use another method') self.solution = 'degenerative' return False @@ -268,18 +268,18 @@ def iteration(self): return # change basis self.tableau.changeBasis(pivot_index,constraint_index) - print 'Basis: '+str(self.tableau.basis) + print('Basis: '+str(self.tableau.basis)) self.gaussianOperation(constraint_index,pivot_index) return True else: - print 'Solution is unbounded' + print('Solution is unbounded') self.solution = 'unbounded' return False def test1(): - print 'Test 1' + print('Test 1') r1 = [2,1,-2,'<',8] r2 = [4,-1,2,'>',2] r3 = [2,3,-1,'>',4] @@ -297,10 +297,10 @@ def test1(): simplex.execute() - print 'Solution: '+str(simplex.solution) + print('Solution: '+str(simplex.solution)) def test2(): - print 'Test 2' + print('Test 2') r1 =[1,2,4,-1,'<',6] r2 = [2,3,-1,1,'<',12] r3 = [1,0,1,1,'<',4] @@ -318,11 +318,11 @@ def test2(): simplex.execute() - print 'Solution: '+str(simplex.solution) - print simplex.tableau + print('Solution: '+str(simplex.solution)) + print(simplex.tableau) def test3(): - print 'Test 3' + print('Test 3') r1 =[1,2,'<',6] r2 = [-2,1,'<',4] r3 = [5,3,'<',15] @@ -340,11 +340,11 @@ def test3(): simplex.execute() - print 'Solution: '+str(simplex.solution) - print simplex.tableau + print('Solution: '+str(simplex.solution)) + print(simplex.tableau) def test4(): - print 'Test 3' + print('Test 3') r1 =[4,1,'<',21] r2 = [2,3,'>',13] r3 = [-1,1,'=',1] @@ -362,12 +362,12 @@ def test4(): simplex.execute() - print 'Solution: '+str(simplex.solution) - print simplex.tableau + print('Solution: '+str(simplex.solution)) + print(simplex.tableau) # problema patologico def test5(): - print 'Test 3' + print('Test 3') r1 =[1,0,0,'<',1] r2 = [20,1,0,'<',100] r3 = [200,20,1,'<',10000] @@ -385,11 +385,11 @@ def test5(): simplex.execute() - print 'Solution: '+str(simplex.solution) - print simplex.tableau + print('Solution: '+str(simplex.solution)) + print(simplex.tableau) def test6(): - print 'Test 6' + print('Test 6') r1 =[1,2,4,-1,'=',6] r2 = [2,3,-1,4,'<=',12] r3 = [1,0,1,1,'<=',4] @@ -407,11 +407,11 @@ def test6(): simplex.execute() - print 'Solution: '+str(simplex.solution) - print simplex.tableau + print('Solution: '+str(simplex.solution)) + print(simplex.tableau) def degenerationExample(): - print 'Exemplo de degeneracao' + print('Exemplo de degeneracao') r1 =[1,0,'<=',3] r2 = [0,1,'<=',4] r3 = [4,3,'<=',12] @@ -429,11 +429,11 @@ def degenerationExample(): simplex.execute() - print 'Solution: '+str(simplex.solution) - print simplex.tableau + print('Solution: '+str(simplex.solution)) + print(simplex.tableau) def solucaoIlimitada(): - print 'Solucao Ilimitada' + print('Solucao Ilimitada') r1 =[4,1,'>=',20] r2 = [1,2,'>=',10] r3 = [1,0,'>=',2] @@ -451,11 +451,11 @@ def solucaoIlimitada(): simplex.execute() - print 'Solution: '+str(simplex.solution) - print simplex.tableau + print('Solution: '+str(simplex.solution)) + print(simplex.tableau) def input1(): - print '' + print('') r1 =[1,1,'>=',2] r2 = [1,2,'>=',5] # r3 = [5,3,'<=',15] @@ -473,20 +473,20 @@ def input1(): simplex.execute() - print 'Solution: '+str(simplex.solution) - print simplex.tableau + print('Solution: '+str(simplex.solution)) + print(simplex.tableau) if __name__ == '__main__': - print 'Simplex Method' + print('Simplex Method') # test1() - print '\n\n\n' + print('\n\n\n') # test2() - print '\n\n\n' + print('\n\n\n') # test3() @@ -494,9 +494,4 @@ def input1(): # degenerationExample() - input1() - - - - - \ No newline at end of file + input1() \ No newline at end of file diff --git a/tableau.py b/tableau.py index 04736ec..b93a73e 100644 --- a/tableau.py +++ b/tableau.py @@ -65,7 +65,7 @@ def initTableau(self): # Add basis for i in range(self.var_count,self.columns-1): self.basis.append(i) - print self.basis + print(self.basis) def __str__(self): s = "" @@ -92,7 +92,7 @@ def changeBasis(self,enter,leave): # change basis out = self.basis.pop(leave) self.basis.insert(leave,enter) - print 'Changing basis: enter '+str(enter)+' leave '+str(out) + print('Changing basis: enter '+str(enter)+' leave '+str(out)) def addColumn(self,key,default_value): for r in self.tableau: