Week 4.1 - Genetic Algorithm
AIM:
- To write a MATLAB code to optimize the stalagmite function and to find the global maxima of the function.
- To explain the concept of genetic Algorithm and the syntax for GA in MATLAB.
- To plot the graphs for all 3 studies and for the F maximum vs Number of Iteration.
THEORY:
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. 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.
Syntax
- x = ga(fun,nvars)x = ga(fun,nvars,A,b)
- x = ga(fun,nvars,A,b,Aeq,beq)
- x = ga(fun,nvars,A,b,Aeq,beq,lb,ub)
- x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon)
- x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options)
- x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon)
- x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options)
- x = ga(problem)
- [x,fval] = ga(___)
- [x,fval,exitflag,output] = ga(___)
- [x,fval,exitflag,output,population,scores] = ga(___)
Description
- 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) finds a local minimum x to fun, subject to the linear inequalities A*x ≤ b. ga evaluates the matrix product A*x as if x is transposed (A*x').
- x = ga(fun,nvars,A,b,Aeq,beq) finds a local minimum x to fun, subject to the linear equalities Aeq*x = beq and A*x ≤ b. (Set A=[] and b=[] if no linear inequalities exist.) ga evaluates the matrix product Aeq*x as if x is transposed (Aeq*x').
- 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) subjects the minimization to the constraints defined in nonlcon. The function nonlcon accepts x and returns vectors C and Ceq, representing the nonlinear inequalities and equalities respectively. ga minimizes the fun such that C(x) ≤ 0 and Ceq(x) = 0. (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,[],[],lb,ub,nonlcon,IntCon) or x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options) requires that the variables listed in IntCon take integer values.
- x = ga(problem) finds the minimum for problem, a structure described in problem.
- [x,fval] = ga(___), for any previous input arguments, also returns fval, the value of the fitness function at x.
- [x,fval,exitflag,output] = ga(___) also returns exitflag, an integer identifying the reason the algorithm terminated, and output, a structure that contains output from each generation and other information about the performance of the algorithm.
- [x,fval,exitflag,output,population,scores] = ga(___) also returns a matrix population, whose rows are the final population, and a vector scores, the scores of the final population.
Stalagmite Function: A stalagmite function generally refers to the function with which we are generating values over a stalagmite kind of surface for finding out the local and global maxima and minima of that particularly given function.
The equation used in the stalagmite function to determine maxima and minima is shown below:

1. A MATLAB code for a Stalagmite Function.
% Creating a Stalagmite function.
function[S_F] = Stalagmite_Func(input)
x = input (1);
y = input (2);
f1x = (sin(5*1*pi*x+0.5))^6;
f1y = (sin(5*1*pi*y+0.5))^6;
f2x = exp ((-2.7726)*(x-(0.0667))^2/0.64);
f2y = exp ((-2.7726)*(y-(0.0667))^2/0.64);
S_F = -(f1x*f1y*f2x*f2y);
end
Explanation:
1. Using the equation given above, a program for the stalagmite function is created.
2. x and y are the two inputs.
3. Stalagmite Function is stored as S_F.
***************************************************************************************************
% To write a MATLABcode to optimize the stalagmite function and to find the global maxima of the function.
close all
clear all
clc
% Array Input
x = linspace (0,0.6,150);
y = linspace (0,0.6,150);
% Defining the number of Iterations.
num_cases = 50;
% 2D array Formation using FOR loop to include every single value of xx and yy in the Stalagmite function
[xx,yy] = meshgrid(x,y);
for i = 1:length(x)
for j = 1:length(y)
input(1) = xx(i,j);
input(2) = yy(i,j);
f(i,j) = Stalagmite_Func(input)
end
end
% Performing Study 1: Statistical Behaviour - Unbounded
% Using the tic-toc command to study the time taken for the study.
tic
% Using For Loop, iterating all values of xx & yy and writing input values
% for the Stalagmite_Func using i & j.
% ga is assigned under for loop so that it can iterate each and every value
% of the function.
for i = 1:num_cases
% Calculating the optimized value of the function.
[inputs, fopt(i)] = ga(@Stalagmite_Func,2)
xopt(i) = inputs (1);
yopt (i) = inputs (2);
end
study1_time = toc
% Generating figure and Plotting multiples plots using SUBPLOT command.
figure(1)
subplot (2,1,1)
hold on
% f is used to generate Global minima, so using negative f (-f) to generate Global Maxima.
surfc(x,y,-f)
% To generate smooth looking plot, using shading interp command.
shading interp
xlabel ('X Axis')
ylabel ('Y Axis')
zlabel ('Function Value')
plot3 (xopt, yopt, -fopt, 'marker','o','markersize',5,'markerfacecolor','b')
title ('Unbounded inputs')
subplot(2,1,2)
plot (-fopt)
xlabel ('Iteration')
ylabel ('Function minimum')
OUTPUTS:

