Program to Optimise the Global Maxima of a Stalgamite Function.
AIM:
To write a code to calculate the global Maxima of a Stalgamite Function.
OBJECTIVE:
To Optimise the Global Maxima using the Principles of Genetic Algorithm.
THESIS:
Global maxima is the highest point of a data set and is a Unique Point. so, for a given data there can be only one Global Maxima. In this Program, our main objective is to optimise the given problem to find the global maxima in the shortest period of time. Here the question comes, what is Optimisation? Optimisation can be explained by the following example.
let us consider an example of a Fomula 1 Car. considering the aerodynamics of a Formula 1 car, the main aim is to produce maximum Downforce with the least amount of Drag Force and using the least amount of components and the lightest of the materials. This is called as Optimisation, which is making the most out of the least.
GENETIC ALGORITHM:
Genetic Algorithm(ga) is a solver in MATLAB inspired by the process of natural selection. Genetic algorithms are commonly used to generate high-quality solutions to optimization and search problems by relying on biologically inspired operators such as mutation, crossover and selection. Genetic Algorithm is based on the concept of Darwin's Theory of Evolution. Genetic Algorithm is widely used in Computer Science and Operations research.
SYNTAX
finds a local minimumx= ga(fun,nvars,A,b)xtofun, subject to the linear inequalitiesA*x≤b.gaevaluates the matrix productA*xas ifxis transposed (A*x').-
subjects the minimization to the constraints defined inx= ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon)nonlcon. The functionnonlconacceptsxand returns vectorsCandCeq, representing the nonlinear inequalities and equalities respectively.gaminimizes thefunsuch thatC(x)≤0andCeq(x) = 0. (Setlb=[]andub=[]if no bounds exist.)
or x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon) requires that the variables listed in x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options)IntCon take integer values.
STOPPING CRITERIA:
You can specify the values of these criteria in the Stopping criteria pane in the Optimization app. The default values are shown in the pane. The following are the options which can be used change when to stop the Algorithm. the algorithm stops once th conditions are met.
-
Generations — The algorithm stops when the number of generations reaches the value of Generations.
-
Time limit — The algorithm stops after running for an amount of time in seconds equal to Time limit.
-
Fitness limit — The algorithm stops when the value of the fitness function for the best point in the current population is less than or equal to Fitness limit.
-
Stall generations — The algorithm stops when the average relative change in the fitness function value over Stall generations is less than Function tolerance.
-
Stall time limit — The algorithm stops if there is no improvement in the objective function during an interval of time in seconds equal to Stall time limit.
-
Stall test — The stall condition is either
average changeorgeometric weighted. Forgeometric weighted, the weighting function is 1/2n, where n is the number of generations prior to the current. Both stall conditions apply to the relative change in the fitness function over Stall generations. -
Function tolerance — The algorithm runs until the average relative change in the fitness function value over Stall generations is less than Function tolerance.
-
Constraint tolerance — The Constraint tolerance is not used as stopping criterion. It is used to determine the feasibility with respect to nonlinear constraints. Also,
max(sqrt(eps),ConstraintTolerance)determines feasibility with respect to linear constraints.
PROGRAM:
the program for the optimization is given below,
clear all
close all
clc
%defining our searchspace
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
%creating a 2Dmesh
[xx,yy]= meshgrid(x,y);
%evaluating stalgamite function
for i = 1:length(xx)
for j = 1:length(yy)
input_vetcor(1)= xx(i,j);
input_vetcor(2)= yy(i,j);
f(i,j)=stalagmite(input_vetcor);
end
end
num_cases = 50;
tic
%study1 - statistical behavior
for i=1:num_cases
[inputs,fopt(i)] = ga(@stalagmite,2);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study_time1 = 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('unbounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maximum')
tic
%study 2 - statistical behavior - with upper and lower bounds
for i = 1:num_cases
[inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.1;0.1]);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study_time = toc;
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
title('bounded inputs')
xlabel('x-axis')
ylabel('y-axis')
zlabel('z-axis')
colorbar
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function minimum')
% study 3 increasing GA interations
options = optimoptions('ga');
options = optimoptions(options,'populationsize',150);
tic
for i= 1:num_cases
[inputs,fopt(i)]= ga(@stalagmite,2,[],[],[],[],[0;0],[0.1;0.1],[],[],options);
xopt(i)= inputs(1);
yopt(i)= inputs(2);
end
study_time3 = toc;
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
title('bounded inputs with modified populationsize')
xlabel('x-axis')
ylabel('y-axis')
zlabel('z-axis')
colorbar
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maxima')
study_time3 = toc
The reason why we do 50 iterations is beacause the probabililty of finding the Global Maxima with unbounded inputs is more than and will atleast appear , when compared to 10 or 20 Interations.
PROGRAM defining the Function.
function [v]=stalagmite (yy);
x =yy(1) ;
y = yy(2);
x1 = (sin(5.1*pi*x + 0.5)).^6;
y1= (sin(5.1*pi*y + 0.5)).^6;
x2= exp(-4*log(2)*((x-0.0667).^2/0.64));
y2 = exp(-4*log(2)*((y-0.0667).^2/0.64));
v=-x1.*y1.*x2.*y2;
end
In MATLAB the 'ga' solver usually solves for MINIMA's. since our intention is to find the MAXIMA, we use a "-" negative sign to flip the point and the plot onto the Positive side. the negative sign is used in the function and in the main program in the 'surfc' command.
OUTPUTS:
Plot for the unbounded inputs:
Plot for the bounded inputs:
Plot for bounded inputs of modified population size: