Week 4 - Genetic Algorithm
Program to optimize the stalagmite function and to find the global maxima.
Aim:
To write program to optimize and to find the global maxima using MATLAB.
Introduction:
Genetic algorithm:
Genetic algorithm is an evolutionary algorithms with working principle based on the mechanics of natural genetics. The genetic algorithm is a method for solving both constrained and unconstrained optimization problems that is based on natural selection, the process that drives biological evolution.
At each step, the genetic algorithm selects individuals at random from the current population to be parents and uses them to produce children for the next generation. Over successive generations, the population evolves towards an optimal solution.
The genetic algorithm uses 3 rules at each step to create the next generation from the current population:
- Selection rule selects the individuals, called parents, that contribute to the population at the next generation.
- Crossover rule combine two parents to form children for the next generation.
- Mutation rule apply random changes to individual parents to form children.
Procedure followed is as below:
- Initially stalagmite function is defined in a separate script.
- A negative sign is used in front of the function to obtain maxima of the function rather minima.
- A nested for loop is utilized to iterate each of the elements.
- Genetic algorithm is used to determine the optimal of the function.
- Plot is generated for different cases.
Global maxima:
Global maxima or minima of f(x) in [a,b] is basically the greatest or least value of f(x) in [a, b]. mathematically, it is written as : the function f(x) has a global maxima at the point ‘a’ in the interval if f(a)>f(x), for all x£I.
Stalagmite function:
Stalagmite function used for optimization is given below:

Where the above equation is given for the optimization for global minima.
By default, in MATLAB ga calculate the minima value of a function. Hence, for optimization of global maxima I took f as -ve as shown in the stalagmite function program.
%stalagmite function
function [f] = stalagmite_function(input_vector);
f1x = (sin((5.1*pi*input_vector(1))+ 0.5)).^6;
f1y = (sin((5.1*pi*input_vector(2))+ 0.5)).^6;
f2x = exp((((input_vector(1)-0.667).^2)/0.64)*(-4*log(2)));
f2y = exp((((input_vector(2)-0.667).^2)/0.64)*(-4*log(2)));
f1 = f1x*f1y*f2x*f2y;
f = -f1;
end
Program to optimize stalagmite function:
Here, I considered three cases to write program. Those are as follows:
- Stalagmite function for global maxima.
- Bounded inputs.
- Unbounded inputs.
- Bounded inputs with increasing number of population size.
program for genetic algorithm is as follow
% optimization for global maxima using genetic algorithm(ga)
clear all
close all
clc
%inputs
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
%creating 2D array using meshgrid
[xx yy] = meshgrid(x,y);
%number of itterations
num_cases = 50;
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
figure(1);
surf(xx,yy,f);
xlabel('x value');
ylabel('y value');
title('Stalagmite function')
shading interp %makes surface look more smoother
[inputs fval] = ga(@stalagmite_function,2);
%case study 1
tic
for i = 1:num_cases
[inputs, fopt(i)] = ga(@stalagmite_function,2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
case_study_1_time = toc
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,f)
title('unbounded inputs')
xlabel('x values')
ylabel('y values')
shading interp
hold on
plot3(xopt,yopt,fopt, 'marker', 'o', 'markersize', 5, 'markerfacecolor', 'r')
subplot(2,1,2)
plot(fopt)
xlabel('itteration')
ylabel('function maximum')
%case study 2 with upper bound and lower bounds
tic
for i = 1:num_cases
[inputs,fopt(i)] = ga(@stalagmite_function,2,[],[],[],[],[0;0],[0.6;0.6]);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
case_study_2 = toc
figure(3)
subplot(2,1,1)
surfc(x,y,f)
shading interp
title('bounded inputs')
xlabel('x value')
ylabel('y value')
hold on
plot3(xopt,yopt,fopt,'marker','o','markersize', 5, 'markerfacecolor', 'r')
subplot(2,1,2)
plot(fopt)
xlabel('itterations')
ylabel('function minimum')
%study case 3 increasing the population size
options = optimoptions(@ga)
options = optimoptions(options, 'populationsize', 500) %increasing the population size
tic
for i = 1:num_cases
[inputs,fopt(i)] = ga(@stalagmite_function, 2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
case_study_3_time = toc %gives the time taken for study
figure(4)
subplot(2,1,1)
surfc(x,y,f) %creats a surface a=to visualise 3D
shading interp
title('bounded inputs with increased population size')
xlabel('x value')
ylabel('y value')
hold on
plot3(xopt, yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(fopt) %plotting -f vs number of itterations
xlabel('itterations')
ylabel('function minimum')
Explanation for program:
Initially we will create our own function to calculate the values of objective function at various values of x and y. we use linspace command for creating various x and y values.
Created 1D array to 2D array using meshgrid().
For iterations we genetic algorithm syntax as ga(function, number of variables).
Using the ga syntax written code for each cases as mentioned. The plots obtained are shown below.
Plots:
The plots obtained from the above program are as shown below:

figure1: shows inverted stalagmite function to get global maxima.

figure2 : shows the plot obtained for unbounded inputs.

figure3: shows the plot obtained for bounded inputs.

figure4 : shows the plot obtained for bounded inputs with increasing size of population.
Result:
The program for global maxima is written and the plots are shown.