Genetic Algorithm Using MATLAB
Objective: To find the Global Maxima of Stalagmite Function Using Genetic Algorithm in MATLAB
Three Study Perform In Genetic Algorithm
- Study with Random Sampling
- Study with Upper and Lower Bound in Sampling
- Study with Upper, Lower Bound with an increase in population size
Introduction
Genetic Algorithm:
The Genetic algorithm is an optimization technique, it uses concepts from evolutionary biology. It starts with an initial generation of candidate solution which are tested against the objective function. Subsequent generations evolve from the previous one through selection, crossover, mutation. Five phases are considered in a genetic algorithm.
- Initial population
- Fitness function
- Selection
- Crossover
- Mutation
Syntax
- x = ga(fun,nvars):
Finds a local unconstrained minimum, x, to the objective function, fun. nvars is the dimension (number of design variables) of fun.
- x = ga(fun,nvars,A,b,Aeq,beq,lb,ub):
Defines a set of lower and upper bounds on the design variables, x, so that a solution is found in the range lb ≤ x ≤ ub. (Set Aeq=[ ] and beq=[ ] if no linear equalities 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. Population size will be defined in options
Stalagmite Function:
- It is a mathematical model that helps to generate local minima and maxima.
- It is a three-dimensional function and the way the function is designed is that it has a lot of local maxima or minima and one global maxima or minima.
`f(x,y)=f_(1,x) f_(2,x) f_(1,y) f_(2,y)`
`f_(1.x)=[sin(5.1πx+0.5)]^6`
`f_(1.y)=[sin(5.1πy+0.5)]^6`
`f_(2.x)=exp[-4 ln(2) (x-0.0667)^2/0.64 ]`
`f_(2.y)=exp[-4 ln(2) (y-0.0667)^2/0.64 ]`
And now we are writing the final stalagmite Function based on the above formula is as, we need to change the sign of function, so that it will give value for the reverse case of minima, i.e. maxima.
Program for Stalagmite function:
function [f] = stalagmite_function(input_vector)
%Input Parameter
x = input_vector(1);
y = input_vector(2);
Term_1 = (sin((5.1*pi*x)+0.5))^6;
Term_2 = (sin((5.1*pi*y)+0.5))^6;
Term_3 = exp(-4*log(2)*((x-0.0667)^2)/0.64);
Term_4 = exp(-4*log(2)*((y-0.0667)^2)/0.64);
%solution of the fucntion
f = -(Term_1*Term_2*Term_3*Term_4)
end
Main Program For Genetic algorithm:
- Study 1 with Random Sampling
clear all;
close all;
clc;
%Defining the range of input
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
n = 50; %no.of Iteration
[xx,yy]= meshgrid(x,y);
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_function(input_vector);
end
end
tic
for i = 1:n
[inputs,fopt(i)]=ga(@stalagmite_function,2);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
Study_1_time=toc
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,-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')
Explanation:
First clear all, close all, clc command is used to clear all the data from the workspace and command window and close all this figure which is open its always good to start with these three commands before starting a code
The percent sign is most commonly used to indicate non-executable text within the body of a program. those non-executable texts are for better understanding the code
Now we need to define the search space for finding the values of x & y in the stalagmite function. The search space is chosen to be between 0 to 0.6 with 150 intervals using 'linspace()' command.
Genetic Algorithm can take no. of iterations each time we run the code giving different values each time. We will consider whatever maximum value that came within those 50 iterations. We have restricted our area of search. These no of iterations is given by 'n=50'.
x & y will be the column vector. meshgrid command is used to transforms the domain specified by vectors x and y into arrays X and Y, which can be used to evaluate functions of two variables and three-dimensional mesh/surface plots. we give the input of the [xx,yy] 2D mesh grid to to obtain corresponding function output using a for loop.
In the next part of the program the Genetic algorithm is called and for input arguments, the stalagmite function is given as function with two design variables and outputs are the input values of x & y for output optimal function value/global maxima.
[inputs,fopt(i)]=ga(@stalagmite_function,2);
Numberer of design variables is 2 for stalagmite_function
These three values xopt, yopt and fopt are plots as a 3D line plot in the same figure window as the surface plot of stalagmite function. Also, another plot is created to showcase the maxima value for each of 50 iterations. There is a negative sign for fopt so that it will give the maxima value
The tic and toc functions work together to measure elapsed time. tic saves the current time that toc uses later to measure the elapsed time. The sequence of commands measures the amount of time MATLAB takes to complete one or more operations and displays the time in seconds.
Output For Study 1 :
Time take = 43.8572 s
- Study 2 with Upper and Lower Bound in Sampling:
clear all;
close all;
clc;
%Defining the range of input
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
n = 50; %no.of Iteration
[xx,yy]= meshgrid(x,y);
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_function(input_vector);
end
end
tic
for i = 1:n
[inputs,fopt(i)]=ga(@stalagmite_function,2,[],[],[],[],[0;0],[1;1]);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
Study_2_time=toc
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,-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')
Explanation:
In the next part of the program the Genetic algorithm is called and for input arguments, the stalagmite function is given as function with two design variables and outputs are the input values of x & y for output optimal function value/global maxima.
[inputs,fopt(i)]=ga(@stalagmite_function,2,[],[],[],[],[0;0],[1;1]); a set of lower and upper bounds on the design variables is defined, so that a solution is found in the range lb ≤ x ≤ ub
These three values xopt, yopt, and fopt are plots as a 3D line plot in the same figure window as the surface plot of the stalagmite function. Also, another plot is created to showcase the maxima value for each of the 50 iterations. There is negative sign for fopt so that it will give the maxima value.
Output For Study 2 :
Time take = 26.3301 s
- Study 3 with Upper, Lower Bound with an increase in population size:
clear all;
close all;
clc;
%Defining the range of input
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
n = 50; %no.of Iteration
[xx,yy]= meshgrid(x,y);
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_function(input_vector);
end
end
options= optimoptions('ga');
options= optimoptions(options,'PopulationSize',500);
tic
for i = 1:n
[inputs,fopt(i)]=ga(@stalagmite_function,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
Study_3_time=toc
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded Inputs with increased Populations')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('Function maximum')
Explanation:
To optimize the results and getting more accuracy we are increasing the iteration in genetic algorithm. Because by default the (ga) takes 50 value. Here we are going to increase the population size because if the initial population is too small then it not going to find out global maxima (or) minima, it only finds the local maxima (or) minima.
The (ga) value is 50 which is changeable by initially (i.e we can use the increased value by default) in the individual program, but in the combined program we need to use the command as follows,
options=optimoptions('ga');
options=optimoptions(options,'PopulationSize',500);
[inputs,fopt(i)]=ga(@stalagmite_function,2,[],[],[],[],[0;0],[1;1],[],[],options); Minimizes with the default optimization parameters replaced by values in options. (Set nonlcon=[ ] if no nonlinear constraints exist.) Created options using optimoptions. Population size will be defined in options.
Output For Study 3 :
Time take = 228.8401 s
Conclusion:
- Study 1 shows how the algorithm behaves when the bounds are not mentioned. The lack of bounds results in random sampling that is beyond and below the search space
- That's why in Study 2 we addition mentioned upper and lower bounds, the level of randomness in the optimal outputs of the function also reduces which is the result of the defined search space
- In study 3 the population size specifies how many individuals there are in each generation. With a large population size, the genetic algorithm searches the solution space more thoroughly, However, a large population size also causes the algorithm to run more slowly. That's why it took 228.8401 s for the algorithm to complete
Error :
The length spelling was wrong that's why its shows undefined function
The syntax was wrong. The correct Syntax is "[inputs,fopt(i)]=ga(@stalagmite_function,2);" the function full name was supposed in the syntax.
The syntax for ga is wrong . The correct Syntax is " [inputs,fopt(i)]=ga(@stalagmite_function,2,[],[],[],[],[0;0],[1;1],[],[],options);"