运行此代码
import numpy as np
import xpress as xp
z = np.array([xp.var () for i in range (200)]).reshape (4,5,10)
t = np.array([xp.var (vartype = xp.binary) for i in range (200)]).reshape (4,5,10)
p = xp.problem()
p.addVariable(z,t)
p.addConstraint(z <= 1 + t)
我得到以下错误
Invalid constraint
---------------------------------------------------------------------------
ModelError Traceback (most recent call last)
3 p = xp.problem()
4 p.addVariable(z,t)
----> 5 p.addConstraint(z <= 1 + t)
6 p.addConstraint(xp.Sum(z[i][j][k] for i in range (4) for j in range (5)) <= 4 for k in range (10))
ModelError: Invalid constraint
任何帮助将是非常感激的,因为我不知道如何解决它!
np数组的dtype
必须显式设置为xp.npvar。如下所示:
NumPy数组的dtype属性必须等于expression .npvar(缩写为xp)。这里是Npvar),以便使用材质/矢量形式的比较(& lt; =, =,祝辞=),算术 (+, -, *,/, **), 和逻辑(&, |)运算符。
如果不将类型设置为npvar,则会使用错误的操作符重载,并且z <= 1 - t
将只是一个布尔值数组。
这是创建数组的正确方法:
z = np.array([xp.var () for i in range (200)], dtype=xp.npvar).reshape (4,5,10)
t = np.array([xp.var (vartype = xp.binary) for i in range (200)], dtype=xp.npvar).reshape (4,5,10)