# Bin Packing Problem using GA, PSO, FA, and IWO

URL: https://yarpiz.com/bin-packing-problem-ypap105
Author: Mostapha Kalami Heris
Published: 2015-09-20
Updated: 2026-10-30
Topics: Applications

> Solve bin packing in MATLAB with GA, PSO, Firefly, or Invasive Weed Optimization, using a shared separator encoding and capacity penalties.

This package solves the Bin Packing Problem in MATLAB with four metaheuristic algorithms: Genetic Algorithm (GA), Particle Swarm Optimization (PSO), Firefly Algorithm (FA) and Invasive Weed Optimization (IWO).

In the Bin Packing Problem, a set of items with known sizes must be packed into bins of equal capacity, and the goal is to use as few bins as possible. It is a classic NP-hard problem. Simple rules such as First Fit Decreasing give good packings quickly, but finding the true minimum is hard for large instances. The problem is a good test case for metaheuristics because a solution is a grouping of items, not a vector of numbers.

## The problem

There are $n$ items with sizes $v_1, \dots, v_n$ and bins with capacity $V_{\max}$. Let $y_k = 1$ if bin $k$ is used, and $x_{ik} = 1$ if item $i$ goes into bin $k$. The standard model is

$$
\min \; \sum_{k=1}^{n} y_k
\quad \text{subject to} \quad
\sum_{k=1}^{n} x_{ik} = 1 \;\; \forall i,
\qquad
\sum_{i=1}^{n} v_i \, x_{ik} \le V_{\max} \, y_k \;\; \forall k
$$

with all $x_{ik}$ and $y_k$ binary. Every item goes into exactly one bin, and the items in a used bin must fit in its capacity.

The sample model in `CreateModel.m` has 30 items with sizes from 2 to 12 and a bin capacity of 30.

## How a solution is decoded into bins

The code does not use the binary variables directly. A solution is a permutation of the numbers 1 to $2n - 1$ (59 numbers for 30 items). The numbers 1 to $n$ are items. The numbers above $n$ are separators. Reading the permutation from left to right, each separator closes the current bin and opens a new one. Empty groups are dropped, so two separators in a row do not create an empty bin.

With $n - 1$ separators, a solution can have between 1 and $n$ bins. This encoding lets the algorithms change both the order of the items and the number of bins with simple moves.

- **GA.** Each individual is the permutation itself. The GA uses roulette wheel selection, permutation crossover and three mutation moves (swap, reversion and insertion).
- **PSO, FA and IWO.** These algorithms work with real numbers in $[0, 1]$. `BinPackingCost.m` sorts the real vector, and the sort order gives the permutation. `Mutate.m` swaps two values to make small changes.

## The cost function

`BinPackingCost.m` returns the number of bins plus a penalty for overfull bins:

$$
z = n_{\text{bin}} + \alpha \cdot \frac{1}{n_{\text{bin}}} \sum_{k=1}^{n_{\text{bin}}} \max\left(\frac{V_k}{V_{\max}} - 1, \; 0\right)
$$

Here $V_k$ is the total size of the items in bin $k$. The penalty is the mean relative violation of the capacity. Its weight is $\alpha = 10n$ in the GA folder and $\alpha = 2n$ in the PSO, FA and IWO folder. A solution with no violation is feasible, and its cost is just the number of bins.

## What is in the download

The download has two folders:

- **GA version:** `ga.m` with the helper files `PermutationCrossover.m`, `PermutationMutate.m` and `RouletteWheelSelection.m`.
- **PSO, FA and IWO version:** `pso.m`, `fa.m`, `iwo.m` and `Mutate.m`.

Each folder also has `CreateModel.m`, which defines the item sizes and the bin capacity, and `BinPackingCost.m`, which computes the cost of a solution. The cost combines the number of bins used with a penalty for bins that exceed the capacity. In the second folder, `main.m` opens a dialog where you choose PSO, FA or IWO.

## How to run

Open one of the two folders in MATLAB and run `main.m`. In the GA folder, `main.m` starts `ga.m` directly. The program prints the best cost in every iteration and plots the best cost at the end. To pack your own items, change `model.v` and `model.Vmax` in `CreateModel.m`. The code calls `randsample` and `unifrnd`, so it probably needs the Statistics and Machine Learning Toolbox.

## Parameters in the code

| Algorithm | Main parameters |
| --- | --- |
| GA | `MaxIt` 1000, `nPop` 40, crossover percentage `pc` 0.4, mutation percentage `pm` 0.8, selection pressure `beta` 5 |
| PSO | `MaxIt` 1000, `nPop` 50, `w` 1 with `wdamp` 0.99, `c1` 1.5, `c2` 2.0, 2 mutations per particle, 5 on the global best |
| FA | `MaxIt` 1000, `nPop` 20, `gamma` 1, `beta0` 2, `alpha` 0.2 with `alpha_damp` 0.98, 3 extra mutations |
| IWO | `MaxIt` 500, `nPop0` 20, `nPop` 100, seeds `Smin` 0 to `Smax` 5, `Exponent` 2, `sigma` from 1 down to 0.001 |

## Check the result

Inspect the decoded bins and verify that every item appears exactly once and no bin exceeds capacity. Report the number of feasible bins separately from the penalized score. The GA and continuous-search folders use different penalty weights, so their raw costs are not directly comparable for infeasible solutions. A simple lower bound is the total item size divided by capacity, rounded up, although that bound need not be attainable.

## Applications

Bin packing appears whenever items must be grouped into containers of fixed size:

- Loading trucks, pallets and shipping containers.
- Cutting stock: cutting pieces from standard rolls or bars with little waste.
- Memory allocation and file storage on disks of fixed size.
- Assigning tasks to machines or servers with a fixed time or capacity limit.

## Related posts

- [Genetic Algorithms in MATLAB](https://yarpiz.com/genetic-algorithms-ypea101)
- [Particle Swarm Optimization in MATLAB](https://yarpiz.com/particle-swarm-optimization-ypea102)
- [Firefly Algorithm in MATLAB](https://yarpiz.com/firefly-algorithm-ypea112)
- [Invasive Weed Optimization in MATLAB](https://yarpiz.com/invasive-weed-optimization-ypea119)
- [Parallel Machine Scheduling using Simulated Annealing](https://yarpiz.com/parallel-machine-scheduling-ypap107), which uses the same separator encoding.

## How to cite

Mostapha Kalami Heris, Bin Packing Problem using GA, PSO, FA, and IWO (YPAP105), Yarpiz, 2015. https://yarpiz.com/bin-packing-problem-ypap105
