Writing objective function in Gurobi Java?

110 views
Skip to first unread message

Muhammad Bilal

unread,
Jul 17, 2016, 3:28:35 PM7/17/16
to Gurobi Optimization
Hi,

I am trying to come up with the convex objective such that Gurobi solver can solve it. This has resulted in another alternative objective function given below:

minimize ((sum(_h)-sum(a*s)) + (sum(_w) - sum(b*s))), where _h and _w are the constant Java array, a and b are the decision variables defined as GRBVar array, and s is a scalar constant.

Since Gurobi recognises constructs that are defined through GRBVar, so I'm defining s as GRBVar, to be used in the Gurobi expressions, as following:

GRBVar s = m.addVar(300.0, 300.0, 300.0, GRB.INTEGER, "s");

And now rewriting the above objective as following Gurobi Java code:

GRBQuadExpr objQE = new GRBQuadExpr();

for (double value: _h) objQE.addConstant(value);
for (int i = 0; i < a.length; i++)
   objQE.addTerm(-1.0, a[i], s);
for (double value: _w) objQE.addConstant(value);
for (int i = 0; i < b.length; i++)
   objQE.addTerm(-1.0, b[i], s);    

m.setObjective(objQE, GRB.MINIMIZE);
            
Can someone check the translation seems fine or has some issues?

Thanks in advance.
Bilal

Tobias Achterberg

unread,
Jul 17, 2016, 3:46:44 PM7/17/16
to gur...@googlegroups.com
You should not declare constants as GRBVar objects with bounds that fix them to
a single value. Instead, you should use the constants as coefficients of your
terms. For example, write

objQE.addTerm(-s, a[i]);

instead of

objQE.addTerm(-1.0, a[i], s);


Tobias

Muhammad Bilal

unread,
Jul 18, 2016, 3:31:48 AM7/18/16
to Gurobi Optimization
Many thanks Tobias for the response and really good explanation. It is very useful.

I used this approach to convert about 18 Quad expressions to linear now.

Thanks again and kind regards. 
Reply all
Reply to author
Forward
0 new messages