*. what is genetic algorithm (ga)? and also explain the syntax for ga.
Genetic Algorithm: It is the one of the method,given by Charles Darwin, to solve the regulated and unregulated optimazation problem that is purely based on natural selection process. At every step, ga automatically selcetion individuals as parents to produce children for the upcoming generation.
There are 3 types of rules used by GA to form the upcoming generation from current one.
1.Selection: select individuals, known as parents, to produce upcoming generation.
2.Crossover: it actually comibnes to parents to form a children for next gen.
3.Mutation: it apply random changes to parents to produce children.
*. Syntax of Genetic Algorithm?
As per the matlab, these are the syntax's for ga based on different application.
x=ga(fun,nvars) % fun=function, nvars= num. of variables
x=ga(fun,nvars,A,b) % A,b= linear inqualities
x=ga(fun,nvars,A,b,Aeq,beq) % Aeq,beq= linear equalities
x=ga(fun,nvars,A,b,Aeq,beq,lb,ub) %lb,ub = lower and upper bound respectively
x=ga(fun,nvars,A,b,Aeq,beq,lb,up,nonlcon) % subjects to minimization to the constraints define in nonlcon.
x=ga(fun,nvars,A,b,Aeq,beq,lb,up,nonlcon,options) % it minimizes the defualt optimazation parameters replaced by value in options.
x=ga(fun,nvars,A,b,Aeq,beq,lb,up,nonlcon,intcon) % variables listed in intcon takes integer values
x=ga(problem) % it finds the minimum for the given problem
[x,fval]=ga(___) %fval, the value of fitness fuction at x.
[x,fval,exitflag,output]=ga(__) %exitflag= an integer identifying the reason why algorithm terminated, output= contains output from each generation.
[x,fval,exitflag,output,population,scores]=ga(__) % it returns as matrix population in which rows are the final population, and vector scores, the scores of final population.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1. Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
Note: ga by default is made to find the minimum value of any function, in order to get the Maximum, need to make some chages in the function itself.
#A. program to find the global maxima.
clear all
close all
clc
% defining range of x and y for ga to work in using linspace.
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_iteration=50;
[xx,yy]=meshgrid(x,y); % using meshgeid, the data gata coverted into 2 dimesional.
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); % calling the stalagmite fuction from seperate data file.
end
end
figure(1)
surfc(xx,yy,-f) % to plot the 3d data, purposefully use -f to find the maxima
title('fmaxima')
shading interp % to neglect the shading effect
[inputs, fval]=ga(@stalagmite,2); % calling ga to find the x/y and f maxima.
%labeling the plot.
xlabel('xvalue')
ylabel('yvalue')
%case 1: program for unbounded condition.
tic
for i= 1:num_iteration
[inputs,fopt(i)]=ga(@stalagmite,2); %using ga syntax, finds a local unconstrained minimum to the objective function
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
toc % time takes to execute the case 1
figure(2)
subplot(2,1,1) % using subplot fuction to plot multiple plots in one figure window.
hold on
surfc(xx,yy,-f)
shading interp % to neglect the shading effect
plot3(xopt,yopt,-fopt,'marker','o','MarkerSize',4,'MarkerFaceColor','r') % to plot 3 coordinates (xopt,yopt,-fopt), used 'plot3' command.
title('unbounded inputs')
subplot(2,1,2)
plot(-fopt)
% labeling the x and y with their headings
xlabel('no of iteration')
ylabel('minimum of function')
%case-2 program for bounded inputs.
tic
for i=1:num_iteration
[inputs,fopt(i)]= ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
toc %time taken to execute the case 2
figure(3)
subplot(3,1,1) % using subplot fuction to plot multiple plots in one figure window.
hold on
surfc(xx,yy,-f)
shading interp % to neglect the shading effect
plot3(xopt,yopt,-fopt,'marker','o','MarkerSize',4,'MarkerFaceColor','r') % to plot 3 coordinates (xopt,yopt,-fopt), used 'plot3' command.
title('bounded inputs')
subplot(3,1,3)
plot(-fopt)
% labeling the x and y with their headings
xlabel('no of iteration')
ylabel('minimum of function')
%case 3 program by increasing no. of iterations.
options=optimoptions('ga');
options=optimoptions(options,'PopulationSize',150);
tic
for i=1:num_iteration
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
toc %time taken to execute the case 3
figure(4)
subplot(3,1,1) % using subplot fuction to plot multiple plots in one figure window.
hold on
surfc(xx,yy,-f)
shading interp % to neglect the shading effect
plot3(xopt,yopt,-fopt,'marker','o','MarkerSize',4,'MarkerFaceColor','r')% to plot 3 coordinates (xopt,yopt,-fopt), used 'plot3' command.
title('bounded inputs[changed population size]')
subplot(3,1,3) % using subplot fuction to plot multiple plots in one figure window.
plot(-fopt)
% labeling the x and y with their headings
xlabel('iteration')
ylabel('minimum of function')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# program for Stalagmite function.
function[stalagmite]=stalagmite_function(input_vector)
x=input_vector(1);
y=input_vector(2);
f1_x=(sin(5.1*pi*x+0.5))^6;
f1_y=(sin(5.1*pi*y+0.5))^6;
f2_x=exp((-4*-0.6931).*(x-0.0667)^2/0.64);
f2_y=exp((-4*-0.6931).*(y-0.0667)^2/0.64);
stalagmite=-(f1_x.*f2_x.*f1_y.*f2_y); % using negative function to find global maxima
by just changing the output sign from negative to positive.
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%