# YPEA: Yarpiz Evolutionary Algorithms

URL: https://yarpiz.com/yarpiz-evolutionary-algorithms-ypea
Author: Mostapha Kalami Heris
Published: 2019-05-09
Updated: 2026-10-30
Topics: Metaheuristics, Evolutionary Algorithms

> Define an optimization problem once in MATLAB and solve it with 15 algorithms through YPEA, with shared variable types, settings, and result histories.

YPEA is a general-purpose MATLAB toolbox for defining and solving optimization problems with Evolutionary Algorithms (EAs) and metaheuristics. Define a problem once, then pass it to different algorithms through the same interface. The source code is on [GitHub](https://github.com/smkalami/ypea).

## Available algorithms

YPEA currently supports these algorithms, listed in alphabetical order. Each name links to the Yarpiz post with a standalone MATLAB implementation of that algorithm.

1. [Artificial Bee Colony (ABC)](https://yarpiz.com/artificial-bee-colony-ypea114)
2. [Ant Colony Optimization for Continuous Domains (ACOR)](https://yarpiz.com/acor-ypea104)
3. [Bees Algorithm (BA)](https://yarpiz.com/bees-algorithm-ypea115)
4. [Biogeography-based Optimization (BBO)](https://yarpiz.com/biogeography-based-optimization-ypea113)
5. [Covariance Matrix Adaptation Evolution Strategy (CMA-ES)](https://yarpiz.com/cma-es-ypea108)
6. [Cultural Algorithm (CA)](https://yarpiz.com/cultural-algorithm-ypea125)
7. [Differential Evolution (DE)](https://yarpiz.com/differential-evolution-ypea107)
8. [Firefly Algorithm (FA)](https://yarpiz.com/firefly-algorithm-ypea112)
9. [Genetic Algorithm (GA)](https://yarpiz.com/genetic-algorithms-ypea101)
10. [Harmony Search (HS)](https://yarpiz.com/harmony-search-ypea117)
11. [Imperialist Competitive Algorithm (ICA)](https://yarpiz.com/imperialist-competitive-algorithm-ypea118)
12. [Invasive Weed Optimization (IWO)](https://yarpiz.com/invasive-weed-optimization-ypea119)
13. [Particle Swarm Optimization (PSO)](https://yarpiz.com/particle-swarm-optimization-ypea102)
14. [Simulated Annealing (SA)](https://yarpiz.com/real-coded-simulated-annealing-ypea106)
15. [Teaching-Learning-based Optimization (TLBO)](https://yarpiz.com/teaching-learning-based-optimization-ypea111)

## Installation

A MATLAB toolbox package file (\*.mltbx) is in the **dist** folder. Right-click this file inside MATLAB and select Install. This is the recommended way to install YPEA.

Alternatively, the source code is in the **src** folder. Download it and add the **ypea** folder to your MATLAB path.

The toolbox has its own documentation, which you can find under **Supplemental Software** in the MATLAB documentation center. To open it from the command line, type **doc ypea**.

## How YPEA works

To set up and solve a problem with YPEA, follow these steps.

1. Define the optimization problem
   - Create an instance of the optimization problem.
   - Set the problem type (minimization or maximization).
   - Define the search space and decision variables.
   - Define the objective function.
     - Note: for a constrained problem, define the constraints and include them in the final objective function with a method such as penalty functions.
2. Use an optimization algorithm
   - Create an instance of the algorithm class.
   - Set the parameters of the algorithm.
   - Call the solve method of the algorithm and pass the problem object to it.
   - Get the results and visualize them if needed.

## A worked example

### Problem definition

Suppose we want to find 20 real numbers in the range $[-10, 10]$ that minimize the well-known sphere function:

$$
f(x) = \sum_{i=1}^{20} x_i^2
$$

Its minimum value is 0, at the origin.

We define this problem with these commands in MATLAB:

```matlab {title="Define a bounded Sphere minimization problem"}
problem = ypea_problem();

problem.type = 'min';

problem.vars = ypea_var('x', 'real', 'size', 20, 'lower_bound', -10, 'upper_bound', 10);

sphere = ypea_test_function('sphere');

problem.obj_func = @(sol) sphere(sol.x);
```

### Particle Swarm Optimization (PSO)

Now we can use any algorithm in YPEA to solve this problem. Here we use Particle Swarm Optimization (PSO). Create an instance of PSO (ypea\_pso) and set its parameters.

```matlab {title="Configure the particle swarm optimizer"}
pso = ypea_pso();

pso.max_iter = 100;
pso.pop_size = 100;

pso.w = 0.5;
pso.wdamp = 1;
pso.c1 = 1;
pso.c2 = 2;
```

You can also use Constriction Coefficients for PSO:

```matlab {title="Use PSO constriction coefficients"}
phi1 = 2.05;
phi2 = 2.05;
pso.use_constriction_coeffs(phi1, phi2);
```

Next, call the solve method of PSO:

```matlab {title="Run PSO on the optimization problem"}
pso_best_sol = pso.solve(problem);
```

The best solution found by PSO is available here:

```matlab {title="Read the best PSO decision vector"}
pso_best_sol.solution.x
```

### Differential Evolution (DE)

You can pass the same problem to another algorithm. For example, to use Differential Evolution (DE), create an instance of DE (ypea\_de) and set its parameters. Here we use DE/best/2/bin, which means:

- using the best solution ever found as the base vector,
- using two difference vectors for mutation (creating the mutant vector),
- and using binomial crossover (bin) to create the trial vector. Each element of the trial vector comes from the mutant vector with the crossover probability, and from the current solution otherwise. One randomly chosen element always comes from the mutant vector.

```matlab {title="Configure Differential Evolution"}
de = ypea_de('DE/best/2/bin');

de.max_iter = 1000;
de.pop_size = 20;

de.beta_min = 0.1;
de.beta_max = 0.9;
de.crossover_prob = 0.1;
```

Call the solve method of DE and pass the problem to it:

```matlab {title="Run Differential Evolution on the problem"}
de_best_sol = de.solve(problem);
```

The best solution found by Differential Evolution is available here:

```matlab {title="Read the best DE decision vector"}
de_best_sol.solution.x
```

### Results of a run

The `solve` method returns the best solution found. Its `solution` field holds the decoded decision variables (here `solution.x`), its `position` field holds the encoded values, and its `obj_value` field holds the objective value.

The algorithm object also keeps a record of the run. The properties `best_obj_value_history` and `nfe_history` hold the best objective value and the number of function evaluations at the end of each iteration, and `run_time` holds the total run time. The `plot` and `semilogy` methods of the algorithm draw the convergence curve, against the iteration or against the number of function evaluations.

## Check the result

Use the Sphere example first: its known minimum is 0 at the origin. When comparing algorithms, keep the objective, bounds, and stopping budget consistent. Iteration counts alone are not comparable when algorithms evaluate different numbers of candidates per iteration, so inspect `nfe_history` alongside the best objective history. For a constrained model, validate the decoded solution against the original constraints as well as the penalized objective.

## Types of decision variables

Dealing with different kinds of decision variables is one of the difficulties of modeling optimization problems. YPEA supports several variable types to make problem definition easier:

- **Real variables** model real-valued (continuous) variables of any size (scalar, vector, matrix, etc.), with any combination of lower and upper bounds.
  - Sample usage: most real-world mathematical programming and optimization problems.
- **Integer variables** model integer-valued (discrete) variables of any size (scalar, vector, matrix, etc.), with any combination of lower and upper bounds.
  - Sample usage: Generalized Knapsack Problem.
- **Binary variables** are a special case of integer variables, with the lower bound set to 0 and the upper bound set to 1.
  - Sample usage: Standard (Binary) Knapsack Problem.
- **Permutation variables** model permutations of items. One variable can hold more than one permutation (as rows of a matrix).
  - Sample usage: Traveling Salesman Problem (TSP), n-Queens Problem, and Quadratic Assignment Problem (QAP).
- **Selection variables** model fixed-length selections of items. One variable can hold more than one selection (as rows of a matrix).
  - Sample usage: Feature Selection.
- **Partition/Allocation variables** partition a list of items into disjoint subsets (also known as allocation and assignment). One variable can hold more than one partitioning (as rows of matrices).
  - Sample usage: Vehicle Routing Problem (VRP) and Hub Location Allocation.
- **Fixed Sum variables** model real-valued variables that must have a fixed target sum (over the whole matrix, over rows, or over columns).
  - Sample usage: Transportation Problem.

All of these variable types are encoded as real values in the range [0,1]. This common encoding lets the algorithms use the same problem definition. The suitability of an algorithm still depends on the objective, constraints, and evaluation budget.

## How to cite

Mostapha Kalami Heris, YPEA: Yarpiz Evolutionary Algorithms (YPEA), Yarpiz, 2019. https://yarpiz.com/yarpiz-evolutionary-algorithms-ypea
