Week 4.1 - Genetic Algorithm
GENERIC ALGORITHM
Aim:
Try to understand the concept of genetic algorithm by referring to varius sources like population algorithms.
Population algorithm means algorithm that maintains a entire set of candidate
Solutions, and each solution corresponds to a unique point in the search space .
Some of the population algorithm are generic algorithm.
At each step the generic algorithm uses the current population to create the children that makes up next generation.
Governing Equations:
X = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlvon,options)
Fun = Objective function
nvar = Number of Variables(specified as positive intiger)
A,b = Linear inequality constraints(A=Real matrix, b=Real vector)
Aeq,beq = Linear equality constraints(A = Real matrix, b =vector
Lb,ub = Lower bounds and upper bounds
Nonlcon = Non linear constraints
Options = Optimization options
Objectives of object:
- Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
- Clearly expalin the concept of genetic algorithm in your own words and also explain the syntax for ga in MATLAB in your report.
- Make sure that your code gives the same output, even if it is made to run several times.
- Plot graphs for all 3 studies and for F maximum vs no. of iterations. Title and axes labels are a must, legends could be shown if necessary.
Body of the content:
function [f] = stalagmite(inputs)
x = inputs(1);
y = inputs(2);
f1x = (sin((5.1*pi*x)+0.5))^6;
f1y = (sin((5.1*pi*y)+0.5))^6;
f2x = exp(-4*log(2)*(((x-0.0667)^2)/0.64));
f2y = exp(-4*log(2)*(((y-0.0667)^2)/0.64));
f = -(f1x*f1y*f2x*f2y);
% Deffining the search place
x = linspace(0,0.7,150);
y = linspace(0,0.7,150);
% To create a 2d mesh
[xx,yy] = meshgrid(x,y);
% Assigning input values
for i = 1:length(x)
for j = 1:length(y)
inputs(1) = xx(i,j);
inputs(2) = yy(i,j);
f(i,j) = stalagmite(inputs);
end
end
% creating a variable and assigning the number to which iterations performed in program
num_case = 50;
% f = stagmite(x,y)
% start the timer
tic
% Unbounded Ga study case
for z = 1:num_case;
[inputs,fopt(z)] = ga(@stalagmite,2);
xopt(z) = inputs(1);
yopt(z) = inputs(2);
end
% Tells the time taken to perform the study case
time_study1 = toc;
figure(1)
% creaters a sub plot in the figure
subplot(2,1,1)
hold on
% create a surface
surfc(x,y,-f)
% Varies the color in each line segment and and it gives true line colour across the face
shading interp
plot3(xopt,yopt,-fopt,'marker','o','MarkerSize',5,'MarkerFaceColor','r')% plot 3 command is used for 3 axis
% Marker is used for visible line on the plots
title('Unbounded inputs')
xlabel('x-axis')
ylabel('y-axis')
zlabel('z-axis')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('functions')
tic
% Bounded Ga study case
for z = 1:num_case;
[inputs,fopt(z)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.7;0.7]);
% defines a set of lower and upper bound on the design variables
xopt(z) = inputs(1);
yopt(z) = inputs(2);
end
% Tells the time taken to perform the study case
time_study2 = toc;
figure(2)
% creaters a sub plot in the figure
subplot(2,1,1)
hold on
% create a surface
surfc(x,y,-f)
% Varies the color in each line segment and and it gives true line colour across the face
shading interp
plot3(xopt,yopt,-fopt,'marker','o','MarkerSize',5,'MarkerFaceColor','r')% plot 3 command is used for 3 axis
% Marker is used for visible line on the plots
title('Bounded inputs')
xlabel('x-axis')
ylabel('y-axis')
zlabel('z-axis')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('functions')
tic
% Bounded Ga study with Population size
% Return a set of default options for the solvername so;ver
options = optimoptions('ga');
% Defining population size
options = optimoptions(options,'PopulationSize',300);
for z = 1:num_case;
[inputs,fopt(z)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.7;0.7],[],[],options);
% defines a set of lower and upper bound on the design variables
xopt(z) = inputs(1);
yopt(z) = inputs(2);
end
% Tells the time taken to perform the study case
time_study3 = toc;
figure(3)
% creaters a sub plot in the figure
subplot(2,1,1)
hold on
% create a surface
surfc(x,y,-f)
% Varies the color in each line segment and and it gives true line colour across the face
shading interp
plot3(xopt,yopt,-fopt,'marker','o','MarkerSize',5,'MarkerFaceColor','r')% plot 3 command is used for 3 axis
% Marker is used for visible line on the plots
title('Bounded inputs with population size')
xlabel('x-axis')
ylabel('y-axis')
zlabel('z-axis')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('functions')
Conclusion:
In the result of the problem we get three figures which are unbounded study,bounded study,bounded inputs with population size which as given in the plot. It shows the generic algorithm of the population where the random points (people) has been selected. From that selected parents were selected for the crossover for children for the next generation based on their fitness valve for the next generation.
There were no errors while running this program.
All results, plot has been attached as a picture.