gpt4 book ai didi

python - 表母 | Couenne求解器|将索引变量域限制为两个整数值

转载 作者:行者123 更新时间:2023-12-05 06:47:33 32 4
gpt4 key购买 nike

我是 Pyomo 的新手,一直在努力理解 Pyomo 语法背后的直觉以及它如何构建模型。这可能就是为什么我无法弄清楚如何使用 Pyomo 和 Couenne 求解器来定义和解决 N 变量必须仅取 ±1 值的“二元”问题。

首先,我尝试使用 bounds=(-1, 1)Integers 域,并尝试添加一个严格的不等式:

import pyomo.environ as pyo
import numpy as np
N = 5
w = np.ones(range(N))
pyoModel = pyo.ConcreteModel('binary model')
pyoModel.o = pyo.Var(range(N), bounds=(-1, 1), within=pyo.Integers, initialize=1)
pyoModel.binaryConstraintP = pyo.Constraint(range(N), rule=strictlyPositive)
pyoModel.binaryConstraintN = pyo.Constraint(range(N), rule=strictlyNegative)
pyoModel.objective = pyo.Objective(expr=pyo.sum_product(pyoModel.o, w, index=range(N)), sense=pyo.maximize)
def strictlyPositive(model, i):
return model.o[i] > 0
def strictlyNegative(model, i):
return model.o[i] < 0

最后是:

ValueError: Constraint 'binaryConstraintP[0]' encountered a strict inequality expression ('>' or '<'). All constraints must be formulated using using '<=', '>=', or '=='.

好吧,不允许严格的不等式(不知道为什么!),我尝试切换到 Binary 域并通过操纵目标中的变量来解决问题,使其位于 { -1, 1} - 即,如果 o ∈ {0, 1} 那么 2 x o - 1 ∈ {-1, 1}:

import pyomo.environ as pyo
import numpy as np
N = 5
w = np.ones(range(N))
pyoModel = pyo.ConcreteModel('binary model')
pyoModel.o = pyo.Var(range(N), within=pyo.Binary, initialize=1)
pyoModel.objective = pyo.Objective(expr=pyo.sum_product(2 * pyoModel.o - 1, w, index=range(N)), sense=pyo.maximize)

得到:

TypeError: unsupported operand type(s) for *: 'int' and 'IndexedVar'

所以我使用了一个数组,而不是 2 和 1,但得到了另一个关于形状广播的错误。我确定我在这里遗漏了一些东西,因为在目标中构建线性方程应该很容易,对吗?

我还尝试将域更改为用户定义的域:

...
pyoModel.domain = pyo.Set(initialize=[-1, 1])
...
pyoModel.o = pyo.Var(range(N), domain=pyoModel.domain, initialize=1)
...
with SolverFactory('couenne') as opt:
results = opt.solve(pyoModel, load_solutions=False)
...

并以 Couenne 错误结束:

TypeError: Invalid domain type for variable with name '%s'. Variable is not continuous, integer, or binary.

我也考虑过使用 SOS,但更难理解它们的工作原理!

同样,我必须在每种方法中遗漏一些东西。如有任何帮助,我们将不胜感激。

旁注:我尽可能简化了原始代码,使其更易于阅读。

最佳答案

由于使用了严格的不等式,您的第一次尝试失败了,这是一个禁忌。这背后有理论作为这些类型问题的求解器在问题空间的“凸包”上工作。有关更多信息,请阅读有关线性规划的文本——它超出了堆栈溢出答案的范围。

您的第二次尝试是在正确的轨道上。如果您正在寻找 ±1 的数学等效值,则让 x 成为二进制变量并使用线性转换 2x-1 是完全合适的。您在那里的尝试失败了,因为您通过在 Var() 构造中提供索引来声明您的 x 变量为索引变量,但您没有在中使用索引目标。

这是一个使用索引变量的示例。如果您只有一个单例,则只需删除索引集引用。

from pyomo.environ import *

some_constants = { 0: 100,
1: 200,
2: -50,
3: 300,
4: 50}

m = ConcreteModel('plus & minus ones project')

m.S = Set(initialize=range(5))
m.c = Param(m.S, initialize=some_constants)
m.x = Var(m.S, within=Binary) # creates {m.x[0], m.x[1], ... , m.x[4]}

# try to maximize the sum of x*c
m.obj = Objective(expr=sum((2*m.x[s] - 1)*m.c[s] for s in m.S), sense=maximize)

# some constraint to limit the number of "+1 picks" to 2...easy with binary vars.
m.C1 = Constraint(expr=sum(m.x[s] for s in m.S) <= 2)

m.pprint()

yield :

1 Set Declarations
S : Size=1, Index=None, Ordered=Insertion
Key : Dimen : Domain : Size : Members
None : 1 : Any : 5 : {0, 1, 2, 3, 4}

1 Param Declarations
c : Size=5, Index=S, Domain=Any, Default=None, Mutable=False
Key : Value
0 : 100
1 : 200
2 : -50
3 : 300
4 : 50

1 Var Declarations
x : Size=5, Index=S
Key : Lower : Value : Upper : Fixed : Stale : Domain
0 : 0 : None : 1 : False : True : Binary
1 : 0 : None : 1 : False : True : Binary
2 : 0 : None : 1 : False : True : Binary
3 : 0 : None : 1 : False : True : Binary
4 : 0 : None : 1 : False : True : Binary

1 Objective Declarations
obj : Size=1, Index=None, Active=True
Key : Active : Sense : Expression
None : True : maximize : (2*x[0] - 1)*100 + (2*x[1] - 1)*200 + (2*x[2] - 1)*-50 + (2*x[3] - 1)*300 + (2*x[4] - 1)*50

1 Constraint Declarations
C1 : Size=1, Index=None, Active=True
Key : Lower : Body : Upper : Active
None : -Inf : x[0] + x[1] + x[2] + x[3] + x[4] : 2.0 : True

5 Declarations: S c x obj C1

关于python - 表母 | Couenne求解器|将索引变量域限制为两个整数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67072210/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com