Dear fellow python users,
I am building a multi period multi product planning model using Pulp. What the model should do is rather simple: plan production against minimal holding and production costs while meeting demand.
I have the following data:
periods = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
products = ['A', 'B', 'C']
And I create the following variables:
prod_vars = pulp.LpVariable.dicts('production', [(i,j) for i in products for j in periods],0)
inv_vars = pulp.LpVariable.dicts('inventory', [(i,j) for i in products for j in periods],0)
There are 2 constraints, 1 to always meet demand and 1 to stay below production capacity. Please note that there is a dataframe (input_data) that retrieves the value of the given demand for that period.
for i in products:
for j in periods[1:]:
model.addConstraint(pulp.LpConstraint(
e=inv_vars[(i,j-1)] + prod_vars[(i,j)] - inv_vars[(i,j)],
sense=pulp.LpConstraintEQ,
name='inv_balance_' + str(i)+ str(j),
rhs=input_data[i][j-1]))
for j in periods:
model.addConstraint(pulp.LpConstraint(
e=(pulp.lpSum(prod_vars[(i,j)] for i in products)),
sense=pulp.LpConstraintLE,
name='total_production_capacity'+str(j),
rhs=(input_data['production_capacity'][j-1])
Then I add cost function and set the objective:
total_production_cost = production_cost*pulp.lpSum(prod_vars)
total_holding_cost =holding_cost * pulp.lpSum(inv_vars)
objective = total_holding_cost + total_production_cost + activation_cost model.setObjective(objective)
This model works all fine and gives me an output like this. prod_vars: (A,1) =5, (B,1)=10, (C,1)=15 and so on for all periods. However: I want to penalize the system for producing multiple products. I.e., adding fixed costs when choosing to produce a second or third product. It would then be more benefical to produce more of product A and hold inventory for some months then to produce A every month. I tried so by adding another variable:
use_vars = pulp.LpVariable.dicts('uselocation', [(i,j) for i in products for j in periods] , 0,1,pulp.LpBinary)
And add fixed costs for using the variable:
activation_cost = pulp.lpSum(activation_cost*use_vars[(i,j)] for i in products for j in periods)
I think that I would need to multiply al my prod_vars by my use_vars in the two constraints. However, if I would do this in my first inventory constraint, Pulp gives the error that my constraint is not linear anymore.
Does someone know how I can make this happen?? Thanks in advance :)