Project

Optimization of Stalagmite Function

Genetic Algorithm:

A genetic algorithm is a heuristic search method used in artificial intelligence and computing. It is used for finding optimized solutions to search problems based on the theory of natural selection (Charles Darwin)and evolutionary biology. Genetic algorithms are excellent for searching through large and complex data sets. They are considered capable of finding reasonable solutions to complex issues as they are highly capable of solving unconstrained and constrained optimization issues. A genetic algorithm makes uses of techniques inspired from evolutionary biology such as selection, mutation, inheritance and recombination to solve a problem. The most commonly employed method in genetic algorithms is to create a group of individuals randomly from a given population.

A genetic algorithm differs from a classical, derivative-based, optimization algorithm in two ways:

  • A genetic algorithm generates a population of points in each iteration, whereas a classical algorithm generates a single point at each iteration.
  • A genetic algorithm selects the next population by computation using random number generators, whereas a classical algorithm selects the next point by deterministic computation.

In this task, we've to maximize(optimize) a function named stalagmite using GA algorithm. First we're creating the plot for the Stalagmite function based on the expression:

`f1 = (sin(5.1*pi*x+0.5))^6` ,`f2 = (sin(5.1*pi*y+0.5))^6`,

`f3 = exp((-4*log(2)*(x-0.0667)^2)/(0.64))`,`f4 = exp((-4*log(2)*(y-0.0667)^2)/(0.64))`

`f = f1*f2*f3*f4`

where x and y are the variables where we desire to find the max. value of the function. First we define the values of x and y and then create a 2D array using "meshgrid" command. For iteration purpose we're perporming 100 iterations. Now, to get value of f at given inputs of x &y, we create a for loop which executes/calls a function with name 'stalagmite' for each and every value of x and y resp. To optimize this function we use an inbulit optimizing tool i.e. ga 'genetic algorithm'. Now the ultimate purpose of this tool is to minimize the fitnessfunction in this case stalagmite, an dour aim is to maximize that very function. And for this very reason we're modifying our function where we calculate `f = 1/(1+f)`, which gives us the max. value of f.

In the first study we optimize the stalagmite function using the ga tool and the syntax for the same is as: x = ga(fun,nvars), where fun is the function handle or the function to be optimized and nvars are the no. of variables i.e. 2 in this case. As discussed ealier, the Genetic Algorithm does the selection based on random samples , hence in the first study, the ga produces the values of x and y exceeding the specified range i.e. 0 to 0.6.

The first plot represents stalagmite formed using the 'surfc' command, where the inputs are the 2D arrays so as to create the surface comprising of x & y data alongwith value of the stalagmite function.The second plot in the above figure, represents maximized (optimized) values 'fopt' for the stalagmite function over those 100 iterations.

So, to the tell the ga regarding the search space, we pass some more parameters during ga function call in the second study.

inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]) consider the following arguement

x = ga(fun,nvars,A,b,Aeq,beq,lb,ub), where 'A' represent the linear inequalities in the form of A*x ≤ b and Aeq does the same for linear eqaulities in the form of Aeq*x = beq as in this case there no either of them possibly occuring we're passing them as empty vectors. [0;0],[1;1] represents the lower bound and the upper bound for the variables i.e. the search space. After doing the above steps we get:

Still after modifying the search space, there still exist some local points along with global maxima point. This is certainly happening because of the lesser no. of the population creating the second generation of the individuals. this issue is prevented by increasing the population size but it may increasing the time of the execution of this third study. In order to optimize the function we use, options = optimoptions('ga') command which follows the syntax 'options = optimoptions(SolverName)' which returns a set of default options for the 'ga' solver. To increase the population consider the syntax 'options = optimoptions(SolverName,Name,Value)' which returns options with the named parameters altered with the specified values. Here the population size is set to 500 by trail and error process. With increase in the population size the optimization proceeds as:

The optimized value or the maximum value of the function is found to be 0.5, and it is occuring same for the 100 iterations as the population size is increased. Population is a subset of solutions in the current generation. It can also be defined as a set of chromosomes. There are several things to be kept in mind when dealing with GA population −The diversity of the population should be maintained otherwise it might lead to premature convergence.The population size should not be kept very large as it can cause a GA to slow down, while a smaller population might not be enough for a good mating pool. Therefore, an optimal population size needs to be decided by trial and error.The population is usually defined as a two dimensional array of – size population, size x, chromosome size.

Code for Stalagmite Optimization:

clear all
close all
clc

% Input Parameters
% Search Space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
% Creating a 2D Array
[xx,yy] = meshgrid(x,y);
num_case = 100; % No. of Iterations
for i = 1:length(x)
    for j = 1:length(y) 
        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
study_time_1 = toc;

figure(1)
subplot(2,1,1)
hold on
surfc(xx, yy, f)
shading interp
xlabel 'X data'
ylabel 'Y data'
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','k')
title('Unbounded Inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function Maximum')

% 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
study_time_2 = toc;

figure(2)
subplot(2,1,1)
hold on
surfc(xx, yy, f)
shading interp
xlabel 'X data'
ylabel 'Y data'
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','k')
title('Bounded Inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function Maximum')

% Study 3
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',450);
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
study_time_3 = toc;

figure(3)
subplot(2,1,1)
hold on
surfc(xx, yy, f)
shading interp
xlabel 'X data'
ylabel 'Y data'
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','k')
title('Bounded Inputs with Increased Population Size')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function Maximum')

max_value = [fopt];




Stalagmite Function:

% Stalagmite Function
function [f] = stalagmite(input_vector)

 f1 = (sin(5.1*pi*input_vector(1)+0.5))^6;
 f2 = (sin(5.1*pi*input_vector(2)+0.5))^6;
 f3 = exp((-4*log(2)*(input_vector(1)-0.0667)^2)/(0.64));
 f4 = exp((-4*log(2)*(input_vector(2)-0.0667)^2)/(0.64));
  f = (f1.*f2.*f3.*f4);
  f = 1/(1+f); % used to maximize the function
end
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.