# How to formulate cumulative sum of LpVariable in Pulp Python

I have a Multiple product LP optimization problem in which the product(B1,B2,D) will be received in variable quantity with respect to date column.

The Optimizer should LP variable Output as Assy Out B1, Assy Out B2, Assy Out D, Open Assy Line (Binary decision to produce or not in given date).

The target is to maximize the assembly output per day.

The constraints are material receipt for each date and not allowed to produce more than material available in each date

here is my data:

I used the code below:


dfs=dfs.set_index(dfs['t'])

x=np.arange(1,10)

Assy_B1=pulp.LpVariable.dicts('Assy_B1',x,0,None,'Integer')

Assy_B2=pulp.LpVariable.dicts('Assy_B2',x,0,None,'Integer')

Assy_D=pulp.LpVariable.dicts('Assy_D',x,0,None,'Integer')

Open_Line=pulp.LpVariable.dicts('Open_Line',x,0,None,'Binary')

model=LpProblem('Assembly_Plan',LpMaximize)

model +=lpSum([ Assy_B1[t] + Assy_B2[t] + Assy_D[t] for t in x])

for i in x:
model+=(Assy_B1[i]+Assy_B2[i]+Assy_D[i])<=(dfs.loc[i,'Max_Capacity ']*Open_Line[i])

model+=lpSum(Assy_B1[i])<=dfs.loc[i,'INPUT B1']

model+=lpSum(Assy_B2[i])<=dfs.loc[i,'INPUT B2']

model+=lpSum(Assy_D[i])<=dfs.loc[i,'INPUT D']

model.solve()


The Model Solution is Optimal and output as below:

Everything is fine except the last date, the model should have capacity to produce 100 but utilized less (date 5/5/2022 cumulative produced is 60 and still 40 of model D can be produced on that day).

Similarly if the input material is available and capacity is less than cumulative material available the model should fit in next best available date for the same.

I am not able to fix this Constrain/Relaxation in Pulp.

• I think providing a reproducible example would increase your chances of getting answers. I can't imagine anyone is willing to type out the data from your image in order to run your code snippet.
– joni
Apr 28, 2022 at 11:33
• On 5/5, your constraint model+=lpSum(Assy_D[i])<=dfs.loc[i,'INPUT D'] forces Assy_D[i] to be 0 since dfs.loc[i,'INPUT D'] is 0 for that date. There isn't much point to the model as is though, since inputs have no weights, you could just select however much available INPUT and sum up to 100 if available. No ILP needed. Apr 28, 2022 at 12:26
• Hi Andy if i remove that constraint the model is filling 100 as output on non material available date also , example for Date 4/30 input of is 0 but the model will fill value for Assy Out D on that date , so to restrict that i need to give this lock Apr 28, 2022 at 12:37
• There is no more input left over for 5/5, hence it does not sum up to 100. It uses up 20 and 40 for B1 and B2 respectively. Apr 28, 2022 at 20:29
• The Cumulative available qty of B2 and D is not utilized fully , example Input D cumulative 220 and Assy_Out D 100 only , at least in last day (5/5) there is a available capacity of 40 and it can be untilized Apr 29, 2022 at 1:00

This is one way you can formulate/model the problem: \begin{align*} x_{i,t} &\in R^{+} \text{ variable denoting amount of raw material i processed/out in time t}\\ q_{i,t} &\in R^{+} \text{ variable denoting leftover raw material i at end of time t}\\ I_{i,t} &\in R^{+} \text{ Parameter denoting amount of raw material i that is provided in time t} \end{align*} Quantity of raw material $$i$$ processed/out in time $$t$$ is less than available inventory & left over inventory needs to carry-forward \begin{align*} x_{i,t}+q_{i,t} &= I_{i,t} \qquad \qquad \forall i,t | t= 0 \\ x_{i,t}+q_{i,t} &= q_{i,t-1} + I_{i,t} \quad \forall i,t|t> 0 \end{align*} Limit on processing capacity \begin{align*} \sum_{i} x_{i,t} &\le C^{\text{max}} \qquad \qquad \forall t \end{align*} Objective is minimize left over of each week to get the behaviour required for total quantity processed across all materials & times $$\text{minimize} \sum_{i,t} q_{i,t}$$ Note: As per your formulation, openLine[i] can always take 1 for each time period and binary is not needed as per requirement.