Week 4.1 - Genetic Algorithm
%G_A Main
clear all
close all
clc
% Defining Our search space
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
%Creating a 2Dimensional Mesh
[xx,yy]= meshgrid(x,y);
%Evaluate the Stalagmite 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)= stalagmite(input_vector);
end
end
num_cases=50
tic
for i=1:num_cases
[inputs,fopt(i)]= ga(@stalagmite,2);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
time_study1=toc
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('Unbound inputs')
%figure(2)
subplot(2,1,2)
plot(fopt)
xlabel('Iteration')
ylabel('Function minimum')
%study2
tic
for i=1:num_cases
[inputs2,fopt2(i)]= ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt2(i)=inputs2(1);
yopt2(i)=inputs2(2);
end
fopt2=-fopt2;
time_study2=toc
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt2,yopt2,fopt2,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs')
%figure(2)
subplot(2,1,2)
plot(fopt2)
xlabel('Iteration')
ylabel('Function minimum')
%Case_study 3: MAximising the population size to optimise the result
options= optimoptions ('ga');
options=optimoptions(options,'PopulationSize',350);
tic
for i=1:num_cases
[inputs3, fopt3(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt3(i)=inputs3(1);
yopt3(i)=inputs3(2);
end
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt3,yopt3,-fopt3,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs with optioptims function')
subplot(2,1,2)
plot(-fopt3)
xlabel('Iteration')
ylabel('Function minimum')
time_study3=toc
rng('default');
%STALAGMITE FUNCTION
function[f]= stalagmite(input_vector)
fx=(sin(5.1*pi*input_vector(1)+0.5))^6;
fy=(sin(5.1*pi*input_vector(2)+0.5))^6;
fxx=exp((-4*log(2)*(((input_vector(1)-0.0667)^2)/0.64)));
fyy=exp(-4*log(2)*(((input_vector(2)-0.0667)^2)/0.64));
f=-(fx*fy*fxx*fyy);
end
EXPLANATIONS:
Genetic Algorithm is mostly used to calculate the populations. It is used to solve constrained and unconstrained optimization problems based on natural selection process.
In this case we have used a 2dimensonal mesh as [XX,YY].
We use the stalagmite function to evaluate the program.
The num_cases are 50.
We have used total 3case study as requested.
The first case and the second case are been set as default values and the third case is set as 350.
For Ex:
Positive integer | {50} when numberOfVariables <= 5, {200} otherwise | {min(max(10*nvars,40),100)} for mixed-integer problems
Number of variables, specified as a positive integer. The solver passes row vectors of length nvars to fun.
Lower bounds, specified as a real vector or array of doubles. lb represents the lower bounds element-wise in lb ≤ x ≤ ub.
Internally, ga converts an array lb to the vector lb(:).
Upper bounds, specified as a real vector or array of doubles. ub represents the upper bounds element-wise in lb ≤ x ≤ ub.
Internally, ga converts an array ub to the vector ub(:).