Applications

Optimal Inventory Control using PSO in MATLAB

Get the code
Comments

Plan integer order quantities in MATLAB with PSO or Firefly search, inventory balances, purchasing and holding costs, and storage-capacity penalties.

Illustration for Optimal Inventory Control using PSO in MATLAB
On this page

This package solves an inventory control problem with Particle Swarm Optimization (PSO) and the Firefly Algorithm, both written in MATLAB.

Inventory control decides how much of each product to order in each period. Ordering too little creates shortages, while ordering too much uses storage and increases holding cost. The code plans the order quantities for several products over a fixed time horizon, with the aim of reducing total cost while meeting demand and storage limits.

The model

The model in CreateModel.m describes three products over a time horizon of ten periods. It holds the demand, the initial inventory, the volume of each item, the item prices, the inventory keeping costs and a maximum storage capacity. A solution gives the quantity to order for each product in each period. The cost function adds the purchase and keeping costs and penalizes negative inventory and storage above the capacity.

Problem definition

Let k=1,…,Kk = 1, \dots, K be the products and t=1,…,Ht = 1, \dots, H the periods. The data are:

  • DktD_{kt}: the demand for product kk in period tt (matrix D).
  • Ik0I_{k0}: the initial inventory (I0, zero for all products).
  • uku_k: the volume of one unit of product kk (u).
  • akta_{kt}: the unit price, and bktb_{kt}: the unit keeping cost (a and b).
  • Umax⁡U_{\max}: the storage capacity (Umax).

The decision variables are the order quantities XktX_{kt}, which are integers. The inventory at the end of each period follows from the balance equation

Ikt=Ik,t−1+Xkt−DktI_{kt} = I_{k,t-1} + X_{kt} - D_{kt}

The problem is to minimize the purchase cost plus the keeping cost:

min⁡  ∑k=1K∑t=1H(aktXkt+bktIkt)\min \; \sum_{k=1}^{K} \sum_{t=1}^{H} \left( a_{kt} X_{kt} + b_{kt} I_{kt} \right)

subject to

Ikt≥0,∑k=1KukIkt≤Umax⁡,Xkt∈{0,1,2,… }I_{kt} \ge 0, \qquad \sum_{k=1}^{K} u_k I_{kt} \le U_{\max}, \qquad X_{kt} \in \{0, 1, 2, \dots\}

The first constraint means that no demand is left unmet. The second limits the volume stored at the end of each period.

How the code handles the constraints

Both algorithms search a continuous matrix x^\hat{x} with values between 0 and 1. ParseSolution.m turns each value into an integer order between 0 and 3max⁡D3 \max D, which is 270 for this data. It then computes the inventory levels and the used capacity in each period.

The two constraints are handled with penalties in MyCost.m:

z=(purchase cost+keeping cost+α Vmin⁡)(1+β Vmax⁡)z = \left( \text{purchase cost} + \text{keeping cost} + \alpha \, V_{\min} \right) \left( 1 + \beta \, V_{\max} \right)

Here Vmin⁡V_{\min} is the mean shortage (the mean of max⁡(0,−Ikt)\max(0, -I_{kt})), and Vmax⁡V_{\max} is the mean amount by which the used capacity exceeds Umax⁡U_{\max}, as a fraction of Umax⁡U_{\max}. The code uses α=100000\alpha = 100000 and β=10\beta = 10. A solution is marked feasible when both violations are zero.

Note that CreateModel.m sets Umax to 1000, but both pso.m and fa.m change it to 400 right after they load the model. So the runs use a capacity of 400. Change that line to try other capacities.

The two algorithms

PSO was introduced by Kennedy and Eberhart in 1995. Each particle moves through the search space under the pull of its own best position and the best position of the swarm. The Firefly Algorithm was proposed by Xin-She Yang in 2008. Each firefly moves toward brighter (better) fireflies, with an attraction that decreases with distance, plus a small random step.

The parameters are set at the top of each file:

File Parameter Value
pso.m MaxIt (iterations) 500
pso.m nPop (swarm size) 150
pso.m w, wdamp (inertia weight and its damping ratio) 1, 0.99
pso.m c1, c2 (learning coefficients) 2, 2
fa.m MaxIt (iterations) 1000
fa.m nPop (number of fireflies) 25
fa.m gamma (light absorption coefficient) 1
fa.m beta0 (base attraction) 2
fa.m alpha, alpha_damp (mutation coefficient and its damping ratio) 0.3, 0.98
fa.m delta (uniform mutation range) 0.07

What is in the download

  • main.m: asks which algorithm to run, PSO or Firefly Algorithm
  • pso.m and fa.m: the two optimizers
  • CreateModel.m: the problem data
  • CreateRandomSolution.m, ParseSolution.m and MyCost.m: solution creation, decoding and the cost function
  • PlotSolution.m: plots a solution
  • license.txt: license terms

To try it, run main.m in MATLAB and choose an algorithm.

A dialog box asks you to pick the algorithm. You can also run pso.m or fa.m directly. In each iteration the program prints the best cost and adds “(Feasible)” when the best solution meets both constraints. It also plots three panels for the best solution: the order amounts, the inventory levels and the used capacity against the limit. At the end it plots the best cost against the iteration. To solve your own problem, change the matrices in CreateModel.m. The number of products and periods follows from the size of D.

Check the result

Recompute inventory period by period from the decoded integer orders. Check that every inventory is nonnegative and the stored volume stays within capacity, then report purchase and holding costs separately from penalties. The running scripts override storage capacity to 400, so editing only the 1000 value in CreateModel.m does not change the actual experiment. The example assumes known demand and immediate period-level replenishment rather than uncertain demand or delivery lead times.

Applications

The same kind of model appears in production and purchase planning for factories, retail stores and warehouses, and in spare parts planning. Real models often add other terms, such as a fixed cost for each order or uncertain demand. This example does not include them, but the cost function in MyCost.m is the place to add such terms.

Downloads

How to cite

If you use this code or tutorial in your work, articles or publications, please cite it.

Plain text
Mostapha Kalami Heris, Optimal Inventory Control using PSO in MATLAB (YPAP111), Yarpiz, 2015. https://yarpiz.com/inventory-control-ypap111
BibTeX
@misc{yarpiz-ypap111,
  author = {{Mostapha Kalami Heris}},
  title = {{Optimal Inventory Control using PSO in MATLAB}},
  howpublished = {Yarpiz},
  year = {2015},
  note = {Code YPAP111},
  url = {https://yarpiz.com/inventory-control-ypap111}
}

Comments

Sign in or create a free account to join the discussion. No password needed.