纸浆能否以最小限度管理约束?
Can pulp manage constraint with minimum?
我想优化下面的代码,但我得到如下错误,关于约束中的第 4 行(我认为)。任何帮助将不胜感激,谢谢:
model += A[i] == min(P[i], C[i])
TypeError: '<' not supported between instances of 'int' and 'LpVariable'
P0 = [[1,0,4],[2,0,3],[4,6,2],[5,2,1],[1,0,0]]
x = [2,3,0]
xMax = [14,12,13]
C = [122, 99, 158, 37, 44]
# Instantiate our problem class
model = pulp.LpProblem("Clem", pulp.LpMaximize)
# Construct our decision variable lists
x = pulp.LpVariable.dicts('pInstal', (i for i in range(3)), lowBound = 0, cat = 'Continuous')
tx = pulp.LpVariable('tauxAutoconso', 0)
for i in range(5):
P = pulp.LpVariable.dicts('vectProduction',(i for i in range(5)), lowBound = 0, cat = 'Continuous')
A = pulp.LpVariable.dicts('vectAutoConso',(i for i in range(5)), lowBound = 0, cat = 'Continuous')
# Objective Function
model += tx
# Constraints
for i in range(3):
model += x[i] <= xMax[i]
for i in range(5):
model += P[i] == sum([P0[i][j] * x[j] for j in range(3)])
model += A[i] == min(P[i], C[i])
model += tx == sum(A) / sum(P)
model += sum(x) == sum(C)
# Solve our problem
if pulp.LpStatus[model.status] != 'Optimal':
print('Solution qualité :', pulp.LpStatus[model.status])
恐怕受限
z = min(x,y)
总的来说不太好办。这是一个带有额外二进制变量 δ 的公式:
z ≤ x
z ≤ y
z ≥ x - M⋅δ
z ≥ y - M⋅(1-δ)
δ ∈ {0,1}
这里M是一个足够大的常数。 (M可以解释为x和y之间的最大可能距离)。
有时我们会利用 objective 推送变量的方式。例如。如果 objective 已经将 z 向上推,我们可以放弃大于约束。
通常建议更改 objective 以包含此内容。 IE。将 objective 从
更改为
max obj
到
max obj + α⋅z
对于某些系数α>0。然而,这基本上是将问题变成了不同的东西。此问题的最优解(通常)与您要求解的模型的最优解不同。
最后:一些高级求解器有一个内置的 min() 函数,可以让建模者更轻松。他们还可以就如何在内部重新表述这一点做出更好的决定(可能有不同的表述;我刚刚展示了一个)。
我想优化下面的代码,但我得到如下错误,关于约束中的第 4 行(我认为)。任何帮助将不胜感激,谢谢:
model += A[i] == min(P[i], C[i])
TypeError: '<' not supported between instances of 'int' and 'LpVariable'
P0 = [[1,0,4],[2,0,3],[4,6,2],[5,2,1],[1,0,0]]
x = [2,3,0]
xMax = [14,12,13]
C = [122, 99, 158, 37, 44]
# Instantiate our problem class
model = pulp.LpProblem("Clem", pulp.LpMaximize)
# Construct our decision variable lists
x = pulp.LpVariable.dicts('pInstal', (i for i in range(3)), lowBound = 0, cat = 'Continuous')
tx = pulp.LpVariable('tauxAutoconso', 0)
for i in range(5):
P = pulp.LpVariable.dicts('vectProduction',(i for i in range(5)), lowBound = 0, cat = 'Continuous')
A = pulp.LpVariable.dicts('vectAutoConso',(i for i in range(5)), lowBound = 0, cat = 'Continuous')
# Objective Function
model += tx
# Constraints
for i in range(3):
model += x[i] <= xMax[i]
for i in range(5):
model += P[i] == sum([P0[i][j] * x[j] for j in range(3)])
model += A[i] == min(P[i], C[i])
model += tx == sum(A) / sum(P)
model += sum(x) == sum(C)
# Solve our problem
if pulp.LpStatus[model.status] != 'Optimal':
print('Solution qualité :', pulp.LpStatus[model.status])
恐怕受限
z = min(x,y)
总的来说不太好办。这是一个带有额外二进制变量 δ 的公式:
z ≤ x
z ≤ y
z ≥ x - M⋅δ
z ≥ y - M⋅(1-δ)
δ ∈ {0,1}
这里M是一个足够大的常数。 (M可以解释为x和y之间的最大可能距离)。
有时我们会利用 objective 推送变量的方式。例如。如果 objective 已经将 z 向上推,我们可以放弃大于约束。
通常建议更改 objective 以包含此内容。 IE。将 objective 从
更改为max obj
到
max obj + α⋅z
对于某些系数α>0。然而,这基本上是将问题变成了不同的东西。此问题的最优解(通常)与您要求解的模型的最优解不同。
最后:一些高级求解器有一个内置的 min() 函数,可以让建模者更轻松。他们还可以就如何在内部重新表述这一点做出更好的决定(可能有不同的表述;我刚刚展示了一个)。