Week 4.1 - Genetic Algorithm
Aim
To optimize the stalgamite function and find out the global maxima of the function by write a program using MATLAB. To plot for the 3 case studies and find time to take running for each cases.
Objective;
- To write a code in MATLAb to optimize the stalagmite function and find the global Maxima Value of the function.
- To explain the concept of Genetic alogorithm and explain the syntax of ga.
- To plot 3 case studies and for F Maxima and No. fo Iterations with repect to time.
- Introduction,
Stalagmite,
Stalagmites are convex deposits that grow upward, commonly fed by an overhanging stalactite, and show flat, rounded, or slightly hollow tops, but not a central canal as shown in fig.The morphology of stalagmites is mostly determined by the drip rate, and in particular by the interannual variability of the drip rate and by the distance from the tip of the feeding stalactite.By contrast, stalagmites fed by drips that show interannual constant time elapsed between two drops yield candle-shaped stalagmites.

Function,
The function which used in stalagmite,

Optimization,
Optimization is the process of making something better. In any process, we have a set of inputs and a set of outputs.It refers to finding the values of inputs in such a way that we get the “best” output values. The definition of “best” varies from problem to problem, but in mathematical terms, it refers to maximizing or minimizing one or more objective functions, by varying the input parameters.
The set of all possible solutions or values which the inputs can take make up the search space. In this search space, lies a point or a set of points which gives the optimal solution. The aim of optimization is to find that point or set of points in the search space.
Genetic Algorithm.
The genetic algorithm is a method for solving both constrained and unconstrained optimization problems that is based on natural selection, the process that drives biological evolution. The genetic algorithm repeatedly modifies a population of individual solutions. At each step, the genetic algorithm selects individuals at random from the current population to be parents and uses them 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 a variety of optimization 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 can address problems of mixed integer programming, where some components are restricted to be integer-valued.
In genetic algorithm there three types of rules to reach next generation for current generation.
- Selection
- Crossover
- Mutation
Selection,
The selection function chooses parents for the next generation based on their scaled values from the fitness scaling function. The scaled fitness values are called the expectation values. An individual can be selected more than once as a parent, in which case it contributes its genes to more than one child.
Crossover
Crossover children by selecting vector entries, or genes, from a pair of individuals in the current generation and combines them to form a child.
Mutation,
Mutation children by applying random changes to a single individual in the current generation to create a child.
Syntax using in Genetic algorithm,
Here we using three syntax , that find the minimun value,
1. x=ga(fun,navrs)
This syntax used to find local unconstrained minimum value
fun- Function
navrs- No of dimensional variables,
2.x=ga(fun,navrs,A,b,Aeq, beq,lb,up)
This syntax used to find local minimum function with apply the limits by upper bounds and lower bounds,
fun-Functions
navrs- No. of dimensional variables,
A,b-linear inequalities
Aeq,beq- Nonlinear inequalities,
lb, up- upper and Lower bounds,
3. x=ga(fun,navrs,A,b,Aeq,beq,up,lb,nonlcon,intcon,options)
This syntax used in minimum function with non linear constraints by using optimoption syntax,
options=optimoptions(solvername)- returns a set of default options for the solvername solver
options=optimoptions(solvername,Name.Value)- returns option with specified parameters set using one or more name-value pair arguments.
abouve syntax used to optimize the particular set of values functions collecting form the total values of arrays.
Programs.
The programs that create a funcition of stalagmite,
%To create a function of stalagmite
function S=stg_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((-4*log(2))*((input_vector(1)-0.0667)^2/0.64));
f2y=exp((-4*log(2))*((input_vector(2)-0.0667)^2/0.64));
S=-f1x*f1y*f2x*f2y;
end
The Program that finding the Global Maxima,
clear all
close all
clc
x=linspace(0,0.6,150); %set input arrays of x values
y=linspace(0,0.6,150); %setinput arrays of y values
% create a parmaeters using to runs no. of time in ga
num_cases=50;
%Creating 2Dimensional mesh
[xx,yy]=meshgrid(x,y);
%Evaluate stalagmite function by using for loop
for i=1:length(xx)
for j=1:length(yy)
input_vector(1)=xx(i,j);
input_vector(2)=yy(i,j);
S(i,j)=stg_fun(input_vector);
end
end
%Study case 1-statistical behaviour
%To create a loop for upto the parameters to find optimation of max values
%using ga function wrt time taken.
tic %To find time that while running
for k=1:num_cases
[inputs, fopt(k)]=ga(@stg_fun,2);
xopt(k)=inputs(1);
yopt(k)=inputs(2);
end
study1_time=toc;
figure(1)
subplot(2,1,1);
hold on
surfc(xx,yy,-S);
shading interp
%plotting the optimizing values in the parameters with x, y values
plot3(xopt,yopt,-fopt,'Marker','o','markersize',5,'markerfacecolor','r');
subplot(2,1,2);
%Plotting optimized functions values
plot(fopt);
hold off
xlabel('iterations');
ylabel('Funtcion Maximum');
title('Unbounded inputs');
%Study case 2- Statistical behaviour with upper and lower bounds
%To create a loop for upto the parameters to find optimation of max values
%using ga function wrt time taken within the limits.
tic
for k=1:num_cases
[inputs,fopt(k)]=ga(@stg_fun,2,[],[],[],[],[0;0],[1;1]);
xopt(k)=inputs(1);
yopt(k)=inputs(2);
end
study2_time=toc;
figure(2)
subplot(2,1,1);
hold on
surfc(x,y,-S);
shading interp;
%plotting the optimizing values in the parameters with x, y values
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r');
subplot(2,1,2);
%Plotting optimized functions values
plot(fopt);
hold off
xlabel('iterations');
ylabel('Funtcion Maximum');
title('Bounded inputs');
%Study case 3-Increasing GA iterations
%To create a loop for upto the parameters to find optimation of max values
%using ga function wrt time taken within the limits and returns set of
%parameters with options solver.
options=optimoptions('ga');
options=optimoptions(options,'PopulationSize',450);
tic
for k=1:num_cases
[inputs,fopt(k)]=ga(@stg_fun,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(k)=inputs(1);
yopt(k)=inputs(2);
end
study3_time=toc;
figure(3)
subplot(2,1,1);
hold on
surfc(x,y,-S);
shading interp;
%plotting the optimizing values in the parameters with x, y values
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r');
subplot(2,1,2);
%Plotting optimized functions values
plot(fopt);
hold off
xlabel('iterations');
ylabel('Funtcion Maximum');
title('Modifying the Population data');
Explanations
First lets we create a random set of array x,y and meshing into 2 Dimensional array by uisng meshgrid command,a nested loop creat a array thats called input_vector.After create a function from stalagmite uisng foprmula of stalagmite functiion.lets make the function in negative, because we have to find Global maxima value bit in genetic algorithm function use dto find out Global minima value so we used negatives. Then we plot a stalagmite surface by using function in the for loop command.
We have to plot graph for three case studies for Function minimum and No. of iterations, In first case, optimized the functions that using ga syntax upto the parameters with tic-toc command(find time while running fuctions), then give the negative sign for fopt due to reason of have to calculate global maxima,then we plot the graph slagamite with optimized values in the parameters and plot optimed functions.
In second cases,we optimized the function using ga with bounding which given more accuracy and compare with surface of stalagemite. In the third case that used to find out global maxima value of the array, using a ga syntax wiht solver 'optimoptions' and when increase the populations to find out exactct maxima value and plot in the stalgmite which used to find easy.
Graph
Stalagmite Graph
Plot for unbounded values

Plot of Bounded Inputs,

Plot of using Increase the Populations,

the plot has obtained the Global maxima.
Workspace data has been attached below

The image which I worked in the MATLAB,

Conclusion
The Stalagmite function has been optimized and found the Global Maxima value is (x=0.0668,y=0.0668). The Genetic alogorithm has explained breifly and told about the ga syntax which used in the program.Hence,Plotted the all three case sudies which used to find the global maxima and sure its been got same values which runs several times.