TO OPTIMIZE STALAGMITE FUNCTION AND FIND GLOBAL MAXIMA USING GENETIC ALGORITHM
AIM: To optimize stalagmite function and find global maxima.
OBJECTIVE:
1)Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function
2)Plot graphs for all 3 studies and for F maximum vs no. of iterations
INTRODUCTION:
WHAT IS GENETIC ALGORITHM?
Genetic Algorithm (GA) is a search-based optimization technique based on the principles of Genetics and Natural Selection. It is frequently used to find optimal or near-optimal solutions to difficult problems which otherwise would take a lifetime to solve. It is frequently used to solve optimization problems, in research, and in machine learning.
OPTIMIZATION:
Optimization is the process of making something better.
In any process, we have a set of inputs and a set of outputs as shown in the following figure.
Nature has always been a great source of inspiration to all mankind. Genetic Algorithms (GAs) are search based algorithms based on the concepts of natural selection and genetics. GAs are a subset of a much larger branch of computation known as Evolutionary Computation.
GAs were developed by John Holland and his students and colleagues at the University of Michigan, most notably David E. Goldberg and has since been tried on various optimization problems with a high degree of success.
In GAs, we have a pool or a population of possible solutions to the given problem. These solutions then undergo recombination and mutation (like in natural genetics), producing new children, and the process is repeated over various generations. Each individual (or candidate solution) is assigned a fitness value (based on its objective function value) and the fitter individuals are given a higher chance to mate and yield more “fitter” individuals.
This is in line with the Darwinian Theory of “Survival of the Fittest”.
In this way we keep “evolving” better individuals or solutions over generations, till we reach a stopping criterion.
Genetic Algorithms are sufficiently randomized in nature, but they perform much better than random local search (in which we just try various random solutions, keeping track of the best so far), as they exploit historical information as well.
Advantages of GAs
GAs have various advantages which have made them immensely popular. These include −
-
Does not require any derivative information (which may not be available for many real-world problems).
-
Is faster and more efficient as compared to the traditional methods.
-
Has very good parallel capabilities.
-
Optimizes both continuous and discrete functions and also multi-objective problems.
-
Provides a list of “good” solutions and not just a single solution.
-
Always gets an answer to the problem, which gets better over the time.
-
Useful when the search space is very large and there are a large number of parameters involved.
Limitations of GAs
Like any technique, GAs also suffer from a few limitations. These include −
-
GAs are not suited for all problems, especially problems which are simple and for which derivative information is available.
-
Fitness value is calculated repeatedly which might be computationally expensive for some problems.
-
Being stochastic, there are no guarantees on the optimality or the quality of the solution.
-
If not implemented properly, the GA may not converge to the optimal solution.
We here have the following fitness function in the stalagmite function to plot the graph in the main functions.
NOTE:
While using the function we use "-ve sign" so that the plot is reversed as the maxima of the given function is to be found using GA.
SYNTAX OF GA.
Where,
finds a local unconstrained minimum, x = ga(fun,nvars)x, to the objective function, fun. nvars is the dimension (number of design variables) of fun.
finds a local minimum x = ga(fun,nvars,A,b)x to fun, subject to the linear inequalities A*x ≤ b. ga evaluates the matrix product A*x as if x is transposed (A*x').
finds a local minimum x = ga(fun,nvars,A,b,Aeq,beq)x to fun, subject to the linear equalities Aeq*x = beq and A*x ≤ b. (Set A=[] and b=[] if no linear inequalities exist.) ga evaluates the matrix product Aeq*x as if x is transposed (Aeq*x')
defines a set of lower and upper bounds on the design variables, x = ga(fun,nvars,A,b,Aeq,beq,lb,ub)x, so that a solution is found in the range lb ≤ x ≤ ub. (Set Aeq=[] and beq=[] if no linear equalities exist.)
subjects the minimization to the constraints defined in x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon)nonlcon. The function nonlcon accepts x and returns vectors C and Ceq, representing the nonlinear inequalities and equalities respectively. ga minimizes the fun such that C(x) ≤ 0 and Ceq(x) = 0. (Set lb=[] and ub=[] if no bounds exist.)
minimizes with the default optimization parameters replaced by values in x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options)options. (Set nonlcon=[] if no nonlinear constraints exist.) Create options using optimoptions.
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.
finds the minimum for x = ga(problem)problem, where problem is a structure
[, for any previous input arguments, also returns x,fval] = ga(___)fval, the value of the fitness function at x.
[ also returns x,fval,exitflag,output] = ga(___)exitflag, an integer identifying the reason the algorithm terminated, and output, a structure that contains output from each generation and other information about the performance of the algorithm
[ also returns a matrix x,fval,exitflag,output,population,scores] = ga(___)population, whose rows are the final population, and a vector scores, the scores of the final population.
CODE:
clear
close all
clc
% search space
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
[X,Y]=meshgrid(x,y);
%Number of itreation for fitness
num_cases=50;
% surf plot for stalagmite fn
for i=1:length(X)
for j=1:length(Y)
input_vector(1)=X(i,j);
input_vector(2)=Y(i,j);
f(i,j)=stalagmite(input_vector);
end
end
surfc(X,Y,-f);
shading interp
% Statical behaviour unbounded
%study time
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)
surfc(X,Y,-f);
hold on
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',6,'markerfacecolor','g');
xlabel('X');
ylabel('Y');
grid on
subplot(2,1,2)
plot(-fopt);
xlabel('Iteration')
ylabel('Min value')
% Statistical behaviour bounded
%study time
tic
for j=1:num_cases
[inputs,fopt(j)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt(j)=inputs(1);
yopt(j)=inputs(2);
end
study2_time=toc
figure(2)
subplot(2,1,1)
hold on
surfc(X,Y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',6,'markerfacecolor','g');
xlabel('X');
ylabel('Y');
grid on
subplot(2,1,2)
plot(-fopt);
xlabel('Iteration')
ylabel('Min value')
% Modify no. of population
options=optimoptions('ga','populationsize',300)
tic
for k=1:num_cases
[inputs,fopt(k)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(k)=inputs(1);
yopt(k)=inputs(2);
end
study3_time=toc
figure(3)
subplot(2,1,1)
hold on
surfc(X,Y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',6,'markerfacecolor','g');
xlabel('X');
ylabel('Y');
grid on
subplot(2,1,2)
plot(-fopt);
axis([0 50 0 1])
xlabel('Iterations')
ylabel('Min value')
SUBFUNCTION:
function [f] = stalagmite(input_vector)
x = input_vector(1);
y = input_vector(2);
f1x = (sin(5.1*pi*x + 0.5)).^6;
f1y = (sin(5.1*pi*y + 0.5)).^6;
t1 = ((x-0.0667).^2)/(0.64);
f2x = exp(-4.*log(2)*t1);
t2 = ((y-0.0667).^2)/(0.64);
f2y = exp(-4.*log(2)*t2);
f = -(f1x.*f2x.*f1y.*f2y);
end
EXPLANATION OF CODE:
-> First the program for stalagmite function was written and as mentioned in the note above it gives the minimum value of the function so to get the maximum value we introduced -ve sign.
-> The values of x and y in function was defined using linspace command and meshgrid is used for inputs to stalagmite function
->The results for bounded and unbounded function is obtained.
-> The output is attached.
OUTPUT:
REFERENCES:
https://in.mathworks.com/help/gads/ga.html
https://in.mathworks.com/discovery/genetic-algorithm.html
https://www.tutorialspoint.com/genetic_algorithms/genetic_algorithms_introduction.htm