Challenge

Week 4.1 - Genetic Algorithm

https://drive.google.com/drive/folders/1Qq_hN2CZ3PboR5tJFe6Kc_UXMkY8-Euj?usp=sharing

AIM:

To write code in MATLAB to optimize the stalagmite function and find the global maxima of the function.

INTRODUCTION:

In a genetic algorithm, a population of the solution to an optimization problem is evolved toward better solutions. Each solution has a set of properties that can be mutated and altered, traditionally, solutions are represented in binary as strings of 0s and 1s, but other encodings are also possible. In some genetic algorithms, not all possible chromosomes represent valid solutions. In some cases, it is
possible to use specialized crossover and mutation operators that are designed to avoid violating the constraints of the problem.

FUNCTION

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

The -ve is used to calculat the maximum of the function F

clear all
close all
clc

% defining our search space
x = linspace (0,0.6,150);
y = linspace (0,0.6,150);

% creating a 2-Dimentional mesh

[xx,yy] = meshgrid(x,y);

% Evaluation the stalagmite function

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 (12)
surface (xx,yy,-f)
xlabel('x value')
ylabel('y value')

shading interp
[inputs fval] = ga(@stalagmite,2);
num_case = 50;
tic
for i= 1;num_case
[input,fopt(i)] = ga(@stalagmite,2);
xopt(i) = input(1)
yopt(i) = input(2)
end

study_time_1 = toc
figure(1)
subplot(2,1,1);
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title ('unbounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('funtion minimum')

% study 2 - statistical behaviours with upper and lower bounds
for i = 1:num_case
[input,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt(i) =inputs(1)
yopt(i) =inputs(2)
end

study_time_2 = toc
figure (2)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded input')
subplot(2,1,2)
plot(fopt)
xlabel('iteraions')
ylabel('funtion minimum')

% study 3 increasing the ga interations

tic
options = optimoptions('ga');
options = optimoptions(options,'populationsize',270);
for i = 1:num_case
[input,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i) = inputs(1)
yopt(i) = inputs(2)
end

study3_time = toc
figure(3)
subplot(2,1,1)
hold on
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title ('bounded input')
subplot(2,1,2)
plot (fopt)
xlabel('iteration')
ylabel('funtion minimum')

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.

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.