Challenge

Week 5 - Genetic Algorithm

% Program for solving Global Optimization function
close all;
clear;
clc;
%{
Optimization is used for improving design parameters in all engineering disciplines
The Genetic Algorithm shall be used for demonstration of the Optimization process
To use GA, we need a sample problem to solve - test function @stalagmite
GA is based on five steps
1. Initialization or initial population (Random solutions for a given problem)
2. Fitness function ( Function that text how the selected solution is close to the solution or
if we are minimizing the error, which solution has the minimum error where we assign a fitness score)
3. Selection - We select another possible set of solutions from step two sets
4. Crossover - Is the most significant phase,
5. Mutation
%}
% Defining the search space by creating a 1D array
x=linspace(0,0.6,150); %nvars that define the search space
y=linspace(0,0.6,150);
%creating a 2D array from the 1D array using the mesh command
[X,Y]=meshgrid(x,y);
%evaluating the stalgamite function
% Using forloop, an input vector to the stalagmite function is created
for ii=1:length(X)
for jj=1:length(Y)
ivec(1)=X(ii,jj); % First value of the row vector 'ivec'
ivec(2)=Y(ii,jj); % Second value of the row vector 'ivec'
f(ii,jj)=stalagmite(ivec); % The row vector 'input_vector' is passed as input to the stalagmite function
end
end
% Create a surface plot using the surf command
surfc(X,Y,f)
xlabel('x value')
ylabel('y value')
shading interp % This command removes the grid lines on the surface plot to make the plot smoother
%{
Use the ga in-built function in MATLAB to conduct the genetic algorithm
to minimize the stalagmite function
using the concept of function handle, the stalagmite function is passed
into the ga function in MATLAB
the
%}
[x,fval]=ga(@stalagmite,2); % Returns the values of the ga function at which it is minimal and the fitness function
% for ploting global minima and finding the minimum of the function ga is used
pop_size = 50; % Population size Positive integer 50 when number of design Variables <=5
tic
%{
Case 1 - Evaluating local unconstrained minimum and MaxGenerations (Maximum number of iterations)
for each member of the population size
Design variables is 2 since population size is 50
%}
for ii=1:pop_size
[x,fval(ii)]=ga(@stalagmite,2); % fval is value of stalagmite fitness at x
x1_min(ii)=x(1); % First unconstrained minimum of stalamite for each population member
x2_min(ii)=x(2); % Second unconstrained minimum of stalamite for each population member
end
Maxtime1 = toc;
% Graphics and Visualization for case 1
h(1) = figure(1)
subplot(2,1,1)
hold on
surfc(X,Y,f)
shading interp
plot3(x1_min,x2_min,fval,'marker','o','markersize',5,'markerfacecolor',[0 0 0.5])
title('Unconstrained minima')
subplot(2,1,2)
plot(fval)
xlabel('Iterations')
ylabel('function minimum')
tic
%{
Case 2 - x = ga(fun,nvars,A,b,Aeq,beq,lb,ub)Defines a set of lower and upper bounds on the
design variables, x, so that a solution
is found in the range lb ≤ x ≤ ub.
Set A=[] and b=[] if no linear inequalities exist
Set Aeq=[] and beq=[] if no linear equalities exist
lb = [0; -1]
ub = [0.1; 0.5]
%}
for ii=1:pop_size
[x,fval(ii)]=ga(@stalagmite,2,[],[],[],[],[0;-1],[0.1;0.5]); %
x1_min(ii)=x(1);
x2_min(ii)=x(2);
end
Maxtime2 = toc;
% Graphics and Visualization for case 2
h(2) = figure(2)
subplot(2,1,1)
hold on
surfc(X,Y,f)
shading interp
plot3(x1_min,x2_min,fval,'marker','o','markersize',7,'markerfacecolor',[0.8 0.4 0])
title('Constrained minima')
subplot(2,1,2)
plot(fval)
xlabel('Iterations')
ylabel('function minimum')
%{
Case 3 - Defining options for ga using optimoptions
options = optimoptions(@ga) uses default values for it fields such as 50
for population size
%}
%{
To create options with a field value that is different from the default
such as different population size of 100, use the below syntax
%}
options = optimoptions(@ga);
options = optimoptions(options,'populationsize',100);
tic
for ii=1:pop_size
[x,fval(ii)] = ga(@stalagmite,2,[],[],[],[],[0;-1],[0.1;0.5],[],[],options);
x1_min(ii)=x(1);
x2_min(ii)=x(2);
end
Maxtime3 = toc;
% Graphics and Visualization for case 3
h(3) = figure(3)
subplot(2,1,1)
hold on
surfc(X,Y,f)
shading interp
plot3(x1_min,x2_min,fval,'marker','o','markersize',7,'markerfacecolor',[0 1 0])
title('Minima Bounds')
subplot(2,1,2)
plot(fval)
xlabel('Iterations')
ylabel('function minimum')
savefig(h,'ga.fig')
% REFERENCES
% MATLAB Genetic Algorithm Documentation
% https://www.youtube.com/watch?v=tvau83TYOak
% https://www.youtube.com/watch?v=ICzcG0ORv6I&list=PLgH3sgdvgO4TQagjfi1FmPcdBjIlmk5s9&index=8
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.