a wrong result

92 views
Skip to first unread message

jack chou

unread,
Mar 30, 2019, 11:29:13 AM3/30/19
to Gurobi Optimization
hi everyone 
When I run this code in python :

from gurobipy import *
m = Model ("operation")
x3 = m.addVar(vtype=GRB.CONTINUOUS,name="MT_qt_3")
x4 = m.addVar(vtype=GRB.CONTINUOUS,name="MT_qt_4")
m.setObjective( x3+x4 ,GRB.MINIMIZE) 
m.addConstr( 2 <= x3 <= 4 ) 
m.addConstr( 2 <= x4 <= 4 ) 
m.optimize()
for v in m.getVars():
    print('%s %g' % (v.varName, v.x))
print('Obj: %g' % m.objVal)

The feedback is "0", but obviously the answer is wrong.The answer should be "4". I wonder whether there is something wrong in my code.I would appreciate if someone could help me to solve this problem.
Thank you!

Robert Luce

unread,
Mar 30, 2019, 11:36:07 AM3/30/19
to Gurobi Optimization
Hello,

the problem is that the synax

m.addConstr( 2 <= x3 <= 4 )

doesn't do what you want to do. Such chained expressions are not supported by gurobipy, and in this case the 2<=x3 part will be silently discarded (hence 0 is a feasible solution).  Instead you chould do

m.addConstr( 2 <= x3 )
m
.addConstr( x3 <= 4 )

or simply pass these bounds as data for the keyword arguments 'lb'/'ub' to the addVar() function, see


Best,

Robert

jack chou

unread,
Apr 1, 2019, 3:11:31 AM4/1/19
to Gurobi Optimization
Dear Robert 
Thank you ! I have solved my problem under your guidance.
Best regards
Jack

在 2019年3月30日星期六 UTC+8下午11:29:13,jack chou写道:
Reply all
Reply to author
Forward
0 new messages