Esempio n. 1
0
 def testPerformPivot(self):
     s = Simplex(testMatrix2)
     row, column = s.choosePivot()
     self.assertEqual((row, column), (1, 0))
     s.performPivot(row, column)
     expected = Array([
         [F(0), F(7, 2), F(-1, 2), F(5, 2), F(0), F(0), F(25, 2)],
         [F(1), F(3, 2), F(1, 2), F(1, 2), F(0), F(0), F(5, 2)],
         [F(0), F(-5), F(0), F(-2), F(1), F(0), F(1)],
         [F(0), F(-1, 2), F(1, 2), F(-3, 2), F(0), F(1), F(1, 2)],
     ])
     for i in range(len(expected)):
         self.assertEqual(s.tableaux[i], expected[i], "(row %d)" % i)
     self.assertEqual(s.basicVariables[1:], [0, 4, 5])
     row, column = s.choosePivot()
     self.assertEqual((row, column), (3, 2))
     s.performPivot(row, column)
     expected = Array([
         [F(0), F(3), F(0), F(1), F(0), F(1), F(13)],
         [F(1), F(2), F(0), F(2), F(0), F(-1), F(2)],
         [F(0), F(-5), F(0), F(-2), F(1), F(0), F(1)],
         [F(0), F(-1), F(1), F(-3), F(0), F(2), F(1)],
     ])
     for i in range(len(expected)):
         self.assertEqual(s.tableaux[i], expected[i], "(row %d)" % i)
     self.assertEqual(s.basicVariables[1:], [0, 4, 2])
Esempio n. 2
0
 def testSolve(self):
     s = Simplex(testMatrix1)
     s.variableFromIndex = {i : str(i) for i in range(s.nbVariables)}
     objective = s.tableaux[0].copy()
     s.tableaux[0] = s.tableaux[0].__class__(([0]*len(s.tableaux[0])))
     s.addVariable()
     expected = Array([
         [F(1), F(0), F(0), F(0), F(0), F(0), F(0), F(0)],
         [F(-1), F(4), F(-2), F(1), F(-1), F(1), F(0), F(-2)],
         [F(-1), F(-1), F(1), F(-1), F(0), F(0), F(1), F(-10)],
     ])
     for i in range(len(expected)):
         self.assertEqual(s.tableaux[i], expected[i], "(row %d)" % i)
     row, value = s.firstPhaseLeavingVariable()
     self.assertEqual(row, 2)
     s.performPivot(row, 0)
     expected = Array([
         [F(0), F(-1), F(1), F(-1), F(0), F(0), F(1), F(-10)],
         [F(0), F(5), F(-3), F(2), F(-1), F(1), F(-1), F(8)],
         [F(1), F(1), F(-1), F(1), F(0), F(0), F(-1), F(10)],
     ])
     for i in range(len(expected)):
         self.assertEqual(s.tableaux[i], expected[i], "(row %d)" % i)
     row, column = 1, 3
     s.performPivot(row, column)
     expected = Array([
         [F(0), F(3, 2), F(-1, 2), F(0), F(-1, 2), F(1, 2), F(1, 2), F(-6)],
         [F(0), F(5, 2), F(-3, 2), F(1), F(-1, 2), F(1, 2), F(-1, 2), F(4)],
         [F(1), F(-3, 2), F(1, 2), F(0), F(1, 2), F(-1, 2), F(-1, 2), F(6)],
     ])
     for i in range(len(expected)):
         self.assertEqual(s.tableaux[i], expected[i], "(row %d)" % i)
     row, column = 2, 2
     s.performPivot(row, column)
     expected = Array([
         [F(1), F(0), F(0), F(0), F(0), F(0), F(0), F(0)],
         [F(3), F(-2), F(0), F(1), F(1), F(-1), F(-2), F(22)],
         [F(2), F(-3), F(1), F(0), F(1), F(-1), F(-1), F(12)],
     ])
     for i in range(len(expected)):
         self.assertEqual(s.tableaux[i], expected[i], "(row %d)" % i)
     s.removeVariable()
     s.tableaux[0] = objective
     s.updateObjective()
     expected = Array([
         [F(1), F(0), F(0), F(1), F(0), F(2), F(-20)],
         [F(-2), F(0), F(1), F(1), F(-1), F(-2), F(22)],
         [F(-3), F(1), F(0), F(1), F(-1), F(-1), F(12)],
     ])
     for i in range(len(expected)):
         self.assertEqual(s.tableaux[i], expected[i], "(row %d)" % i)