Challenge

Week 4.1 - Genetic Algorithm

  • Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
  • Clearly expalin the concept of genetic algorithm in your own words and also explain the syntax for ga in MATLAB in your report.
  • Make sure that your code gives the same output, even if it is made to run several time

function stalagmite

function [f1] = stalagmite(input_vector)

x = input_vector(1);
y = input_vector(2);

f_1x = [sin(5.1*pi*x + 0.5)].^6;
f_1y = [sin(5.1*pi*y + 0.5)].^6;
f_2x = exp(-4*log(2)*((x-0.0667).^2)/0.64);
f_2y = exp(-4*log(2)*((y-0.0667).^2)/0.64);

f1= -(f_1x*f_1y*f_2x*f_2y);

mainprogram stalagmite matlab code

close all
clear 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

%study 1_satistical behavior
for i = 1:num_cases
[inputs,fopt(i)] = ga(@stalagmite,2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);

end
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5)
title('Unbounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Eanction minimum')
%study 2_statical behavior_with Upper and Lower bounds
for i = 1:num_cases
[inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0,0],[1,1]);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end

figure(2)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5)
title('Bounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Interions')
ylabel('Function minimum')
%study 3_increasing ga iterations
options = optimoptions('ga');
options=optimoptions(options,'PopulationSize',500);
for i = 1:num_cases
[inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0,0],[1,1],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end

figure(3)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5)
title('Bounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Interions')
ylabel('Function 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.