Пример #1
0
def builder_from_file(filepath):

    obj, constraints = read(filepath)

    builder = TableauBuilder()
    for left_side, ctype, right_side in constraints:
        if ctype == "<=":
            builder.add_constraint(LessEqualThan(left_side, right_side))
        elif ctype == ">=":
            builder.add_constraint(GreaterEqualThan(left_side, right_side))
        else:
            raise NotImplementedError

    builder.set_objective(obj)
    return builder
Пример #2
0
    def test_str_2(self):
        builder = TableauBuilder()

        self.tableau = builder.add_constraint(GreaterEqualThan([2, 5], 30))\
            .set_objective([5, 10])\
            .get()
        self.assertEqual(str(self.tableau), "[x_1, x_2, _s_1, _a_1]\n"
                                            "[2.0, 5.0, -1.0, 1.0, 30.0]\n"
                                            "[-5.0, -10.0, 0.0, 1000.0, 0.0]")
Пример #3
0
class Test(unittest.TestCase):
    def setUp(self):
        self.t2 = TableauBuilder()
        self.tableau = self.t2.add_constraint(GreaterEqualThan([2,  5], 30)).\
            add_constraint(GreaterEqualThan([-3,  5], 5)).\
            add_constraint(LessEqualThan([8, 3], 85)).\
            add_constraint(LessEqualThan([-9, 7], 42)).\
            set_objective([2, 7]).get()

    """
    @unittest.skip
    def test_pivot_step_0(self):
        self.assertEqual([1, 1], find_pivot_from_row(self.tableau.table))
    """

    def test_pivot_step_1(self):
        self.assertEqual([1, 1], find_pivot(self.tableau.table))
Пример #4
0
class Test(unittest.TestCase):
    def setUp(self):
        self.t3 = TableauBuilder()
        self.t3.add_constraint(LessEqualThan([2, 1], 18))
        self.t3.add_constraint(LessEqualThan([2, 3], 42))
        self.t3.add_constraint(LessEqualThan([3, 1], 24))
        self.t3.set_objective([3, 2])

        self.var_names = ['x_1', 'x_2', '_s_1', '_s_2', '_s_3']

    @unittest.skip
    def test_variable_name(self):
        t = self.t3.get()
        res = PlainTableau(t.table, model_vars=['x_1',
                                                'x_2']).get_variable_names()
        self.assertEqual(['x1', 'x2'], res)

    def test_example4(self):
        t = self.t3.get()
        tbl = PlainTableau(t.table,
                           var_names=self.var_names,
                           model_vars=['x_1', 'x_2'],
                           base_var_indices=[2, 3, 4])

        res = tbl.var_result(only_model_vars=True)
        # print(res)
        self.assertEqual(VariableValues.from_dict({'x_1': 0, 'x_2': 0}), res)

    def test_step(self):
        t = self.t3.get()
        # print(t)
        t2, pp = Optimization.simplex_step(t.table)

        res = PlainTableau(
            t2,
            var_names=self.var_names,
            model_vars=['x_1', 'x_2'],
            base_var_indices=[0, 2, 3]).var_result(only_model_vars=True)

        self.assertEqual(VariableValues.from_dict({'x_1': 8.0, 'x_2': 0}), res)

    def test_step_2(self):
        t = self.t3.get()
        t2, pp = Optimization.simplex_step(t.table)
        t3, pp = Optimization.simplex_step(t2)

        res = PlainTableau(
            t3,
            var_names=self.var_names,
            model_vars=['x_1', 'x_2'],
            base_var_indices=[0, 1, 3]).var_result(only_model_vars=True)
        self.assertEqual(
            VariableValues.from_dict({
                'x_1': 6.0,
                'x_2': 5.999999999999999
            }), res)

    def test_step_3(self):
        t = self.t3.get()
        t2, pp = Optimization.simplex_step(t.table)
        t3, pp = Optimization.simplex_step(t2)
        t4, pp = Optimization.simplex_step(t3)

        res = PlainTableau(
            t4,
            var_names=self.var_names,
            model_vars=['x_1', 'x_2'],
            base_var_indices=[0, 1, 4]).var_result(only_model_vars=True)
        self.assertEqual(
            VariableValues.from_dict({
                'x_1': 2.9999999999999996,
                'x_2': 12.0
            }), res)

    def test_step_4(self):
        t = self.t3.get()
        t2, pp = Optimization.simplex_step(t.table)
        t3, pp = Optimization.simplex_step(t2)
        t4, pp = Optimization.simplex_step(t3)
        t5, pp = Optimization.simplex_step(t4)

        res = PlainTableau(
            t5,
            var_names=self.var_names,
            model_vars=['x_1', 'x_2'],
            base_var_indices=[0, 1, 4]).var_result(only_model_vars=True)
        self.assertEqual(
            VariableValues.from_dict({
                'x_1': 2.9999999999999996,
                'x_2': 12.0
            }), res)
Пример #5
0
 def setUp(self):
     tb = TableauBuilder()
     self.tableau = tb.add_constraint(GreaterEqualThan([2, -1], 10))\
         .add_constraint(LessEqualThan([1, 1], 20))\
         .set_objective([5, 10]).get()