纸浆可以最小限度地管理约束吗?

时间:2020-01-24 13:25:57

标签: python linear-programming pulp

我想优化以下代码,但出现以下错误,涉及约束的第四行(我认为)。任何帮助将不胜感激,谢谢:

 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])

1 个答案:

答案 0 :(得分:2)

恐怕会受到限制

z = min(x,y)

通常不太容易处理。这是一个具有额外的二进制变量δ的公式:

z ≤ x
z ≤ y
z ≥ x - M⋅δ
z ≥ y - M⋅(1-δ)
δ ∈ {0,1}

这里M是足够大的常数。 (M可以解释为x和y之间的最大距离)。

有时,我们利用目标如何推动变量。例如。如果目标已经将z向上推,则可以删除大于约束。

通常建议更改目标以包括此目标。即更改目标

max obj

max obj + α⋅z

对于某些系数α> 0。但是,这基本上将问题变成了其他问题。通常,此问题的最佳解决方案与您要解决的模型的最佳解决方案不同。

最后:某些高级求解器具有内置的min()函数,以使建模者更轻松。他们还可以就如何在内部重新制定此方案做出更好的决策(可能有不同的表述;我刚刚展示了一个)。