'ga' represents Genetic Algorithm used for finding global minima from a set of values.It can be used for solving both Bounded and Unbounded conditions.
Syntax for ga:
x = ga (fun,nvars,A,b)
This is the syntax for 'ga' code for unbounded conditions; Here
fun=function
nvars= no of variables
'ga' syntax for bounded condition as follows:
x=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6])
Here 'stalagmite' is the function name
2 is the no of variables
0 & 0.6 is the limits of the x & y inputs.
'ga' syntax for increasing population size and bounded condition:
ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],option)
Here in last part 'option' we will provide our population size as per our requirement.
%study-3 increasing ga iterations
option=optimoptions('ga')
option=optimoptions(option,'populationsize',170)
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],option);
xopt(i)=inputs(1)
yopt(i)=inputs(2)
end
figure(3)
subplot(2,1,1);
hold on
surfc(x,y,-f);
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r');
title('bounded inputs');
subplot(2,1,2);
plot(-fopt);
xlabel('No of iterations');
ylabel('F maximum');
% After running this code I got the exact plot and figure beacuse I increased that population size as 170 and I mentioned the limits also.Plot which I got after running these codes I am attaching along with this
% after case-1 , we didn't get the correct solutions beacuse there was no boundaries. So in study-2 we gave limit to the input values and as per that we modified the 'ga' code and other steps.it is given below:
%study-2 statistical behaviour with upper and lower bounds
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
(modified version of ga code, here we given the limits for x & y inputs)
xopt(i)=inputs(1)
yopt(i)=inputs(2)
end
figure(2)
subplot(2,1,1);
hold on
surfc(x,y,-f);
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r');
title('bounded inputs');
subplot(2,1,2);
plot(-fopt);
xlabel('No of iterations');
ylabel('F maximum');
(all other steps are same as previous case)
%after running these codes I got another plot which was more accurate than 1st case but still this also not gave the exact answer.I am attaching that plot along with this.
%So as study -3 I increased the population size as 170,so the result will be more accurate & according to that I modified the synatx of 'ga' code.If we are not increasing the populationsize ,'ga' automatically took 50 as the population size and we will not get the exact answer.
clear all
close all
clc
% creating 1D array
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_cases=50;
% evaluating the stalagmite function
for i=1:length(x)
for j=1:length(y)
input_vector(1)=x(i);
input_vector(2)=y(j);
f(i,j)=stalagmite(input_vector);
end
end
surfc(x,y,f);
xlabel('xvalue');
ylabel('yvalue');
shading interp
[input,fval]=ga(@stalagmite,2)
% study1-statistical behaviour
for i=1:num_cases (opening for loop for 50 iterations)
[inputs,fopt(i)]=ga(@stalagmite,2);
(Generating Stalgamite function and getting optimum f value)
xopt(i)=inputs(1); (storing x values from the 1st column of inputs)
yopt(i)=inputs(2); (storing y values from the 2nd column of inputs)
end ( ending for loop)
figure(1); (opening figure window)
subplot(2,1,1); (subplot generates multiple plots in a figure)
hold on
surfc(x,y,-f); (creating 3d plot)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r'); (plotting x,y,and optimum f values)
title('Unbounded inputs'); (giving tiltle)
subplot(2,1,2);
xlabel('No of iterations'); (giving name for x axis)
ylabel('F maximum'); (giving name for y axis)
%After running these codes I got one figure with 2 subplots and that I am attaching along with this.But in that bottom plot I didnt get that graph.So I tried to find that error in my code and finally I understood my error and I corrected that.So I am attaching the corrected figure for study-1 also along with this.
clear all (for clearing screen)
close all (for closing all things)
clc (clear screen)
% creating 1D array
x=linspace(0,0.6,150); (giving 150 x values from 0 to 0.6)
y=linspace(0,0.6,150); (giving 150 y values from 0 to 0.6)
% evaluating the stalagmite function
for i=1:length(x) (opening 1st for loop)
for j=1:length(y) (opening 2nd for loop)
input_vector(1)=x(i); (assingning eaach x values to input_vector(1))
input_vector(2)=y(j); (assingning eaach x values to input_vector(2))
f(i,j)=stalagmite(input_vector); (opening stalgamite function for finding maximum value)
end ( ending 1st for loop)
end ( ending 2nd for loop)
surfc(x,y,f); (for plotting 3d graph with inputs x,y and f)
xlabel('xvalue'); (labelling x axis as 'xvalue')
ylabel('yvalue'); (labelling y axis as 'yvalue')
shading interp (removing lines from the plot)
[input,fval]=ga(@stalagmite,2); (introducing 'ga' code for getting minimum value of the stalgamite function)
%Program for 'stalgamite' function we written seperately and that is given below:
function[f]=stalagmite(input_vector) (opening stalgamite function)
x=input_vector(1); ( assigning 1st column of input_vector as x)
y=input_vector(2); ( assigning 2nd column of input_vector as y)
f1x=(sin(5.1*3.14*x+0.5))^6; (defining f1x as per the given datas)
f1y= (sin(5.1*3.14*y+0.5))^6; (defining f1y as per the given datas)
f2x=exp(-4*log(2)*(x-0.0667)^2/0.64); (defining f2x as per the given datas)
f2y=exp(-4*log(2)*(y-0.0667)^2/0.64); (defining f2y as per the given datas)
f=-(f1x*f1y*f2x*f2y); (defining f=f1x*f1y*f2x*f2y and negative sign used for converting the plotting direction)
end (ending the function)
%after running these codes I got an inverted 3d plot which I attached with this and from that we got minimum value and if we change its sign we get maximum value.But when I run the code several time I got diffrent different values.So I tried to modify this code