Genetic Algorithm Optimization in MATLAB
A genetic algorithm is a population-based optimization method inspired by natural selection. It repeatedly selects promising candidate solutions and creates new solutions using crossover and mutation.
Genetic algorithms can be useful when:
- The objective function is nonlinear, discontinuous or difficult to differentiate.
- The search space contains several local optima.
- Traditional derivative-based methods are unsuitable.
A genetic algorithm is not automatically faster or better than every other
optimization technique. The appropriate method depends on the problem.
For the linear examples below, linprog would normally be more
efficient, but they provide a simple introduction to MATLAB’s
ga syntax.
Main components
- Fitness function: measures the quality of a candidate solution.
- Selection: chooses candidates for reproduction.
- Crossover: combines information from selected candidates.
- Mutation: introduces controlled random changes.
MATLAB syntax
[x, fval, exitflag] = ga( ...
fitnessFunction, ...
numberOfVariables, ...
A, b, ...
Aeq, beq, ...
lowerBounds, upperBounds);
MATLAB expresses linear inequality constraints in this form:
A × x ≤ b
Use empty brackets, [], when an optional constraint is not
required.
Problem 1: Minimization with linear constraints
Minimize:
z = −x1 + 2x2
Subject to:
- −x1 + 3x2 ≤ 10
- x1 + x2 ≤ 6
- x1 − x2 ≤ 2
- x1 ≥ 0 and x2 ≥ 0
Fitness function: fitness1.m
function y = fitness1(x)
y = -x(1) + 2*x(2);
end
MATLAB commands
A = [-1 3;
1 1;
1 -1];
b = [10;
6;
2];
lb = [0 0];
[x, fval, exitflag] = ga( ...
@fitness1, 2, A, b, [], [], lb, []);
The solution should be approximately:
x ≈ [2 0]
fval ≈ -2
Genetic algorithms use randomness, so the displayed values may differ slightly between runs.
Problem 2: Converting greater-than constraints
Minimize:
z = 20x1 + 40x2
Subject to:
- 36x1 + 6x2 ≥ 108
- 3x1 + 12x2 ≥ 36
- 20x1 + 12x2 ≥ 100
- x1 ≥ 0 and x2 ≥ 0
MATLAB requires inequalities in the form A × x ≤ b. Therefore, multiply each greater-than constraint by −1:
- −36x1 − 6x2 ≤ −108
- −3x1 − 12x2 ≤ −36
- −20x1 − 12x2 ≤ −100
Fitness function: fitness2.m
function y = fitness2(x)
y = 20*x(1) + 40*x(2);
end
MATLAB commands
A = [-36 -6;
-3 -12;
-20 -12];
b = [-108;
-36;
-100];
lb = [0 0];
[x, fval, exitflag] = ga( ...
@fitness2, 2, A, b, [], [], lb, []);
The solution should be approximately:
x ≈ [3.7647 2.0588]
fval ≈ 157.6471
Important observation
The original third row of matrix A used
[-20 -10], although the stated constraint contained
12x₂. It has been corrected to [-20 -12].
Learn With Champak: Experiment with different population sizes, mutation settings and stopping criteria to observe how they affect the quality and speed of the result.











