Week 4 - Genetic Algorithm
Optimise the stalagmite function and find the global maxima on MATLAB:-
An area of study that deals with how to minimize or maximize a particular
function that depends on multiple parameters.
Genetic Algorithm:-
'Genetic Algorithm' is a random sampling, which starts from random inputs and based on
those inputs evaluate the function then processed to natural selection. For that
we take 2 generations (parent) and either muted or cross over them and create a batter
new generation. It also gives different results after every run.
Code and Syntax Explanation:-
Initially, with 'linspace' command we create 1d array of x and y which does not define the search space for genetic algorithm and based on that we create 2d array(matrix) using 'meshgrid' command.The 'num_case' is the number of times(50) we run the code. For each value of x and y,
we create an array(row vector) called 'input_vector' and then put as input to a 'stalagmite function' (which we have created), by calling the function and calculate value of function at x and y. It is inbuilt in MATLAB(genetic algorithm) used only to minimizes a function For this we are passing two inputs the first is function handle which is a separate function code and the second is the number of variables. This will evaluate an optimum value from our inputs. These two 'for-loop' are generating the data for the plot.
The first study is to calling the 'ga' by passing the number of inputs and store the optimum value of x, y that the function predicts. Open a new figure window and create a sub plot for multiple plot inside a same figure window. To visualize the plot batter use 'surfc' command and use here '-f' to get the maximum value and use 'shading inturp' to remove the grid line from the plot.
Then plot3 to plot the x, y, and fopt that ga predicts over 50 iteration. That helps to visualize the algorithm performance and use another subplot to make it more clear by plotting directly the optimum value that the function is predicting as a function of iteration which is a case number. Then fix the timer for this study using 'tic-toc' command and also provide the title.
2nd study setup the upper and lower bounds for the variables to set the limits. For this we have a different function definition syntax-
`x = ga(fun,2,A,b,Aeq,beq,lb,ub)`
In the 3rd study we change the bound and the initial population size for that we increase the 'ga' iteration with population size to 300 using 'optimoptions'. In this study we use another function definition syntax-
`[x,fval,exitflag,output,population,scores] = a(fun,2,A,b,Aeq,beq,lb,ub,nonlcon,IntCon)`
A = Linear inequality constraints
b = Linear inequality constraints
Aeq = Linear equality constraints
beq = Linear equality constraints
nonl= Nonlinear constraints
IntCon = Integer variables
lb = Lower bounds
ub = Upper bounds
Main Code:-
clear all
close all
clc
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_case=50;
% 2d inputs
[xx,yy] = meshgrid(x,y);
% finding stalagmint function
for i = 1 : length(xx)
for j = 1 : length(yy)
input_vector(1) = xx(i,j);
input_vector(2) = yy(i,j);
f(i,j)= stalagmite(input_vector);
end
end
% study 1
tic
for i = 1:num_case
[inputs,fopt(i)]= ga(@stalagmite,2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study1_time = toc;
figure (1)
subplot(2,1,1)
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 Minimum')
% study 2
tic
for i = 1:num_case
[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)
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 Minimum')
% study 3 increasing ga itreation
options = optimoptions('ga');
options = optimoptions(options,'populationSize',300);
tic
for i = 1:num_case
[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)
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 Minimum')
Function:- we creat a stalagmite function with the following equation to get the function value.
`f_1x = (sin((5.1*pi*x)+0.5))^6`
`f_1y = (sin((5.1*pi*y)+0.5))^6`
`f_2x = exp[-4*ln(2) (x-0.0667)^2/0.64]`
`f_2y = exp[-4*ln(2) (y-0.0667)^2/0.64]`
`f = -(f_1x*f_2x*f_1y*f_2y)`
function[f] = stalagmite(input_vector)
x= input_vector(1);
y= input_vector(2);
f_1x = (sin((5.1*pi*x)+0.5)).^6;
f_1y = (sin((5.1*pi*y)+0.5)).^6;
f_2x = exp((-4*log(2)*(x-0.0667).^2)/0.64);
f_2y = exp((-4*log(2)*(y-0.0667).^2)/0.64);
f = -(f_1x*f_2x*f_1y*f_2y);
end
Figure 1:
Figure 2 and 3:
Errors:-
- Using 'f' in the code instead of '-f' for maxima.
- markersize must be written in the way 'markersize'.
https://in.mathworks.com/help/gads/ga.html#mw_f8e8d81b-aba1-4254-9f1a-cf93ff0ccc2b