diff --git a/pyomo/solvers/plugins/solvers/gurobi_direct.py b/pyomo/solvers/plugins/solvers/gurobi_direct.py index ed66a4e0e7b..9cd81ba8a55 100644 --- a/pyomo/solvers/plugins/solvers/gurobi_direct.py +++ b/pyomo/solvers/plugins/solvers/gurobi_direct.py @@ -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') @@ -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: @@ -317,7 +316,7 @@ 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: @@ -325,7 +324,7 @@ def _get_expr_from_pyomo_expr(self, expr, max_degree=2): 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(): @@ -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. @@ -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 @@ -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 ) @@ -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), @@ -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), @@ -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 ) diff --git a/pyomo/solvers/plugins/solvers/gurobi_persistent.py b/pyomo/solvers/plugins/solvers/gurobi_persistent.py index 94a2ac6b734..447d1de9b40 100644 --- a/pyomo/solvers/plugins/solvers/gurobi_persistent.py +++ b/pyomo/solvers/plugins/solvers/gurobi_persistent.py @@ -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 ) @@ -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 )