Beispiel #1
0
 def testTrivialUnbounded(self):
     """
         Maximize x_0 st x_0 >= 4
     """
     s = Simplex(Array([
         [F(-1), F(0), F(0)],
         [F(-1), F(1),F(-4)]
     ]))
     s.variableFromIndex = {i : str(i) for i in range(s.nbVariables)}
     with self.assertRaises(Unbounded):
         s.solve()
Beispiel #2
0
 def testSolve2(self):
     s = Simplex(Array(testMatrix1))
     s.variableFromIndex = {i : str(i) for i in range(s.nbVariables)}
     opt, optSol = s.solve()
     self.assertEqual(opt, -20)
     self.assertEqual(optSol, {
         '0' : 0,
         '1' : 12,
         '2' : 22,
         '3' : 0
     })
     s = Simplex(testMatrix2)
     s.variableFromIndex = {i : str(i) for i in range(s.nbVariables)}
     opt, optSol = s.solve()
     self.assertEqual(opt, 13)
     self.assertEqual(optSol, {
         '0' : 2,
         '1' : 0,
         '2' : 1
     })
Beispiel #3
0
 def testTrivialEmpty(self):
     """
         Maximize 0 st x_0 <= 3 AND x_0 >= 4
     """
     s = Simplex(Array([
         [F(0), F(0), F(0), F(0)],
         [F(1), F(1), F(0), F(3)],
         [F(-1), F(0), F(1), F(-4)]
     ]))
     s.variableFromIndex = {i : str(i) for i in range(s.nbVariables)}
     with self.assertRaises(Empty):
         s.solve()
Beispiel #4
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)