コード例 #1
0
 def test_export_dict_LP_no_obj(self):
     prob = LpProblem("test_export_dict_LP_no_obj", const.LpMinimize)
     x = LpVariable("x", 0, 4)
     y = LpVariable("y", -1, 1)
     z = LpVariable("z", 0)
     w = LpVariable("w", 0, 0)
     prob += x + y >= 5, "c1"
     prob += x + z == 10, "c2"
     prob += -y + z <= 7, "c3"
     prob += w >= 0, "c4"
     data = prob.toDict()
     var1, prob1 = LpProblem.fromDict(data)
     x, y, z, w = [var1[name] for name in ["x", "y", "z", "w"]]
     print("\t Testing export dict for LP")
     pulpTestCheck(
         prob1, self.solver, [const.LpStatusOptimal], {x: 4, y: 1, z: 6, w: 0}
     )
コード例 #2
0
 def test_export_dict_max(self):
     prob = LpProblem("test_export_dict_max", const.LpMaximize)
     x = LpVariable("x", 0, 4)
     y = LpVariable("y", -1, 1)
     z = LpVariable("z", 0)
     w = LpVariable("w", 0)
     prob += x + 4 * y + 9 * z, "obj"
     prob += x + y <= 5, "c1"
     prob += x + z >= 10, "c2"
     prob += -y + z == 7, "c3"
     prob += w >= 0, "c4"
     data = prob.toDict()
     var1, prob1 = LpProblem.fromDict(data)
     x, y, z, w = [var1[name] for name in ["x", "y", "z", "w"]]
     print("\t Testing maximize continuous LP solution")
     pulpTestCheck(
         prob1, self.solver, [const.LpStatusOptimal], {x: 4, y: 1, z: 8, w: 0}
     )
コード例 #3
0
    def test_export_dict_MIP(self):
        import copy

        prob = LpProblem("test_export_dict_MIP", const.LpMinimize)
        x = LpVariable("x", 0, 4)
        y = LpVariable("y", -1, 1)
        z = LpVariable("z", 0, None, const.LpInteger)
        prob += x + 4 * y + 9 * z, "obj"
        prob += x + y <= 5, "c1"
        prob += x + z >= 10, "c2"
        prob += -y + z == 7.5, "c3"
        data = prob.toDict()
        data_backup = copy.deepcopy(data)
        var1, prob1 = LpProblem.fromDict(data)
        x, y, z = [var1[name] for name in ["x", "y", "z"]]
        print("\t Testing export dict MIP")
        pulpTestCheck(
            prob1, self.solver, [const.LpStatusOptimal], {x: 3, y: -0.5, z: 7}
        )
        # we also test that we have not modified the dictionary when importing it
        self.assertDictEqual(data, data_backup)