]> gitweb.michael.orlitzky.com - dead/census-tools.git/commitdiff
Added the unit tests for the SimplexIteration class.
authorMichael Orlitzky <michael@orlitzky.com>
Mon, 14 Jun 2010 08:14:35 +0000 (04:14 -0400)
committerMichael Orlitzky <michael@orlitzky.com>
Mon, 14 Jun 2010 08:14:35 +0000 (04:14 -0400)
src/Tests/Unit/SimplexIterationTest.py [new file with mode: 0644]

diff --git a/src/Tests/Unit/SimplexIterationTest.py b/src/Tests/Unit/SimplexIterationTest.py
new file mode 100644 (file)
index 0000000..1d616a0
--- /dev/null
@@ -0,0 +1,87 @@
+import unittest
+
+import LinearProgramming
+
+
+class SimplexIterationTest(unittest.TestCase):
+
+    def testSolutionVectorUnchanged(self):
+        sm = LinearProgramming.SimplexIteration()
+        sm.solution_vector = [1,2,3,4,5]
+        self.assertTrue(sm.solution_vector == [1,2,3,4,5])
+
+
+    def testBasicVariables(self):
+        sm = LinearProgramming.SimplexIteration()
+        sm.solution_vector = [0,1,0,1,0,1]
+        sm.basic_variables = "x1,  x2, x3,x4,x5  "
+        self.assertTrue("x1" in sm.basic_variables)
+        self.assertTrue("x2" in sm.basic_variables)
+        self.assertTrue("x3" in sm.basic_variables)
+        self.assertTrue("x4" in sm.basic_variables)
+        self.assertTrue("x5" in sm.basic_variables)
+
+
+    def testNonBasicVariables(self):
+        sm = LinearProgramming.SimplexIteration()
+        sm.solution_vector = [1,0,0,4,5,6]
+        sm.basic_indices = [0,3,4,5]
+        self.assertTrue("x2" in sm.nonbasic_variables)
+        self.assertTrue("x3" in sm.nonbasic_variables)
+
+
+    def testBasisMatrix(self):
+        sm = LinearProgramming.SimplexIteration()
+
+        sm.solution_vector = [1, 0, 1, 1, 1, 0, 0, 1]
+        sm.basic_variables = "x1,x3,x4,x5,x8"
+        
+        sm.constraint_matrix = [ [1, 1, 1, 0, 0, 0, 1, 0],
+                                 [0, 0, 0, 1, 1, 1, 0, 1],
+                                 [1, 0, 0, 1, 0, 0, 0, 0],
+                                 [0, 1, 0, 0, 1, 0, 0, 0],
+                                 [0, 0, 1, 0, 0, 1, 0, 0] ]
+
+        expected_basis_matrix = [ [1, 1, 0, 0, 0],
+                                  [0, 0, 1, 1, 1],
+                                  [1, 0, 1, 0, 0],
+                                  [0, 0, 0, 1, 0],
+                                  [0, 1, 0, 0, 0] ]
+
+        self.assertTrue(sm.basis_matrix() == expected_basis_matrix)
+
+
+    def testOptimal(self):
+        sm = LinearProgramming.SimplexIteration()
+        sm.objective_coefficients = [ 5, 100, 30, 10, 20, 300, 0, 0 ]
+        sm.solution_vector = [300, 0, 200, 100, 300, 0, 0, 200]
+        sm.basic_variables = "x1,x3,x4,x5,x8"
+
+        sm.constraint_matrix = [ [1, 1, 1, 0, 0, 0, 1, 0],
+                                 [0, 0, 0, 1, 1, 1, 0, 1],
+                                 [1, 0, 0, 1, 0, 0, 0, 0],
+                                 [0, 1, 0, 0, 1, 0, 0, 0],
+                                 [0, 0, 1, 0, 0, 1, 0, 0] ]
+
+        self.assertTrue(sm.optimal)
+
+        
+    def testNonOptimal(self):
+        sm = LinearProgramming.SimplexIteration()
+        sm.objective_coefficients = [ 5, 100, 30, 10, 20, 300, 0, 0 ]
+        sm.solution_vector = [0, 100, 200, 400, 200, 0, 200, 0]
+        sm.basic_variables = "x2,x3,x4,x5,x7"
+
+        sm.constraint_matrix = [ [1, 1, 1, 0, 0, 0, 1, 0],
+                                 [0, 0, 0, 1, 1, 1, 0, 1],
+                                 [1, 0, 0, 1, 0, 0, 0, 0],
+                                 [0, 1, 0, 0, 1, 0, 0, 0],
+                                 [0, 0, 1, 0, 0, 1, 0, 0] ]
+
+        self.assertTrue(not sm.optimal)
+
+
+def suite():
+    suite = unittest.TestSuite()
+    suite.addTest(unittest.makeSuite(SimplexIterationTest))
+    return suite