Experiment 5: -
5.1. Aim of the Experiment: -
Solution single objective optimization problem using Genetic algorithm in MATLAB.
5.2. Software/tools required: -
MATLAB/ optimization tool.
5.3. Theory: -
Genetic Algorithm: -
The genetic algorithm is a method for solving both constrained and unconstrained
optimization problems that is based on natural selection, the process that drives
biological evolution. The genetic algorithm repeatedly modifies a population of
individual solutions. At each step, the genetic algorithm selects individuals from the
current population to be parents and uses them to produce the children for the next
generation. Over successive generations, the population "evolves" toward an optimal
solution.
5.4. Minimizing Rastrigin's Function: -
This is a common test function for optimization algorithms. You can use the ga function
in MATLAB to find the minimum of this highly nonlinear function.
MATLAB Code: -
nvars = 2; % Number of variables
lb = [-5.12, -5.12]; % Lower bounds
ub = [5.12, 5.12]; % Upper bounds
options = optimoptions('ga', 'Display', 'iter');
[x, fval] = ga (@rastriginsfcn, nvars, [], [], [], [], lb, ub, [], options);
Single objective optimization: -
2 Variables.
Options: -
CreationFcn: @gacreationuniform
CrossoverFcn: @crossoverscattered
SelectionFcn: @selectionstochunif
MutationFcn: @mutationadaptfeasible
Tabulation: -
Generation Func-count Best f(x) Mean f(x) Stall Generations
1 100 6.04 31.34 0
2 147 5.355 22.62 0
3 194 3.285 17.29 0
4 241 3.285 13.45 1
5 288 2.224 12.1 0
….
75 3578 4.745e-10 1.812e-06 20
76 3625 4.745e-10 1.62e-06 21
77 3672 4.745e-10 1.279e-06 22
78 3719 4.745e-10 1.546e-06 23
79 3766 4.745e-10 1.359e-06 24
ga stopped because the average change in the fitness value is less than options.
FunctionTolerance.
5.5. Minimizing a Quadratic Function: -
This example demonstrates how to use a genetic algorithm to find the minimum of a
simple quadratic function, (f(x)=x2+3x+2).
MATLAB Code: -
% Define the quadratic function
quadraticFcn = @(x) x.^2 + 3*x + 2;
% Set the bounds for the variable
lb = -10; % Lower bound
ub = 10; % Upper bound
% Run the genetic algorithm
options = optimoptions('ga', 'Display', 'iter');
[x, fval] = ga(quadraticFcn, 1, [], [], [], [], lb, ub, [], options);
disp(['Optimal x:', num2str(x)]);
disp(['Function value at optimal x:', num2str(fval)]);
Single objective optimization: -
1 Variable.
Options: -
CreationFcn: @gacreationuniform
CrossoverFcn: @crossoverscattered
SelectionFcn: @selectionstochunif
MutationFcn: @mutationadaptfeasible
Tabulation: -
Generation Func-count Best f(x) Mean f(x) Stall Generations
1 100 -0.2458 28.33 0
2 147 -0.2458 16.99 1
3 194 -0.2458 9.229 2
4 241 -0.2494 3.561 0
5 288 -0.2494 1.245 1
6 335 -0.2494 1.102 2
7 382 -0.2494 -0.211 3
……
50 2403 -0.25 -0.25 23
51 2450 -0.25 -0.25 24
52 2497 -0.25 -0.25 25
53 2544 -0.25 -0.25 26
54 2591 -0.25 -0.25 27
55 2638 -0.25 -0.25 28
56 2685 -0.25 -0.25 29
57 2732 -0.25 -0.25 30
58 2779 -0.25 -0.25 31
ga stopped because the average change in the fitness value is less than options.
FunctionTolerance.
Optimal x : -1.5
Function value at optimal x : -0.25
5.6. Maximizing a Simple Function: -
This example demonstrates how to use a genetic algorithm to find the maximum of a
simple function (f(x)=−x^2+4x).
MATLAB Code: -
% Define the function to maximize (note the negative sign for maximization)
simpleFcn = @(x) -(-x.^2 + 4*x);
% Set the bounds for the variable
lb = 0; % Lower bound
ub = 4; % Upper bound
% Run the genetic algorithm
options = optimoptions('ga', 'Display', 'iter');
[x, fval] = ga (simpleFcn, 1, [], [], [], [], lb, ub, [], options);
disp(['Optimal x:', num2str(x)]);
disp(['Function value at optimal x:', num2str(-fval)]);
Single objective optimization: -
1 Variable.
Options: -
CreationFcn: @gacreationuniform
CrossoverFcn: @crossoverscattered
SelectionFcn: @selectionstochunif
MutationFcn: @mutationadaptfeasible
Tabulation : -
Generation Func-count Best f(x) Mean f(x) Stall Generations
1 100 -4 -3.126 0
2 147 -4 -3.368 1
3 194 -4 -3.554 2
4 241 -4 -3.823 3
5 288 -4 -3.863 4
…..
45 2168 -4 -4 33
46 2215 -4 -4 34
47 2262 -4 -4 35
48 2309 -4 -4 36
49 2356 -4 -4 37
50 2403 -4 -4 38
ga stopped because the average change in the fitness value is less than options.
FunctionTolerance.
Optimal x : 2.0001
Function value at optimal x : 4
5.7.Minimizing a Rosenbrock Function: -
The Rosenbrock function is a common test problem for optimization algorithms. Here's
how you can minimize it using a genetic algorithm.
MATLAB Code : -
% Define the Rosenbrock function
rosenbrockFcn = @(x) 100*(x (2) – x (1) ^2) ^2 + (1 – x (1)) ^2;
% Set the bounds for the variables
lb = [-5, -5]; % Lower bounds
ub = [5, 5]; % Upper bounds
% Run the genetic algorithm
options = optimoptions('ga', 'Display', 'iter');
[x, fval] = ga (rosenbrockFcn, 2, [], [], [], [], lb, ub, [], options);
disp(['Optimal x:', num2str(x)]);
disp(['Function value at optimal x: ', num2str(fval)]);
Single objective optimization: -
2 Variables.
Options: -
CreationFcn: @gacreationuniform
CrossoverFcn: @crossoverscattered
SelectionFcn: @selectionstochunif
MutationFcn: @mutationadaptfeasible
Tabulation : -
Generation Func-count Best f(x) Mean f(x) Stall Generations
1 100 -4 -3.126 0
2 147 -4 -3.368 1
3 194 -4 -3.554 2
4 241 -4 -3.823 3
5 288 -4 -3.863 4
…..
195 9218 0.008677 0.0129 0
196 9265 0.008677 0.01238 1
197 9312 0.008677 0.01123 2
198 9359 0.008677 0.0102 3
199 9406 0.008626 0.009765 0
200 9453 0.00859 0.009577 0
ga stopped because it exceeded options.
MaxGenerations.
Optimal x : 0.96429 0.92962
Function value at optimal x : 00.0012807
5.8. Minimizing a Sinusoidal Function: -
This example demonstrates how to use a genetic algorithm to find the minimum of a
sinusoidal function (f(x)=sin(x)+0.1x).
MATLAB Code: -
% Define the sinusoidal function
sinusoidalFcn = @(x) sin(x) + 0.1*x;
% Set the bounds for the variable
lb = 0; % Lower bound
ub = 10; % Upper bound
% Run the genetic algorithm
options = optimoptions('ga', 'Display', 'iter');
[x, fval] = ga(sinusoidalFcn, 1, [], [], [], [], lb, ub, [], options);
disp(['Optimal x:', num2str(x)]);
disp(['Function value at optimal x:', num2str(fval)]);
Single objective optimization: -
1 Variable.
Options: -
CreationFcn: @gacreationuniform6
CrossoverFcn: @crossoverscattered
SelectionFcn: @selectionstochunif
MutationFcn: @mutationadaptfeasible
Tabulation : -
Generation Func-count Best f(x) Mean f(x) Stall Generations
1 100 -0.5155 0.5503 0
2 147 -0.53 0.2616 0
3 194 -0.53 0.03286 1
4 241 -0.53 -0.2177 2
5 288 -0.53 -0.3221 3
….
55 2638 -0.5338 -0.5338 23
56 2685 -0.5338 -0.5338 24
57 2732 -0.5338 -0.5338 25
58 2779 -0.5338 -0.5338 26
59 2826 -0.5338 -0.5338 27
60 2873 -0.5338 -0.5338 28
ga stopped because the average change in the fitness value is less than options.
FunctionTolerance.
Optimal x : 4.6122
Function value at optimal x : -0.53377
Conclusion :-
In this study, we successfully demonstrated the application of Genetic Algorithm (GA) in
solving a single-objective optimization problem using MATLAB. The GA, inspired by the
process of natural selection, proved to be an effective and robust optimization tool
capable of handling complex and non-linear problem spaces. By leveraging MATLAB's
built-in GA functions, we were able to define objective functions, apply constraints, and
efficiently explore the solution space to find near-optimal solutions. The results indicate
that GA can achieve satisfactory performance in scenarios where traditional gradient-
based methods may struggle, particularly with non-differentiable, multi-modal, or
discontinuous functions. Overall, Genetic Algorithm offers a flexible and powerful
approach for single-objective optimization, and MATLAB provides a convenient and
efficient environment for its implementation and analysis.