# Practical Genetic Algorithms in Python and MATLAB: Video Tutorial

URL: https://yarpiz.com/practical-genetic-algorithms-in-python-and-matlab-ypga191215
Author: Mostapha Kalami Heris
Published: 2020-01-08
Updated: 2026-10-30
Topics: Metaheuristics, Courses, Evolutionary Algorithms

> Build binary and real-coded genetic algorithms in MATLAB and Python, with seven videos on selection, crossover, mutation, bounds, and implementation.

Genetic algorithms (GAs) are optimization techniques inspired by natural selection and genetic inheritance. Potential solutions are individuals in a population. Through selection, crossover (combining traits) and mutation (introducing variations), the population evolves over generations, and the fittest individuals are favored.

GAs are a subset of Evolutionary Algorithms (EAs), which simulate an artificial environment based on the theory of evolution to solve mathematical problems, especially optimization problems. They also belong to the broader class of metaheuristics, which are general-purpose search methods that need only the values of the objective function.

## About the course

The free course "Practical Genetic Algorithms in Python and MATLAB" is taught by Mostapha Kalami Heris on Yarpiz. It has seven videos, about 4 hours and 14 minutes in total, in three parts:

1. **Theory.** The core ideas of evolutionary algorithms and genetic algorithms, and the main GA operators.
2. **MATLAB implementation.** A Binary Genetic Algorithm built from scratch, then changed into a Real-Coded Genetic Algorithm.
3. **Python implementation.** The real-coded GA implemented from scratch in Python with VS Code.

In both MATLAB and Python, the code is run, results are plotted, and parameters are changed to show how the algorithm behaves. The source code from the course is available in the downloads section of this page.

## How a genetic algorithm works

The course starts from a general optimization problem: find the vector $x = (x_1, \dots, x_n)$ that minimizes a function $f(x)$. It compares this with evolution in nature, where living forms are solutions to the problem of living in different conditions.

Every evolutionary algorithm follows the same loop. It creates a random initial population, selects members to reproduce, creates offspring, and selects again from parents and offspring, until a stopping condition is met. The GA in the course runs these steps:

1. **Initialization.** Create and evaluate a random population of $n_{\text{pop}}$ individuals.
2. **Crossover.** Select pairs of parents. Each pair creates two offspring.
3. **Mutation.** Mutate the offspring.
4. **Merging.** Merge the main population and the offspring.
5. **Evaluation, sorting and selection.** Sort the merged population by cost and keep the best $n_{\text{pop}}$ members.
6. **Termination.** Stop, or go back to step 2.

### Crossover

Crossover combines the genes of two parents. For binary chromosomes, the course shows three operators:

- **Single point crossover** cuts both parents at one random point and swaps the right-hand parts.
- **Double point crossover** cuts at two random points and swaps the middle parts.
- **Uniform crossover** decides gene by gene which parent each gene comes from.

Uniform crossover can be written with a vector $\alpha$ of random zeros and ones:

$$
y_{1i} = \alpha_i x_{1i} + (1-\alpha_i)\, x_{2i}, \qquad y_{2i} = \alpha_i x_{2i} + (1-\alpha_i)\, x_{1i}
$$

### Mutation

Mutation simulates errors in copying genes. It makes the algorithm more explorative and lets it find new solutions. For binary chromosomes, a mutation flips a gene from 0 to 1 or from 1 to 0. The mutation rate $\mu$ is the share of genes that change. For example, changing 2 genes out of 100 is a mutation rate of 2%.

### Parent selection

The course compares three ways to select parents:

- **Random selection** gives every member the same chance. It is simple, but it ignores the quality of the solutions.
- **Tournament selection** picks a few members at random and chooses the best of them, or chooses one of them with a probability that is higher for better members.
- **Roulette wheel selection** gives each member a selection probability, and better members get higher probabilities.

For the roulette wheel, the course uses a rule similar to the Boltzmann distribution. With costs $c_i$ and their mean $\bar{c}$, the selection probability is

$$
p_i \propto e^{-\beta c_i / \bar{c}}
$$

The selection pressure $\beta$ controls how strongly the better members are favored. With $\beta = 0$ all members have the same chance. Larger values concentrate selection on the members with lower costs.

### Operators for real numbers

When the decision variables are real numbers with lower and upper bounds, the operators change:

