GENETIC ALGORITHM
AIM:
To calculate global maxima of a stalagmite function
INTRODUCTION
A genetic algorithm is a heuristic search method used in artificial intelligence and computing. It is used for finding optimized solutions to search for problems based on the theory of natural selection and evolutionary biology. Genetic algorithms are excellent for searching through large and complex data sets
FIVE STEPS IN GENETIC ALGORITHM:
The process of natural selection starts with the selection of fittest individuals from a population. They produce offspring which inherit the characteristics of the parents and will be added to the next generation. If parents have better fitness, their offspring will be better than parents and have a better chance of surviving. This process keeps on iterating and at the end, a generation with the fittest individuals will be found.
HOW Genetic ALGORITHM WORKS?
a)Initial Population: The algorithm begins by creating a random initial population.
b)The algorithm then creates a sequence of new populations. At each step, the algorithm uses the individuals in the current generation to create the next population. To create a new population, the algorithm performs the following steps:
-
Fitness Function: Scores each member of the current population by computing its fitness value. These values are called raw fitness scores.
-
Scales the raw fitness scores to convert them into a more usable range of values. These scaled values are called expectation values.
-
Selection: Selects members, called parents, based on their expectations.
-
Some of the individuals in the current population that have lower fitness are chosen as elite. These elite individuals are passed to the next population.
-
Crossover and Mutation: It produces children from their parents. Children are produced either by making random changes to a single parent—mutation—or by combining the vector entries of a pair of parents—crossover.
-
Replaces the current population with the children to form the next generation.
c) The algorithm stops when one of the stopping criteria is met
CODE:
In Matlab, GA is used to find the minima of a function. But here maxima is found by using negative of the function.
Steps :
- x,y is defined between 0 to 150 spacing 0.6
- mesh grid command is used to make a 2D grid xx yy
- For loop to solve GA for every element in mesh grid
- input declared for each value in mesh grid
- the output is stored inside the stalgamitneg function
- ga code is entered for unbounded statistical study.
- For the minimum value of the function is obtained, further its x and y values are stored.
- further figure is plotted.
- surfc used for plotting contour.
- shading intrep to smoothen the plots.
- plotting maxima of the function.
clc
clear all
close all
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
%forming mesh grid
[xx yy]=meshgrid(x,y);
%evaluating the function
for i=1:length(xx)
for j=i:length(yy)
input_vector(1)=xx(i,j);
input_vector(2)=yy(i,j);
f(i,j)=stalagmiteneg(input_vector);
end
end
options=optimoptions(\'ga\')
options=optimoptions(options,\'PopulationSize\',300);
tic
for i=1:100
[min ,fval(i)]=ga(@stalagmiteneg,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i)=min(1);
yopt(i)=min(2);
end
%plotting
figure(1);
subplot(2,1,1);
surfc(xx,yy,f);
shading interp;
hold on
plot3(xopt,yopt,fval,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\');
label(\'x-values\');
label(\'y-values\');
subplot(2,1,2);
plot(fval);
FUNCTION:
- Function declared and inputs specified.
- Equations are written.
- Further the we take -ve to obtain the maxima.
function [f] = stalagmiteneg(input_vector)
x= input_vector(1);
y=input_vector(2);
term_1=(sin(16.02*x +0.5))^6;
term_2=(sin(16.02*x +0.5))^6;
term_3=exp((-4*log(2)*(x-0.0667)^2)/(0.64));
term_4=exp((-4*log(2)*(x-0.0667)^2)/(0.64));
f=-1*term_1*term_2*term_3*term_4;
end
PLOTS: