Matlab code to optimize the Stalagmite function
Working of Genetic Algorithm
The following outline summarizes how the genetic algorithm works:-
- The algorithm begins by creating a random initial population.
- 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 the new population, the algorithm performs the following steps:
- Scores each member of the current population by computing its fitness value. These values are called the 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.
- It then selects members, called parents, based on their expectation.
- 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.
- It then Produces children from the 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.
- The algorithm stops when one of the stopping criteria is met.
Matlab code to optimize the Stalagmite function
clc
clear all
close all
%Defining our search space
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
%creating a 2D mesh
[xx yy ]=meshgrid(x,y);
%evaluate the stalagmite function
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
surfc(xx,yy,F)
xlabel(\'x values\')
ylabel(\'y values\')
shading interp
[inputs fval]=ga(@stalagmite,2);
globalmaxima=max(max(F))
for i=1:length(xx)
for j=1:length(yy)
if globalmaxima==F(i,j)
xval=xx(i,j)
yval=yy(i,j)
end
end
end
Defined Stalagmite Function
function [f]=stalagmite(input)
f1=(sin(5.1*pi*input(1))+0.5)^6;
f2=(sin(5.1*pi*input(2))+0.5)^6;
f3=exp(-4*log(2)*(input(1)-0.0667)^2/0.64);
f4=exp(-4*log(2)*(input(2)-0.0667)^2/0.64);
f=(f1*f2*f3*f4);
end
Plot