Python numpy function in Gurobi objective

1,855 views
Skip to first unread message

herge...@gmail.com

unread,
Oct 31, 2016, 3:17:18 AM10/31/16
to Gurobi Optimization
Hello,

I am trying to write a Gurobi model in Python. For my original version I used the optimization modeling language Pyomo, but I prefer using Gurobi's Python API instead.

This is my current implementation of the model (happy to share the Pyomo version as well if useful):
m = Model("MSEtest")

#-- Create Variables
x
= [m.addVar(vtype=GRB.BINARY, name="x_{"+str(i)+"}") for i in range(N)]

#-- Integrate new variables
m
.update()

#-- Set Objective
m
.setObjective(np.sum((v_ref - (np.sum([v_model[i,:] * x[i] for i in range(N_models)],axis=0) / K))**2 * v_wgtmat ) / (np.sum(v_wgtmat)), GRB.MINIMIZE)

#-- Add constraint
m
.addConstr(sum(x) == K)

m
.optimize()

Where,
N = 81, K = 30
v_ref = Vector of length 900'00 (filled with floats)
v_model = Array of dimension N x 900'00 (filled with floats)
v_wgtmat = Vector of length 900'00 (filled with floats)

The line where I set the objective function (m.setObjective()) currently causes problems. I am using np.sum() in my objective function, which Gurobi does not seem to be able to deal with. Do you have any suggestions how this line could be rewritten in an efficient way? Is it possible to rewrite it using quicksum?

Thanks for your help!

Kind regards,
Nadja





herge...@gmail.com

unread,
Oct 31, 2016, 3:51:32 AM10/31/16
to Gurobi Optimization
Sorry, the objective function should be as follows (N rather than N_models):
#-- Set Objective
m
.setObjective(np.sum((v_ref - (np.sum([v_model[i,:] * x[i] for i in range(N)],axis=0) / K))**2 * v_wgtmat ) / (np.sum(v_wgtmat)),GRB.MINIMIZE)

The error message I obtain with this implementation is the following: "TypeError: unsupported operand type(s) for /: 'LinExpr' and 'int'".

Cheers,
Nadja

Kostja Siefen

unread,
Oct 31, 2016, 4:16:53 AM10/31/16
to Gurobi Optimization
Hi Nadja,

setObjective() accepts linear or quadratic terms (see https://www.gurobi.com/documentation/7.0/refman/py_model_setobjective.html).

The first thing that is likely not to work is 

np.sum([v_model[i,:] * x[i] for i in range(N_models)],axis=0)

since you're multiplying a list with a single variable object. 

We have greatly enhanced our Python modeling features in release 7.0 to support element-wise multiplication/addition for lists of coefficients and variables. Please take a look at


Kostja

herge...@gmail.com

unread,
Nov 1, 2016, 4:14:27 AM11/1/16
to Gurobi Optimization
Hi Kostja,

I updated Gurobi on my machine to version 7.0 and played around with those two functions. However, I did not manage to get it working. Would you mind giving me a concrete example how those functions can be applied to my problem?

Thanks,
Nadja
Message has been deleted

herge...@gmail.com

unread,
Nov 2, 2016, 1:48:55 AM11/2/16
to Gurobi Optimization
Hi,

The following implementation of the objective function seems to work. I tested it with random data for v_modelv_ref and v_wgtmat, but unfortunately it runs really slowly (hardly faster than brute-force).
m.setObjective(quicksum((v_ref - (sum([v_model[i,:] * x[i] for i in range(N)]) * (1./K)))**2 * v_wgtmat ) / sum(v_wgtmat), GRB.MINIMIZE)

Does anyone have any suggestions how to speed it up?

According to this site: "Note that while quicksum is much faster than sum, it isn't the fastest approach for building a large expression. Use addTerms or the LinExpr() constructor if you want the quickest possible expression construction." How can that be applied to the line above?


Thanks,
Nadja




On Monday, 31 October 2016 19:16:53 UTC+11, Kostja Siefen wrote:

Sonja Mars

unread,
Nov 3, 2016, 5:13:28 AM11/3/16
to gur...@googlegroups.com
Hi Nadja,

You might want to take a look at this documentation page:
http://www.gurobi.com/documentation/7.0/quickstart_linux/py_setting_the_objective.html
and this example:
http://www.gurobi.com/documentation/7.0/examples/dense_py.html

There we construct an objective using a LinExpr() and add the terms using +=

Best regards,
Sonja


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

Reply all
Reply to author
Forward
0 new messages