Genetic Algotithm to optimize the global maxima using matlab
AIM: TO OPTIMIZE THE STALAGMITE FUNCTION AND TO FIND THE GLOBAL MAXIMA USING MATLAB.
OBJECTIVE:
- To write a code in MATLAB to optimize function.
- To find the global maxima of the function.
- To explain the concept of genetic algorithm.
- To plot the graph for the global maxima.
- To plot the graphs of ‘Function maximum vs no. of iteration’ for 3 studies for different conditions.
OVERVIEW:
The genetic algorithm is a method for solving both constrained and unconstrained optimization problems that is based on natural selection, the process that drives biological evolution. We can apply the genetic algorithm to solve a variety of optimization problems that are not well suited for standard optimization algorithms, including problems in which the objective function is discontinuous, nondifferentiable, stochastic, or highly nonlinear. The genetic algorithm can address problems of mixed integer programming, where some components are restricted to be integer-valued.
The genetic algorithm differs from a classical, derivative-based, optimization algorithm in two main ways, as summarized in the following table.
|
Classical algorithm |
Genetic algorithm |
|
Generates a single point at each iteration. The sequence of points approaches an optimal solution. |
Generates a population of points at each iteration. The best point in the population approaches an optimal solution. |
|
Selects the next point in the sequence by a deterministic computation. |
Selects the next population by computation which uses random number generators. |
BODY OF CONTENT:
Firstly, defining the stalagamite function:
Code for stalagmite function:
% Stalagmite function
function [f]=stalagmite(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*log(2).*((x-0.0667).^2/0.64));
f2_y=exp(-4*log(2).*((y-0.0667).^2/0.64));
f=-[f1_x.*f1_y.*f2_x.*f2_y];
end
Evaluating the stalagmite function and defining the genetic algorithm function -
clear all
close all
clc
%defining our search space
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
%creating a 2 dimensional mesh
[xx yy] = meshgrid(x,y);
%Evaluating 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);
endfor
endfor
surfc(xx,yy,-f)
xlabel('x value')
ylabel('y value')
shading interp
colorbar
[Inputs,fval]=ga(@stalagmite,2)
We got the plot for the above, as:
TO FIND THE GLOBAL MAXIMA:
1. Study of genetic algorithm for the random sampling-
'ga' syntax :
x = ga(fun,n) finds a local minima.
here, fun is the function which is to be minimise & n is the number of variables.
Code:
%Code for Global maximization-STUDY1
clear all
close all
clc
%Input array for x and y
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_cases=50;
[xx yy] = meshgrid(x,y);
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
[Inputs,fval]=ga(@stalagmite,2)
%study1 - Statistical behaviour
tic
for i=1:num_cases
[inputs,fopt(i)] = ga(@stalagmite,2);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study1_time=toc
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
colorbar
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Unbounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('Iteration')
ylabel('Function Maximum')
(here, '-' sign in f is taken to find the maxima)
Plot:
Result:
- There is large range of maximum values.(Incorrect result)
- Study time is 5.9116 sec.
2. Study of genetic algorithm with upper and lower bounds-
'ga' Syntax:
x = ga(fun,n,A,B,Aeq,Beq,LB,UB) finds the minima with lower and upper bounds.(LB`<=` x `<=`UB)
here, A=B=Aeq=Beq=[] ,
Code:
%Code for Global maximization-STUDY2
clear all
close all
clc
%Input array for x and y
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_cases=50;
[xx yy] = meshgrid(x,y);
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
[Inputs,fval]=ga(@stalagmite,2)
%study2-statistical behavior upper and lower bounds
tic
for i=1:num_cases
[inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study2_time=toc
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
colorbar
plot3(xopt,yopt,-fopt,'marker','o','markarsize',5,'markerfacecolor','r')
title('Bounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('Iteration')
ylabel('Function Maximum')
(here, '-' sign in f is taken to find the maxima)
Plot:
Result:
- Range for maximum values is reduced but is still not satisfactory.
- Study time is 10.5905 sec.
3. Study of genetic algorithm with increased no. of iterations-
'ga' Syntax:
x = ga(fun,n,A,B,Aeq,Beq,LB,UB,nonlcon,options) finds global minima with increased number of iteration by creating options using 'optimoptions'.
here, nonlcon = []
Code:
%Code for Global maximization-STUDY3
clear all
close all
clc
%Input array for x and y
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_cases=50;
[xx yy] = meshgrid(x,y);
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);
endfor
endfor
[Inputs,fval]=ga(@stalagmite,2)
%study3-Increasing GA Iteration
options=optimoptions('ga');
options=optimoptions(options,'PopulationSize',410);
tic
for i=1:num_cases
[inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study3_time=toc
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
colorbar
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs with increased Iterations')
subplot(2,1,2)
plot(-fopt)
xlabel('Iteration')
ylabel('Function Maximum')
(here, '-' sign in f is taken to find the maxima)
Plot:
Result:
- Very narrow range.(Satisfacory results).
- Study time is 41.0784 sec.
-
The concept of local and global maxima/minima:
> With low size iteration (like here, 50) : we get local maxima
> With high size iteration (like here, 410) : we get global maxima
- Coordinates of maxima-
EXPLANATION OF THE CODES:
- Clear all, close all, clc command is used to clear all previous commands and plots.
- The ‘linspace’ command is for defining 150 values between 0 to 0.6.
- Num_cases is for defining no. of iteration as 50.
- The ‘meshgrid’ command is for x,y arrays.
- For-loop is taken to running it for 50 times.
- The ‘tic-toc’ command is for registering the time taken by it.
- For plotting multiple plots in a single image, ‘subplot’ command is used.
- Here , '-' sign in f is taken to find the maxima.
- To showing the graphs is surface view ‘surfc’ command is used.
- To show the variation in color, ‘colorbar’ command is used.
- For 3-D figure, ‘plot3’ command is used.
- To entitle the graph, ‘title’ command is used.
- For labeling the axis, ‘xlabel’ & ‘ylabel’ are used.
ERROR:
1. Stalagmite undefined
2. Spelling mistake
3. 'ga' undefind due to using of 'octave' previously(finally all codes are run on matlab to get results)
REFERENCES:
https://www.mathworks.com/help/gads/what-is-the-genetic-algorithm.html