MATLAB PROGRAM TO FIND THE GLOBAL MAXIMA OF THE STALAGMITE FUNCTION
AIM :
Write an matlab code to optimize the stalagmite function and also find the global maxima of the function
optimization :
The area of study that deals with how to minimize or maximize the particular function which depends upon the different input parametres
GENETIC ALGORITHM :
Genetic algorithm is based upon charle sdarwin theory of evolution
The phases that are involved to work a genetic algorithm
1. firstly it selects the random initial population ,Algorithm uses every individual point to create next generation
2 . At every individual algorithm computes the fitness test and the obtained score values are converts into more usable range of values.
3. Let them cross over every individual to create next generation ;and two pairs(parents)
are selected according to their fitness scores ;low fitness score pairs are just passed to create next generation
4 . Mutation process should be done to create next generation with high fitness scores
5. The algorithm stops when it reaches when the time limit is reached.
SYNTAX FOR GA :
EXPLANATION FOR GA SYNTAX :
1. In matlab various options are used, in ga syntax here x is the output that represents the
minimum of the function.
2. nvars is the dimension of the function and it shows number of designed variables of function
3. x = ga(fitnessfcn,nvars,A,b,Aeq,beq) finds a local minimum x to fitnessfcn(fitnessfunction)and subjected to Aeq*x = beq , A=[],b=[](if no linear inequalities are exists .this equation used in part of our 1st case study
4. x= ga(fitnessfcn,nvars,A,b,Aeq,beq,lb,ub) here lb,ub are stands for lower and upper bound values as part of in our case study 2 we used this lower and upper bound values,if any of those are not provided the Matlab will take default values.
5. nonlcon is represents for non linear equalities and inequalities,for this a seperate function need to be define.
6. intcon represents for integer outputs and this also needs to be define .
7. and finally by using optimoptions or gaoptimset to increase the individuals in population .
STEPWISE EXPLANATION FOR CODE :
1 . set of values are defined for the function by using linspace command and afterwards for converting of 2D we used mesh grid command .
2. The for loop and Nested for loop are used in 2D arrays to insert variables for both row and column.
3.input vector (1) takes the value fromxx matrix; input vector (2) takes the value from yy matrix
4. to know the value of f (i,j) we need to find the stalagmite(input_vector) value that is caluculated in seperate function as shown in the below
function [f] = stalagmite1(input_vector)
x1 = input_vector(1);
y1 = input_vector(2);
f1x = (sin(5.1*pi*x1+0.5))^6;
f1y = (sin(5.1*pi*y1+0.5))^6;
f2x = exp(-4*log(2)*((x1-0.0667).^2)/0.64);
f2y = exp(-4*log(2)*((y1-0.0667).^2)/0.64);
f = -(f1x*f1y*f2x*f2y);
end
WHAT IS STALAGMITE :
It is an mathematical model that really helps to generate the local minima and local maxima
In our study the stalagmite mathematical model is to found the global maxima. just for the
sake of convience i used stalagmite1 instead of stalagmite.
5. After finding the value of f ,as we discussed if no bounds and constraints are provided in case 1 to make the selection of the initial population we will get the different resuluts each time.and also in case 1 a for loop is also used to caluculate the optimum values of i (opt1(i)).
6. In plotting of case 1 we use subplots for create the multiple plots in the same figure window. and also 'surf' command also used to create surface plot with values of x, y,f
7. plot3 command is used to plot the optimum values of x and y and f .and here we put the negative sign that means it doesnt change its location but gives the negative value of the function and the plots are also changed accordingly.
8. and the subplot is to plot the optimum values for each iteration and here we uses the 50 iterations .and it is clearly shown in the output.
9.for case 2
In case 2 the to find the global maxima we used the upper and lower bounds by using this boundary conditions we might get reduced optimum values as compared to case 1 and also it gives better results as compared to case 1 ,the input variables are stored in xopt2 and yopt2. and here tic and toc commands used for determine the study time
shading interp command is used for the plot looks smoother and it hides the grids .
10.for case 3
in case 3 we increase the population size up to 300 for better results by using optimoptions command
plot is created in figure 3 .and also caluculated the study time now we can find the global maxima point .
MAIN CODE :
clear all
close all
clc
% SET OF VALUES FOR STALGAMITE FUNCTION
% in other words defining the search space
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) = stalagmite1(input_vector);
end
end
% study 1
num_iterations = 50;
tic
for i = 1:num_iterations
[input1,fopt1(i)] = ga(@stalagmite1,2);
xopt1(i) = input1(1);
yopt1(i) = input1(2);
end
study_time_1 = toc
% plotting
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,-f)
xlabel('x values')
ylabel('y values')
shading interp
plot3(xopt1,yopt1,-fopt1,'marker','o','markersize',5,'markerfacecolor','r')
title('unbound points')
subplot(2,1,2)
plot(-fopt1)
xlabel('iterations')
ylabel('function minima')
% study 2
tic
for i = 1:num_iterations
[input2,fopt2(i)] = ga(@stalagmite1,2,[ ],[ ],[ ],[ ],[0; 0],[1;1]);
xopt2(i) = input2(1);
yopt2(i) = input2(2);
end
study_time_2 = toc
% plotting
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-f)
xlabel('x values')
ylabel('y values')
shading interp
plot3(xopt2,yopt2,-fopt2,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded points')
subplot(2,1,2)
plot(-fopt2)
xlabel('iterations')
ylabel('function minima')
% study 3
options = optimoptions('ga');
options = optimoptions(options,'populationsize',300)
tic
for i = 1:num_iterations
[input3,fopt3(i)] = ga(@stalagmite1,2,[ ],[ ],[ ],[ ],[0;0],[1;1],[ ],[ ],options);
xopt3(i) = input3(1);
yopt3(i) = input3(2);
end
study_time_3 = toc
% plotting
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,-f)
xlabel('x value')
ylabel('y value')
shading interp
plot3(xopt3,yopt3,-fopt3,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded points')
subplot(2,1,2)
plot(-fopt3)
xlabel('iterations')
ylabel('function minima')
RESULTS 1:
Above fiigure shows for random sampling and each and every time run thr code we get different results.
RESULTS 2 :
From this above fig.we can clearly see that the min point lies between 4 points ,which is betterthan case 1
RESULTS 3:
The minimum point lies betwwn the two peaks by increasing the population size.
ERRORS :
1
SOLUTION FOR THAT ERROR :
checed that line and found that missed the input argument as @ .
2.
forgot to give the nonlcon (nonlinearconstraints), and intiger constrains
and checked thoroughly and found that error .
REFERENCES:
https://en.wikipedia.org/wiki/Genetic_algorithm
https://in.mathworks.com/help/gads/how-the-genetic-algorithm-works.html#f6243