Skip to content

Commit

Permalink
[#93] Implement basic NLExpr support
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbowly committed Nov 14, 2024
1 parent a37a03d commit 5e18882
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/gurobipy_pandas/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from gurobipy_pandas.util import create_names, gppd_global_options

CONSTRAINT_SENSES = frozenset([GRB.LESS_EQUAL, GRB.EQUAL, GRB.GREATER_EQUAL])
GUROBIPY_MAJOR_VERSION, *_ = gp.gurobi.version()


def add_constrs_from_dataframe(
Expand Down Expand Up @@ -138,6 +139,15 @@ def _add_constr(model, lhs, sense, rhs, name):
name = ""
if not isinstance(sense, str) or sense[0] not in CONSTRAINT_SENSES:
raise ValueError(f"'{sense}' is not a valid constraint sense")
if GUROBIPY_MAJOR_VERSION >= 12:
# Check for nonlinear constraints; accept only y = f(x)
if isinstance(lhs, gp.NLExpr):
raise ValueError("Nonlinear constraints must be in the form y = f(x)")
if isinstance(rhs, gp.NLExpr):
if isinstance(lhs, gp.Var) and sense == GRB.EQUAL:
return model.addGenConstrNL(lhs, rhs, name=name)
else:
raise ValueError("Nonlinear constraints must be in the form y = f(x)")
if isinstance(lhs, gp.QuadExpr) or isinstance(rhs, gp.QuadExpr):
return model.addQConstr(lhs, sense, rhs, name=name)
return model.addLConstr(lhs, sense, rhs, name=name)
Expand Down

0 comments on commit 5e18882

Please sign in to comment.