python 中的 xpress 库使用 addConstraint 时约束无效
Invalid Constraint when using the addConstraint with the xpress library in python
当运行这段代码
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。这表示 here:
The NumPy arrays must have the attribute dtype equal to xpress.npvar
(abbreviated to xp.npvar here) in order to use the matricial/vectorial
form of the comparison (<=, =, >=), arithmetic (+, -, *, /, **), and
logic (&, |) operators.
如果您不将类型设置为 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)
当运行这段代码
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。这表示 here:
The NumPy arrays must have the attribute dtype equal to xpress.npvar (abbreviated to xp.npvar here) in order to use the matricial/vectorial form of the comparison (<=, =, >=), arithmetic (+, -, *, /, **), and logic (&, |) operators.
如果您不将类型设置为 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)