Multi-objective API in a simple example

915 views
Skip to first unread message

ayous...@gmail.com

unread,
Mar 26, 2017, 3:56:39 PM3/26/17
to Gurobi Optimization
Hi, I just started using Gurobi and I don't understand the API for adding multi-objectives. I am using the Python package. I try to modify this code http://www.gurobi.com/documentation/7.0/examples/multiobj_py.html#subsubsection:multiobj.py to a trivial example but it is not working.

Here is my code: 

---

m = Model()
x = m.addVar(lb=0, ub=10, vtype=GRB.CONTINUOUS)
y = m.addVar(lb=0, ub=10, vtype=GRB.CONTINUOUS)

m.ModelSense = GRB.MAXIMIZE
m.NumObj = 2

m.setParam(GRB.Param.ObjNumber, 0)
m.ObjNPriority = 1
m.setAttr(GRB.Attr.ObjN, x + y)

m.setParam(GRB.Param.ObjNumber, 1)
m.ObjNPriority = 2
m.setAttr(GRB.Attr.ObjN, x - y)

m.optimize()

----

I think the command "m.setAttr(GRB.Attr.ObjN, x + y)" is ill formulated and is not adding the objective. How should I modify it?

Thanks!

Sonja Mars

unread,
Mar 27, 2017, 6:28:16 AM3/27/17
to gur...@googlegroups.com
Hi,

If you take a look at the multiobj.py example, the way the multiple objectives are set is this:
model.setAttr(GRB.Attr.ObjN, Elem, Set[i])
In this call Elem is a tupledict and Set[i] is a list. ObjN is a variable attribute. So the idea is to set an objective coefficient for the n-th objective for each variable. In our example we do this for all variables at once using a list of coefficients.

So what you would need to do in your code is something like this:
m.setAttr(GRB.Attr.ObjN, [x,y], [1,1])
and 
m.setAttr(GRB.Attr.ObjN, [x,y], [1,-1])

Best regards, 
Sonja 


----
Dr. Sonja Mars
Gurobi Optimization - Technical Support 





Daniel Espinoza

unread,
Mar 27, 2017, 7:45:23 AM3/27/17
to Gurobi Optimization

Ahmed YOUSSEF

unread,
Mar 27, 2017, 10:39:52 AM3/27/17
to gur...@googlegroups.com
Thank you very much Sonja! 

Best regards,
Ahmed


--

---
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.

Reply all
Reply to author
Forward
0 new messages