How to Solve a Production Planning and Inventory Problem in Python | by Khuyen Tran | Towards Data Science

Imagine you are an owner of a clothing store. The demand for clothes varies from day to day (more people prefer to go shopping on the weekend than on the weekday). The production cost also varies from day to day (it costs more to hire workers to work on the weekend).
Your job is to determine how many units of clothes to produce each day.
Since you can store your clothes, you might decide to produce as many clothes as possible on the cheapest day. However, it costs $1 to store 1 unit per day, so you also need to take into account that the holding cost increases as the number of units being stored increases.
How many products should you produce a day so that the total cost is minimized?
You can experiment with different numbers of units to produce per day and choose the one with the lowest cost, but this is time-consuming. Luckily, we can solve this problem using Python and linear programming.
Linear programming is a mathematical modeling technique in which a linear function is maximized or minimized when subjected to various constraints.
CVXPY is a Python tool that provides interfaces to many linear programming solvers. To install CXVPY, type:
pip install cxvpy
Let’s identify the input parameters, decision variables, objectives, and constraints of this problem.
We want to determine how many units to produce and how many units are stored by the end of day t.
Note that the ending inventory variable starts at day 0 since the constraints need to know the inventory at the beginning of day 1.
We want to minimize the total cost of 4 days. This includes the production cost and the holding cost.
Note that since the inventory cost is $1 per unit per day, the ending inventory cost for day t is:
We can rewrite the sum as a matrix multiplication like below:
The ending inventory is equal to the sum of the beginning inventory and the production quantity of day t subtracted by the demand in day t:
In general, we have:
The number of units in inventory at the beginning of day 1 is 0:
The daily number of units in inventory cannot be negative:
The daily production quantity cannot be negative:
Wait, but don’t we also need to include the constraints for the demand? That is not necessary. The daily number of units in the ending inventory is equal to or greater than 0, which indicates that the daily demand is fulfilled.
Now we have the input parameters, constraints, and objectives, we’re ready to solve the problem!
1890.0
Awesome! The problem is solved and the value of the objective is 1890. This means the optimal solution results in a total cost of $1890.
What is the production quantity for days 1, 2, 3, and 4?
[ 75. 250.   0.  60.]
Let’s also find the ending inventory for each day:
[  0. 100.   0.   0.]
Cool! Based on the results, we will:
produce just enough to fulfill the demand of day 1 on day 1
produce enough to fulfill the demand of day 2 and 3 on day 2
produce just enough to fulfill the demand of day 3 on day 3
When using a new tool, we should double-check to make sure it gives us an optimal solution. Let’s double-check the solution CXVPY gives us.
To do that, we will first create a function to calculate the total cost based on the production quantity of each day.
Then we will experiment with different possible combinations. We want to make sure these combinations satisfy the constraints we wrote above.
Since the production cost of the first day is the cheapest, let’s see what the total cost would be if we produce every unit needed for 4 days on the first day.
2070
This solution is less optimal than the objective value we got earlier.
The total cost of producing everything on the first day is so high can because of the accumulation of the holding cost. What if we produce some on the first day and some on the second day?
2070
This solution is not better either.
Okay. What if we prevent the holding cost altogether and only produce enough for each day?
1990
Nope. This solution is still not good.
What if we produce everything on the first day and the last day?
1890
Aha! We found a solution that results in the same objective value. That means there are 2 different solutions that produce the same objective value of 1890. This still means that we cannot find a better objective value than 1890.
I hope you are convinced that the solution we find above is the optimal solution. If you are still not convinced, feel free to experiment with other possible combinations.
Congratulations! You have just learned how to solve production and inventory problems using CXVPY. I hope this article will give you the motivation to solve similar problems using Python.
The problem above might be easy to solve in your head. But as the problem becomes bigger, you will save yourself time by using linear programming.
The source code of this article could be found here:
github.com
I like to write about basic data science concepts and play with different algorithms and data science tools. You could connect with me on LinkedIn and Twitter.
Star this repo if you want to check out the codes for all of the articles I have written. Follow me on Medium to stay informed with my latest data science articles like these: