From 72af8a1e9ac3647d5a7b19c756c6894a8857067f Mon Sep 17 00:00:00 2001 From: torressa <23246013+torressa@users.noreply.github.com> Date: Mon, 3 Oct 2022 17:50:39 +0100 Subject: [PATCH] [coin-or/pulp/586] Fix and add test. Remove deprecation message for model.addConstr --- pulp/apis/gurobi_api.py | 65 +++++++++++++++++++++-------------------- pulp/tests/test_pulp.py | 10 ++++++- 2 files changed, 42 insertions(+), 33 deletions(-) diff --git a/pulp/apis/gurobi_api.py b/pulp/apis/gurobi_api.py index 7f51cb05..8d1480da 100644 --- a/pulp/apis/gurobi_api.py +++ b/pulp/apis/gurobi_api.py @@ -158,34 +158,35 @@ def findSolutionValues(self, lp): var.isModified = False status = gurobiLpStatus.get(solutionStatus, constants.LpStatusUndefined) lp.assignStatus(status) - try: - # populate pulp solution values - for var, value in zip( - lp._variables, model.getAttr(GRB.Attr.X, model.getVars()) - ): - var.varValue = value - - # populate pulp constraints slack - for constr, value in zip( - lp.constraints.values(), - model.getAttr(GRB.Attr.Slack, model.getConstrs()), - ): - constr.slack = value - - if not model.getAttr(GRB.Attr.IsMIP): + if status in [constants.LpStatusOptimal, constants.LpStatusNotSolved]: + try: + # populate pulp solution values for var, value in zip( - lp._variables, model.getAttr(GRB.Attr.RC, model.getVars()) + lp._variables, model.getAttr(GRB.Attr.X, model.getVars()) ): - var.dj = value + var.varValue = value - # put pi and slack variables against the constraints + # populate pulp constraints slack for constr, value in zip( lp.constraints.values(), - model.getAttr(GRB.Attr.Pi, model.getConstrs()), + model.getAttr(GRB.Attr.Slack, model.getConstrs()), ): - constr.pi = value - except gp.GurobiError as e: - raise e + constr.slack = value + + if not model.getAttr(GRB.Attr.IsMIP): + for var, value in zip( + lp._variables, model.getAttr(GRB.Attr.RC, model.getVars()) + ): + var.dj = value + + # put pi and slack variables against the constraints + for constr, value in zip( + lp.constraints.values(), + model.getAttr(GRB.Attr.Pi, model.getConstrs()), + ): + constr.pi = value + except gp.GurobiError as e: + raise e return status def available(self): @@ -209,10 +210,7 @@ def initGurobi(self): self.model = gp.Model(env=self.env) # Environment handled by user or default Env else: - if self.env is not None: - self.model = gp.Model(env=self.env) - else: - self.model = gp.Model() + self.model = gp.Model(env=self.env) # Set solver parameters for param, value in self.solver_params.items(): self.model.setParam(param, value) @@ -277,16 +275,19 @@ def buildSolverModel(self, lp): list(constraint.values()), [v.solverVar for v in constraint.keys()] ) if constraint.sense == constants.LpConstraintLE: - relation = gp.GRB.LESS_EQUAL + constraint.solverConstraint = lp.solverModel.addConstr( + expr <= -constraint.constant, name=name + ) elif constraint.sense == constants.LpConstraintGE: - relation = gp.GRB.GREATER_EQUAL + constraint.solverConstraint = lp.solverModel.addConstr( + expr >= -constraint.constant, name=name + ) elif constraint.sense == constants.LpConstraintEQ: - relation = gp.GRB.EQUAL + constraint.solverConstraint = lp.solverModel.addConstr( + expr == -constraint.constant, name=name + ) else: raise PulpSolverError("Detected an invalid constraint type") - constraint.solverConstraint = lp.solverModel.addConstr( - expr, relation, -constraint.constant, name - ) lp.solverModel.update() def actualSolve(self, lp, callback=None): diff --git a/pulp/tests/test_pulp.py b/pulp/tests/test_pulp.py index f9e2d940..b3ad7845 100644 --- a/pulp/tests/test_pulp.py +++ b/pulp/tests/test_pulp.py @@ -1211,7 +1211,12 @@ def test_measuring_solving_time(self): time_limit = 10 solver_settings = dict( - PULP_CBC_CMD=30, COIN_CMD=30, SCIP_CMD=30, GUROBI_CMD=50, CPLEX_CMD=50 + PULP_CBC_CMD=30, + COIN_CMD=30, + SCIP_CMD=30, + GUROBI_CMD=50, + CPLEX_CMD=50, + GUROBI=50, ) bins = solver_settings.get(self.solver.name) if bins is None: @@ -1233,6 +1238,9 @@ def test_measuring_solving_time(self): delta=delta, msg="optimization time for solver {}".format(self.solver.name), ) + self.assertTrue(prob.objective.value() is not None) + for v in prob.variables(): + self.assertTrue(v.varValue is not None) def test_invalid_var_names(self): prob = LpProblem(self._testMethodName, const.LpMinimize)