Week 4 - Genetic Algorithm FIND GLOBAL MAXIMA
Abstract:
In this project/Challenge our main aim is to calculate global maxima using genetic algorithm in Matlab using the ga function and optimize the solution.
What is Genetic Algorithm?
- A genetic algorithm (GA) is a method for solving both constrained and unconstrained optimization problems based on a natural selection process.
- This use concepts from biology to find the global minimum from a optimization problem.
- The algorithm repeatedly modifies a population of individual solutions.
- It starts with an initial generation of candidate solutions that are tested against the objective function. Subsequent generations evolve from the 1st through selection, crossover and mutation.
- Using genetic alogorithm we can optimize the solution by changing input parameter.
Syntax for GA:
f=ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options)
fun - fitness function. We will use fitfun as @stalagmite,
nvars- number of variables,
lb- Lower bound(limit),
ub- Upper bound(limit),
options- We will use to set population size.
Note: GA give Global minimum value but our problem requires Global maximum. In order to compensate for that we will be making changes in our code accordingly. So, to find global maxima we have to invert the equations, so to do that just multiply with -1.
Find below equations to find global maxima from:
Kindly find below code for given equation.
%Clear command window and workspace window
close all
clear all
clc
%Define Search Space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
%cCreate 2-D array
[xx,yy]=meshgrid(x,y);
%Evaluate stalagmite function
for i=1:length(xx)
for j =1:length(yy)
vector(1)=xx(i,j);
vector(2)=yy(i,j);
f(i,j)=stalagmite(vector);
end
end
surf(xx,yy,f)
shading interp
Shading interp Command used to remove generated mesh from result surface in below diagram for easy undersating like below:
Salagmite function:
In order to find global maxima we have to invert the equations, so to do that just multiply with -1.
function f= stalagmite(vector)
x=vector(1);
y=vector(2);
f1x = (sin(5.1 * pi * x + 0.5))^6;
f2x = exp((-4) * log(2) * ((x - 0.0667)^2/0.64));
f1y = (sin(5.1 * pi * y + 0.5))^6;
f2y = exp((-4) * log(2) * ((y - 0.0667)^2/0.64));
f=(-1)*f1x*f2x*f1y*f2y;
end
Below is the Visual Result of above codes:
Soin order to find global maxima we will use ga:
Study1-Iteration without constraint(or limit):
clc,close all,clear all
%Define Search Space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
%Create 2D array
[xx,yy]=meshgrid(x,y);
for i=1:1:length(xx)
for j =1:length(yy)
vector(1)=xx(i,j);
vector(2)=yy(i,j);
f(i,j)=stalagmite(vector);
end
end
num_cases=50;
%study1-Calculations under Unbound Limit
tic
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2)
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study_time=toc
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,f);
shading interp
plot3(xopt,yopt,fopt,\'marker\',\'o\',\'markersize\',4,\'markerfacecolor\',\'r\');
title(\'Unbounded inputs\')
subplot(2,1,2)
plot(fopt)
xlabel(\'Iterations\')
ylabel(\'Function minimum\')
Subplot command is used for showing two plots in one image.
Kindly find below Visual Result of above code/ Study 1:
This code is taking study_time =4.8073 for 50 successive iterations.
Note: Becasue of genetic algorithm doesn\'t have a spcific search space, ga ries to find minimum value (global minima) outside the 0 to 0.6 limit (Specified Limit)
Study 2-Iteration with constraint(or limit):
Bounded Iteration.
In order to solve the above issue that ga tried to find Local minima outside specified limit Even though we have values from 0 to 0.6. We have to specify the Upper limit and Lowe limit in ga function.
In this case study2, we have provided UB as [0.6;0.6] and LB as [0;0].
clc,close all,clear all
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
[xx,yy]=meshgrid(x,y);
for i=1:1:length(xx)
for j =1:length(yy)
vector(1)=xx(i,j);
vector(2)=yy(i,j);
f(i,j)=stalagmite(vector);
end
end
num_cases=50;
%study2-Calculations under Bound Limit
tic
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study_time=toc
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,f);
shading interp
plot3(xopt,yopt,fopt,\'marker\',\'o\',\'markersize\',4,\'markerfacecolor\',\'r\');
title(\'Unbounded Inputs\')
subplot(2,1,2)
plot(fopt)
xlabel(\'Iterations\')
ylabel(\'Function minimum\')
Kindly find below Visual Result of above code/ Study 2:
This code is taking study_time =7.1389 for 50 successive iterations.
This above bounded iteration code helps us to find global minima and maxima but in order to calculate more rubost answer,
In this we have narrowed down the search space, now we can increase population size to get global minima.
Note: In our case global maxima, because we have multiplied equation with -1.
Study3- Bounded Iterations, UB and LB with increased population to 200:
clc,close all,clear all
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
[xx,yy]=meshgrid(x,y);
for i=1:1:length(xx)
for j =1:length(yy)
vector(1)=xx(i,j);
vector(2)=yy(i,j);
f(i,j)=stalagmite(vector);
end
end
num_cases=50;
%study3-Calculations under Bound Limit and Adding Population size
tic
%Add population size
options=optimoptions(\'ga\',\'PopulationSize\',500);
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study_time=toc
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,f);
shading interp
plot3(xopt,yopt,-fopt,\'marker\',\'o\',\'markersize\',4,\'markerfacecolor\',\'r\');
title(\'Unbounded Inputs\')
subplot(2,1,2)
plot(fopt)
xlabel(\'Iterations\')
ylabel(\'Function minimum\')
Kindly find below Visual Result of above code/ Study 3:
In above graph we can clearly see that the global minima is at appx -0.9999.
In this problem \"global minima is our global maxima\" as we have multiplied by a minus sign to main equatoion.
study_time =22.5707 for case study 3.
As we seen as we add syntax in ga for optimum solution timefor iteration goes on increases.
Below is the complete code:
clc,close all,clear all
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
[xx,yy]=meshgrid(x,y);
for i=1:1:length(xx)
for j =1:length(yy)
vector(1)=xx(i,j);
vector(2)=yy(i,j);
f(i,j)=stalagmite(vector);
end
end
num_cases=50;
%study1-Calculations under Unbound Limit
tic
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study_time=toc
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,-f);
shading interp
plot3(xopt,yopt,-fopt,\'marker\',\'o\',\'markersize\',4,\'markerfacecolor\',\'r\');
title(\'Unbounded Inputs\')
subplot(2,1,2)
plot(fopt)
xlabel(\'Iterations\')
ylabel(\'Function minimum\')
%study2-Calculations under Bound Limit
tic
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study_time=toc
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,-f);
shading interp
plot3(xopt,yopt,-fopt,\'marker\',\'o\',\'markersize\',4,\'markerfacecolor\',\'r\');
title(\'Bounded Inputs\')
subplot(2,1,2)
plot(fopt)
xlabel(\'Iterations\')
ylabel(\'Function minimum\')
%study3-Calculations under Bound Limit and Adding Population size
tic
%Add population size
options=optimoptions(\'ga\',\'PopulationSize\',500);
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study_time=toc
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,-f);
shading interp
plot3(xopt,yopt,-fopt,\'marker\',\'o\',\'markersize\',4,\'markerfacecolor\',\'r\');
title(\'Bounded Inputs with Add population size\')
subplot(2,1,2)
plot(fopt)
xlabel(\'Iterations\')
ylabel(\'Function minimum\')
Combied Output:
- Error Facing While Plotting result as below:
Error troublshoot:
This error occured in result visual figure, the figure can not be shown correctly becasue i used while plotting graph as plot command instead of plot3, in this the parameter are 3 and in order to draw 3D plot we need to use plot 3 command.
In order to plot graph correctly we can multiply by Negative sign to f and fopt as belows:
Please find below references: