AIM:
* To write code in matlab to stalagmite function and find the global maxima of the function.
*To understand more about genetic algoritham and to determine how it works.
MAIN PROGRAM :
clear all
close all
clc
% defining search space
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
% creating second mesh
[xx,yy]=meshgrid(x,y);
for i=1:length(xx)
for j=1:length(yy)
inputs(1)=xx(i,j);
inputs(2)=yy(i,j);
F(i,j)=stalgmite_fun(inputs);
end
end
surfc(x,y,F)
% study-1 statistical behaviour of ga
num_cases=50;
tic
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalgmite_fun,2);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study1_time=toc;
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,-F)
shading interp
plot3(xopt,yopt,-fopt,'Marker','o','markersize',5,'markerfacecolor','r')
title('unbounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('functions maxima')
% statistical bahaviour with upper bound and lower bound
tic
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalgmite_fun,2,[],[],[],[],[0;0],[0.3;0.3],[],[]);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study2_time=toc;
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,-F)
shading interp
plot3(xopt,yopt,-fopt,'Marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('functions maxima')
%case3 increasing no of iterations
tic
options=optimoptions('ga');
options=optimoptions(options,'populationsize',500);
for i=1:num_cases
[inputs,fopt(i)] = ga(@stalgmite_fun,2,[],[],[],[],[0;0],[0.3;0.3],[],options);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study3_time=toc;
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,-F)
shading interp
plot3(xopt,yopt,-fopt,'Marker','o','markersize',5,'markerfacecolor','b')
title('bounded inputs with increased population size')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('functions maxima')
FUNCTION CODE:
function [f] = stalgmite_fun(inputs)
x=inputs(1);
y=inputs(2);
term1=(sin((5.1*pi*x)+0.5)).^6;
term2=(sin((5.1*pi*y)+0.5)).^6;
term3 = exp((-4)*log(2)*((x-0.0667).^2)/0.64);
term4 = exp((-4)*log(2)*((y-0.0667).^2)/0.64);
f = -(term1*term2*term3*term4);
end
STEP BY STEP EXPLANATION:
> First of all we write function code todefine the stalagmite function ,here we break down the formula intofpor terms for convenience.
> To write main code were we define two inputs xand y both of which are one dimensional equally spaced array containing 150 elements ,here we use meshgrid command to create it.
> Now use the for loop to input values of 2D array in stalagmite ,here we can see the every time the code executed different values stalagmite function are obtained that why we introducing another loop into the code
>ga fun used for the inputing the values into the programm,here 3 studys to optimize the function so that we can accurately find the global maxima,negative sign is provided to get the maxima otherwise it could be negative.
>In first case evaluate the all possible global maxima for all the iterations using a loop,to plot two in one figure using subplot command,'surf' command is used for x v/s y graph and for function it get unbounded values.
> In second case put upper bound and lower bound on ga as getting the same asa that of the first. in third case we increase the iterations,in ga after successive iterations the population evolves towards on optimal.
>Run the program to get the plots on screen and saved it.
SYNTAX OF GA :
* x=ga(fun,nvars)
fun -function name/function handle
nvars -number of variables
* x=ga(fun,nvars,A,b)
A -linear inequality constrains as real matrix
b -linear inequality constrainsas real vector
* x=ga(fun,nvars,A,b,Aeq,beq)
Aeq,beq -linear equalities of A&b
* x=ga(fun,nvars,A,b,Aeq,beq,lb,ub)
lb -lower bound
ub -upper bound
* x=ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonicon)
nonicon -non linewar constrains specified as a function handle or a function name
* x=ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonicon,options)
options -optimisation options,specified as output optimisation
REORT:
Errors are detected on plot of figure one and corrected the mistakes with commands to make new figure and ploted it .To get a population study of the generation using a ga command program and plotted the graph as shown the files .it easiest and essential to new or upcoming generations make the sipler to other now a days data analysis study .