PuLP Conditional Sum based on key of the loop

Question:

I am trying to use this conditional sum in Pulp’s objective function. For the second lpSum, I am trying to calculate the costs of when we don’t have enough chassis’ to cover the demand and will need pool chassis’ with a higher costs. Of course, I only want to calculate this when we don’t have enough dedicated chassis'(dedicated_chassis_needed) to cover the demand(chassis_needed) for each day.

The problem is a cost minimizing one. The last "if" part doesn’t seem to be working and the lpSum seems to be summing up every date’s pool cost and ignoring the if condition, and it just sets the decision variable of dedicated_chassis_needed to 0(lower constraint) and the objective value is a negative number which should not be allowed.

prob += lpSum(dedicated_chassis_needed * dedicated_rate for date in chassis_needed.keys()) + 
        lpSum(((chassis_needed[(date)] - dedicated_chassis_needed) * pool_rate_day) 
             for date in chassis_needed.keys() if ((chassis_needed[(date)] - dedicated_chassis_needed) >= 0))
Asked By: Alex Yang

||

Answers:

In general, in LP, you cannot use a conditional statement that is dependent on the value of a variable in any of the constraints or objective function because the value of the variable is unknown when the model is built before solving, so you will have to reformulate.

You don’t have much information there about what the variables and constants are, so it isn’t possible to give good suggestions. However, a well-designed objective function should be able to handle extra cost for excess demand without a condition as the model will select the cheaper items first.

For example, if:

demand[day 5] = 20

and

cheap_units[day 5] = 15 @ $100  (availability)

and

reserve units = 100 @ $150  (availability from some pool of reserves)

and you have some constraint to meet demand via both of those sources and an objective function like:

min(cost) s.t. cost[day] = cheap_units[day] * 100 + reserve_units * 150

it should work out fine…

Answered By: AirSquid
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.