- **Crossover** uses the same blend formula, but each $\alpha_i$ is a real random number. To explore more, it can be drawn from $[-\gamma, 1+\gamma]$ instead of $[0, 1]$, with a small $\gamma$ such as 0.1.
- **Mutation** adds a random step to the chosen genes, $x_j' = x_j + \sigma z$, where $z$ is drawn from the standard normal distribution. The step size $\sigma$ is an important parameter.
- **Bounds.** After crossover and mutation, every value outside its range is set back to the nearest bound.

## Videos in the course

::: video {playlist="PLgH3sgdvgO4TQagjfi1FmPcdBjIlmk5s9" title="Practical Genetic Algorithms in Python and MATLAB: Video Tutorial"}
:::

The playlist above contains all seven videos. The topics of each video are listed below, in order, with start times.

### 1. Introduction to Genetic Algorithms (39:12)

The theory described in the section above, in this order: the optimization problem and evolution (from 1:26), the structure of an evolutionary algorithm (from 4:04), the steps of a GA (from 8:05), crossover (from 12:02), mutation (from 24:17), parent selection (from 29:36), and merging, sorting and truncation (from 34:55).

### 2. Binary Genetic Algorithm in MATLAB, Part A (43:37)

The test problem is MinOne: minimize $f(x) = \sum_{i=1}^{n} x_i$ with binary $x_i$, whose solution is all zeros. The video writes `MinOne.m` and a main script `app1.m` with three sections: problem definition, GA parameters, and a call to `out = RunGA(problem, params)`. It stores the problem and the parameters in structures (from 5:26), builds the population with an individual template and `repmat` (from 13:25), and creates random binary chromosomes with `randi` (from 16:04). It keeps the best solution ever found, which starts with a cost of infinity (from 21:25), and an array of best costs per iteration created with `nan`. In the main loop, the number of offspring comes from a parameter `pC` and is rounded to an even number (from 27:21). Parents are first chosen at random with `randperm` (from 32:11), and `SinglePointCrossover.m` is written (from 35:00).

### 3. Binary Genetic Algorithm in MATLAB, Part B (36:41)

The video writes `Mutate.m`, which flips each gene with probability $\mu$ by comparing `rand` with the mutation rate and using logical indexing (from 0:08). It then evaluates the mutants, merges the populations, sorts them with `sort` and its second output, and moves the sorting into `SortPopulation.m` (from 9:34). With iteration information shown with `disp`, the GA runs for the first time (from 18:56). When the problem grows, single point crossover struggles, and a smaller mutation rate helps (from 20:14). The video then writes `DoublePointCrossover.m` (from 22:53) and `UniformCrossover.m`, with a short review of element-wise products in MATLAB (from 28:21). Finally, `MyCrossover.m` picks one of the three operators at random (from 32:19).

### 4. Binary Genetic Algorithm in MATLAB, Part C (26:47)

The video explains roulette wheel selection by unrolling the wheel into a line and comparing a random number with the cumulative sum of the probabilities, using `cumsum` and `find` (from 0:06). It writes `RouletteWheelSelection.m` so that the probabilities do not need to add up to 1 (from 9:35). It then derives the selection probabilities from the costs with the Boltzmann type rule and the selection pressure $\beta$ (from 13:34), and handles a mean cost of zero, which first causes an error (from 20:07). The video ends with a review of the whole binary GA and a convergence plot (from 21:30).

### 5. Real-Coded Genetic Algorithm in MATLAB (31:19)

The video defines real-valued problems with lower and upper bounds, and the real-coded crossover and Gaussian mutation, including the $\gamma$ extension (from 0:07). The test problem is the sphere function, $f(x) = \sum_{i=1}^{n} x_i^2$ (from 6:45). The binary code is copied and changed: real $\alpha$ in the crossover, a step size $\sigma$ in the mutation, and random positions from `unifrnd` (from 12:08). The convergence plot changes from `plot` to `semilogy` (from 18:46), and $\gamma$ becomes a parameter (from 21:22). The video then clips the offspring to the bounds (from 22:43). It shows that each variable can have its own bounds, given as vectors. With one variable limited to $[5, 8]$, the best cost is 25, and without the bound check the GA returns an infeasible answer (from 26:48).

### 6. Genetic Algorithm in Python, Part A (42:40)

