Skip to content

Commit

Permalink
[coin-or/pulp/586] Fix and add test. Remove deprecation message for m…
Browse files Browse the repository at this point in the history
…odel.addConstr
  • Loading branch information
torressa committed Oct 4, 2022
1 parent 70c156d commit 72af8a1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 33 deletions.
65 changes: 33 additions & 32 deletions pulp/apis/gurobi_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
Expand Down Expand Up @@ -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):
Expand Down
10 changes: 9 additions & 1 deletion pulp/tests/test_pulp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand Down

0 comments on commit 72af8a1

Please sign in to comment.