Week 4.1 - Genetic Algorithm
% MATLAB code to optimise the stalagmite function and find the global
% maxima.
clear all
close all
clc
% defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 50;
% creating a 2 Dimensional space
[xx,yy] = meshgrid(x,y);
% Evaluating the stalagmite function
for i = 1:length(xx)
for j = 1:length(yy)
input_vector(1)= xx(i,j);
input_vector(2)= yy(i,j);
input_vector = [input_vector(1),input_vector(2)];
f(i,j) = stalagmite(input_vector);
end
end
% study1-statistical behavior
for i = 1:num_cases
[input,fopt(i)] = ga(@stalagmite,2);
% optimum value that function predicts
xopt(i)= input(1);
yopt(i)= input(2);
end
study1_time = toc
figure(1)
subplot(2,1,1) %multiple plots inside the same figure window
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 maximum')
tic
%Study2 - Statistical behavior - with opper and lower bounds
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) %multiple plots inside the same figure window
surfc(x,y,-f)
shading interp
hold on
plot3(xopt,yopt,-fopt,'Marker','o','MarkerSize',5,'MarkerFaceColor','r')
title('bounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('Function maximum')
tic
% study3- increasing GA iterations
options = optimoptions('ga')
options = optimoptions(options,'PopulationSize',240); % increasing the population size
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
study3_time = toc
figure(3)
subplot(2,1,1) %multiple plots inside the same figure window
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 maximum')
tic
function [f] = stalagmite(input_vector)
x = input_vector(1);
y = input_vector(2);
term1 = 5.1*pi*x ;
term2 = 5.1*pi*y ;
term3 = [(x-0.0667)^2/0.64];
term4 = [(y-0.0667)^2/0.64];
term5 = (-4)*log(2);
xf1 = (sin(term1+0.5)).^6 ;
yf1 = (sin(term2+0.5)).^6 ;
xf2 = exp(term5*term3);
yf2 = exp(term5*term4);
f = -(xf1*yf1*xf2*yf2);
end
Concept of Genetic 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.It is based on Darwinian Theory of 'Survival of the Fittest'. 'ga' is inbuilt in Matlab(in-built function). This routine only minimizes a function.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 populations 'evolves' toward an optimal solution.
You can apply the genetic algorithm to solve problems that are not well suited for standard optimization algorithm including problems in which the objective function is discontinuous,nondifferential,stochastic or highly nonlinear.
GA generates a population of points at each iteration.The best point in the population approaches an optimal solution.
GA Syntax in Matlab is given by-
x = ga(fun,nvars,A,b,Aeq,beq,LB,UB,nonlcon,intcon,options)
x=ga(fun,nvars) finds a local unconstrained minimum ,n, to the objective function, fun. nvars is the dimension (number of design variables) of fun.
x=ga(fun,nvars,A,b) finds a local minimum x to the fun, object to the linear inequalities A*x=<b.
x=ga(fun,nvars,A,b,Aeq,beq) finds a local minimum to fun, subject to the linear equalities Aeq*=beq and A*x=<b. (set A=[] and b=[] if no linear inequilities exist)
x=ga(fun,nvars,A,b,Aeq,beq,LB,UB) defines a set of lower and upper bounds on the design variables.
x=ga(fun,nvars,A,b,Aeq,beq,LB,UB,nonlcon) subjects the minimization to the constraints defined in nonlcon, representing the nonlinear inequilities and equilities respectively.[set LB=[] and UB=[] if no bounds exist]
x=ga(fun,nvars,A,b,Aeq,beq,LB,UB,nonlcon,options) minimizes with the default optimization parameters replaced by values in options (set nonlcon=[] if no nonlinear constraints exist) create options using optimoptions.
x=ga(fun,nvars,A,b,Aeq,beq,LB,UB,nonlcon,intcon,options) requires that the variables listed in intcon take integer values.
Explanation of the code-
A code started with clear all, close all ,clc commands then defines the search space with 2 variables x and y. for that creates 1 dimensional array using linspace command to provide 150 value between 0 to 0.6 after that created 2 dimensional array based on our 1 dimensional arry using meshgrid command. then created 'for loop' for the input vector then created a function for stalagmite(input vector) now to calculate maxima used below formulae-
f(x,y)=f1,xf2,xf1,yf2,y
f1,x = [sin(5.1*pi*x+0.5)]^6 f1,y = [sin(5.1*pi*y+0.5)]^6
f2,x =exp[-4 log(2)*(x-0.0667)^2/0.64 ] f2,y =exp[-4 log(2)*(y-0.0667)^2/0.64 ]
and stored its value in function then this function is called in for loop and passing the variables as input. GA is actually based on random sampling it starts with random inputs based on input it evaluates function then it comes to natural selection where you can take two generations basically muted them also did crossover then you created new generation.
But everytime we run 'ga' we get differant results which is not useful. to get beneficial results i am doing some changes in inputs . After creating x,y variables, as it does not define the search space for 'ga', 'ga' does not know its actual space and the value for x,y go beyond or below 0.6 or 0. For that i use num_cases = 50.
I use the ' for loop' and run the code from 1 to 50 times after that i call 'ga' and use the new function which created @stalagmite ,pass 2 variables as a input and store the output in fopt, and then store the value of 'x' and 'y' in 'xopt' and 'yopt'.
now to visualise my output , i use surface command(surfc), shading command (shading interp) is used to get smooth plot (without grid lines), in figure window (figure 1), I also use toc tic command to calculate the time taken for evolution . I use subplot command for multiple plotting.
1st plot to show stalagmite function and plotting the minimum value that 'ga ' trying to predict over 50 studies.
2nd plot for showing the no.of iteration at x-axis and during each iteration value changes which represents as function maximum on y axis.
Results for study 1
*Here on plotting stalagmite function, i am plotting the minimum value that 'ga' predicts, which is all over the place on graph, it happens due to random sample during each iterations.
*The time taken for iteration is (study_1_time)= 4.673 sec (in previous run not in submitted)
Study 2- statistical behavior ( with upper and lower bound)
in this study, i setup the bound condition for the variables x and y. by using syntax 'ga' i am giving x and y the upper and lower limit to 0 to 1, I am applying the boundry condition, to get graph more clean and optimum than study 1.
*it is observed that the function predicts minimum points in between 3 peaks. also we can observe that the time taken for iteration is (study 2 time = 7.06)sec which is more than the study1.
study 3 - Increasing GA iterations
In addition with specifing bounds, I am also increasing initial population size. if population size is to small, we cannot find the global ,maxima.increasing the population size to 240 and applying the constriant 'optimoptions' and save its values in variables 'options' to allow the ga to predict the optimum solution from 240 population size.
*here from subplot -1, it shows maxima only lies around 1 peak.
*maxima lies at around the value 0.84 that ga predicts.
*but these study has its drawbacks, as ga slows down as it have more number of samples, to process and evaluate them for the function.
*It takes (study3 time= 24sec) which is 6 times more than study1.
% errors occured during solving the code but by taking help from support enginear able resolve it and also take help from 'Mathworks'.
Referance-
1.Mathworks.
2.www.tutorialspoint.com/genetic_algorithm.