我试图将几个lpSum表达式连接到一个长表达式,这将是我的目标函数。然而,我以优雅的方式合并这些表达式的尝试导致了不希望的结果。我想要这样的东西: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)]输出做它应该做的。然而,这既不是一个很好的代码,也不能分配给目标函数。有没有聪明的实施方式?
1 回答
杨__羊羊
TA贡献1943条经验 获得超7个赞
没有看到错误,我可以 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
添加回答
举报
0/150
提交
取消