% Performing Study 2: Statistical Behaviour - Upper and Lower Bounds.
% Using the tic-toc command to study the time taken for the study.
tic
% Using For Loop, iterating all values of xx & yy and writing inout values for the Stalagmite_Func using i & j.
% ga is assigned under for loop so that it can iterate each and every value of the function.
for i = 1:num_cases
% Calculating the optimized value of the function.
[inputs,fopt(i)] = ga(@Stalagmite_Func,2,[],[],[],[],[0;0],[1;1]);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study2_time = toc
% Generating figure and Plotting multiples plots using SUBPLOT command.
figure(2)
subplot (2,1,1)
hold on
% f is used to generate Global minima, so using negative f (-f) to generate Global Maxima.
surfc(x,y,-f)
% To generate smooth looking plot, using shading interp command.
shading interp
xlabel ('X Axis')
ylabel ('Y Axis')
zlabel ('Function Value')
plot3 (xopt, yopt, -fopt, 'marker','o','markersize',5,'markerfacecolor','b')
title ('Bounded inputs')
subplot(2,1,2)
plot (-fopt)
xlabel ('Iteration')
ylabel ('Function minimum')
OUTPUTS:
Here providing the upper and the lower limit in the ga helps to reduce the search space with the upper and lower boundary.

% Performing study 3 - Increasing GA iterations.
% Increasing the size of the function using "optimoptions"
options = optimoptions('ga');
options = optimoptions (options,'PopulationSize',170);
% Using tic-toc command to study the time taken for the study.
tic
% Using For Loop, iterating all values of xx & yy and writing inout values for the Stalagmite_Func using i & j.
% ga is assigned under for loop so that it can iterate each and every value of the function.
for i = 1:num_cases
% Calculating the optimized value of the function.
[inputs,fopt(i)] = ga(@Stalagmite_Func,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study3_time = toc
% Generating figure and Plotting multiples plots using SUBPLOT command.
figure (3)
subplot (2,1,1)
hold on
% f is used to generate Global minima, so using negative f (-f) to generate Global Maxima.
surfc(x,y,-f)
% To generate smooth looking plot, using shading interp command.
shading interp
xlabel ('X Axis')
ylabel ('Y Axis')
zlabel ('Function Value')
plot3 (xopt, yopt, -fopt, 'marker','o','markersize',5,'markerfacecolor','b')
title ('Increasing GA Iterations')
subplot(2,1,2)
plot (-fopt)
xlabel ('Iteration')
ylabel ('Function Maximum')
OUTPUTS:
The benefit of using optimoptions is that, it will be working on a large set of numbers and which will be led to an increase in the possibilities of finding the better fitness of the function. However, the time for evaluation will increase.

*****************************************************************************************
Merging all the studies into 1 single program.
2. A MATLAB code to optimize the stalagmite function and to find the global maxima of the function.
% To write a MATLABcode to optimize the stalagmite function and to find the global maxima of the function.
close all
clear all
clc
% Array Input
x = linspace (0,0.6,150);
y = linspace (0,0.6,150);
% Defining the number of Iterations.
num_cases = 50;
% 2D array Formation using FOR loop to include every single value of xx and
% yy in the Stalagmite function
[xx,yy] = meshgrid(x,y);
for i = 1:length(x)
for j = 1:length(y)
input(1) = xx(i,j);
input(2) = yy(i,j);
f(i,j) = Stalagmite_Func(input)
end
end
% Performing Study 1: Statistical Behaviour - Unbounded
% Using tic-toc command to study the time taken for the study.
tic
% Using For Loop, iterating all values of xx & yy and writing input values for the Stalagmite_Func using i & j.
% ga is assigned under for loop so that it can iterate each and every value of the function.
for i = 1:num_cases
% Calculating the optimized value of the function.
[inputs, fopt(i)] = ga(@Stalagmite_Func,2)
xopt(i) = inputs (1);
yopt (i) = inputs (2);
end
study1_time = toc
% Generating figure and Plotting multiples plots using SUBPLOT command.
figure(1)
subplot (2,1,1)
hold on
% f is used to generate Global minima, so using negative f (-f) to generate Global Maxima.
surfc(x,y,-f)
% To generate smooth looking plot, using shading interp command.
shading interp
xlabel ('X Axis')
ylabel ('Y Axis')
zlabel ('Function Value')
plot3 (xopt, yopt, -fopt, 'marker','o','markersize',5,'markerfacecolor','b')
title ('Unbounded inputs')
subplot(2,1,2)
plot (-fopt)
xlabel ('Iteration')
ylabel ('Function minimum')
% Performing Study 2: Statistical Behaviour - Upper and Lower bounds.
% Using tic-toc command to study the time taken for the study.
tic
% Using For Loop, iterating all values of xx & yy and writing input values for the Stalagmite_Func using i & j.
% ga is assigned under for loop so that it can iterate each and every value of the function.
for i = 1:num_cases
% Calculating the optimized value of the function.
[inputs,fopt(i)] = ga(@Stalagmite_Func,2,[],[],[],[],[0;0],[1;1]);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study2_time = toc
% Generating figure and Plotting multiples plots using SUBPLOT command.
figure(2)
subplot (2,1,1)
hold on
% f is used to generate Global minima, so using negative f (-f) to generate Global Maxima.
surfc(x,y,-f)
% To generate smooth looking plot, using shading interp command.
shading interp
xlabel ('X Axis')
ylabel ('Y Axis')
zlabel ('Function Value')
plot3 (xopt, yopt, -fopt, 'marker','o','markersize',5,'markerfacecolor','b')
title ('Bounded inputs')
subplot(2,1,2)
plot (-fopt)
xlabel ('Iteration')
ylabel ('Function minimum')
% Performing study 3 - Increasing GA iterations.
% Increasing the size of the function using "optimoptions"
options = optimoptions('ga');
options = optimoptions (options,'PopulationSize',170);
% Using tic-toc command to study the time taken for the study.
tic
% Using For Loop, iterating all values of xx & yy and writing input values for the Stalagmite_Func using i & j.
% ga is assigned under for loop so that it can iterate each and every value of the function.
for i = 1:num_cases
% Calculating the optimized value of the function.
[inputs,fopt(i)] = ga(@Stalagmite_Func,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study3_time = toc
% Generating figure and Plotting multiples plots using SUBPLOT command.
figure (3)
subplot (2,1,1)
hold on
% f is used to generate Global minima, so using negative f (-f) to generate Global Maxima.
surfc(x,y,-f)
% To generate smooth looking plot, using shading interp command.
shading interp
xlabel ('X Axis')
ylabel ('Y Axis')
zlabel ('Function Value')
plot3 (xopt, yopt, -fopt, 'marker','o','markersize',5,'markerfacecolor','b')
title ('Increasing GA Iterations')
subplot(2,1,2)
plot (-fopt)
xlabel ('Iteration')
ylabel ('Function Maximum')
TIME TAKEN FOR STUDIES:
- study1_time = 3.6207 sec
- study2_time = 4.4252 sec
- study3_time = 10.9558 sec
WORKSPACE:

ERROR FACED AND SOLUTION:
1. There was just a minor error while defining the function name. Instead of uppercase, I used lowercase causing the error.

It was corrected by using the proper function name used in the code for the stalagmite function.
CONCLUSION:
1. Three different studies are performed to optimize the stalagmite function and find global maxima.
2. Out of three, the third study output is most close to desired values.
REFERENCES: