Week 4.1 - Genetic Algorithm
AIM :
to write the code to calculate the global maxima of the stalagmite function
GENETIC ALGORITHM (GA):
this is the method for solving both constrained and unconstrained optimisation problem that is based on natural selction the process that drives biological evolution
STEP USED IN THE GENETIC ALOGRITHM(GA)
- initialize population
- seletion according to fitness
- crossover between the selected chromosomes
- perform the mutation
THE STALAGMITE FUNCTION CODE :

function[f] = stalagmite(input_vector);
a = input_vector(1)
b = input_vector(2)
f1_x = [sin(5.1*pi*a+0.5)]^6
f1_y = [sin(5.1*pi*b+0.5)]^6
f2_x = exp(-4*log(2)*(((a-0.0667)^2/0.64)));
f2_y = exp(-4*log(2)*(((b-0.0667)^2/0.64)));
f = -(f1_x*f1_y*f2_x*f2_y)
end
main code
clear all
close all
clc
x= linspace(0,0.6,150);
Y = linspace(0,0.6,150);
num_cases = 50;
[xx,yy] = meshgrid(x,y);
for i =1:length(xx)
for j =1:length(yy)
input_vector(1) = xx(i,j);
input_vector(2) = yy(i,j);
f(i,j) = stalagmite(input_vector);
end
end
figure(1)
surfc(xx,yy,-f)
shading interp
xlabel('x value')
ylabel('y value')
title('stalagmite function')
%study 1
for i= 1 : num_cases
[input,fopt(i)] = ga(@stalagmite,2);
xopt(i) = input(1);
yopt(i) = input(2);
end
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-f)
title('unbounded_input')
xlabel('x value')
ylabel('y value')
shading interp
hold on
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('unbounded_input')
subplot(2,1,2)
plot(fopt)
x_label = ('intrations');
y_label = ('function minimum');
tic;
%case 2nd with upper and lower bounds
for i = num_cases
[input,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
xopt(i) = input(1);
yopt(i) = input(2);
end
study_2_time = toc;
figure(3)
subplot(2,1,1)
surfc(x,Y,-f)
shading interp
title('bounded inputs')
xlabel('x value')
ylabel('y value')
hold on
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt)
xlabel('interations')
ylabel('function maximum')
%study 3 icresing the population size
options = optimoptions(@ga);
options = optimoptions(@ga,'populationsize',500);
tic;
for i = 1 : num_cases
[input,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
xopt(i) = input(1);
yopt(i) = input(2);
end
study_3_time = toc;
figure(4)
subplot(2,1,1)
surfc(x,y,-f)
shading interp
title('bounded inputs with increased population size')
xlabel('x value')
ylabel('y value')
hold on
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt)
xlabel('interations')
ylabel('function maximum')
- clear all - remove data from the workspace and freeing up system memory
- close all -delete all the current figure or the specified memory
- clc - clear all the input and output variables
- the linspace set the range of data between the two number
- num_case is used number for interation in the algorithm
- the meshgrid command is used to create an array of the m*n martrix
- the for command is used for loop operation. the input vector stored the value of matrix xx and yy the output stored in the f martix after evaluted from the stalagamite operation surfc(xx,yy,-f)
- surfc(x,y,z) creates a three dimensional surface plot with a contour plot underneath a surface plot is a three dimesional surface that has solid face colors .the function plots the values in martix z as heights above a grid in the x - y plane defined by x and y the color of the surface varies according to the heights specified by z
- the label is used to name the axis in the figure shading interp
- the shading interp is used to hide the grid plot and smooth the [input fval] = ga(@stalagmite,2);
- tic toc command is used to calculate the time of running command


