# Economic Dispatching using PSO and Nested PSO

URL: https://yarpiz.com/economic-dispatching-ypap113
Author: Mostapha Kalami Heris
Published: 2015-09-22
Updated: 2026-10-30
Topics: Applications

> Study economic dispatch in MATLAB with PSO and nested PSO, generator limits, prohibited zones, transmission losses, and plant-selection costs.

This post shares an open-source MATLAB implementation of Particle Swarm Optimization (PSO) and Nested PSO for the economic power dispatching problem.

Economic dispatch is a classic problem in power system operation. A set of generating plants must together supply the demand of the network, plus the power lost in the transmission lines. Each plant has its own fuel cost and operating limits. The task is to choose the output of every plant so that the demand is met at the lowest total cost.

## Problem definition

Let $P_i$ be the output of plant $i$, for $i = 1, \dots, N$. The cost of each plant is a quadratic function of its output, and the problem is:

$$
\min_{P} \; \sum_{i=1}^{N} C_i(P_i), \qquad C_i(P_i) = \alpha_i + \beta_i P_i + \gamma_i P_i^2
$$

subject to

- **Power balance:** the total output equals the demand plus the losses, $\sum_i P_i = P_D + P_L$.
- **Output limits:** $P_i^{\min} \le P_i \le P_i^{\max}$.
- **Prohibited operating zones:** each plant has ranges of output where it should not run, for example because of vibration in the shaft or valve problems. $P_i$ must lie outside these ranges.

The transmission loss $P_L$ is computed with the B coefficient loss formula:

$$
P_L = P^\top B \, P + B_0^\top P + B_{00}
$$

where $P$ is the vector of plant outputs, $B$ is a square matrix, $B_0$ is a vector and $B_{00}$ is a constant.

Because of the prohibited zones, the feasible range of each plant is split into several separate parts. This makes the problem nonconvex, so classic methods based on gradients have trouble with it. A population based method such as PSO only needs the cost of each candidate, but the constraints still need explicit encoding and validation.

## The example model

`CreateModel.m` defines an example with six power plants and a total demand of 1263. Each plant has minimum and maximum output limits, cost coefficients, and prohibited operating zones. The model also includes loss coefficients. `ParseSolution.m` maps each decision variable from the range 0 to 1 to the output range of its plant. It then moves any output that falls inside a prohibited zone to the nearest edge of that zone.

In the first project, the model also has ramp rate limits. Each plant has a previous output `P0`, an up ramp limit `UR` and a down ramp limit `DR`. Its usable range is then

$$
\max(P_i^{\min}, \; P_i^0 - DR_i) \le P_i \le \min(P_i^{\max}, \; P_i^0 + UR_i)
$$

and `ParseSolution.m` maps the decision variables to this range (`PminActual` and `PmaxActual`). The second project does not use ramp limits.

`ModelCalculations.m` computes the costs, the loss and a power balance violation, $v = \max\left(0, \; 1 - (\sum_i P_i - P_L)/P_D\right)$. This measures how far the net output falls short of the demand. The first project uses the cost $z = C_{\text{total}} (1 + 100 \, v)$. The second project uses $z = C_{\text{total}} + 10^6 \, v$.

## What is in the download

The download has two projects.

### 01 Economic Dispatching using PSO

PSO solves the problem directly. The files are:

- `CreateModel.m`: defines the power system model.
- `ParseSolution.m`: converts a particle position into plant outputs.
- `ModelCalculations.m` and `MyCost.m`: compute the cost of a solution.
- `pso.m`: the PSO algorithm.

Each particle has six variables, one for each plant. The main script `pso.m` uses 200 iterations and a swarm of 100 particles, with the constriction coefficients of Clerc and Kennedy ($\phi_1 = \phi_2 = 2.05$).

### 02 Economic Dispatching using Nested PSO

This project adds an internal PSO. It has the same files as the first project, plus `CreateInternalModel.m`, `RunInternalPSO.m`, `MyCostExt.m`, and `ParseSolutionExt.m`.

Here the problem has two levels. The model has six slots for plants and six plant types. Each type has the same data as above, plus a fixed cost `F`. The question is which plants to use, and then how much each one should produce.

- **The outer PSO** (`pso.m`) decides the plant set. Each particle has six variables. `ParseSolutionExt.m` turns each one into an integer from 0 to 6. The value 0 leaves the slot empty, and a value $k$ puts a plant of type $k$ in that slot. The same type may appear in more than one slot.
- **The inner PSO** (`RunInternalPSO.m`) solves the economic dispatch for that plant set. `CreateInternalModel.m` builds a model with the chosen plants, and an empty slot gets zero limits and zero costs.

`MyCostExt.m` joins the two levels. The cost of an outer particle is the sum of the fixed costs of the chosen plants plus the best dispatch cost found by the inner PSO:

$$
z = \sum_{\text{chosen plants}} F_k + \min_{P} \, z_{\text{dispatch}}(P)
$$

Every evaluation of the outer PSO runs an inner PSO. The outer search uses 10 iterations and 10 particles, while the inner search uses 100 iterations and 50 particles. This nested structure requires many more objective evaluations than the direct formulation.

## How to run the code

Neither project has a `main.m`. Open a project folder in MATLAB and run `pso.m`.

- In the first project, the program prints the best cost in each iteration and plots the best cost against the iteration at the end.
- In the second project, the outer PSO prints its best cost in each iteration, and each inner run prints a start and an end message. The best cost of the outer PSO is plotted at the end.

The final solution is stored in the variable `BestSol`. To use your own system, change the plant data, the loss coefficients and the demand `PD` in `CreateModel.m`.

## Check the result

Check generator limits, prohibited zones, ramp limits where present, and the full power-balance residual $\sum_i P_i - P_L - P_D$. The implemented violation penalizes a supply shortfall but does not symmetrically enforce the stated balance equality, so surplus generation can remain. A low penalized cost is therefore not enough to establish feasibility. In the nested version, the outer cost uses the best inner result found, not a certified inner optimum, and random variation in that inner search can affect comparisons.

## Applications

Economic dispatch is solved many times a day by power system operators, to share the load between generating units at the lowest cost. Related problems include unit commitment (which units to switch on) and generation expansion planning (which plants to build). The nested approach of the second project shows one way to combine a selection problem with a dispatch problem.

## Related posts

- [Particle Swarm Optimization in MATLAB](https://yarpiz.com/particle-swarm-optimization-ypea102)

## How to cite

Mostapha Kalami Heris, Economic Dispatching using PSO and Nested PSO (YPAP113), Yarpiz, 2015. https://yarpiz.com/economic-dispatching-ypap113
