GENETIC ALGORITHM
AIM : To optimize the stalagmite function using genecti algorithm techniques using matlab.To understand the genectic algorithm first,we need to understand the following concepts to build a basic understanding of the genetics alogorithm.
OPTIMIZATION : Optimization is the process of making somathings better (such as a design, system,or decision)as fully perfect,functional,or effective as possible ,this also helps us to find the best possible results under a set of constraints. This includes maximizing the product,strength,reliability,and utilization.
EXAMPLE : Taking the design of an aircraft,handling a load of 1 tonne.Length,width,(aspect ratio)the factor of safety,weight,and stress-induced,etc.
DETERMINISTICS METHOD : In this method,the output of the initial conditions.This uses specific rules from moving from one solution to another.let us understand with the help of an example:
stress analysis of wing is determined as the output result (stress value)is totally dependent on the initial condition (loading condition)and the parameters like material and forces acting on the component.
STOCHASTIC METHOD : This method is used for minimizing and maximizing an objective function,this processes some inherent randomness,it is a probabilistic approach.this method does not possess.Let us understand with an
EXAMPLE :Imagine a company that provide energy to households.This company is responsible for delivering much they demand.
THE FOLLOWING IS THE PROCEDURE FOLLOWED BY AN OPTIMIZATION PROCESS.

VARIABLES - Variable is a process that may vary during optimization is my variables.an example is the dimension of a design that can be scale - up or vary to get minimum stress.
CONSTRAINTS - During the optimmization your output must not go beyond certain limits are known as constraints.An example one can reduce the the material in such a way that the stress must not exit the ultimate stress of the material.
OBJECTIVE FUNCTION - This can be predicted by the mathematical model that includes all the variables as input.foe example z = (x,y)here the z output is totally dependent on the input variable (x,y).
VARIABLE BOUND - These are the range of the variable input,that will be varied at every iteration to provide an optimized result.for example,the design variable range to get the minimum stress.
THE CONCEPT OF GENETIC ALGORITHM:
A Genetic algorithm is a natural process that is used for the selection of the fittest individuals.The main use of the genetics algorithmis for reproduction to produce offspring of the next generation,this process of selecting and reproducing the fittest individual is inspired by CHARLES DARWIN'S and named it the "THEORY OF EVOLUTION".

The process of natural selection starts with selection starts with selection of the fittest individuals from a population. They produce offsring which inherit the characteristics of the parents and will be added to the next generation .If parents have better fitness,their offspring will be better chance at surviving.This process keeps on iterating and in the end,a generation with fittest individuals will be found.
FIVE PHASES ARE CONSIDERED IN A GENETIC ALGORITHM.THEY ARE
* INITIAL POPULATION
* FITNESS FUNCTION
* SELECTION
* CROSSOVER
* MUTATION

* INITIAL POPULATION-the process begins with a set of individuals which is called a population.an individual is characterized by a set of parameters (variables)know as genes.genes are joined into a string to form a chromosome(solution).

SELECTION - The idea of the selection phase is to select the fittest individuals and let them pass their genes to the next generation.Here,two pairs of individuals are selected according to their fitness score,higher will be the fitness the more chance is for the selection for reproduction.
CROSS OVER - Cross over is a process of talking two-parent's solution and producing a child from them. For each pair of parents to be mated,a crossover point is chosen random from within the genes.

now the new offspring are added to the population.

