arctan in gUrobi optimization (python)
AnsweredHello,
how can I add this constraint?
for i in N:
for e in M:
b[i][e]=arctan(P[i] - Pc[i][e] * x [i,e])
where x is the binary variable, Pc is the decision variable, and P is a constant.
error (TypeError: must be real number, not gurobipy.LinExpr)
Thanks
0
-
Hi Chaymae,
Currently, the \(\arctan\) function is not supported in Gurobi. The only way to add your constraint would be to use an approximation of the \(\arctan\) function. You could use the addGenConstrPWL method to construct a piecewise-linear approximation.
import gurobipy as gp
from gurobipy import GRB
import math
def f(u):
return math.atan(u)
m = gp.Model()
lbz = -2
ubz = 2
z = m.addVar(lb=lbz, ub=ubz, vtype=GRB.CONTINUOUS, name="z")
y = m.addVar(lb=-math.pi/2, ub=math.pi/2, vtype=GRB.CONTINUOUS, name="y")
# Compute piecewise-linear arctan function for z
npts = 101
ptu = []
ptf = []
for i in range(npts):
ptu.append(lbz + (ubz - lbz) * i / (npts - 1))
ptf.append(f(ptu[i]))
# Add constraint y = arctan(z) as piecewise-linear approximation
m.addGenConstrPWL(z,y,ptu,ptf,"PWLarctan")Best regards,
Jaromił0
Please sign in to leave a comment.
Comments
1 comment