Modeling square root of squared variable
AnsweredDear community,
I try to model the following objective function in Python:

x is the only variable, a and b are parameters.
I tried the following but it is not working properly:
x = m.addVars(i_max, j_max, vtype=GRB.CONTINUOUS, name='x')
# use z as auxiliary variable for (a+b*x)
z = m.addVars(k_max, 1, vtype=GRB.CONTINUOUS, name='z')
for k in range(0, k_max):
z[k,0] = gp.quicksum(
(a['a'][i,j,k] + b['b'][j,k] * x[i,j])
for j in range(0, j_max)
for i in range(0, i_max)
)
z2 = m.addVar(vtype=GRB.CONTINUOUS, name='z2')
z2 = gp.quicksum( z[k,0] * z[k,0] for k in range(0, k_max) )
z3 = m.addVar(vtype=GRB.CONTINUOUS, name='z3')
c = m.addGenConstrLogA(z2, z3, 0.5, "c")
obj = gp.quicksum(
(z[k,0] / z3)
for k in range(0, k_max)
)
model.setObjective(obj, GRB.MINIMIZE)
model.optimize()
Thanks for your help!
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 DR,
I cannot reproduce the exact issue you might be seeing because there are several definitions in your code which are not provided, e.g. i_max, j_max, k_max, a, b, etc
However I can see an error where you are using z2 as an argument in
c = m.addGenConstrLogA(z2, z3, 0.5, "c")
Both the first two arguments need to be Gurobi variables. You initially define z2 to be a Gurobi variable but then you overwrite on the next line, resulting in it being an expression. You are probably wanting to do the following instead.
z2 = m.addVar(vtype=GRB.CONTINUOUS, name='z2')
m.addConstr(z2 == gp.quicksum( z[k,0] * z[k,0] for k in range(0, k_max) )- Riley
1 -
Thanks a lot, Riley!
0
Post is closed for comments.
Comments
3 comments