Genetic Algorithm, optimization of Stalagmite function
- 1. Aim: Write code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
- 2. Objective:
- To understand the concept and syntax of a genetic algorithm.
- To form a function of stalagmite expression.
- To plot the graph for unbounded condition and then for constrained one.
- 3. Method:
- Optimization: It is basically an area of study that deals with how to minimize or maximize a particular function which depends on different input parameters. Here we get optimum output depending upon the constraints or some criteria. It is a process which executed iteratively by comparing various solutions until an optimum or satisfactory solution is found.
- Stalagmite function: Stalagmite function is a mathematical model which help to generate maxima and minima. It has local maxima and minima. Also, it has global maxima and that is the objective of the study.
- Genetic algorithm:
- Genetic algorithm is based on random sampling, that means, we start with random input based on input we evaluate the function, then comes the concept of natural selection, where we take two-generation we basically mutate them, do cross over then we create a next-generation which is hopefully better.
- Genetic algorithm function which is in MATLAB always minimize the function.
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.
clear
close all
clc
% defining our search space. We dont want the values of x&y to go beyound
% the 0 or 0.6, but the ga dont know that the values can go below 0.6 or 0.
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
limit = 50; % to run the ga 50 times, this helps us to visualize how the algorith is performing
% crreating a 2D mesh
[xx, yy] = meshgrid(x, y);
% creating the stalagmite function
for i = 1 : length(xx)
for j = 1 : length(yy)
input_vector(1) = xx(i,j); % is an array for each value of x&y
input_vector(2) = yy(i,j); % it is a row vector
f(i,j) = stalagmite(input_vector);
end
end
%after executing the program it is observed that the ga gives random values
%every time we run it and this is not useful as we need one optimum value.
%To show how the random values are getting generated the following plots
%are used.
for i = 1 : limit
[inputs ,fopt(i)] = ga(@stalagmite,2);
x_opt(i) = inputs(1); % as stalagmite requires 2 inputs 1 is x & 2nd is y
y_opt(i) = inputs(2);
end
tic % to measure the time taken
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(x_opt,y_opt,fopt,'marker','o','markersize',10)
title('Unbounded input')
subplot(2,1,2)
plot(fopt)
xlabel('iteration')
ylabel('function minimum')
study_time = toc;
tic
% study 2-perfomring upper and lower bound, for bounded plot
for i = 1 : limit
[inputs ,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
x_opt(i) = inputs(1);
y_opt(i) = inputs(2);
end
study_time2 = toc;
figure(2)
subplot(2,1,1);
hold on
surfc(x,y,f);
shading interp
plot3(x_opt,y_opt,fopt,'marker','o','markersize',5);
title('bounded input');
subplot(2,1,2);
plot(fopt);
xlabel('iteration');
ylabel('function minimum');
% study 3 = increasing the population
options = optimoptions('ga');
options = optimoptions(options,'populationsize',170);
tic
% study 2-perfomring upper and lower bound, for bounded plot
for i = 1 : limit
[inputs ,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
x_opt(i) = inputs(1);
y_opt(i) = inputs(2);
end
study_time3 = toc;
figure(3)
subplot(2,1,1);
hold on
surfc(x,y,f);
shading interp
plot3(x_opt,y_opt,fopt,'marker','o','markersize',5);
title('bounded input');
subplot(2,1,2);
plot(fopt);
xlabel('iteration');
ylabel('function minimum');
|
The output of 1st study case, where no bounds are there and not constrained which will give different output values each time the code is executed. Here we can see, we plot the stalagmite function, we can see minimum values, ga is trying to plot them and those are all over the place. This happening because of random sampling. In 2nd plot, x is interesting during each iteration and because of this, this result is not of use. |
- To solve the above issue, we need to bound the lower and upper values and that is done by the following syntax.
x = ga(fun,nvars,A,b,Aeq,beq)
- This finds a local minimum x to func, subject to the linear equalities Aeq*x = beq and A*x>=b.
- Set A=[] and b=[] if no linear inequalities exist.
- This is done in study case 2.
|
The bounded plot got after study case 2. |
- Study case 3:
- In addition to bound we are going to change the initial population size. As the genetic algorithm uses Brandon sample, so by default it takes 50 as a number of samples. Since there are 2 variables there are 50sets of two variables and that is our population. If the population is small then it will give local minuma and global minima. But now we are going to change the population. The drawback of this is that it slow down the ga.
|
Plot 3, after optimization. |
- Stalagmite function:
- The code shown below gives an idea about the stalagmite function. This particular step is called a function definition and 'f' is the term where we are storing the output.
- Command for the '-' sign is given and explained as well.

function [f] = stalagmite(input_vector)
% input_vector has 1 values of x&y each to seprate them use (1)&(2)
x = input_vector(1);
y = input_vector(2);
f1x = (sin(5.1*pi*x + 0.5)).^6;
f1y = (sin(5.1*pi*y + 0.5)).^6;
f2x = exp(-4*log(2)*(x-0.0667).^2/0.64);
f2y = exp(-4*log(2)*(y-0.0667).^2/0.64);
f = -f1x*f1y*f2x*f2y;
% ga always give the minima values, if the '-' is avoided then in the output all minima values can be seen
% in order to get maxima values '-' sign is necessary.
end
- 4. Learning Outcome:
- Understanding the Genetic algorithm and its uses.
- Forming a function for stalagmite expression.
- Use of upper and lower bounds to constrain the values.
- Plotting for maxima values.
- 5. Conclusion:
- Genetic algorithm always uses random values and it helps to get the best possible outcome or satisfying outcome.
- Stalagmite function always gives minima values but for the purpose maxima values has been taken.
- Reference:
https://skill-lync.com/knowledgebase/genetic-algorithm-explained
https://www.tutorialspoint.com/genetic_algorithms/genetic_algorithms_introduction.htm
https://in.mathworks.com/help/gads/ga.html#mw_4a8bfdb9-7c4c-4302-8f47-d260b7a43e26


