# Optimal Inventory Control using PSO in MATLAB

URL: https://yarpiz.com/inventory-control-ypap111
Author: Mostapha Kalami Heris
Published: 2015-09-22
Updated: 2026-10-30
Topics: Applications

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

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, \dots, K$ be the products and $t = 1, \dots, H$ the periods. The data are:

- $D_{kt}$: the demand for product $k$ in period $t$ (matrix `D`).
- $I_{k0}$: the initial inventory (`I0`, zero for all products).
- $u_k$: the volume of one unit of product $k$ (`u`).
- $a_{kt}$: the unit price, and $b_{kt}$: the unit keeping cost (`a` and `b`).
- $U_{\max}$: the storage capacity (`Umax`).

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

$$
I_{kt} = I_{k,t-1} + X_{kt} - D_{kt}
$$

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

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

subject to

$$
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 $\hat{x}$ with values between 0 and 1. `ParseSolution.m` turns each value into an integer order between 0 and $3 \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 = \left( \text{purchase cost} + \text{keeping cost} + \alpha \, V_{\min} \right) \left( 1 + \beta \, V_{\max} \right)
$$

Here $V_{\min}$ is the mean shortage (the mean of $\max(0, -I_{kt})$), and $V_{\max}$ is the mean amount by which the used capacity exceeds $U_{\max}$, as a fraction of $U_{\max}$. The code uses $\alpha = 100000$ and $\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.

## Related posts

- [Particle Swarm Optimization in MATLAB](https://yarpiz.com/particle-swarm-optimization-ypea102)
- [Firefly Algorithm (FA) in MATLAB](https://yarpiz.com/firefly-algorithm-ypea112)

## How to cite

Mostapha Kalami Heris, Optimal Inventory Control using PSO in MATLAB (YPAP111), Yarpiz, 2015. https://yarpiz.com/inventory-control-ypap111
