% stalagmite function program.
function [f] = stalagmite(input_vector)
a = input_vector(1);
b = input_vector(2);
f1_x = [sin((5.1*pi*a)+0.5)].^6;
f1_y = [sin((5.1*pi*b)+0.5)].^6;
f2_x = exp(-4*log(2)*(((a-0.0667)^2)/(0.64)));
f2_y = exp(-4*log(2)*(((b-0.0667)^2)/(0.64)));
f = -(f1_x*f1_y*f2_x*f2_y);
end
% First iteration coad
clear all
close all
clc
% x and y inputs
x = linspace(0,0.6,150); % X value inputs
y = linspace(0,0.6,150); % Y value inputs
% Even though search space is defined is GA Algorithm do not run within
% this search place
[xx,yy] = meshgrid(x,y);
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(input_vector);
end
end
num_cases = 50; % GA runs for 50 iterstions
tic % Study time coun starts
for i = 1:num_cases
[inputs,f_opt(i)] = ga(@stalagmite,2); % GA function is used to run the stalagmite data and give the optimal solutions.
x_opt(i) = inputs(1);
y_opt(i) = inputs(2);
opt_f(i) = -f_opt(i); % ga gives us Minima value. inverse f_opt is taken for Maxima value.
end
study1_time = toc % study time count end
subplot(2,1,1)
hold on
surfc(xx,yy,f) % Generation of stalagmite function.
shading interp % To bring visually smooth curve.
plot3(x_opt,y_opt,f_opt,'marker','o','MarkerSize',4,'MarkerFaceColor','r') % plotting the optimum values with a red dot marker along the stalagmite 3D curve.
title('Unbounded inputs')
subplot(2,1,2)
plot(opt_f) % plot of opt_f for every iterations
xlabel('Number of iterations') % lable the axis
ylabel('Function Max') % Lable the axis
% Second iteration coad.
clear all
close all
clc
% x and y inputs
x = linspace(0,0.6,150); % X value inputs
y = linspace(0,0.6,150); % Y value inputs
% Even though search space is defined is GA Algorithm do not run within
% this search place
[xx,yy] = meshgrid(x,y);
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(input_vector); % Stalagmite function is called for every loop and output is stored in f.
end
end
num_cases = 100; % GA runs for 100 iterstions
tic % Study time coun starts
for i = 1:num_cases
% objective function is stalagmite and the number of variable is two
% A,B,Aeq and Beq = [],as their is no linear equalities and
% inequalities constrains in this function.
% Lower boundary of variable x and y in ga alogrithm is set as [0,0]
% upper boundary of variable x and y in ga alogrithm is set as [0.6,0.6]
[inputs,f_opt(i)] = ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6]); % GA function is used to run the stalagmite data and give the optimal solutions.
x_opt(i) = inputs(1);
y_opt(i) = inputs(2);
opt_f(i) = -f_opt(i); % ga gives us Minima value. inverse f_opt is taken for Maxima value.
end
study2_time = toc % study time count end
subplot(2,1,1)
hold on
surfc(xx,yy,f) % Generation of stalagmite function.
shading interp % To bring visually smooth curve.
plot3(x_opt,y_opt,f_opt,'marker','o','MarkerSize',4,'MarkerFaceColor','r') % plotting the optimum values with a red dot marker along the stalagmite 3D curve.
title('With Bounded inputs')
subplot(2,1,2)
plot(opt_f) % plot of opt_f for every iterations
xlabel('Number of iterations') % lable the axis
ylabel('Function Max') % Lable the axis
% Third iteration coad.
clear all
close all
clc
% x and y inputs
x = linspace(0,0.6,150); % X value inputs
y = linspace(0,0.6,150); % Y value inputs
% Even though search space is defined is GA Algorithm do not run within
% this search place
[xx,yy] = meshgrid(x,y);
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(input_vector); % Stalagmite function is called for every loop and output is stored in f.
end
end
num_cases = 150; % GA runs for 150 iterstions
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',450);
tic % Study time coun starts
for i = 1:num_cases
% objective function is stalagmite and the number of variable is two
% A,B,Aeq and Beq = [],as their is no linear equalities and
% inequalities constrains in this function.
% Lower boundary of variable x and y in ga alogrithm is set as [0,0]
% upper boundary of variable x and y in ga alogrithm is set as [0.6,0.6]
% population size is defined by using options in the algorithm.
[inputs,f_opt(i)] = ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6],[],options); % GA function is used to run the stalagmite data and give the optimal solutions.
x_opt(i) = inputs(1);
y_opt(i) = inputs(2);
opt_f(i) = -f_opt(i); % ga gives us Minima value. inverse f_opt is taken for Maxima value.
end
study3_time = toc % study time count end
subplot(2,1,1)
hold on
surfc(xx,yy,f) % Generation of stalagmite function.
shading interp % To bring visually smooth curve.
plot3(x_opt,y_opt,f_opt,'marker','o','MarkerSize',5,'MarkerFaceColor','r') % plotting the optimum values with a red dot marker along the stalagmite 3D curve.
title('Bounded inputs with increased population size')
subplot(2,1,2)
plot(opt_f) % plot of opt_f for every iterations
xlabel('Number of iterations') % lable the axis
ylabel('Function Max') % Lable the axis
Report-
Aim- To write coad to calculate global maxima using genetic algorithm, using matlab.
procedure-
* the stalagmite function is seperatly. this function is later called into the main GA
program.
* now we define X and Y inputs using linspace command.
* then the input vectors are transformed into array[xx,yy], so that it can be used to
evaluate functions of two variables and pot three-dimentional mesh/surface plot.
* now the values in the different matrixs created can be put as an input into the
function that we have created, by calling it. by this we can get the value of the
stalagmite function at various values of x and y.
* Now since we have got the value of the function at various values, we now need to
calculate using the ga command, which is an inbuild function in matlab.
* then the total number of time the ga needs to run is defined using num_cases, and
with help of for loop ga is made to run for each iteration and tje value is stored
in an array .
* GA gives the minima value (in built in MATLAB), but we need the maxima
hence we can just inverse the stalagmite function by introducing a 'Negative
sign'(this inverses the stalagmite curve and the globle maximas can be found,
instead minima.
* the total study time is calculated using the 'tic' and 'toc' command at the start
and end of the ga for loop respectivly.
* the results are then ploted using the surfc command and plot command.
* GA is made to run for 3 iterations, with each iteration having bounded inputs,
unbounded inputs and increasing population size.
What is GA-
Genetic algorithms are search based on the concepts of natural selection and
genetics. GAs are subset of a much larger branch of computation known as
evolutionary computation.
errors facing-
1- i forget brackets many times thats why showing error.
2- mistacally i forget '1' in for loops like i write 'i = length' it showing error .
How to overcome errors.
1- by adding bracket error is solved.
2- by checking that line and then corrected the error.
i take a referance from youtube to undastand what is GA how it workes, and take referance from google to solve some issus.