简体   繁体   中英

How to apply math.ceil function to the part of objective function in python pulp

Am trying to apply the math.ceil function on one part of the objective function in python-pulp. But, Am getting

"TypeError: must be real number, not LpAffineExpression" error.

Following is the example.

models = [A, B, C, D, E]
modelCost = {A: 10, B: 15, C: 20, D: 25, E: 30}
TotalModelsCost = pulp.lpSum([(model*modelCost[model]) for model in models])
OverHeadCost = (math.ceil(pulp.lpSum([model for model in models])/4))*100

prob += TotalModelsCost + OverHeadCost

How to apply the ceil function to OverHeadCost ?

Create a new decision variable called, say, OverHeadCostCeil , which is a general integer (not binary) variable:

OverHeadCostCeil = pulp.LpVariable('OverHeadCostCeil', 0, None, LpInteger)

Require OverHeadCostCeil >= OverHeadCost via a constraint:

prob += OverHeadCostCeil >= OverHeadCost

Replace term in objective function:

prob += TotalModelsCost + OverHeadCostCeil

Then solve. Note that you need to use a solver that can handle general integer variables.

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