AIM:
To optimise the stalagmite function and find the global maxima of the function using MATLAB
Objectives:
To explain the concept of genetic algorithm and its syntax and also find the global maxima of the function
Genetic Algorithm
A genetic algorithm (GM) is a method for solving both constrained and unconstrained optimization problem based on a natural selection process that mimics biological evolution the algorithm repeatedly modifies a population of individual solution at each step
GA SYNTAX:
The genetic algorithm use three main type of rules at each step to create the next generation from the current population
1] Selection rule select the individuals called parents that contribute to the population at the next generation
2] Crossover rule combine two parents two from children for the next generation
3] Mutation rules apply random changes to individual parents to from children
• X = ga(fun,nvars)
• X = ga (fun,nvars ,A,b)
• X = ga(fun,nvars,A,b,Aeq,beq)
• X = ga(fun,nvars,A,b,beq,lb,ub)
• X = ga(fun,nvars,A,b,beq,lb,ub,nonlcon)
• X = ga((fun,nvars,A,b,beq,lb,ub,nonlcon,option)
• X = ga(fun,nvars,A,b,beq,lb,ub,nonlcon,intcon)
• X = ga(problem)
• [x,fval] = ga(_)
• X,fval,exiting,output] = ga(_)
Working of GA:
1] the algorithm being by creating a random initial population
2] scales the raw fitness scores to convent them into a more usable range of values
3] some of the individuals in the current population that have fitness are chosen at elite
4] children are produced either by marking random changes
5] the algorithm stops when one of the stopping criteria is met
Stopping Criteria:
1] Stopping criteria determines what cause the algorithm to terminates
2] Generation specifies the maximum number of iteration genetic algorithm performs
3] time limit specifies the maximum time in second the genetic algorithm runs before stopping
•Fitness limit
•Stall generation
•Stall time limit
•Stall test
•Function tolerance
•Constraint tolerance
% stalagmite function programing
function [f]=func(input_vector)
a1=(sin(5.1*pi*input_vector(1)+0.5))^6;
a2=(sin(5.1*pi*input_vector(2)+0.5))^6;
a3=exp(-4*log(2)*((input_vector(1)-0.0667)^2)/(0.64));
a4=exp(-4*log(2)*((input_vector(2)-0.0667)^2)/(0.64));
f=-(a1*a2*a3*a4);
end
% code for all three studies
close all
clear all
clc
% input
x = linspace(0,0.6,150)
y = linspace(0,0.6,150)
num_cases = 60
%storing in 2d arry
[xx,yy] = meshgrid(x,y);
% calling the function
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) = func(input_vector);
end
end
% study 1 behaviour
tic
for i = 1:num_cases
[inputs,fopt(i)] = ga(@func,2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
studytime1 = toc
% unbounded inputs
figure (1)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'Marker','o','MarkerSize',5,'MarkerFaceColor','r')
title('unbounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('interations')
ylabel('function maximum')
% study 2 behaviour
tic
for i=1:num_cases
[inputs,fopt(i)] = ga(@func,2,[],[],[],[],[0,0],[0.5,0.5]);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
studytime2 = toc
%bounded inputs
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('iteration')
ylabel('function maximu
% study 3 behaviour
tic
options = optimoptions('ga')
options = optimoptions(options,'PopulationSize',200);
for i=1:num_cases
[inputs,fopt(i)] = ga(@func,2,[],[],[],[],[0;0],[0.5;0.5],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
studytime3 = toc
% optimisation
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('iteration')
ylabel('function maximum')