The Python version implements the real-coded GA in two files, `app.py` and `ga.py`, with NumPy and Matplotlib. Problem and parameters are stored with the `structure` class of the `ypstruct` package, installed with `pip install ypstruct` (from 1:28). The video writes the function `run(problem, params)` (from 6:46) and the population, using `repeat` for the template and `np.random.uniform` for random positions (from 10:43). Because structures are references, it uses `deepcopy` for the best solution (from 16:01). It computes the number of offspring, selects random parents with `np.random.permutation` (from 21:25), and writes `crossover` with the $\gamma$ extension (from 28:01) and `mutate` with `np.argwhere` and `np.random.randn` (from 33:18).

### 7. Genetic Algorithm in Python, Part B (33:49)

The video adds the parameters $\mu$ and $\sigma$, and an `apply_bound` function based on `np.maximum` and `np.minimum` (from 0:06). It evaluates the offspring, merges and sorts the population with `sorted` and a lambda key (from 6:45), and runs the GA for the first time after fixing the type of the offspring count (from 12:10). It draws the convergence plot with Matplotlib, first with `plot` and then with `semilogy` (from 13:30). It writes `roulette_wheel_selection` with `np.cumsum` and `np.argwhere` (from 17:34) and adds the Boltzmann type selection probabilities (from 24:15). It ends with different bounds for each variable, where the best cost is 16 (from 29:32), and a remark that most problems can be coded with real variables (from 32:11).

## The course code

The download holds the final code of the course:

- `MATLAB/Binary GA`: `app1.m`, `RunGA.m`, `MinOne.m`, `SinglePointCrossover.m`, `DoublePointCrossover.m`, `UniformCrossover.m`, `MyCrossover.m`, `Mutate.m`, `RouletteWheelSelection.m` and `SortPopulation.m`.
- `MATLAB/Real-Coded GA`: `app1.m`, `RunGA.m`, `Sphere.m`, `UniformCrossover.m`, `Mutate.m`, `RouletteWheelSelection.m` and `SortPopulation.m`.
- `Python`: `app.py` and `ga.py`.

To run a version, open its folder and run `app1.m` in MATLAB, or `app.py` in Python. The parameters are set in these files:

| Parameter | Binary GA (MATLAB) | Real-coded GA (MATLAB) | Real-coded GA (Python) |
| --- | ---: | ---: | ---: |
| Number of variables | 100 | 5 | 5 |
| Maximum iterations | 100 | 100 | 100 |
| Population size | 100 | 1000 | 50 |
| Selection pressure $\beta$ | 1 | 1 | 1 |
| Offspring ratio `pC` | 1 | 1 | 1 |
| Mutation rate $\mu$ | 0.02 | 0.02 | 0.01 |
| Crossover range $\gamma$ | | 0.1 | 0.1 |
| Mutation step $\sigma$ | | 0.1 | 0.1 |

The real-coded versions use a different range for each variable. The MATLAB code also uses `unifrnd`, which comes from the Statistics and Machine Learning Toolbox, so the real-coded MATLAB version probably needs that toolbox.

## Check the result

For the binary MinOne example, the minimum cost is 0 at the all-zero chromosome. For the real-coded examples, use the stated bounds when identifying the expected minimum: excluding the origin changes the best feasible Sphere value. Check both the returned variables and the cost after each change to crossover, mutation, or bound handling. The course implementation and the separate MATLAB code package have different defaults, so compare their own parameter tables rather than mixing settings.

## Requirements

- Basic programming skills, because the course implements GAs in MATLAB and Python.
- Basic mathematics, such as algebra and some probability.
- MATLAB, Python with NumPy, Matplotlib and `ypstruct`, and a code editor such as VS Code.

No prior knowledge of genetic algorithms is needed. The course suits students and engineers who work on optimization problems, and anyone interested in AI and algorithms.

## Related posts

- [Binary and Real-Coded Genetic Algorithms in MATLAB](https://yarpiz.com/genetic-algorithms-ypea101) shares another structured GA implementation, with roulette wheel, tournament and random selection.
- [YPEA](https://yarpiz.com/yarpiz-evolutionary-algorithms-ypea) is a MATLAB toolbox with a GA and 14 other algorithms that share one problem definition.

## How to cite

Mostapha Kalami Heris, Practical Genetic Algorithms in Python and MATLAB: Video Tutorial (YPGA191215), Yarpiz, 2020. https://yarpiz.com/practical-genetic-algorithms-in-python-and-matlab-ypga191215