ADVANTAGES OF GAs
* GAs have various advantages that have made them immensely popular.These include.
* Does not require any derivative information (which may not be a available for many not be available for many real-world problems).
*Has very good parallel capabilities.
*useful when the search space is very large and there are a large number of parameters involved.
* Always provide good results over time .
LIMITATIONS OF GAs:
* If not implemented properly,the GA may not converge to the optimal solution.
* Where there is simple problem and for that the dervivative values is known then this is not suited i.e genetic algorithm.
MATLAB CODE
1. STALAGMITE FUNCTION :
Initially we will start our code with all the input parameters and defining a function into another script '.mscript'
clear all
close all
clc
%defining our search space
x =linspace(0,0.6,150);
y =linspace(0,0.6,150);
num_case=50;
%creating a 2D 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
tic
for k=1:num_case;
[input,fval(k)]=ga(@stalagmite,2);
x_1(k)=input(1);
y_1(k)=input(2);
end
study1_time=toc
figure (1)
hold on
grid on
surfc(xx,yy,-f)
shading interp
title('stalagmite plot')
xlabel('x')
ylabel('y')
figure(2)
subplot(2,1,1)
surfc(xx,yy,-f)
shading interp
hold on
plot3(x_1,y_1,-fval,'marker','o','markersize',5,'markerfacecolor','g')
title('unbounded inputs')
subplot(2,1,2)
plot(-fval)
xlabel('iterations')
ylabel('function minimum')
% 2nd case study statistical behavior
tic
for s = 1:num_case;
[input,fval(s)]=ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6])
x_2(s)=input(1);
y_2(s)=input(2);
end
study2_time = toc
figure(3)
subplot(2,1,1)
surfc(xx,yy,-f)
shading interp
hold on
plot3(x_2,y_2,-fval,'marker','o','MarkerSize',5,'MarkerFaceColor','g')
title('unbounded inputs')
subplot(2,1,2)
plot(-fval)
xlabel('iterations')
ylabel('function minimum')
% 3rd case study increasing ga iterations
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',150);
tic
for m = 1:num_case;
[input,fval(m)]=ga(@stalagmite,2,[],[],[],[],[0,0],[.6,.6],[],[],options);
x_3(m)= input(1);
y_3(m)= input(2);
end
study3_time = toc
figure(4)
subplot(2,1,1)
surfc(xx,yy,-f)
shading interp
hold on
plot3(x_3,y_3,-fval,'marker','o','MarkerSize',5,'MarkerFaceColor','g')
title('unbounded inputs')
subplot(2,1,2)
plot(-fval)
xlabel('iterations')
ylabel('function minimum')
FUNCTION FILE :
function A=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);
A=-(f1*f2*f3*f4);
end
RESULTS FOR BASIC STALAGMITE FUNCTION AND CASE STUDY(1)
STALAGMITE BASIC:
As we can see that there are more up-growing peaks,basically are my stalagmite structure. However,the function tends to give the negative value which gives the local mimima's,so to make the function positive for getting the local and global maxima's.(upgrowing peaks).The highest peak is called global maxima and others are local maxima's.
CASE STUDY (1)
Here,in the case study (1),the solutions are not bounded so the GA will calculate all the possible maxima in 'x' and 'y' cooridinates. And the time taken to calculate the possible solution is 4.4339.
STUDY(2):
As the solution is disperesed in space we are limiting it to the global maxima simple wer are converging the area for GA function for the loop for 50 times means we are evolving our solutiuon to get better results for every generation to generation.
MATLAB CODE(2):
% 2nd case study statistical behavior
tic
for s = 1:num_case;
[input,fval(s)]=ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6])
x_2(s)=input(1);
y_2(s)=input(2);
end
study2_time = toc
figure(3)
subplot(2,1,1)
surfc(xx,yy,-f)
shading interp
hold on
plot3(x_2,y_2,-fval,'marker','o','MarkerSize',5,'MarkerFaceColor','g')
title('unbounded inputs')
subplot(2,1,2)
plot(-fval)
xlabel('iterations')
ylabel('function minimum')
RESULT:
As we can see that the time taken ion result two studies is 6.1952 for the reading of global maxima.here,are using genetic algorithm tool to bound the lower and upper value,because when we bopund the upper and lower value then the ga tool will search in that range only.Here,the lower and upper bound are taken as (0 and 6) for the x and y-direction.
CASE STUDY(3):
MATLAB CODE
% 3rd case study increasing ga iterations
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',150);
tic
for m = 1:num_case;
[input,fval(m)]=ga(@stalagmite,2,[],[],[],[],[0,0],[.6,.6],[],[],options);
x_3(m)= input(1);
y_3(m)= input(2);
end
study3_time = toc
figure(4)
subplot(2,1,1)
surfc(xx,yy,-f)
shading interp
hold on
plot3(x_3,y_3,-fval,'marker','o','MarkerSize',5,'MarkerFaceColor','g')
title('unbounded inputs')
subplot(2,1,2)
plot(-fval)
xlabel('iterations')
ylabel('function minimum')
RESULT :
STUDY3_TIME = 36.4559
The resulted line froim high peak to low peak is the critical point for my global maxima and the time taken by it is 36.4559 which is approx 3 time greater than the case study one. As the population size is large enough to carry out 50 iterations at the same time.However,we got our optimum maximized solution.
MATLAB FINAL CODE FOR ALL THREE STUDIES:
clear all
close all
clc
%defining our search space
x =linspace(0,0.6,15
y =linspace(0,0.6,150);
num_case=50;
%creating a 2D 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
tic
for k=1:num_case;
[input,fval(k)]=ga(@stalagmite,2);
x_1(k)=input(1);
y_1(k)=input(2);
end
study1_time=toc
figure (1)
hold on
grid on
surfc(xx,yy,-f)
shading interp
title('stalagmite plot')
xlabel('x')
ylabel('y')
figure(2)
subplot(2,1,1)
surfc(xx,yy,-f)
shading interp
hold on
plot3(x_1,y_1,-fval,'marker','o','markersize',5,'markerfacecolor','g')
title('unbounded inputs')
subplot(2,1,2)
plot(-fval)
xlabel('iterations')
ylabel('function minimum')
% 2nd case study statistical behavior
tic
for s = 1:num_case;
[input,fval(s)]=ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6])
x_2(s)=input(1);
y_2(s)=input(2);
end
study2_time = toc
figure(3)
subplot(2,1,1)
surfc(xx,yy,-f)
shading interp
hold on
plot3(x_2,y_2,-fval,'marker','o','MarkerSize',5,'MarkerFaceColor','g')
title('unbounded inputs')
subplot(2,1,2)
plot(-fval)
xlabel('iterations')
ylabel('function minimum')
% 3rd case study increasing ga iterations
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',150);
tic
for m = 1:num_case;
[input,fval(m)]=ga(@stalagmite,2,[],[],[],[],[0,0],[.6,.6],[],[],options);
x_3(m)= input(1);
y_3(m)= input(2);
end
study3_time = toc
figure(4)
subplot(2,1,1)
surfc(xx,yy,-f)
shading interp
hold on
plot3(x_3,y_3,-fval,'marker','o','MarkerSize',5,'MarkerFaceColor','g')
title('unbounded inputs')
subplot(2,1,2)
plot(-fval)
xlabel('iterations')
ylabel('function minimum')
Some questions to be clarified can we trust genetic algorithms?if yes?why?
In many problems,GAs have a tendency to converge towards local optima or even arbitrary point rather than the global optimum of the problem. this means that it does not "know-how" to sacrifice short-term fitness to gain longer-term fitness.In
evolution strategies and evolutionary programming,diversity is not essential because of a greater reliance on mutation.
CONCLUSION:
By using basic commands in matlab dwe got the stalagmite plot.The statistical behavior of the stalagmite plot is generated.And finally,we optimized our plot and go to its critical point to reach the global maxima by using the GA tool.We also came to know that increasing the population sized and iteration it resulted in optimization of the stalagmite function generation to GA.