Skip to content

Commit

Permalink
Merge pull request Pyomo#3350 from quantresearch1/gurobipy_addconstr_…
Browse files Browse the repository at this point in the history
…deprecated

Remove deprecated gurobipy addConstr call
  • Loading branch information
mrmundt authored Aug 28, 2024
2 parents 99a9c77 + 79f4954 commit cda2d02
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
43 changes: 25 additions & 18 deletions pyomo/solvers/plugins/solvers/gurobi_direct.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
from pyomo.opt.results.solver import TerminationCondition, SolverStatus
from pyomo.opt.base import SolverFactory
from pyomo.core.base.suffix import Suffix
import pyomo.core.base.var


logger = logging.getLogger('pyomo.solvers')
Expand Down Expand Up @@ -308,7 +307,7 @@ def _get_expr_from_pyomo_repn(self, repn, max_degree=2):

new_expr += repn.constant

return new_expr, referenced_vars
return new_expr, referenced_vars, degree

def _get_expr_from_pyomo_expr(self, expr, max_degree=2):
if max_degree == 2:
Expand All @@ -317,15 +316,15 @@ def _get_expr_from_pyomo_expr(self, expr, max_degree=2):
repn = generate_standard_repn(expr, quadratic=False)

try:
gurobi_expr, referenced_vars = self._get_expr_from_pyomo_repn(
gurobi_expr, referenced_vars, degree = self._get_expr_from_pyomo_repn(
repn, max_degree
)
except DegreeError as e:
msg = e.args[0]
msg += '\nexpr: {0}'.format(expr)
raise DegreeError(msg)

return gurobi_expr, referenced_vars
return gurobi_expr, referenced_vars, degree

def _gurobi_lb_ub_from_var(self, var):
if var.is_fixed():
Expand Down Expand Up @@ -404,10 +403,12 @@ def _create_model(self, model):
self._init_env()
if self._solver_model is not None:
self._solver_model.close()
if model.name is not None:
self._solver_model = gurobipy.Model(model.name, env=self._env)
else:
self._solver_model = gurobipy.Model(env=self._env)

self._solver_model = (
gurobipy.Model(model.name, env=self._env)
if model.name is not None
else gurobipy.Model(env=self._env)
)

def close(self):
"""Frees local Gurobi resources used by this solver instance.
Expand Down Expand Up @@ -489,6 +490,13 @@ def _set_instance(self, model, kwds={}):
def _add_block(self, block):
DirectOrPersistentSolver._add_block(self, block)

def _addConstr(self, degree, lhs, sense=None, rhs=None, name=""):
if degree == 2:
con = self._solver_model.addQConstr(lhs, sense, rhs, name)
else:
con = self._solver_model.addLConstr(lhs, sense, rhs, name)
return con

def _add_constraint(self, con):
if not con.active:
return None
Expand All @@ -499,15 +507,11 @@ def _add_constraint(self, con):
conname = self._symbol_map.getSymbol(con, self._labeler)

if con._linear_canonical_form:
gurobi_expr, referenced_vars = self._get_expr_from_pyomo_repn(
gurobi_expr, referenced_vars, degree = self._get_expr_from_pyomo_repn(
con.canonical_form(), self._max_constraint_degree
)
# elif isinstance(con, LinearCanonicalRepn):
# gurobi_expr, referenced_vars = self._get_expr_from_pyomo_repn(
# con,
# self._max_constraint_degree)
else:
gurobi_expr, referenced_vars = self._get_expr_from_pyomo_expr(
gurobi_expr, referenced_vars, degree = self._get_expr_from_pyomo_expr(
con.body, self._max_constraint_degree
)

Expand All @@ -523,7 +527,8 @@ def _add_constraint(self, con):
)

if con.equality:
gurobipy_con = self._solver_model.addConstr(
gurobipy_con = self._addConstr(
degree=degree,
lhs=gurobi_expr,
sense=gurobipy.GRB.EQUAL,
rhs=value(con.lower),
Expand All @@ -535,14 +540,16 @@ def _add_constraint(self, con):
)
self._range_constraints.add(con)
elif con.has_lb():
gurobipy_con = self._solver_model.addConstr(
gurobipy_con = self._addConstr(
degree=degree,
lhs=gurobi_expr,
sense=gurobipy.GRB.GREATER_EQUAL,
rhs=value(con.lower),
name=conname,
)
elif con.has_ub():
gurobipy_con = self._solver_model.addConstr(
gurobipy_con = self._addConstr(
degree=degree,
lhs=gurobi_expr,
sense=gurobipy.GRB.LESS_EQUAL,
rhs=value(con.upper),
Expand Down Expand Up @@ -636,7 +643,7 @@ def _set_objective(self, obj):
else:
raise ValueError('Objective sense is not recognized: {0}'.format(obj.sense))

gurobi_expr, referenced_vars = self._get_expr_from_pyomo_expr(
gurobi_expr, referenced_vars, degree = self._get_expr_from_pyomo_expr(
obj.expr, self._max_obj_degree
)

Expand Down
4 changes: 2 additions & 2 deletions pyomo/solvers/plugins/solvers/gurobi_persistent.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ def cbCut(self, con):
if is_fixed(con.body):
raise ValueError('cbCut expected a non-trivial constraint')

gurobi_expr, referenced_vars = self._get_expr_from_pyomo_expr(
gurobi_expr, referenced_vars, degree = self._get_expr_from_pyomo_expr(
con.body, self._max_constraint_degree
)

Expand Down Expand Up @@ -656,7 +656,7 @@ def cbLazy(self, con):
if is_fixed(con.body):
raise ValueError('cbLazy expected a non-trivial constraint')

gurobi_expr, referenced_vars = self._get_expr_from_pyomo_expr(
gurobi_expr, referenced_vars, degree = self._get_expr_from_pyomo_expr(
con.body, self._max_constraint_degree
)

Expand Down

0 comments on commit cda2d02

Please sign in to comment.