Example #1
0
 def test_logPath(self):
     name = self._testMethodName
     prob = LpProblem(name, const.LpMinimize)
     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"
     logFilename = name + ".log"
     self.solver.optionsDict["logPath"] = logFilename
     if self.solver.name in [
         "CPLEX_PY",
         "CPLEX_CMD",
         "GUROBI",
         "GUROBI_CMD",
         "PULP_CBC_CMD",
         "COIN_CMD",
     ]:
         print("\t Testing logPath argument")
         pulpTestCheck(
             prob, self.solver, [const.LpStatusOptimal], {x: 4, y: -1, z: 6, w: 0}
         )
         if not os.path.exists(logFilename):
             raise PulpError("Test failed for solver: {}".format(self.solver))
         if not os.path.getsize(logFilename):
             raise PulpError("Test failed for solver: {}".format(self.solver))
Example #2
0
def pulpTestCheck(prob,
                  solver,
                  okstatus,
                  sol=None,
                  reducedcosts=None,
                  duals=None,
                  slacks=None,
                  eps=10**-3,
                  status=None,
                  objective=None,
                  **kwargs):
    if status is None:
        status = prob.solve(solver, **kwargs)
    if status not in okstatus:
        dumpTestProblem(prob)
        raise PulpError(
            "Tests failed for solver {}:\nstatus == {} not in {}\nstatus == {} not in {}"
            .format(solver, status, okstatus, const.LpStatus[status],
                    [const.LpStatus[s] for s in okstatus]))
    if sol is not None:
        for v, x in sol.items():
            if abs(v.varValue - x) > eps:
                dumpTestProblem(prob)
                raise PulpError(
                    "Tests failed for solver {}:\nvar {} == {} != {}".format(
                        solver, v, v.varValue, x))
    if reducedcosts:
        for v, dj in reducedcosts.items():
            if abs(v.dj - dj) > eps:
                dumpTestProblem(prob)
                raise PulpError(
                    "Tests failed for solver {}:\nTest failed: var.dj {} == {} != {}"
                    .format(solver, v, v.dj, dj))
    if duals:
        for cname, p in duals.items():
            c = prob.constraints[cname]
            if abs(c.pi - p) > eps:
                dumpTestProblem(prob)
                raise PulpError(
                    "Tests failed for solver {}:\nconstraint.pi {} == {} != {}"
                    .format(solver, cname, c.pi, p))
    if slacks:
        for cname, slack in slacks.items():
            c = prob.constraints[cname]
            if abs(c.slack - slack) > eps:
                dumpTestProblem(prob)
                raise PulpError(
                    "Tests failed for solver {}:\nconstraint.slack {} == {} != {}"
                    .format(solver, cname, c.slack, slack))
    if objective is not None:
        z = prob.objective.value()
        if abs(z - objective) > eps:
            dumpTestProblem(prob)
            raise PulpError(
                "Tests failed for solver {}:\nobjective {} != {}".format(
                    solver, z, objective))
Example #3
0
def pulpTestCheck(prob,
                  solver,
                  okstatus,
                  sol=None,
                  reducedcosts=None,
                  duals=None,
                  slacks=None,
                  eps=10**-3,
                  status=None,
                  objective=None,
                  **kwargs):
    if status is None:
        status = prob.solve(solver, **kwargs)
    if status not in okstatus:
        dumpTestProblem(prob)
        print("Failure: status ==", status, "not in", okstatus)
        print("Failure: status ==", const.LpStatus[status], "not in", \
              [const.LpStatus[s] for s in okstatus])
        raise PulpError("Tests failed for solver %s" % solver)
    if sol is not None:
        for v, x in sol.items():
            if abs(v.varValue - x) > eps:
                dumpTestProblem(prob)
                print("Test failed: var", v, "==", v.varValue, "!=", x)
                raise PulpError("Tests failed for solver %s" % solver)
    if reducedcosts:
        for v, dj in reducedcosts.items():
            if abs(v.dj - dj) > eps:
                dumpTestProblem(prob)
                print("Test failed: var.dj", v, "==", v.dj, "!=", dj)
                raise PulpError("Tests failed for solver %s" % solver)
    if duals:
        for cname, p in duals.items():
            c = prob.constraints[cname]
            if abs(c.pi - p) > eps:
                dumpTestProblem(prob)
                print("Test failed: constraint.pi", cname, "==", c.pi, "!=", p)
                raise PulpError("Tests failed for solver %s" % solver)
    if slacks:
        for cname, slack in slacks.items():
            c = prob.constraints[cname]
            if abs(c.slack - slack) > eps:
                dumpTestProblem(prob)
                print("Test failed: constraint.slack", cname, "==", c.slack,
                      "!=", slack)
                raise PulpError("Tests failed for solver %s" % solver)
    if objective is not None:
        z = prob.objective.value()
        if abs(z - objective) > eps:
            dumpTestProblem(prob)
            print("Test failed: objective ", z, " != ", objective)
            raise PulpError("Tests failed for solver %s" % solver)