Challenge

Week 4.1 - Genetic Algorithm

clear all
close all
clc

%To give elements of values to the variable a & b using linspace comman
a = linspace(0,0.5,150)
b = linspace(0,0.5,150)

%To get matrix of 150 X 150 by using meshgrid command
[aa bb]= meshgrid(a, b)

%To get value to input_vector(x) from the obtained matrix
so total of 150 values assinged to each input_vector 1&2 by using for loop.
for i=1:length(a)
  for  j=1:length(b)
    input_vector(1) = aa(i,j);
    input_vector(2) = bb(i,j);

% To get the x & y value by using the stalagmite funciton
    f(i,j)= stalagmite(input_vector);
    end
end

figure(1)
%Subplot used to get multiple plot in same window
subplot(2,1,1)

%Surf is used to generate surface from the values of elements
surf(a,b,f)

%Shading interp: To switch off the default grid lines on the generated surface
shading interp

% Declaration of total number of time the GA should run
no_of_cases= 5;



%%
%%Study1-Statistical Behaviour
tic
for k=1:no_of_cases

%The ga "genitic algortith" is used on the stalagmite function to simplfy the solution to find the global maximum.
  [inputs, fval(k)]= ga(@stalagmite,2)
   X(k)=inputs(1);
   Y(k)=inputs(2);
    
end
time1=toc;
title('Case1:Unbounded')
hold on
plot3( X,Y,fval,'-o','MarkerSize',5,'markerfacecolor','r','markeredgecolor','r')
xlabel('x-valuse')
ylabel('y-values')
subplot(2,1,2)
plot(fval)
xlabel('No of Iteration')
ylabel('Maxi-Function value')
Study_time=sprintf('Time taken for third Study is %fs% ', time1)
annotation('textbox','string',Study_time,'fitboxtotext','on')
%%

%Study2-With bounded condition
tic
for k=1:no_of_cases
    [inputs, fval(k)]= ga(@stalagmite,2,[],[],[],[],[0:0],[0.5:0.5])
    X(k)=inputs(1);
    Y(k)=inputs(2);
    end
time1=toc;
title('Case2:Bounded with upper and lower limits')
hold on
 plot3( X,Y,fval,'-o','MarkerSize',5,'markerfacecolor','r','markeredgecolor','r')
xlabel('x-value')
ylabel('y-value')
subplot(2,1,2)
plot(fval)
xlabel('No of Iteration')
ylabel('Maxi-Function value')
Study_time=sprintf('Time taken for third Study is %fs% ', time1)
annotation('textbox','string',Study_time,'fitboxtotext','on')


%%
%Study3-With bounder condition

options=optimoptions('ga')
options=optimoptions(options,'populationsize',300)
tic
for k=1:no_of_cases
    [inputs, fval(k)]= ga(@stalagmite,2,[],[],[],[],[0:0],[0.5:0.5],[],[],options)
    X(k)=inputs(1);
    Y(k)=inputs(2);
    
end
time1=toc;
title('Case3:Bounded with defined population size')
hold on
 plot3( X,Y,fval,'-o','MarkerSize',5,'markerfacecolor','r','markeredgecolor','r')

xlabel('x-value')
ylabel('y-value')
subplot(2,1,2)

plot(fval)
xlabel('No of Iteration')
ylabel('Maxi-Function value')
Study_time=sprintf('Time taken for third Study is %fs% ', time1)
annotation('textbox','string',Study_time,'fitboxtotext','on')


%%stalagmit function
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


What is Genetic Algorithm?

It is an optimization technique obtained from Darwin's theory of evolution.

this alogoritm solves the function to find good solution and the solution obtained will get better over the perioud of time.




Syntax for GA function

x=ga(fucntion,Nvar)

This GA can give better results by constraining with suitable parameters that definition the function.









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.