Optimization of stalagmite function using Genetic algorithm in Matlab
AIM:-
To write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
OBJECTIVE:-
- To understand the concept of genetic algorithm
- Plot graphs for all 3 studies and for F maximum vs no. of iterations.
THEORY:-
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.
Five phases are considered in a genetic algorithm.
- Initial population
- Fitness function
- Selection
- Crossover
- Mutation
Working of genetic algorithm:-
-
The algorithm begins by creating a random initial population.
-
The algorithm then creates a sequence of new populations. At each step, the algorithm uses the individuals in the current generation to create the next population. To create the new population, the algorithm performs the following steps:
-
Scores each member of the current population by computing its fitness value. These values are called the raw fitness scores.
-
Scales the raw fitness scores to convert them into a more usable range of values. These scaled values are called expectation values.
-
Selects members, called parents, based on their expectation.
-
Some of the individuals in the current population that have lower fitness are chosen as elite. These elite individuals are passed to the next population.
-
Produces children from the parents. Children are produced either by making random changes to a single parent—mutation—or by combining the vector entries of a pair of parents—crossover.
-
Replaces the current population with the children to form the next generation.
-
-
The algorithm stops when one of the stopping criteria is met.
MATLAB has a predefined in-built function for genetic algorithm 'ga'. It is used to minimise or solve naturally unconstrained problems and optimize them based on our options. This is similar to a process that performs biological evolution.
Syntax of General Algorithm:-
finds a local unconstrained minimum, x, to the objective function, fun. nvars is the dimension (number of design variables ) of fun
finds a local unconstrained minimum, x, to the objective function, fun. nvars is the dimension (numnber of design variables ) of fun.
finds a local minimum 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 uppewr bounds on the design variables ,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 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 options. (Set nonlcon=[] if no nonlinear constraints exist.) Create options using optimoptions.
or 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 problem, where problem is a structure.
for any previous input arguments, also returns fval, the value of the fitness function at x.
also returns 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 population, whose rows are the final population, and a vector scores, the scores of the final population
Stalagmite function:-
CODE:-
Main code:-
clear all
close all
clc
% Defining our search space
%Input Arrays
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_cases = 50; % for iteration
%To generate the stalagmite function
f=get_stalagmite(x,y);
% To measure how long it would take to complete the study we use the tic
% and toc command
tic
%Study 1 - Satistical behaviour
for i = 1:num_cases
[inputs, fopt(i)]=ga(@stalagmite, 2);%fopt means the optimum value that the function predicts
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study1_time=toc
%To measure how long it takes to complete the study
figure(1)
subplot(2,1,1)% to obtain mutiply subplot inside the same figure window
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 behaviour- with upper and lower bounds
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
study2_time=toc
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs ')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('Function maximum')
% Study 3 increasing GA iteration
options= optimoptions('ga');
options= optimoptions(options,'PopulationSize',230);
tic
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
study3_time=toc
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs with modified population size')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('Function maximum')
Function code-
- get_stalagmite
function[f]=get_stalagmite(x,y) % Evaluating the stalagmite function [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 end - stalagmite
function [F]=stalagmite(input_vector) x=input_vector(1); y=input_vector(2); %Solution of the function f(x,y) = (f1,x)*(f2,x)*(f1,y)*(f1,y) %f1=f1,x %f2=f2,x %f3=f1,y %f4=f2,y f1=(sin(5.1*pi.*x+0.5)).^6; f2=exp(-4*log10(2).*(x-0.0667).^2/0.64); f3=(sin(5.1*pi*y+0.5))^6; f4=exp(-4*log10(2).*(y-0.0667).^2/0.64); F=-f1.*f2.*f3.*f4;
Errors Encountered:-
Typing errors were encountered ,which where later rectified.
Output:-
Study - 1
Study -2
Study- 3
Workspace
CONCLUSION:-
In all the three studies , the 3rd study takes the most number of time as population size is the greatest among the three cases. On increasing the population we get a more precise and accurate value. And the Boundaries play a major role in optimization.
REFERENCES:-