Challenge

Week 4.1 - Genetic Algorithm

the code for optimizing and finding global maxima is given bellow with help of genetic algorithm

genetic algorthim is a sorting and searching technique based on theory of natural evalution by charles darwin in which the larger number is selected and replaced to smaller or in other words large numbers or strongest ones survive and get selected by manuplating this one can find optimized point in large variety of data such as in population one can find maximum and minimum points

in each study case the code searches and sorts the data and gives it boundary limits as a result in third case one get optimal condition

clear all 
close all
clc
%inputs
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_spaces = 50;

%creating a 2d array
[xx,yy]=meshgrid(x,y);

%getting values for study case 1 from function by sending array
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
surfc(x,y,-f) %surfc command is used to plot surface with contor
%study case 1
%tic toc commmand gives time taken to complete 1 study
tic
for i=1:num_spaces
    [input,fopt(i)]=ga(@stalagmite,2);
    xopt(i)=input(1);
    yopt(i)=input(2);
end

studytime1=toc
figure(1)
subplot(2,1,1)
surfc(x,y,-f)
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maximum')

%study case 2 limiting the boundary
tic
for i=1:num_spaces
    [input2,fopt2(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
    xopt2(i)=input2(1);
    yopt2(i)=input2(2);
end

studytime2=toc
figure(2)
subplot(2,1,1)
surfc(x,y,-f)
shading interp
hold on
plot3(xopt2,yopt2,-fopt2,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt2)
xlabel('iterations')
ylabel('function maximum')

%study case 3 incresing the band with of population by option command which
%increses the propability of getting exactness
tic
options=optimoptions('ga');
options  =optimoptions(options,'populationsize',500);
for i=1:num_spaces
    [input3,fopt3(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
    xopt3(i)=input3(1);
    yopt3(i)=input3(2);
end

studytime3=toc
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt3,yopt3,-fopt3,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt3)
xlabel('iterations')
ylabel('function maximum')

the xlabel and ylabel commands are used to give name to x and y axis

the function code stalagmite should have sin as the formula for optimizations has degrees in radians and all the calculations are done in this code

function g=stalagmite(input_vector)

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

t1=(sin(pi*5.1*x+0.5))^6;
t2=(sin(pi*5.1*y+0.5))^6;
t3=exp((-4*log(2))*(x-0.0667)^2/0.64);
t4=exp((-4*log(2))*(y-0.0667)^2/0.64);
g=-(t1*t2*t3*t4);
end

links to view the screen shot as i was unable to upload them because box to choose file is not opening

https://photos.app.goo.gl/UgkGazJB212G6HpH6

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.