How to linearize the nonlinear constraint in modelling
回答済みhi gurobi teams:
I am trying to use PWL method on tackling my nonlinear constraints, e.g., z^2=x^2-y^2
I have went through an example on the gurobi portal and found this:
import gurobipy as gp
from math import exp
def f(u):
return exp(-u)
def g(u):
return 2 * u * u - 4 * u
try:
# Create a new model
m = gp.Model()
# Create variables
lb = 0.0
ub = 1.0
x = m.addVar(lb, ub, name='x')
y = m.addVar(lb, ub, name='y')
z = m.addVar(lb, ub, name='z')
# Set objective for y
m.setObjective(-y)
# Add piecewise-linear objective functions for x and z
npts = 101
ptu = []
ptf = []
ptg = []
for i in range(npts):
ptu.append(lb + (ub - lb) * i / (npts - 1))
ptf.append(f(ptu[i]))
ptg.append(g(ptu[i]))
m.setPWLObj(x, ptu, ptf)
m.setPWLObj(z, ptu, ptg)
# Add constraint: x + 2 y + 3 z <= 4
m.addConstr(x + 2 * y + 3 * z <= 4, 'c0')
# Add constraint: x + y >= 1
m.addConstr(x + y >= 1, 'c1')
# Optimize model as an LP
m.optimize
It seems like the nonlinear term in objective function is linearized, but what should I do if I want linearize the nonlinear in constraints?
many thanks!
-
正式なコメント
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?. -
Hi,
Could you please elaborate why you have to linearize the nonlinear terms on your own?
Gurobi can solve non-convex quadratically constrained problem meaning that you can introduce the term \(z^2 = x^2 - y^2\) as a constraint via
m.addConstr(z*z == x*x - y*y, name="myConstr")
For terms such as \(\exp\) or \(\log\), you can use the general constraint methods.
Best regards,
Jaromił0 -
Hi Jaromił
Thanks for you replying. If I try NLP in my model, it will be tough to solve as there are a large amount of variables. So that is why I have to linearise it. I tried SOCP before and it was fast but the accuracy is not good, so I am going to use PWL.
Many thanks!
0
投稿コメントは受け付けていません。
コメント
3件のコメント