Genetic Algorithm
GENETIC ALGORITHM
INTRODUCTION :-
A genetic algorithm is a search heuristic that is inspired by charles darwin\'s theory of natural evolution. this algorithm reflects the process if the natural selection where the fittest individuals are selected for reproduction in order to produce offsprings of the next generation.
there are 5 phases considered in genetic algorithm.
1. initial population
2. fitness function
3. selection
4. crossover
5. mutation
Agenetic (GA) is a method of solving both constrained and unconstrained optimization problem based on natural selection process that mimics biological evolution.(GA) comes under the category of evolunationary algorithms with working principle based on the mathematics of the natural genetics.
Procedure :-
- Initialize population.
- Selection according to population.
- Crossovers between the selected chromosomes.
- Perform the mutation.
- Repeat the cycle till the condition of the stop if true.
MATLAB CODE
clear all
close all
clc
%defining our space
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
%creating a 2 dimensional mesh
[xx,yy]=meshgrid(x,y);
%evaluating the stalagmite function
for i=1:length(xx)%incrementing i to next level until it stops
for j=1:length(yy)%incrementing j to next value until it stops
input_vector(1)=xx(i,j);
input_vector(2)=yy(i,j);
f(i,j)=stalagmite_func(input_vector);
end
end
figure (1)
surfc(xx,yy,-f)
Shading interp
xlabel(\'x value\')
ylabel(\'y value\')
title(\'stalagmite function\')
%study 1
tic
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study_1=toc
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-f)
tittle(\'unbounded inputs\')
xlabel(\'x value\')
ylabel(\'y value\')
shading interp
hold on
plot3(xopt,yopt,-fopt,\'marker\',\'0\',\'markersize\',5,\'markerfacecolor\',\'r\')
subplot(2,1,2)
plot(-fopt)
xlabel(\'iterations\')
ylabel(\'function maximum\')
% case 2nd with upper and lower bounds
tic;
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
study_2_time=toc
figure(3)
subplot(2,1,1)
surfc(x,y,-f)
shading interp
tittle(\'bounded inputs\')
x label(\'x value\')
y label(\'y value\')
hold on
plot3(xopt,yopt,-fopt,\'marker\',\'0\',\'markersize\',5,\'markerfacecolor\',\'r\')
subplot(2,1,2)
plot(-fopt)
xlabel(\'iterations\')
ylabel(\'function maximum\')
%study 3 incresing the population size
%by doing this we are increasing the probablity of function
%to converge at global maximum
options=optioptions(@ga)
options=optioptions(@ga,\'populationsize\',500) %increasing the population size
tic
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
study_3_time=toc %gives the time taken for study
figure(4)
subplot(2,1,1)
surfc(x,y,-f) %creates a surface to visualise 3D
shading interp
tittle(\'bounded inputs with increased population size\')
xlabel(\'x value\')
ylabel(\'y value\')
hold on
plot3(xopt,yopt,-fopt,\'marker\',\'0\',\'markersize\',4,\'markerfacecolor\',\'r\')
subplot(2,1,2)
plot(-fopt) %plotting -f vs number of iterations
xlabel(\'iterations\')
ylabel(\'function maximum\')
RESULTS
projects.skill-lync.com/projects/manage/?id=xhKgPnFgthRiZK0B