From d9fd0480b7ef6023b97de948c050f975f9e2c91f Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Mon, 14 Jun 2010 04:14:35 -0400 Subject: [PATCH] Added the unit tests for the SimplexIteration class. --- src/Tests/Unit/SimplexIterationTest.py | 87 ++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 src/Tests/Unit/SimplexIterationTest.py diff --git a/src/Tests/Unit/SimplexIterationTest.py b/src/Tests/Unit/SimplexIterationTest.py new file mode 100644 index 0000000..1d616a0 --- /dev/null +++ b/src/Tests/Unit/SimplexIterationTest.py @@ -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 -- 2.44.2