GENETIC ALGORITHM

 GENETIC ALGORITHM:

Basically,the genetic algorithm is inspired by the natural process that belongs to larger scale of evolutionary algorithm.

It used to generate a high quality solution for optimation and search type problem.

It relying on biologically operation like mutation,cross over and selection.

Generates a population of points at each iteration. The best point in the population approaches an optimal solution.

EXAMPLE;

Automatically solve suduko puzzle,

hyperparameter optimization....etc...

SYNTEX:

 call the genetic algorithm function ga with the syntax

 [x fval] = ga(@fitnessfun, nvars, options) 

where ,

@fitnessfun is a handle to the fitness function. 

nvars is the number of independent variables for the fitness function. 

options is a structure containing options for the genetic algorithm.

 

WRITE A FUNCTION CODE:

for genetic algorithm,we wrote a function program by this formula as given below,

f1x=(sin(5.1*pi*x+0.5))^6;
f2y=(sin(5.1*pi*y+0.5))^6;
f3x=exp(-4*log(2)*(x-(0.0667))^2/0.64);
f4y=exp(-4*log(2)*(y-(0.0667))^2/0.64);

then,we implemented this as it is for calling a function....

function [f]=stalagmite(inputvector)

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

f1x=(sin(5.1*pi*x+0.5))^6;
f2y=(sin(5.1*pi*y+0.5))^6;
f3x=exp(-4*log(2)*(x-(0.0667))^2/0.64);
f4y=exp(-4*log(2)*(y-(0.0667))^2/0.64);

f= -(f1x*f2y*f3x*f4y);
end

 

after that we write a main program...

MAIN PROGRAM..

 

clear all
close all 
clc
%INPUTS
lower_bound=input('enter the value:');
upper_bound=input('enter the value:');;
%assign to variable...
x=linspace(lower_bound,upper_bound,150);
y=linspace(lower_bound,upper_bound,150);
%convert 2D into 3D
[xx,yy]=meshgrid(x,y);
count=50;
%study the timing ,we use tic and toc
tic
for i=1:length(xx)
    for j=1:length(yy)
        inputvector(1)=xx(i,j);
         inputvector(2)=yy(i,j);
         f(i,j)=stalagmite(inputvector);
    end

end
study_time=toc



%studty1 unbounded function

tic

for i=1:count
    [input,fopt(i)]=ga(@stalagmite,2);
        xopt(i)=input(1);
         yopt(i)=input(2);
end
study_time1= toc
figure(1)

subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
xlabel('xlabel')
ylabel('ylabel')
title('unbounded inputs')
legend("xlabel","ylabel")

plot3(xopt,yopt,-fopt,'Marker','o','MarkerSize',5,'MarkerFaceColor','m')
subplot(2,1,2)
plot(-fopt)
xlabel('interation')
ylabel('func max')
%study2 bounded function

tic

for i=1:count
    [input,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[lower_bound;lower_bound],[upper_bound;upper_bound]);

        xopt(i)=input(1);
         yopt(i)=input(2);
end
study_time2= toc
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
xlabel('xlabel')
ylabel('ylabel')
title('bounded inputs')
legend("xlabel","ylabel")

plot3(xopt,yopt,-fopt)
subplot(2,1,2)
plot(-fopt)
xlabel('iteration')
ylabel('func max')


%study3
%population with bounded func.
options=optimoptions(@ga)
option=optimoptions(options,'PopulationSize',170);
tic

for i=1:count
    [input,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[lower_bound;lower_bound],[upper_bound;upper_bound],[],[],options);

       xopt(i)=input(1);
         yopt(i)=input(2);
end
study_time3= toc
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
xlabel('xlabel')
ylabel('ylabel')
title('bounded inputs with population size')
legend("xlabel","ylabel")

plot3(xopt,yopt,-fopt,'Marker','o','MarkerSize',5,'MarkerFaceColor','m')
subplot(2,1,2)
plot(-fopt)
xlabel('iteration')
ylabel('func max')

global_max=-fopt

 

outputs:

ERRORS:

By using warning and this program supporter,i just correct all my mistakes...

REFFERENCE:

https://www.sciencedirect.com/topics/engineering/genetic-algorithm

https://en.wikipedia.org/wiki/Genetic_algorithm

 

 

 

 


Comments

Popular posts from this blog

simple pendulum using matlab

DRAG FORCE

2r robotic arm