# Facility Layout Design using PSO in MATLAB

URL: https://yarpiz.com/layout-design-ypap109
Author: Mostapha Kalami Heris
Published: 2015-09-22
Updated: 2026-10-30
Topics: Applications

> Design a facility layout in MATLAB with PSO, block positions and rotations, material-flow costs, clearance gaps, and overlap penalties.

This package is a MATLAB implementation of facility layout design using Particle Swarm Optimization (PSO). The project title in the code is "Facility Layout Design using PSO in MATLAB".

The model describes eight blocks (machines). Each block has a width, a height, a gap and the positions of its input and output gates. The solver searches for block positions that give a good layout.

In a facility layout problem, the task is to place machines or departments on a floor so that material moves short distances between them. This code solves the continuous version with blocks of unequal size: each block can go anywhere on the floor and can be rotated, but blocks must not overlap. The code places the blocks only. It does not decide where a facility should be built or which customers it serves, which is what a location allocation problem would do.

## The problem

The floor is a rectangle of width $W = 100$ and height $H = 80$. Block $i$ has a width $w_i$, a height $h_i$ and a gap $\delta_i$ that must stay free around it. Material leaves block $i$ at its output gate and enters block $j$ at its input gate. The amount of flow from $i$ to $j$ is $a_{ij}$, given as an 8 by 8 matrix in `CreateModel.m`.

For each block, the decision variables are:

- the position of its center, $(x_i, y_i)$,
- its rotation, one of 0, 90, 180 or 270 degrees.

The distance $d_{ij}$ is the rectilinear (Manhattan) distance from the output gate of block $i$ to the input gate of block $j$, after the rotation. Material handling usually moves along aisles, so this distance is a common choice.

## The cost function

`ParseSolution.m` computes the cost of a layout as the sum of three terms:

$$
z = \sum_{i}\sum_{j} a_{ij} \, d_{ij} \; + \; \phi \cdot \frac{A_{\text{box}} - A_{\text{blocks}}}{A_{\text{box}}} \; + \; \alpha \, v
$$

- The first term is the material handling cost: flow times distance, summed over all pairs.
- The second term is the unused area. $A_{\text{box}}$ is the area of the smallest rectangle that holds all blocks, and $A_{\text{blocks}}$ is the total area of the blocks. The weight is $\phi = 50000$. This term makes the layout compact.
- The third term is the overlap penalty. For each pair of blocks, the code measures how much they overlap in $x$ and in $y$, including the gap, and $v$ is the mean overlap over all pairs. The weight is $\alpha = 10^{12}$, which strongly penalizes overlap. A layout with $v = 0$ is feasible, and the program marks it with "(Feasible)".

## How PSO handles the layout

Each particle holds three vectors with one value per block, all in $[0, 1]$:

- `xhat` and `yhat` are scaled to the range of positions where the block fits on the floor, with its gap.
- `rhat` is split into four equal parts, and each part gives one of the four rotations.

The swarm moves all three vectors with the usual PSO velocity update. Two extra steps help the search:

- **Mutation.** After each move, `Mutate.m` shifts the $x$ value of one random block and the $y$ value of another random block (they may be the same block) by a small random amount. The change is kept if it is not worse, and also with probability 0.2 when it is worse.
- **Local search.** In each iteration, `ImproveSolution.m` visits the blocks of the global best layout in random order. For each block it tries staying in place and eight moves (up, down, left, right and the four diagonals), each with all four rotations, and keeps the best result.

## Files in the download

- `pso.m`: the PSO algorithm.
- `CreateModel.m`: defines the blocks and their gates.
- `CreateRandomSolution.m`, `ParseSolution.m` and `MyCost.m`: create a candidate layout, decode it and compute its cost.
- `Mutate.m` and `ImproveSolution.m`: modify a solution and improve it by moving machines.
- `PlotSolution.m`: draws the layout.
- `license.txt`: license terms.

## How to run

Open the folder in MATLAB and run `pso.m`. There is no separate `main.m`.

The program prints the best cost in every iteration. `PlotSolution.m` draws the floor with each block as a numbered colored rectangle, the input gates as open circles, the output gates as filled circles and the bounding rectangle as a red dash-dot line. At the end, a second figure shows the best cost against the iteration.

To design your own layout, change the block sizes, gaps, gate positions, the flow matrix `a` and the floor size in `CreateModel.m`. The gate positions `rin` and `rout` are numbers in $[0, 1]$ that run once around the edge of the block. The code calls `unifrnd`, so it probably needs the Statistics and Machine Learning Toolbox.

## Parameters in the code

| Parameter | Meaning | Value |
| --- | --- | ---: |
| `MaxIt` | Number of iterations | 500 |
| `nPop` | Swarm size | 50 |
| `w` | Inertia weight | 1.0 |
| `wdamp` | Inertia weight damping ratio | 0.99 |
| `c1` | Personal learning coefficient | 0.7 |
| `c2` | Global learning coefficient | 1.5 |
| `phi` | Weight of the unused area (in `CreateModel.m`) | 50000 |
| `alpha` | Weight of the overlap penalty (in `ParseSolution.m`) | $10^{12}$ |

## Check the result

Check the decoded geometry directly: each rotated block and its required gap must fit within the floor, and no pair may overlap. A large finite penalty is not a substitute for checking those constraints. Inspect material-flow cost, compactness, and overlap separately when comparing layouts. Keep units consistent across dimensions, gate positions, and flow weights, and compare several runs before selecting a practical layout.

## Applications

Facility layout design is used in:

- Factory and workshop planning, to cut material handling cost.
- Hospital layout, where patients and staff move between departments.
- Warehouse design.
- Placing components on a chip or a circuit board.

## Related posts

- [Particle Swarm Optimization in MATLAB](https://yarpiz.com/particle-swarm-optimization-ypea102)
- [Quadratic Assignment Problem using GA, PSO and FA](https://yarpiz.com/quadratic-assignment-problem-ypap104), the discrete version of layout design, where units go to fixed sites.
- [p-Hub Location Allocation Problem in MATLAB](https://yarpiz.com/hub-location-allocation-ypap110), a location allocation problem.

## How to cite

Mostapha Kalami Heris, Facility Layout Design using PSO in MATLAB (YPAP109), Yarpiz, 2015. https://yarpiz.com/layout-design-ypap109
