I use gurobipy to solve a simple problem,but after adding constraints containing exponential terms and solving them, I got a wrong result

Viewed 28

I had trouble solving a simple problem with gurobi:

e^x+x=lnP
x=1

In Gurobipy,it transforms into this form:

x+y=temp
y=e^x
lnP=temp
x=1

The result is here:

Variable            X 

       x            1 
       P      749.103 
       y      2.71828 
    Temp      3.71828

The code is as follows:

from gurobipy import *

model = Model('Antoine')
P = model.addVar(vtype=GRB.CONTINUOUS, name='P',lb=0)
x = model.addVar(vtype=GRB.CONTINUOUS, name='x',lb=0)
y = model.addVar(vtype=GRB.CONTINUOUS, name='y',lb=-GRB.INFINITY)
temp = model.addVar(vtype=GRB.CONTINUOUS, name='Temp1',lb=-GRB.INFINITY)

model.addConstr(x == 1)
model.addGenConstrExp(x,y)
model.addConstr(x+y == temp)
model.addGenConstrLog(P,temp)
model.setObjective(P, GRB.MINIMIZE)
model.write("test.lp")
model.optimize()

I don't know why the result of P is wrong

1 Answers

Gurobi represents nonlinear functions by piecewise linear approximations. When I solve the original model on my computer using Gurobi Optimizer 9.5.2, I get the following warning:

Warning: max constraint violation (2.9006e+00) exceeds tolerance
Warning: max general constraint violation (2.9006e+00) exceeds tolerance
  Piecewise linearization of function constraints often causes big violation.
  Try to adjust the settings of the related parameters, such as FuncPieces.

This means the default automatic linearization is not sufficiently accurate for this model. As suggested in the warning message, adjust the FuncPieces parameter to get a more accurate representation for this model. For example, with model.Params.FuncPieces=-1 on my computer, I get this more accurate result:

    Variable            X 
-------------------------
           P        41.29 
           x            1 
           y      2.71828 
       Temp1      3.71828 
Related