Challenge

Week 4.1 - Genetic Algorithm


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

clear all
close all
clc
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 50
f = get_stalagmite(x,y);

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

study1 time = toc

figure(1)
subplot(2,1,1)
hold on
surfc(x, y, f)
shading interp
plot3(xopt,yopt,fopt,'Marker','0''markersize',5,'markerfacecolor''r')
title('unbounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function minimum')

tic
% study 2 - satistical 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

study2 time = toc

figure(2)
subplot(2,1,1)
hold on
surfc(x, y, f)
shading interp
plot3(xopt,yopt,fopt,'Marker','0''markersize',5,'markerfacecolor''r')
title('bounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function minimum')

tic
% study 3 - increasing GA iterations
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',150);

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

study3 time = toc

figure(3)
subplot(2,1,1)
hold on
surfc(x, y, f)
shading interp
plot3(xopt,yopt,fopt,'Marker','0''markersize',5,'markerfacecolor''r')
title('bounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function minimum')

explanation:-

  • clear all ,close all,clc reprasant its deletes all tempeavery memory , close all plots and clears the command window
  • we assume x and y values using the linspace command
  • the uses of linspace command are that set lower and upper limits num of values we need it
  • the mesh grade command is used in order to form an arrey and row and column valuse and xx , yy, variabels
  • and the studyb 1 is used to diffend as num_cases equal to 50
  • the tic command is used to order to find to timr taken of program
  • then subplot command is used to plote in the same figure
  • thus indicates we need to have better methad
  • study 2 is as the same of study and little changes is here
  • then study 3 is all the command used in the study are also the same 1 and 2 study
  • the population value is randamly incress up to single exact maximum value
  • by incressing single excate point at the population size is applyed
  • this are the explanation
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.