USING GENETIC ALGORITHM TO OPTIMIZE THE RESULTS OF A STALAGMITE FUNCTION
OBJECTIVE
To determine the global maxima of the stalgamite function using genetic algorithm.
GENETIC ALGORITHM (ga)
ga is a in-bulit funcion in matlab to calculate the global minima of a function.
It follows the theory of \'survival of the fittest\'.
Random population is created from the inputs and these population are tested for their fitness.
The best values are selected to generate next population.
Then the crossover is done to get the children population.
Lastly, it takes the children population values to run the funcation and decide whether end condition is satisfied.
These four steps are followed for every generation, untill the optimized output is achieved.
syntax of the ga
x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options)
The ga always gives the global minima value of th function and so the stalgamite function is modified such that ga gives the global maximum value.
STALGAMITE FUNCTION
A stalagmite is an upward-growing mound of mineral deposits that have precipitated from water dripping onto the floor of a cave. Most stalagmites have rounded or flattened tips. There are many other types of mineral formations found in caves.
CODE FOR STALGMITE FUNCTION.
% stalgamite function
function[f] = stalgamite(inputs)
x = inputs(1);
y = inputs(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
In this code the final value is multiplied by negative sign(-) to give the global maximim value, when optimized by the ga function
CODE FOR OPTIMIZING THE STALGMITE FUNCITON USING THE ga FUNCTION.
close all
clc
% x and y values
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
[xx,yy] = meshgrid(x,y);
% loop for z values
for i = 1: length(xx)
for j = 1: length(yy)
inputs(1) = xx(i,j);
inputs(2) = yy(i,j);
z(i,j) = stalgamite(inputs);
end
end
% case 1: variables without bounds
count = 50;
tic
for i = 1:count
[inputs_opt,f_opt(i)] = ga(@stalgamite,2);
x_opt(i) = inputs_opt(1);
y_opt(i) = inputs_opt(2);
end
time1 = toc;
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,z)
shading interp
xlabel(\'x-axis\')
ylabel(\'y-axis\')
plot3(x_opt,y_opt,f_opt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\')
title(\'Unbounded inputs\')
subplot(2,1,2)
plot(f_opt)
xlabel(\'Iterations\')
ylabel(\'Function maximum\')
% case 2: variables with bounds
tic
for i = 1:count
[inputs_opt1,f_opt1(i)] = ga(@stalgamite,2,[],[],[],[],[0;0],[1;1]);
x_opt1(i) = inputs_opt1(1);
y_opt1(i) = inputs_opt1(2);
end
time2 = toc;
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,z)
shading interp
xlabel(\'x-axis\')
ylabel(\'y-axis\')
plot3(x_opt1,y_opt1,f_opt1,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\')
title(\'Bounded inputs\')
subplot(2,1,2)
plot(f_opt1)
xlabel(\'Iterations\')
ylabel(\'Function maximum\')
% case 3: increased population
options = gaoptimset(\'populationsize\',200);
tic
for i = 1:count
[inputs_opt2,f_opt2(i)] = ga(@stalgamite,2,[],[],[],[],[0;0],[1;1],[],[],options);
x_opt2(i) = inputs_opt2(1);
y_opt2(i) = inputs_opt2(2);
end
time3 = toc;
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,z)
shading interp
xlabel(\'x-axis\')
ylabel(\'y-axis\')
plot3(x_opt2,y_opt2,f_opt2,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\')
title(\'Bounded inputs\')
subplot(2,1,2)
plot(f_opt2)
axis([0 50 -2 0])
xlabel(\'Iterations\')
ylabel(\'Function maximum\')
CASE 1 INPUTS WITHOUT ANY BOUNDS.
To plot the stalgamites the x and y values are created in a array fromat and then by using the meshgrid function the [xx,yy] values are created . xx and yy values ae passed on as inputs to the stalgamite funciton to get the output z values. These three values are used to create surface plot.
In this case study, the inputs are given to ga function without any upper and lower bounds, no of generation is taken as 50. The optimum value is plotted at each iteration and its changes are recorded.
It is clearly seen that, for each iteration the global maximum value varies a lot. This is not a optimum solution .
CASE 2 INPUTS WITH BOUNDS.
In case study 2, the inputs are given with an upper and lower bound values (0-1). This boundary limit helps the ga to narrow down its search annd produce much more optimized results.
The optimum output varies between 1 and 0.5.
CASE 3 INPUTS WITH INCREASED POPULATION.
To increase the accuracy of the ga funciton , the population size of a generation is increased to 200. This gives better result but takes longer duration of time for calculation.
From the results of the test it is clearly seen that bounded inputs with increased population size gives better solution.