attribute error '__cindex__' using absolute value and square root
AnsweredHello!
I'm trying to implement a maximization of the following function: \sqrt(\abs(I)) + \sqrt(\abs(J)), where I and J are linear combinations of other variables. For that, I have add auxiliary variables for I and J and then I add also auxiliary variables for the absolute value (Iabs and Jabs) and for the square root of them (Isqrt and Jsqrt) and add the correspond constraints as follows:
m.addGenConstrAbs(Iabs,I)m.addGenConstrAbs(Jabs,J)m.addGenConstrPow(Iabs,Isqrt,0.5)m.addGenConstrPow(Jabs,Jsqrt,0.5)
However I got the following error: AttributeError: 'gurobipy.LinExpr' object has no attribute '__cindex__'
Do you know what am I doing wrong? Thank you!
-
Official comment
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 try Gurobot, our chatbot interface offering instant, expert-level support. -
Hi María,
How did you add your auxiliary variables?
Doing like thisimport gurobipy as gp
m = gp.Model("abs")
x = m.addVars(5, vtype = gp.GRB.CONTINUOUS, name = "x")
Iabs = m.addVar(vtype = gp.GRB.CONTINUOUS, lb = 0, name = "Iabs")
I = gp.quicksum(x[i] for i in range(4))
m.addGenConstrAbs(Iabs, I, "abs")would result in the error you mentioned.
You need to add the auxiliary variable and the constraint also to the model:
import gurobipy as gp
m = gp.Model("abs")
x = m.addVars(5, vtype = gp.GRB.CONTINUOUS, name = "x")
Iabs = m.addVar(vtype = gp.GRB.CONTINUOUS, lb = 0, name = "y")
I = m.addVar(vtype = gp.GRB.CONTINUOUS, name = "I")
m.addConstr(I == gp.quicksum(x[i] for i in range(4)))
m.addGenConstrAbs(Iabs, I, "abs")I hope this helps,
Marika0 -
Thank you very much! It has been very helpful!
0
Post is closed for comments.
Comments
3 comments