0

I'm attempting to cast a CVXPY problem to Gurobi's Python API.

The CVXPY problem is the following (I'm ommitting previous variables definitions as they're secondary for my issue. Check comments for their types and shapes.):

import cvxpy as cp
import numpy as np

"""
ma, mx, mb, my: type=int
radius: type=float
meas: type=numpy.ndarray, shape=(3, 3)
detp: type=numpy.ndarray, shape=(18, 10000)
state_vecs: type=numpy.ndarray, shape=(3, 3)

weights: type=cvxpy.expressions.variable.Variable, shape=(10000,)
d: type=cvxpy.expressions.variable.Variable, shape=(1,)
"""

ma, mx, mb, my, meas, radius, detp, state_vecs = # Previously defined 

weights = cp.Variable(detp.shape[1])
d = cp.Variable(1)

dot = np.inner(state_vecs, meas).flatten()

# Attention to this line:
behaviors = 0.5 * (1 + d * dot / radius)

constrs = [behaviors == detp @ weights, d >= 0, d <= 1, sum(weights) == 1, weights >= 0]
prob = cp.Problem(cp.Maximize(d), constrs)
prob.solve(solver=cp.GUROBI, verbose=True)

This works fine, but for large instances CVXPY eats all memory and takes a long time setting the problem before calling the solver.

I now attempt to translate this to Gurobi's API:

import gurobipy as gp
import numpy as np


"""
ma, mx, mb, my: type=int
radius: type=float
meas: type=numpy.ndarray, shape=(3, 3)
detp: type=numpy.ndarray, shape=(18, 10000)
state_vecs: type=numpy.ndarray, shape=(3, 3)

weights: type=gurobipy.MVar, shape=(10000,)
d: type=gurobipy.Var, has no attribute shape
"""

ma, mx, mb, my, meas, radius, detp, state_vecs = # Previously defined 

prob = gp.Model("LPMModel")
weights = prob.addMVar(shape=detp.shape[1])
d = prob.addVar()
prob.setObjective(d, GRB.MAXIMIZE)

dot = np.inner(state_vecs, meas).flatten()

# So far so good, but now:
behaviors = 0.5 * (1 + d * dot / radius)

prob.addConstr(behaviors == detp @ weights)
prob.addConstr(weights >= 0)
prob.addConstr(weights.sum() == 1)
prob.addConstr(d >= 0)
prob.addConstr(d <= 1)
prob.optimize()

The line after the comment is what troubles me. I intend to multiply every element of dot by the optimization variable d, which should be maximized. The interpreter spits the following error stack:

TypeError                                 Traceback (most recent call last)
var.pxi in gurobipy.Var.__mul__()

TypeError: only size-1 arrays can be converted to Python scalars

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
linexpr.pxi in gurobipy.LinExpr.__imul__()

TypeError: only size-1 arrays can be converted to Python scalars

During handling of the above exception, another exception occurred:

GurobiError                               Traceback (most recent call last)
<ipython-input-135-5b4ee6d552a9> in <module>
----> 1 grb = lpm.grb_local_model(st)

~/lpm.py in grb_local_model(state_vecs, detp, meas, verb)
     97 
     98     dot = np.inner(state_vecs, meas).flatten()
---> 99     behaviors = 0.5 * (1 + d * dot / radius)
    100 
    101     prob.addConstr(behaviors == detp @ weights)

var.pxi in gurobipy.Var.__mul__()

linexpr.pxi in gurobipy.LinExpr.__imul__()

linexpr.pxi in gurobipy.LinExpr.__mul__()

linexpr.pxi in gurobipy.LinExpr._mul()

GurobiError: Invalid argument to LinExpr multiplication

And this is my issue.
What am I missing out?

(Any suggestions on how to improve the code elsewhere are also welcome.)

cab20
  • 35
  • 1
  • 5
  • Since many of us won't have those modules installed. Can you post what *d*, *dot*, and *radius* look like including their `type()`? [Cvxpy](https://www.cvxpy.org/tutorial/intro/index.html#vectors-and-matrices) docs indicate `Variable(1)` would be a 1-D array and with 1 may be a scalar but [Gurobi](https://www.gurobi.com/documentation/9.0/refman/py_model_addvar.html) docs indicate a *new variable object* but dimension is not defined. Possibly it is more than a scalar and cannot be multiplied with flatted numpy array.. – Parfait Mar 02 '20 at 00:39
  • Thank you for asking. I've included this information as comments on both snippets. You made a nice observation that ```d``` has no attribute ```shape```. This is certainly an issue. I'll try declaring it as ```d = prob.addMVar(shape=1)``` to investigate what happens. – cab20 Mar 02 '20 at 01:02

0 Answers0