To optimize the stalagmite function to get the global maxima value using genetic algorithm and various cases in MATLAB
OBJECTIVE: This code is the main code for the calculation of the generic algorith problem using stalagamite functions and the knowledge of loops.
The main oobjective of this code is to find the global maxima of a function and use this knnowledge to optimise stalagmite function. The code written also makes use of various terminologies such as function definition, function call and parameter passing.
The main ideology behind this function is to familiarize oneself with he usage of stalagamite function.
METHODOLOGY:
In computer science and operations research, a Genetic Algorithm (GA) is a metaheuristic inspired by the process of natural selection that belongs to the larger class of evolutionary algorithms (EA). Genetic algorithms are commonly used to generate high-quality solutions to optimization and search problems by relying on bio-inspired operators such as mutation, crossover and selection. John Holland introduced genetic algorithms in 1960 based on the concept of Darwin’s theory of evolution; afterwards, his student David E. Goldberg extended GA in 1989.
Optimisation usage of genetic algorithm
In a genetic algorithm, a population of candidate solutions (called individuals, creatures, or phenotypes) to an optimization problem is evolved toward better solutions. Each candidate solution has a set of properties (its chromosomes or genotype) which can be mutated and altered; traditionally, solutions are represented in binary as strings of 0s and 1s, but other encodings are also possible.
The evolution usually starts from a population of randomly generated individuals, and is an iterative process, with the population in each iteration called a generation. In each generation, the fitness of every individual in the population is evaluated; the fitness is usually the value of the objective function in the optimization problem being solved. The more fit individuals are stochastically selected from the current population, and each individual\'s genome is modified (recombined and possibly randomly mutated) to form a new generation. The new generation of candidate solutions is then used in the next iteration of the algorithm. Commonly, the algorithm terminates when either a maximum number of generations has been produced, or a satisfactory fitness level has been reached for the population.
For the given problem we have used genetic algorith and coded the following in MATLAB to obtain certain results which were for certaim unbounded plots.
MAIN CODE:
close all
clear all
clc
% Input variables
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_case = 250; % No of iteration for the ga function
% Creating An Array
[xx, yy] = meshgrid(x,y);
% For loop for passing the stalagmite function
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_func1(input_vector);
end
end
surfc(x,y,-f)
colorbar
xlabel(\'x Values\')
ylabel(\'y Values\')
zlabel(\'Function Values\')
title(\'Stalagmite Function\')
shading interp
[x_output,fval] = ga(@stalagmite_func1,2);
% CASE STUDY 1
tic
for i = 1:num_case
[inputs, f_optima(i)] = ga(@stalagmite_func1,2);
x_optima(i) = inputs(1);
y_optima(i) = inputs(2);
end
case1 = toc;
figure(2)
subplot(2,1,1)
surfc(x,y,-f)
shading interp
xlabel(\'x Values\')
ylabel(\'y values\')
zlabel(\'Function Values\')
hold on
plot3(x_optima,y_optima,-f_optima,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'g\')
title(\'Unbounded Input\')
subplot(2,1,2)
plot(-f_optima);
xlabel(\'No Of Iterations\')
ylabel(\'Maximum Value Of The Function\')
% CASE STUDY 2
tic
for i = 1:num_case
[inputs, f_optima(i)] = ga(@stalagmite_func1,2,[],[],[],[],[0;0],[0.5;0.5]);
x_optima(i) = inputs(1);
y_optima(i) = inputs(2);
end
case2 = toc;
figure(3)
subplot(2,1,1)
surfc(x,y,-f)
shading interp
xlabel(\'x Values\')
ylabel(\'y values\')
zlabel(\'Function Values\')
hold on
plot3(x_optima,y_optima,-f_optima,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'g\')
title(\'Unbounded Input\')
subplot(2,1,2)
plot(-f_optima);
xlabel(\'No Of Iterations\')
ylabel(\'Maximum Value Of The Function\')
% CASE STUDY 3
options = optimoptions(\'ga\'); % Default values for the ga Function
options = optimoptions(options, \'PopulationSize\', 250); % Setting the population size to 250
tic
for i=1:num_case
[inputs,f_optima(i)] = ga(@stalagmite_func1,2,[],[],[],[],[0;0],[0.5;0.5],[],[],options);
x_optima(i) = inputs(1);
y_optima(i) = inputs(2);
end
case3 = toc;
figure(4)
subplot(2,1,1)
surfc(x,y,-f)
shading interp
xlabel(\'x Values\')
ylabel(\'y values\')
zlabel(\'Function Values\')
hold on
plot3(x_optima,y_optima,-f_optima,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'g\')
title(\'Unbounded Input\')
subplot(2,1,2)
plot(-f_optima);
xlabel(\'No Of Iterations\')
ylabel(\'Maximum Value Of The Function\')
FUNCTION DEFINITION OF STALAGMITE FUNCTION:
function [f] = stalagmite_func1(input_vector)
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;
end
RESULTS:
The global maxima for my code occurs at [x,y]=[0.06846,0.2617] for a maximum value of 0.8454.
CASE STUDY 1
In this case we can clearly see the randomness of the plots created by the stalagmite function using genetic algorithm.
CASE STUDY 2
In this case we took the upper and lower bounds of the function and plotted them against the values that are calculated by the function. This plot shows us the ccoverage and dthe range of the global maxima and its peaks. This is the region that is covered by the function and its values.
CASE STUDY 3
The third case was aimed to show the covergence of the data to one final value of global maxima as opposed to a local maxima. This value can be seen from the graph that has convered after varying the population size and hovered to a value of 1 (0.9999.....).
ERRORS CAUSED:
1. The main error that was casued due to the addition of another function due to which some of the variables that i had definied were being stated as undeclared.
This error was rectified by removing the f_func = stalagmite(xx,yy) statement.
2. The function inverse_stalagmite was unnessesarily added to the code which got me this error.
This ws then rectified by remooving the function as it was not needed.
CONCLUSION:
The genetic algorithm has been studied and all the plots are deruved and understood as to their existence is concerned.