Example #1
0
    def test_simplex_2b(self):
        # Now check for LEAST enjoyable diet.
        logging.info("\nFINDING VERTEX FOR 2(b) BY FLIPPING SIGN ON c")
        self.c *= -1.0
        x = undertest.all_inequality(self.c, self.x_0, self.constraints)

        # Now check for optimality.
        original_constraint_idxs = range(4)
        self.assertTrue(np.all(
            np.dot(self.A, x) >= _build_ndarray(self.b, original_constraint_idxs)))
        logging.info("Checking vertex for 2(b). Ax = {0}, b = {1}".format(
            np.dot(self.A, x), self.b))
Example #2
0
    def test_simplex_2a(self):
        """
        Use the initial vertex x_0 to run simplex.
        """
        logging.info("\nFINDING VERTEX FOR 2(a)")
        x = undertest.all_inequality(self.c, self.x_0, self.constraints)

        # Now check for optimality. x should satisfy constraints and yield non-negative lambda.
        original_constraint_idxs = range(4)
        self.assertTrue(np.all(
            np.dot(self.A, x) >= _build_ndarray(self.b, original_constraint_idxs)))
        logging.info("Checking vertex for 2(a). Ax = {0}, b = {1}".format(
            np.dot(self.A, x), self.b))
Example #3
0
    def test_simplex_toy_problem(self):
        """
        The notes include a toy problem. Test the simplex method on this problem, make sure it
        returns the right solution.
        """
        raw_constraints = [[0, -1], [1, 2], [-1, 1], [1, -1]]
        b = [-1, 0, -1, -1]
        working_set = [0, 2]
        constraints = undertest.Constraints(raw_constraints, b, working_set)
        c = np.array([[1, -1/3]]).T
        x_0 = np.array([2, 1])

        # Check the optimal solution
        x = undertest.all_inequality(c, x_0, constraints)
        nptst.assert_array_almost_equal(x, np.array([-2/3, 1/3]))