Week 5 - Genetic Algorithm
Genetic Algorithm:
Genetic Algorithm (GA) is a heuristic optimization technique inspired by the process of natural selection and evolution. It is used to solve complex problems by mimicking the principles of survival of the fittest and genetic variation that occur in biological evolution. The main idea behind the GA is to iteratively evolve a population of potential solutions to the problem and let the best individuals survive and reproduce to produce better solutions over successive generations.
Here's a step-by-step explanation of how a Genetic Algorithm works:
-
Initialization: The GA starts by creating an initial population of potential solutions to the problem. Each potential solution is represented as an individual, often encoded in a chromosome or string of genes.
-
Evaluation: Each individual's fitness is evaluated based on how well it solves the problem. The fitness function quantifies the quality or performance of the solution. Higher fitness values indicate better solutions.
-
Selection: Individuals from the current population are selected to act as parents for the next generation. The probability of selection is proportional to an individual's fitness; better-fit individuals have a higher chance of being selected.
-
Crossover: Selected parents undergo crossover (also known as recombination). During crossover, genetic information from the parents is exchanged, creating new offspring solutions that inherit traits from both parents.
-
Mutation: To introduce genetic diversity and exploration into the population, some individuals undergo random changes (mutations). Mutations modify one or more genes in an individual's chromosome.
-
Replacement: The offspring and some individuals from the previous generation form the next population. The replacement process selects individuals based on their fitness, favoring the better-performing individuals.
-
Termination: The algorithm proceeds through several generations. Termination criteria can be based on reaching a maximum number of generations, finding a satisfactory solution, or when the algorithm's progress stagnates.
The Genetic Algorithm repeatedly applies these steps, allowing the population to evolve toward better solutions. By combining selection, crossover, and mutation, the GA efficiently explores the solution space and can often find good solutions in complex optimization problems.
The syntax for ga in MATLAB:
The ga function is used in MATLAB to run the Genetic Algorithm solver. Here's the basic syntax for using ga:
[x, fval] = ga(FitnessFunction, nvars, A, b, Aeq, beq, lb, ub, ConstraintFunction, options);
-
Fitness Function: This is the objective function to be minimized or maximized. It takes a vector of variables as input and returns a single scalar value representing the fitness or objective value. -
nvars: The number of variables in the problem. -
A,b,Aeq,beq: Matrices and vectors representing linear inequality and equality constraints. -
lb,ub: Vectors representing the lower and upper bounds for each variable. -
ConstraintFunction: (Optional) A function that provides nonlinear constraints in the form ofc(x) <= 0andceq(x) = 0. -
options: (Optional) An options structure created usingoptimoptionsto specify various settings like population size, number of generations, display options, etc. -
The optimoptions function is used to create an options structure that can be passed as an argument to various optimization solvers in MATLAB, including ga
The ga function returns the optimal solution vector x and its corresponding objective value fval. The GA searches for either the maximum or minimum value, depending on the nature of the problem.
clear
close all
clc
% Define the stalagmite function
stalagmite_function = @(x) stalagmite([x(1); x(2)]);
% Input value in x and y
x_vals = linspace(0, 0.6, 150);
y_vals = linspace(0, 0.6, 150);
% number of iteration value
n = 50;
% Forming 2D array
[xx, yy] = meshgrid(x_vals, y_vals);
k = zeros(length(x_vals), length(y_vals));
% for loop for both x and y values and K to calculate the stalagmite function
% values
for i = 1:length(x_vals)
for j = 1:length(y_vals)
input_vector = [xx(i, j); yy(i, j)];
k(i, j) = stalagmite_function(input_vector);
end
end
% Study 1 - Unbounded optimization
tic % this is to start the clock for the study
% for loop for the ist unbounded study of the stalgamite
%The optimoptions function is used to create an options
% structure that can be passed as an argument to various optimization solvers in MATLAB, including ga
for i = 1:n
options = optimoptions('ga', 'Display', 'off'); % The option 'Display', 'off' means that the GA solver will not display any output during its execution
[results, fopt(i)] = ga(stalagmite_function, 2, [], [], [], [], [], [], [], options); % all of the syntax related information is explained in the report.
xopt(i) = results(1); %x-values after unbound optimization
yopt(i) = results(2); %y-values after unbound optimization
end
study_time1 = toc; % end of the time for the study 1-unbound optimization
figure(1) % creating the 1st figure
subplot(2, 1, 1) % creating 1st subplot to show the animation
surf(x_vals, y_vals, -k) % surf to represent the graph with x, y and k
shading interp % to eliminate the mesh lines on the plot
hold on % hold on command wait for the next process
plot3(xopt, yopt, -fopt, 'marker', 'o') % plot3 command to represent the way the unbound optimization works
subplot(2, 1, 2) % creating 2nd subplot to show the graph
plot(-fopt) % plot the values
xlabel('iteration') % x label
ylabel('maximum value') % y label
% Study 2 - Bounded optimization
tic % this is to start the clock for the study
% for loop for the ist unbounded study of the stalgamite
%The optimoptions function is used to create an options
% structure that can be passed as an argument to various optimization solvers in MATLAB, including ga
for i = 1:n
options = optimoptions('ga', 'Display', 'off');
lb = [0; 0]; %setting up the lower bound
ub = [0.6; 0.6]; %setting up the upper bound
[results, fopt(i)] = ga(stalagmite_function, 2, [], [], [], [], lb, ub, [], options);% all of the syntax related information is explained in the report.
xopt(i) = results(1); %x-values after bound optimization
yopt(i) = results(2); %y-values after bound optimization
end
study_time2 = toc; % end of the time for the study 2-bound optimization
figure(2) % creating the 2nd figure
subplot(2, 1, 1) % creating 1st subplot to show the animation
surf(x_vals, y_vals, -k) % surf to represent the graph with x, y and k
shading interp % To eliminate the mesh lines on the plot
hold on % hold on command wait for the next process
plot3(xopt, yopt, -fopt, 'marker', 'o') % plot3 command to represent the way the bound optimization works
subplot(2, 1, 2) % creating 2nd subplot to show the graph
plot(-fopt) % plot the values
xlabel('iteration') % X LABEL
ylabel('maximum value') % y label
% Study 3 - Increased initial population
tic % this is to start the clock for the study
% for loop for the ist unbounded study of the stalgamite
%The optimoptions function is used to create an options
% structure that can be passed as an argument to various optimization
% solvers in MATLAB, including ga with increased population size
for i = 1:n
options = optimoptions('ga', 'Display', 'off', 'PopulationSize', 300);
[results, fopt(i)] = ga(stalagmite_function, 2, [], [], [], [], lb, ub, [], options);
xopt(i) = results(1); %x-values after bound optimization with increased population size
yopt(i) = results(2); %y-values after bound optimization with increased population size
end
study_time3 = toc; %% end of the time for the study 3-bound optimization with increased population size
figure(3) % creating the 3rd figure
subplot(2, 1, 1) % creating 1st subplot to show the animation
surf(x_vals, y_vals, -k) % surf to represent the graph with x, y and k
shading interp % To eliminate the mesh lines on the plot
hold on % hold on command wait for the next process
plot3(xopt, yopt, -fopt, 'marker', 'o') % plot3 command to represent the way the bound optimization works
subplot(2, 1, 2) % creating 2nd subplot to show the graph
plot(-fopt) % plot the values
xlabel('iteration') % X LABEL
ylabel('maximum value') % y label
%Stalgamite code
function A = stalagmite(input_vector) % definition of stalgamite function
x=input_vector(1); % x-values from input
y=input_vector(2); % y-values from input
% Equations based on the x and y directions f1x and f1y and f2x and f2y
f1=(sin(5.1*pi*x+0.5)).^6;
f2=exp((-4*log(2)*((x-0.0667).^2))/0.64);
f3=(sin(5.1*pi*y+0.5)).^6;
f4=exp((-4*log(2))*((y-0.0667).^2)/0.64);
A =-(f1*f2*f3*f4);
end % end of fuction
Errors faced :
Err1:error in stalganmite_f in lines 3 and 28
resolution: changed the name of the function from the previous stalgamite_f to stalgamite_function
Err2: Invalid expression. when calling a function or indexing a variable, use parentheses. otherwise, check for mismatched delimiters.
resolution: added zeros in front of the expression the expression
Err3: error using optimoptions
resolution: display off instead of on, which explained in syntax
Err4:eror in ga
resolution: added one extra square bracket and semicolon
Explanation:
IN THE FIRST UNBOUND SIMULATION, WE DON'T HAVE ANY BOUNDS SETUP, SO THE SIMULATION WILL TAKE A NUMBER OF ITERATIONS TO SET DIFFERENT MAXIMUM VALUES .fOR THE NEXT BOUNDED SIMULATION WE HAVE THE LOWER BOUND AND UPPER BOUND BUT ONLY HAVE LESS NUMBER OF SAMPLE SIZE SO WE HAVE FEW MAXIMUM VALUES. AS WE INCREASE THE POPULATION SIZE FOR THE 3RD SIMULATION TO 300(TRIAL AAND ERROR),WE CAN SEE THE MAXIMUM VALUE IN THE SYSTEM.
REFERENCES;
1.https://www.mathworks.com/help/gads/ga.html
2.https://www.mathworks.com/discovery/genetic-algorithm.html
3.https://www.mathworks.com/help/gads/genetic-algorithm-options.html
4.https://www.mathworks.com/help/gads/options-and-outputs.html