简体   繁体   English

PuLP:目标函数:连接多个lpSum

[英]PuLP: Objective Function: Concatenate multiple lpSum

I am trying to concatenate several lpSum expressions to one long expression which shall be my objective function. 我试图将几个lpSum表达式连接为一个长表达式,这将是我的目标函数。 However, my attempts on merging these expressions in an elegant way lead to undesired results. 但是,我尝试以优雅的方式合并这些表达式会导致不良结果。

I want something like this: 我想要这样的东西:

a = pulp.lpSum(...)
b = pulp.lpSum(...)
c = pulp.lpSum(...)

prob += a + b - c

More concrete to my code: 我的代码更具体:


    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

I already tried different nested approaches, eg: 我已经尝试了不同的嵌套方法,例如:

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

The output does what it should. 输出会执行应有的操作。 However, this is neither a nice code nor can it be assigned to the objective function. 但是,这既不是很好的代码,也不能分配给目标函数。 Is there a smart way of implementing? 有没有明智的实施方式?

Thanks! 谢谢!

Without seeing the error, I can be 100% sure but I think the name that you are including in the lpsum is causing the problem try the following 没有看到错误,我可以百分百确定,但是我认为您在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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM