NotImplementedError

623 views
Skip to first unread message

jack chou

unread,
Mar 30, 2019, 3:11:57 AM3/30/19
to Gurobi Optimization

hello everyone,
When I establish a simple model as follows, the feedback show me the erro (NotImplementedError).I would appreciate it if someone could help me to figure out this problem.Your answer will help me a lot.

from gurobipy import *
m = Model ()
x1 = m.addVar(vtype=GRB.BINARY,name="MT_qt_1")
x2 = m.addVar(vtype=GRB.BINARY,name="MT_qt_2")
m.update()
m.setObjective(1+0.9*max(0,x1-x2),GRB.MINIMIZE)
m.optimize()
for v in m.getVars():
    print('%s %g' % (v.varName, v.x))
print('Obj: %g' % m.objVal) 



Thank you!

Silke Horn

unread,
Mar 30, 2019, 3:37:49 AM3/30/19
to noreply-spamdigest via Gurobi Optimization
Hi,

The reason for the error is that the max function is called ‘max_’. (You are missing an underscore.)

But even with the correct function name, you cannot use it the way you are trying to: The arguments of max_ can only be variables or a constant (not an expression like x1-x2) and you can only use max_ in a constraint of this form: m.addConstr(z == max_(x, y, 3))
Here is the documentation: https://www.gurobi.com/documentation/8.1/refman/py_max_.html

A quick way to fix your code would be to introduce two auxiliary variables, say x3 and x4, and then do the following:
m.addConstr( x3 == x1 - x2 )
m.addConstr( x4 == max_(0, x3) )
m.setObjective( 1 + 0.9 * x4 )

Also note that ‘m.update()’ is not necessary.

- Silke
> --
>
> ---
> You received this message because you are subscribed to the Google Groups "Gurobi Optimization" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to gurobi+un...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

jack chou

unread,
Mar 30, 2019, 11:29:13 AM3/30/19
to Gurobi Optimization
Dear horn, 
Thank you! I have solved my problem under your guidance.
Best regards

在 2019年3月30日星期六 UTC+8下午3:37:49,horn写道:
> To unsubscribe from this group and stop receiving emails from it, send an email to gur...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages