How to use functions in objective & constraint specification in Gurobi Java API?

369 views
Skip to first unread message

Muhammad Bilal

unread,
Jul 14, 2016, 7:23:24 AM7/14/16
to Gurobi Optimization
Hi All,

I am trying to rewrite Matlab code for Java Gurobi API. 

I have the following Matlab specification for the objective function: 

minimize (sum(Ap) - (h'*w))

Similarly one of the Matlab constraint is specified as below:

y(1) + h(1) + r <= h(8) + mod (hp(1), const);

My question is how to use functions (like sum and mod) in Java API for Gurobi while specifying objectives and constraints of the model.

Any help or guidance will be highly appreciated.

Many Thanks and Kind Regards
Bilal

Renan Garcia

unread,
Jul 14, 2016, 10:09:37 AM7/14/16
to gur...@googlegroups.com
Can you please specify which of the arrays p, h, w and y are decision variables vs constants?

--

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

Muhammad Bilal

unread,
Jul 14, 2016, 11:46:07 AM7/14/16
to Gurobi Optimization
  • Constant: Ap, r, hp (array), const
  • Decision variables: h, y, w 

Renan Garcia

unread,
Jul 14, 2016, 12:43:15 PM7/14/16
to gur...@googlegroups.com
For the objective, you can do something like:

  // Set objective: minimize (sum(Ap) - (h'*w))
  GRBQuadExpr obj = new GRBQuadExpr();
  for (double value: Ap) obj.addConstant(value);
  for (int i = 0; i < h.length; i++) obj.addTerm(-1.0, h[i], w[i]);
  model.setObjective(obj, GRB.MINIMIZE);

Note that this means you are minimizing a non-convex quadratic function, which is not supported by Gurobi.

Assuming r is an array, then you want to create a constraint for each value of r? If so, you can do something like:

  // Add constraints: y(1) + h(1) - h(8) <= mod (hp(1), con) - r
  for (double value: r)
  {
    GRBLinExpr expr = new GRBLinExpr();
    expr.addTerm(1.0, y[0]);
    expr.addTerm(1.0, h[0]);
    expr.addTerm(-1.0, h[7]);
    double rhs = (hp[0] % con) - value;
    model.addConstr(expr, GRB.LESS_EQUAL, rhs, "");
  }

Note that Matlab indexes from 1 to n, while Java indexes from 0 to n-1. Also, you shouldn't use the variable name 'const' in Java, since it's a keyword.

Muhammad Bilal

unread,
Jul 14, 2016, 1:54:15 PM7/14/16
to Gurobi Optimization
Many thanks for really nice explanation and code. 

Your help is much appreciated.
Bilal

Muhammad Bilal

unread,
Jul 14, 2016, 2:59:42 PM7/14/16
to Gurobi Optimization
Is there a work around to use Gurobi API for this problem. I mean is it possible to convert this objective to alternate convex objective?

Muhammad Bilal

unread,
Jul 14, 2016, 3:35:27 PM7/14/16
to Gurobi Optimization
I tried the following setting for the model to execute but no successful:
model.getEnv().set(GRB.IntParam.PreQLinearize, 1);
Reply all
Reply to author
Forward
0 new messages