Project

Genetic Algorithm

Genetic algorithm is a method used to solve constrained or unconstrained optimization problems based on natural selection,the process is inspired by Charls darwins theory of evolution.It selects individuals at random from the current population as parents and use them to produce children for next generation.Elite children are the individuals with the best fitness values they survive to the next generation.Mutation is done by making random changes to a single parent and Crossover is by combining the vector entries of a pair of parents.After many generations the population evolves toward an optimal solution.

How the genetic algorithm works:
The algorithm creates a random initial population.Fitness value of(function minimum value)each member of the population is calculated and finds more usable range of values.Individuals in the current population that have lower fitness (best fitness value means minimum value) are selected as parents.The algorithm uses the individuals of current generation to create the next population.
The algorithm stops when one of the stopping criteria is met.Optimal solution is found.ie best fitness value for a function is found.

Code to optimize the Stalagmite function

clear all
close all
clc

x=linspace(0,0.6,150); 
y=linspace(0,0.6,150); 
num_cases=100
%creating 2-dimensional workspace
[xx yy]=meshgrid(x,y);

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
tic
%study 1 statistical manner 
%fitness function takes some random numbers 
for i=1:num_cases
    [inputs,fopt(i)]=ga(@stalagmite,2);
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end
study1_time=toc;

figure(1)
subplot(2,1,1)
hold on
surfc(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(\'iterations\')
ylabel(\'function minimum\')

%study 2 statistical behaviour - with upper and lower bounds
tic
for i=1:num_cases
    [inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end
study2_time=toc;
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\')
title(\'Bounded inputs\')
subplot(2,1,2)
plot(fopt)
    xlabel(\'iterations\')
    ylabel(\'function minimum\')

%study3  increasing ga iterations above 100
%more generations are evaluated and optimal solution is obtained
options = gaoptimset(\'ga\');
options = gaoptimset(options,\'PopulationSize\',150);
tic
for i=1:num_cases
    [inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end
study_time=toc;

figure(3)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\')
title(\'Bounded inputs\')
subplot(2,1,2)
plot(fopt)
xlabel(\'iterations\')
ylabel(\'function minimum\')

sub program used

function [f]=stalagmite(input_vector)
f1=(sin(5.1*pi*input_vector(1)+0.5))^6;
f2=(sin(5.1*pi*input_vector(2)+0.5))^6;
f3=exp(-4*log(2)*(input_vector(1)-0.0667)^2/0.64);
f4=exp(-4*log(2)*(input_vector(2)-0.0667)^2/0.64);
F=f1*f2*f3*f4;
f=1/(1+F); %or f=-F;
end

code gives three output figures coressponding to the 3 studies,third study gives the same output, if run for several times iterations [figure 3] .

screenshot of the output function value

Thus calculated the global maxima of a stalagmite function ,value is [0.5001].

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.