Python 将gurobi变量与np.array元素相乘

Python 将gurobi变量与np.array元素相乘,python,gurobi,cvxpy,Python,Gurobi,Cvxpy,我试图将CVXPY问题转换为Gurobi的Python API CVXPY的问题如下(我正在匹配前面的变量定义,因为它们是我的问题的第二个变量。请检查注释以了解它们的类型和形状): 这是我的问题。 我错过了什么 (对于如何改进其他地方的代码,我们也欢迎提出任何建议。)因为我们中的许多人都不会安装这些模块。你能公布d、圆点和半径的形状,包括它们的类型()?文档表明变量(1)将是一个一维数组,1可以是标量,但文档表明一个新的变量对象,但未定义维度。它可能不仅仅是一个标量,而且不能与平坦的numpy数

我试图将CVXPY问题转换为Gurobi的Python API

CVXPY的问题如下(我正在匹配前面的变量定义,因为它们是我的问题的第二个变量。请检查注释以了解它们的类型和形状):

这是我的问题。
我错过了什么


(对于如何改进其他地方的代码,我们也欢迎提出任何建议。)

因为我们中的许多人都不会安装这些模块。你能公布d、圆点和半径的形状,包括它们的
类型()
?文档表明变量(1)将是一个一维数组,1可以是标量,但文档表明一个新的变量对象,但未定义维度。它可能不仅仅是一个标量,而且不能与平坦的numpy数组相乘。谢谢你的提问。我在这两个代码段中都添加了这些信息作为注释。您很好地观察到
d
没有属性
shape
。这当然是一个问题。我将尝试将其声明为
d=prob.addMVar(shape=1)
,以调查发生了什么。
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)
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()
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