简体   繁体   中英

PuLP: Objective Function: Concatenate multiple lpSum

I am trying to concatenate several lpSum expressions to one long expression which shall be my objective function. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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