Calculate the Global Maxima of a stalagmite function using Genetic Algorithm
Aim:
To write code to calculate the global maxima of a stalagmite function using GA.
Objective:
- To optimise the stalagmite function and find the global maxima of the function.
- Expalin the concept of genetic algorithm in your own words and also explain the syntax for ga in MATLAB in your report.
- Your code gives the same output, even if it is made to run several times.
- Plot graphs for all 3 studies and for F maximum vs no. of iterations.
Theory:
Genetic algorithm (GA):
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 genetic algorithm differs from a classical, derivative-based, optimization algorithm in two main ways, as summarized in the following table.
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.
- Fitness Function: The fitness function is the function you want to optimize. For standard optimization algorithms,this is known as the objective function.
The syntax for genetic algorithm:
x = ga(fun,nvars)
x = ga(fun,nvars,A,b)
x = ga(fun,nvars,A,b,Aeq,beq)
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub)
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon)
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options)
x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon)
Input Equation:
`f(x,y)=f_1,_xf_2,_xf_1,_yf_2,_y`
`f_1._x=[sin(5.1pix+0.5)]^6 f_1._y=[sin(5.1piy+0.5)]^6`
`f_2,_x=exp[-4ln(2)(x-0.0667)^2/(0.64)]`
`f_2,_y=exp[-4ln(2)(x-0.0667)^2/(0.64)]`
Code Explained:
1) The linspace comand is used to define x and y with 150 elements between zero and 0.6 including the bounds.
2) Then a @D grid is made using meshgrid command is created from x and y inputs to be outputted to xx and yy.
3) For loop is used to solve the genetic algorithm for every element in mesh grid.
% Defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
% Creating a 2Dimentional mesh
[xx yy] = meshgrid(x,y);
Stalagamite Function:
1) The function is declared and its input vector is specified.
2) Equations are written with inputs from mesh grid.
3) In the final step the negative sign is added to get maximum in function.
function[f]=stalagamite(input_vector)
x=input_vector(1);
y= input_vector(2);
f_1x=(sin(5.1*pi*x+0.5))^6;
f_1y=(sin(5.1*pi*y+0.5))^6;
f_2x=exp(-4*log(2)*((x-0.0667)^2)/0.64);
f_2y=exp(-4*log(2)*((y-0.0667)^2)/0.64);
f=-(f_1x*f_1y*f_2x*f_2y);
end
Explaing Main program:
1) GA is based on random sampling its because if we input random values and it come in the concept of random selection. Where we take two generation and we mutate them also do cross over.
2) Every time we run the code we get different ga results.
3) Search space: We want GA to take x and y as search space but it doesnt know the values of x and y can go beyond 0.6 or below 0.
4) The num_cases is set to 50 as a counter for iterations.we can get the value to converge with less iteration and if we incresae the the iteration the time taken to get the valuse will increase.
5) Subplots is used to get multiple plots in one figure.
6) Here in plot function is creating x,y and the optimum value the GA predicts over the 50 studies. It helps to visualise how the algorythm working.
7) Tic and Toc is introduced to measure how long it takes to study.
8) In study two we have to set upper bounds and lower bounds, since in case one we know that the x and y value goes below 0 and above 0.6. In study two we want it to be between 0 and 1 by using upper and lower bounds.
9) Search space depends upon how we run our optimization or what type of problem we are solving.
10) Search space: It is a set of domain through which an algorithm searches.
11) In study three we will specify the bounds and change the initial population size, We know that GA takes random samples.
12) I have add this code fprintf('\n') to get my stutime results in rows.
13) Introducing Population size: The population size field in Population options determines the size of the population at each generation. Increasing the population size enables the genetic algorithm to search more points and thereby obtain a better result. However, the larger the population size, the longer the genetic algorithm takes to compute each generation.
Note:
- If our initial population is too small it wont be able to find the global minima and it can find the local minima which happened in study two.
- If we have more iteration ga will run gfor long time.
clear all
close all
clc
% Defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
%Running the ga 50 times
num_cases=50
% Creating a 2Dimentional 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);
end
end
% To measure the time taken to study we intro (tic and toc)
tic
%case study one with unbounded condition
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)
% To get multiple plots in the same figure window
subplot(2,1,1)
surfc(x,y,-f)
colorbar
xlabel('x values')
ylabel('y values')
zlabel('f values')
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Unbounded inputs')
% To make the plots clear we intro subplot
subplot(2,1,2)
plot(-fopt)
xlabel('Interation')
ylabel('Function maximum')
zlabel('f values')
%case study two adding lower bound and upper bound conditions
tic
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0,0],[0.5;0.5]);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study_time2=toc;
figure(2)
subplot(2,1,1)
surfc(x,y,-f)
colorbar
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Lower and Upper Bounded inputs')
xlabel('x values')
ylabel('y values')
zlabel('f values')
subplot(2,1,2)
plot(-fopt)
xlabel('Interations')
ylabel('Function minimum')
zlabel('f values')
%case study three with increasing GA iteration
options=optimoptions('ga');
options=optimoptions(options,'populationsize',300);
tic
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0,0],[0.5;0.5],[],[],options);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study_time3=toc;
figure(3)
subplot(2,1,1)
surfc(x,y,-f)
colorbar
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded Inputs')
xlabel('x values')
ylabel('y values')
zlabel('f values')
subplot(2,1,2)
plot(-fopt)
xlabel('Interations')
ylabel('Function minimum')
zlabel('f values')
fprintf('study time for study one is %f n',study_time1);
fprintf('\n');
fprintf('study time for study two is %f n',study_time2);
fprintf('\n');
fprintf('study time for study three is %f n',study_time3);
Output graph:
Study one:
The global maxima cannot be determined because the ga() function is not converging 50 iteration.
Study Two:
The stalagmite function has lower and upper bounds, So that ga() function will take individuals from the bounded region.
Study Three:
Here the population size is increased so that i will get individual values near the required point as global maxima.
Comparing study two and three:
In study two we have to set upper bounds and lower bounds to get the global maxima i decreased the region to get the closest values.
From study three i have increased the population size with same region used in study two, So that i will get more number of points from that region to gain global maxima.
Study Time:
Errors:
Got error in coding ga function .All the errors corrected by going throught the codes again.
Result:
Thus calculating the Global Maxima of a stalagmite function using Genetic Algorithm is explained with MATLAB.
Refrence:
https://in.mathworks.com/discovery/genetic-algorithm.html
https://in.mathworks.com/help/gads/how-the-genetic-algorithm-works.html