Stalagmite Function Optimization Using Matlab
AIM:
To optimise the stalagmite function and to find the global maxima of the function.
Introduction:
A genetic algorithm is a optimization technique inspired by Charles Darwin's theory of natural evolution. This algorithm reflects the process of natural selection where the fittest individuals are selected for reproduction in order to produce offspring of the next generation.
In the above figure A1 to A4 are the parent data and A5 and A6 are the offspring data created.
The Genetic Algorithm involves 5 steps:
- Initial population:
Population is a subset of solutions in the current generation. It can also be defined as a set of chromosomes. There are several things to be kept in mind when dealing with GA population.The diversity of the population should be maintained otherwise it might lead to premature convergence.The population size should not be kept very large as it can cause a GA to slow down, while a smaller population might not be enough for a good mating pool. Therefore, an optimal population size needs to be decided by trial and error.
- Fitness Function:
Fitness function determines how fit an individual is(the ability of the individual to compete with other individuals).It gives a fitness score to each individual.The probability that this individual will be selected for reproduction depends on this fitness score.
- Selection:
A number of individuals are selected from the population in the mating pool.Based on the previously calculated fitness values,the best individuals based on thresold are selected.Individuals with high fitness score have more chance to be selected for reproduction.
- Crossover:
Offsprings are created by exchanging the genes of parents among themselves until the crossover point is reached.The new offspring created are added to the population which have both characters of the parents.
Exchanging of Genes Crossover Offspring
- Mutation:
In certain new offsprings formed, some of their genes are subjected to mutation with a low random probability.The new offspring created does not have the property of parents.
Stalagmite Function Used:
Global Maxima:
The absolute maximum value of function over its entire range.
Global Minima:
The absolute minimum value of function over its entire range.
Syntax of Genetic Algorithm:
ga() is used to find the minimum value of the function.The various syntaxes used are
given below:
x=ga(fun,nvars)
x=ga(fun,nvars,A,b,Aeq,beq,lb,ub)
x=ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options)
where,
fun- Function name
nvars- Number of variables
A- Linear inequality constraints as real matrix
b- Linear inequality constraints as real vector
Aeq- Linear equality constraints as real matrix
beq- Linear equality constraints as real vector
lb- Lower Boudary
ub- Upper Boundary
nonicon- non Linear constraints
options- Creating optimization options using 'optimoptions'
Explanation of Program:
- Defining the values of x and y using the linspace() command.
- With the values of x and y we create a 2D array using meshgrid()
- An external function is created inorder to solve the stalagmite function.
- For loop is used inorder to call the function and to assign the evaluated values to f(i,j).
- The surface plot is plot with x,y and z axis.
- nums is assigned to 30 times so that the ga can get minima value correctly.
- tic-toc command is used to calculate the time taken to run each study.
- The plot is made using the x,y and the z values.
Answer for the questions in the challenges.
- GA syntax:
x=ga(fun,nvars)
x=ga(fun,nvars,A,b,Aeq,beq,lb,ub)
x=ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options)
2.nums=40, so that it will run the input values 40 times so that ga() can get the minimum value correctly.
3.In study 3,the polpulation size is increased inorder to get more solutions.
4.Inorder to get a proper view(i.e non-inverted)we have given a negative sign for f at the time of plot.
Stopping Criteria:Stopping criteria are typically based on the appearance of necessary optimality conditions for the problem you are solving.Here the stopping criterion is number of time the loop will get exexuted.
Program to plot the given stalagmite function using GA:
%Start Of the Program
close all
clear all
clc
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
nums=30;
%Creating 2D array using meshgrid function
[xx yy]=meshgrid(x,y);
%Nested loop for array value input
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_fun(input_vector);
end
end
tic
for i=1:nums
[input,fval(i)]=ga(@stalagmite_fun,2);
x_out(i)=input(1);
y_out(i)=input(2);
end
case_time_1=toc
figure(1)
subplot(2,1,1)
surfc(xx,yy,-f)
xlabel('x')
ylabel('y')
title('Case 1(Unbounded)','color','k')
shading interp
hold on;
plot3(x_out,y_out,-fval,'marker','o','markersize',6,'markerfacecolor','r');
subplot(2,1,2)
plot(-fval);
xlabel('No Of Iterations')
ylabel('max Function Value')
%Case:2 Inserting Bound conditions
tic
for i=1:nums
[input,fval(i)]=ga(@stalagmite_fun,2,[],[],[],[],[0;0],[0.6;0.6]);
x_out(i)=input(1);
y_out(i)=input(2);
end
case_time_2=toc
figure(2);
subplot(2,1,1)
hold on;
surfc(xx,yy,-f)
xlabel('x')
ylabel('y')
title('case 2','color','k')
shading interp
plot3(x_out,y_out,-fval,'marker','o','markersize',6,'markerfacecolor','r');
subplot(2,1,2)
plot(-fval)
xlabel('No Of Iterations')
ylabel('max Function Value')
%Case:3
options=optimoptions('ga');
options=optimoptions(options,'populationsize',100)
tic
for i=1:nums
[input,fval(i)]=ga(@stalagmite_fun,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
x_out(i)=input(1);
y_out(i)=input(2);
end
case_time_3=toc
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
xlabel('x');
ylabel('y');
title('Case 3','color','k')
plot3(x_out,y_out,-fval,'marker','o','markersize',6,'markerfacecolor','r')
subplot(2,1,2)
plot(-fval)
xlabel('No Of Iterations')
ylabel('max Function Value')
%Case:4
options=optimoptions('ga');
options=optimoptions(options,'populationsize',300)
tic
for i=1:nums
[input,fval(i)]=ga(@stalagmite_fun,2,[],[],[],[],[0;0],[0.1;0.1],[],[],options);
x_out(i)=input(1);
y_out(i)=input(2);
end
case_time_4=toc
figure(4)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
xlabel('x');
ylabel('y');
title('Case 4','color','k')
plot3(x_out,y_out,-fval,'marker','o','markersize',6,'markerfacecolor','r')
subplot(2,1,2)
plot(-fval)
xlabel('No Of Iterations')
ylabel('max Function Value')
display(input)
Function used:
function [f]= stalagmite_fun(input_vector)
f1x= (sin(5.1*pi*input_vector(1)+0.5)).^6;
f1y= (sin(5.1*pi*input_vector(2)+0.5)).^6;
f2x= exp((((input_vector(1)-0.0667).^2)/0.64).*(-4*log(2)));
f2y= exp((((input_vector(2)-0.0667).^2)/0.64).*(-4*log(2)));
f1=f1x*f1y*f2x*f2y;
f=-f1;
end
Program Explanation:`
Errors Encountered:
Used plot command instead of plot3 for plotting 3 points.I rectified it by using the plot3 command
Sample Results:
CASE 1(Unbounded):
Case 2(Bounded):
Case 3(Bounded): Increased population Size
Case 4(Bounded): With more increase in population size
Workspace Values:
Conclusion:
Global maxima is calculated using the GA algorithm method.