Week 4.1 - Genetic Algorithm
Code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
clear all
close all
clc
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_spaces=50
%2dimensional array
[xx yy]=meshgrid(x, y);
for i=1:length(x)
for j=1:length(y)
input_vector(1)=xx(i,j);
input_vector(2)=yy(i,j);
f(i,j)=stalagmite(input_vector);
end
end
%study1
tic
for i=1:num_spaces
[results,fopt(i)]=ga(@stalagmite,2)
xopt(i)=results(1)
yopt(i)=results(2)
end
study_time1=toc
figure(1)
subplot(2,1,1)
surf(x,y,-f)
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o')
subplot(2,1,2)
plot(-fopt)
xlabel('iteration')
ylabel('function maximum')
%Study2
tic
for i=num_spaces
[results,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6])
xopt(i)=results(1)
yopt(i)=results(2)
end
study_time2=toc
figure(2)
subplot(2,1,1)
surf(x,y,-f)
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o')
subplot(2,1,2)
plot(-fopt)
xlabel('iteration')
ylabel('function maximum')
%Study3-Increased initial population
tic
options=optimoptions('ga')
options=optimoptions(options,'populationsize',250)
for i=1:num_spaces
[results,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6,0.6],[],[],options)
xopt(i)=results(1)
yopt(i)=results(2)
end
study_time3=toc
figure(3)
subplot(2,1,1)
surf(x,y,-f)
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o')
subplot(2,1,2)
plot(-fopt)
xlabel('iteration')
ylabel('function maximum')
Code Explanation:
- First we initiate the value of x & y using linspace command in interval of 0 to 0.6 of 150 values.Considering the values of num_spaces 150.
- Afer that using the meshgrid command for 2 dimensional array. Input vector array is used to call the stalgamite function.
- first study:
- We take number of iteraions=num_spaces,used genetic algorithm function (ga) to calculate maximum value where maxima we get.
- using the command of surf for plotting 3 dimensional graph.tic toc command is used for getting the time to complete the cycle. Study time for first study1 is 3.6470s.
4.Second Study:
- We make study for bonded values for x & y. ga function is used for random population.plot3 command is used to plot the 3d plot.tic toc command used for study time complete cycle. study time2=0.2088s.
5.Third study:
- Increased initial population
- Optimum option command is used to set initial population.storing the result in xopt,yopt.
- plot3 command is used to plot xopt,yopt,-fopt to plot optimum maximus.plotted fopt function to visualize fopt at every iteration.tic tic command is used to get the time for the cycle. study time=12.9741s.
- Time required to complete study time is more to find correct maxima.
function A=stalagmite(input_vector) x=input_vector(1); y=input_vector(2); f1=(sin(5.1*pi*x+0.5))^6; f2=exp((-4*log(2)*((x-0.0667)^2))/0.64); f3=(sin(5.1*pi*y+0.5))^6; f4=exp((-4*log(2)*((y-0.0667)^2))/0.64); A=-(f1*f2*f3*f4); end - Creating stalagamite function whre we consider -f as output which gives us maxima.
output:

Plot1:

Plot2:

Plot3:

Explanation of output is in program explanation.
Concept of Generic Algorithm:
A genetic algorithm (GA) is a method for solving both constrained and unconstrained optimization problems based on a natural selection process that mimics biological evolution. The algorithm repeatedly modifies a population of individual solutions. At each step, the genetic algorithm randomly selects individuals from the current population and uses them as parents to produce the children for the next generation. Over successive generations, the population "evolves" toward an optimal solution.You can apply the genetic algorithm to solve problems that are not well suited for standard optimization algorithms, including problems in which the objective function is discontinuous, nondifferentiable, stochastic, or highly nonlinear.
Syntax for ga :