Using math.sqrt in m.addConstr

1,214 views
Skip to first unread message

Assyncronimous

unread,
Apr 23, 2017, 8:16:52 AM4/23/17
to Gurobi Optimization
Hi; I need to have a squareroot of a decision variable which is also a function with absolute value. When I run the program, Gurobi return with '' m.addConstr(STD == math.sqrt(VAR))

TypeError: a float is required'' error. I tried to introduce some slack variables and make the calculation step by step however could not be able to figure it out. I would be glad if someone can hand in? 

Thanks,

Here is the part which creates the problem

z = {}
for t in range(0,Yearly_Period):  
    z[t] = m.addVar(lb=-GRB.INFINITY,ub=GRB.INFINITY,vtype=GRB.CONTINUOUS)
    m.addConstr(z[t] == EG[t]+STG[t]-PPA)
    m.addGenConstrAbs(PEN[t], z[t], "PEN Constraint")

m.addConstr(VAR == quicksum(PEN[t] for t in range(Yearly_Period)) / Yearly_Period)
m.addConstr(STD == math.sqrt(VAR))


Daniel Espinoza

unread,
Apr 24, 2017, 9:22:03 AM4/24/17
to Gurobi Optimization
Hi,

Gurobi is not able to handle directly square roots of variables, however, in your case you could use

m.addConstr(STD*STD == VAR)

instead

Assyncronimous

unread,
Apr 25, 2017, 1:13:30 PM4/25/17
to Gurobi Optimization
Thank you Daniel, I think this way it will work out. I really appreciate your Company supports all the time, thank you!

Regards,
Enes
Message has been deleted

Daniel Espinoza

unread,
Apr 26, 2017, 10:55:21 AM4/26/17
to Gurobi Optimization
Hi Enes,

The problem is that a quadratic equality constraint is not convex.
Probably you can use the following:

m.addQConstr(VAR >= quicksum(PEN[t]*PEN[t] for t in range(Yearly_Period)) / Yearly_Period)
m.addQConstr(STD*STD >= VAR)

or even better (for the last part)

VAR.Ub = STD * STD

The meaning would be slightly different for this model, but it should answer the same base problem.

Best
Daniel

Assyncronimous

unread,
Apr 26, 2017, 2:15:19 PM4/26/17
to Gurobi Optimization
Hi again :);


Gurobi returns with error after converting the equation :

m.addQConstr(VAR == quicksum(PEN[t]*PEN[t] for t in range(Yearly_Period)) / Yearly_Period)
m.addQConstr(STD*STD == VAR)

GurobiError: Quadratic equality constraints

How do you think I can solve this problem and what is the main cause?

Thanks,
Enes
Reply all
Reply to author
Forward
0 new messages