Genetic Algorithm
PROJECT TITLE: TO OPTIMIZE THE STALAGMITE FUNCTION AND TO FIND OUT THE GLOBAL MAXIMA OF THE FUNCTION.
OBJECTIVE : 1.Understanding Genetic Algorithm.
2.To optimize the problem statement and find the maxima and minima at global scale.
THEORY:
Genetic Algorithms (GAs) are search algorithm based on the evolutionary ideas of natural selection and genetics. As such they represent an intelligent exploitation of a random search used to solve optimization problems.
The basic techniques of the GAs are designed to simulate processes in natural systems necessary for evolution, specially those follow the principles first laid down by Charles Darwin of "survival of the fittest."
- Individuals in a population compete for resources and mates.
- Those individuals most successful in each 'competition' will produce more offspring than those individuals that perform poorly.
- Genes from `good\' individuals propagate throughout the population so that two good parents will sometimes produce offspring that are better than either parent.
- Thus each successive generation will become more suited to their environment.
It is basically an iterative process which helps us to find the best generation based upon some fitness function among certain group of individuals.
It mainly consists of -
a) Search Space
- selection which equates to survival of the fittest;
- crossover which represents mating between individuals;
- mutation which introduces random modifications.
b)Evaluation(based upon the fitness function)
These processes are repeated until and unless we get the best generation.GA reduces the time required for during such operations.
Obviously since GA takes random values, it is highly expected that each time we can get different generation as the fittest.
Code To Optimize the Stalagmite Function.
clc
clear all
close all
x= linspace(0,0.6,100);
y= linspace(0,0.6,100);
itera_s=100;
%forming 2D mesh
[xx yy]= meshgrid(x,y);
a=length(xx)
b=length(yy)
%evaluating the function
for i=1:a
for j=1:b
input_1=xx(i,j);
input_2=yy(i,j);
M(i,j)= stalagmite(input_1,input_2);
end
end
options=optimoptions(\'ga\')
options=optimoptions(options,\'PopulationSize\',270);
for i=1:itera_s
[inputs,fval(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],options);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
%plotting
figure(1);
subplot(2,1,1);
surfc(xx,yy,M);
shading interp;
hold on
plot3(xopt,yopt,fval,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\');
xlabel(\'x-values\');
ylabel(\'y-values\');
subplot(2,1,2);
figure(2);
plot(-1*fval);
xlabel(\'iteration\');
ylabel(\'Function Maxima\');
axis([0,100,0,1.5]);
The Stalagmite Function
function [M]= stalagmite(input_1,input_2)
f1_x=[sin(5.1*pi*input_1 +0.5)]^6;
f1_y=[sin(5.1*pi*input_2 +0.5)]^6;
term1= (input_1-0.0067)^2/2;
term2= (input_2-0.0067)^2/2;
f2_x= exp(-4*0.6931*term1);
f2_y= exp(-4*0.6931*term2);
M= -1*(f1_x*f2_x*f1_y*f2_y)
Now the plots.
Population size= 270; No. of iterations=100;
Population size= 370; No. of iterations=120;
The first plot will remain the same.
While the second plot will look like this:
CONCLUSION:
We know that ga function always gives us the minimum value of the function(in this case stalagmite) and the corresponding value of the variables for which the function is minimum.
We can get the maximum value of the function by simply putting a negative sign before the variable which is an arrray containing each value of the function for the corresponding values of its variables.
We see that as we increase the population size,the difference in the maximum value of the function decreases.