Optimizing Stalagmite Function Using GA in Matlab
Project:-Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
Project Objective:- To understand the concept of genetic algorithm.
concept of genetic algorithm:-
A genetic algorithm (GA) is a method for solving both constrained and unconstrained optimization problems based on a natural selection process that mimics biological evolution. The algorithm repeatedly modifies a population of individual solutions. At each step, the genetic algorithm randomly selects individuals from the current population and uses them as parents to produce the children for the next generation. Over successive generations, the population "evolves" toward an optimal solution.
You can apply the genetic algorithm to solve 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 following outline summarizes how the genetic algorithm works:
- 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.
- Code to Generate Stalagmite Function:-
function [F]=stalagmite(input_vector)
x=input_vector(1);
y=input_vector(2);
f1=(sin(5.1*pi*x+0.5)).^6;
f2=exp(-4*log(2)*((x-0.0667).^2)/0.64);
f3=(sin(5.1*pi*y+0.5)).^6;
f4=exp(-4*log(2)*((y-0.0667).^2)/0.64);
F=(f1*f2*f3*f4);
end
- Code to Optimise Stalagmite function:-
clear all
close all
clc
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
[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
num_cases=50
% study 1: 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
plot3(Xopt,Yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Unbounded Inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iteration')
ylabel('F maximum')
%Study 2:- statistical behaviour with upper and lower bound
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
plot3(Xopt,Yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded Inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iteration')
ylabel('F maximum')
% study 3: increasing ga iterations
options=optimoptions('ga');
options=optimoptions(options,'PopulationSize',170);
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
plot3(Xopt,Yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded Inputs with modified population size')
subplot(2,1,2)
plot(fopt)
xlabel('Iteration')
ylabel('F maximum')
Explanation of Code:-
- First write a code for stalagmite function which stored in seprate "stalagmite.m" file.
- stalagmite function is two variable function namely x and y.
- stalagmite function cosists of four terms multiplication namely f1,f2,f3 and f4.
- f1=(sin(5.1*pi*x+0.5)).^6;
f2=exp(-4*log(2)*((x-0.0667).^2)/0.64);
f3=(sin(5.1*pi*y+0.5)).^6;
f4=exp(-4*log(2)*((y-0.0667).^2)/0.64);
F=(f1*f2*f3*f4); - after that write code for optimisation of stalagmite function .
- In this code first generate search space for x and y.
- convert this x and y search space into 2D form by using meshgrid command.
- use the value of this search space as inpute vector which required during the calling of stalagmite function.
- after that call the stalagmite function whose output stored in f(i,j).
- take number of iteration is equal to 50 for which genetic algorithm run.
- after that write code for optimising the stalagmite function for three types of study.
- In study 1 first write for loop which 50 times,which consists of "ga" which optimise the stalagmite function.
- results of optimum value stored in fopt(i),Xopt(i),Yopt(i).
- calculate evaluation time required for study 1 by using tic and toc.
- plot this reults which shown in results.
- similarly write 'for loop' for study 2 , this study simillar to study 1 except here give the upper and lower boundary.
- then write code for study 3 by further modification in study 2 by increasing the population size.
Results :-
- for study 1
study1_time =
1.9311
- for study 2
study2_time =
3.2040
- for study 3
study3_time =
7.1975
Conclusion:-
from study 1 it is not possible to predict accurate results.Study 2 give better result than study 1 because of here boundary are defined.Study 3 give more smoother results than study 1 and 2 because of here boundary are defined and also population size increased to 170.
Hence, the genetic algorithm predict maximum and minimum value of function that simplify the work of designer.
Referene:-