Challenge

Week 4.1 - Genetic Algorithm

 
% Optimise the stalagmite function and find the Global maxima of the
% function.

clear all
close all
clc

tic %  this command start the timer.

% one dimensional arrays
x = linspace(0, 0.6, 200);
y = linspace(0 ,0.6 ,200);

num_cases = 50; %from this condition we can obtain 50 values. 
[xx yy] =meshgrid(x, y) % meshgrid command use for make 2D arrays.

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 
% Ganetic Algorithm for Maximum value

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

study1_time = toc % this command read total time from start to end.

% from this study we will find the global maxima .

figure(1)
 subplot(2,1,1)
 hold on
 surf(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('Interations')
 ylabel('Function maximum')
 % from this we found that there is too much output but we want single
 % output so for that we should applied limits .

tic
% Study 2 - Statical Behavior -with upper and lower bounds

%in 1st study we found that there complicated outputs so that  not gonna
%help.so thats why we extended our ga syntax upto lower and upper bounds.

for i =1:num_cases
[inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6])
 xopt(i) = inputs(1);
 yopt(i) = inputs(2);
end

study2_time = toc

figure(2)

subplot(2,1,1) % this plot used for tiled lines which are in 3D plots.
hold on
surf(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','MarkerSize',5,'MarkerFaceColor','r') % for the 3D plots.
title('Upper and Lower bounds')
subplot(2,1,2)
plot(fopt)
xlabel('Interations')
ylabel('Function Maximum')

% from this study we found that the upper and lower bounds are help to reduce the output. but 
% still we have to reduce because there should be only one global maxima
% point.

tic
% Study 3 - Statical Behavior - Use optimoptions -increase GA interation 
% so after 2nd study i understand that i need to change ga syntax and add
% upto options command. in this we can increase population size .
% after that ga will take more time but it will give us proper output.

options = optimoptions('ga')
options = optimoptions(options,'PopulationSize',250)

for i = 1:num_cases
    [inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options)
    xopt(i) = inputs(1)
    yopt(i) = inputs(2)
end
 study3_time = toc 

 figure(3)
 subplot(2,1,1)
 surf(x,y,f)
 hold on
 shading interp
 plot3(xopt,yopt,fopt,'marker','o','MarkerSize',5,'MarkerFaceColor','r')
 title('Increase GA interation')
 xlabel('Interation')
 ylabel('Function Maximum')

% form this study3 we found our global mAxima.





% THIS FFUNCTION FOR GA MAXIMUM VALUE


function [f] = stalagmite(input_vector)

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

f1 = [sin(5.1*pi*x + 0.5)]^6;
f2 = [sin(5.1*pi*y + 0.5)]^6;
f3 = exp(-4*log(2)*(x-0.0667)^2/0.64);
f4 = exp(-4*log(2)*(y-0.0667)^2/0.64);

f = -(f1*f2*f3*f4);
end

% this is the subprogram for function stalagmite.
%the inbuild ga is programed for find the minimum value .
%to find maximun value we should change the sign of the fuction 
% to positive(+) to Negative(-). then we can find out Global maxima.




%Genetic Algorithm -

% Genetic Algorithm is concept in which we can find the solution of complex problem.There are random values of population

% and in there are so many problem and aslo there are solution. but gentic algorithm mainly focus on the 'Optimisation'.in

% given condition lots of solutions are available. but need to find out best solution .so for basic problems we can use normal % algorithm but for complex problems genetic algorithm is must.

%Syntax -

%so during this challenge i went through three 'ga' syntax. first one is actually contain function and nervs. in that we can say %that the functions and the variables (x & y) .so after evaluate i saw complicated output.and that not gonna help me . so for %that decrease the output/maxima points. we should apply limits on ga syntax after using limits i got result and output points %decreases as compared to 1st syntax.but we need to find out global maxima point and it must one single points so genetic %algorithm help to find best solution via optimisation.so after increase populations genetic algorithm fiound global maxima.

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.