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
-
Official comment
This post is more than two 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 try Gurobot, our chatbot interface offering instant, expert-level support. -
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
Post is closed for comments.
Comments
2 comments