PuLP: Objective 函数:连接多个 lpSum
PuLP: Objective Function: Concatenate multiple lpSum
我正在尝试将几个 lpSum
表达式连接成一个长表达式,这将是我的 objective 函数。然而,我试图以优雅的方式合并这些表达式导致了不希望的结果。
我想要这样的东西:
a = pulp.lpSum(...)
b = pulp.lpSum(...)
c = pulp.lpSum(...)
prob += a + b - c
我的代码更具体:
alloc_prob = pulp.LpProblem("Supplier Allocation Problem", pulp.LpMinimize)
TPC_func = pulp.lpSum(X[s][p]*procCosts[s][p] for s in supplier for p in
project), "Total Procurement Costs"
TTC_func = pulp.lpSum(X[s][p]*transCosts[s][p] for s in supplier for p in
project), "Total Transportation Costs (incl. taxes/duties)"
TD_func = pulp.lpSum(X_SEP[c][1]*discountFactor['Bonus / ton [€/t]'][c] for
c in company), "Total Discounts"`
# Objective function: TPC + TTC - TD -> min
alloc_prob += TPC_func + TTC_func - TD_func
我已经尝试过不同的嵌套方法,例如:
prob += [pulp.lpSum(X[s][p]*procCosts[s][p] + X[s][p]*transCosts[s][p] for s
in supplier for p in project) - pulp.lpSum(X_SEP[c][1]*discountFactor['Bonus
/ ton [€/t]'][c] for c in company)]
输出符合预期。然而,这既不是一个好的代码,也不能分配给 objective 函数。有没有聪明的实施方式?
谢谢!
在没有看到错误的情况下,我可以 100% 确定,但我认为您在 lpsum 中包含的名称导致了问题请尝试以下操作
alloc_prob = pulp.LpProblem("Supplier Allocation Problem", pulp.LpMinimize)
TPC_func = pulp.lpSum(X[s][p]*procCosts[s][p] for s in supplier for p in
project)
TTC_func = pulp.lpSum(X[s][p]*transCosts[s][p] for s in supplier for p in
project)
TD_func = pulp.lpSum(X_SEP[c][1]*discountFactor['Bonus / ton [€/t]'][c] for
c in company)
# Objective function: TPC + TTC - TD -> min
alloc_prob += TPC_func + TTC_func - TD_func
我正在尝试将几个 lpSum
表达式连接成一个长表达式,这将是我的 objective 函数。然而,我试图以优雅的方式合并这些表达式导致了不希望的结果。
我想要这样的东西:
a = pulp.lpSum(...)
b = pulp.lpSum(...)
c = pulp.lpSum(...)
prob += a + b - c
我的代码更具体:
alloc_prob = pulp.LpProblem("Supplier Allocation Problem", pulp.LpMinimize)
TPC_func = pulp.lpSum(X[s][p]*procCosts[s][p] for s in supplier for p in
project), "Total Procurement Costs"
TTC_func = pulp.lpSum(X[s][p]*transCosts[s][p] for s in supplier for p in
project), "Total Transportation Costs (incl. taxes/duties)"
TD_func = pulp.lpSum(X_SEP[c][1]*discountFactor['Bonus / ton [€/t]'][c] for
c in company), "Total Discounts"`
# Objective function: TPC + TTC - TD -> min
alloc_prob += TPC_func + TTC_func - TD_func
我已经尝试过不同的嵌套方法,例如:
prob += [pulp.lpSum(X[s][p]*procCosts[s][p] + X[s][p]*transCosts[s][p] for s
in supplier for p in project) - pulp.lpSum(X_SEP[c][1]*discountFactor['Bonus
/ ton [€/t]'][c] for c in company)]
输出符合预期。然而,这既不是一个好的代码,也不能分配给 objective 函数。有没有聪明的实施方式?
谢谢!
在没有看到错误的情况下,我可以 100% 确定,但我认为您在 lpsum 中包含的名称导致了问题请尝试以下操作
alloc_prob = pulp.LpProblem("Supplier Allocation Problem", pulp.LpMinimize)
TPC_func = pulp.lpSum(X[s][p]*procCosts[s][p] for s in supplier for p in
project)
TTC_func = pulp.lpSum(X[s][p]*transCosts[s][p] for s in supplier for p in
project)
TD_func = pulp.lpSum(X_SEP[c][1]*discountFactor['Bonus / ton [€/t]'][c] for
c in company)
# Objective function: TPC + TTC - TD -> min
alloc_prob += TPC_func + TTC_func - TD_func