Challenge

Week 4.1 - Genetic Algorithm

clear all
close all
clc
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_case = 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);
%input_vector = [input_vector(1),input_vector(2)];
GA(i,j) = stalagmite(input_vector);
end
end
tic
for i =1:num_case
[input, GA_opt(i)] = ga(@stalagmite,2);
xopt(i) = input(1);
yopt(i) = input(2);
end
study_1_time = toc
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,-GA)
shading interp
plot3(xopt,yopt,-GA_opt,'MarkerSize',5,'marker','o','MarkerFaceColor','r')
title('unbounded inputs')
subplot(2,1,2)
plot(-GA_opt)
xlabel('Iterations')
ylabel('function minimum')
tic
for i =1:num_case
[input, GA_opt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt(i) = input(1);
yopt(i) = input(2);
end
study_2_time = toc
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-GA)
shading interp
plot3(xopt,yopt,-GA_opt,'MarkerSize',5,'marker','o','MarkerFaceColor','r')
title('bounded inputs')
subplot(2,1,2)
plot(-GA_opt)
xlabel('Iterations')
ylabel('function minimum')
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',240)
tic
for i =1:num_case
[input, GA_opt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i) = input(1);
yopt(i) = input(2);
end
study_3_time = toc
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,-GA)
shading interp
plot3(xopt,yopt,-GA_opt,'MarkerSize',5,'marker','o','MarkerFaceColor','r')
title('bounded inputs')
subplot(2,1,2)
plot(-GA_opt)
xlabel('Iterations')
ylabel('function minimum')
function [GA] = stalagmite(input_vector)

x = input_vector(1);
y = input_vector(2);
%f(x,y)=f1,x*f2,x*f1,y*f2,y


f1x = [sin((5.1*pi*x)+0.5)].^6;

f1y = [sin(5.1*pi*y+0.5)].^6;

f2x = exp(-4*log(2)*((x-0.0667)^2/0.64));

f2y = exp(-4*log(2)*((y-0.0667)^2/0.64));

GA = -(f1x*f1y*f2x*f2y);

end

- creating 1D arrays.
- based on 1D creating 2D array.
- use mesh grid command
- for each value of x and y creat input vector in for loop.
- using stalagmite function and equations in other folder
-again given for loop with tic toc time.
-it is study case 1. figure 1 with subplot and plot,title with labels.
- same followed for second case study but we use ga here.
-it is study case 2. figure 2 with subplot and plot,title with labels.
- in study case 3 options will be given to find maximum point.
- using optimoptions and giving population size.
- now here will give boundary options.
- it is study case 3. figure 3 with subplot and plot,title with labels.

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.