NLP and MINLP in gurobipy
AnsweredDear All,
Does python-based gurobi (gurobipy) support non linear programming (NLP) and mixed integer non linear programming (MINLP) models?
-
Hi Rahmat,
Yes, you can build non-linear models using gurobipy. Please have a look at our Python API documentation, particularly, the details on Nonlinear Expression Helper Functions. In addition, you may find the following examples useful:
- genconstrnl: Demonstrates the use of general nonlinear constraints.
- gc_funcnonlinear: Builds and solves a simple nonlinear model.
- qp: Builds a trivial QP model, solves it, converts it to an MIQP model, and solves it again.
- qcp: Builds and solves a trivial QCP model.
Regards,
Simran0 -
Thank you Simran,
I created a simple NLP model using google colab but got the following error GurobiError: Objective must be linear or quadratic
The script:
!pip install gurobipyimport gurobipy as gpfrom gurobipy import GRBm = gp.Model("NLP")
x1 = m.addVar(vtype=GRB.CONTINUOUS, lb = 1, ub = 3,name="x1")x2 = m.addVar(vtype=GRB.CONTINUOUS, lb = 1, ub = 3,name="x2")x3 = m.addVar(vtype=GRB.CONTINUOUS, lb = 1, ub = 3,name="x3")x4 = m.addVar(vtype=GRB.CONTINUOUS, lb = 1, ub = 3,name="x4")
OF = x1*x4*(x1+x2+x3)+x2
m.setObjective(OF,GRB.MAXIMIZE)
m.addConstr(x1*x2*x3*x4 >= 20)m.addConstr( x1**2 + x2**2 + x3**2 + x4**2 == 30)
m.optimize()But when the model was written using AIMMS, Gurobi was able to solve the model.0 -
Hi Rahmat,
Gurobi supports linear and quadratic objective functions. If the objective expression is non-linear, one can store it in an auxiliary variable and optimize that variable. In your example, we can add the constraint
m.addConstr( z == x1*x4*(x1+x2+x3)+x2)
and maximize z. AIMMS might be doing this transformation internally.
In addition, please note that it is not possible to use <= or >= operators to specify nonlinear inequality constraints using NLExpr objects. In your example, the constraint m.addConstr(x1*x2*x3*x4 >= 20) can be replaced by
m.addConstr( y == x1*x2*x3*x4 )
m.addConstr( y >= 20)where y is an auxiliary variable. For more details, please see the documentation of NLExpr objects.Regards,
Simran0 -
Hi Simran
Thank you very much the explanation.
For Nonlinear Expression Helper Functions, are there functions for inverse trigonomteri?
0 -
Hi Rahmat,
We don't currently have helper functions for inverse trigonometric functions. However, you can model them using simple trigometric transformations, such as \( y = arcsin(x) <=> x = sin(y) \) for \( -\pi/2 <= y <= \pi/2 \).
Regards,
Simran0 -
Hi Simran
Thank you very much the explanation.
0
Please sign in to leave a comment.
Comments
6 comments