Genetic Algorithm
Genetic Algorithm
Aim-To Optimize the stalagmite function and find the global maxima of the function .
Theory-
Genetic Algorithm (GA) is a search-based optimization technique based on the principles of Genetics and Natural Selection. It is frequently used to find optimal or near-optimal solutions to difficult problems which otherwise would take a lifetime to solve. It is frequently used to solve optimization problems, in research, and in machine learning.
Introduction to Optimization
Optimization is the process of making something better. In any process, we have a set of inputs and a set of outputs as shown in the following figure.

Optimization 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.
What are Genetic Algorithms?
Nature has always been a great source of inspiration to all mankind. Genetic Algorithms (GAs) are search based algorithms based on the concepts of natural selection and genetics. GAs are a subset of a much larger branch of computation known as Evolutionary Computation.
GAs were developed by John Holland and his students and colleagues at the University of Michigan, most notably David E. Goldberg and has since been tried on various optimization problems with a high degree of success.
In GAs, we have a pool or a population of possible solutions to the given problem. These solutions then undergo recombination and mutation (like in natural genetics), producing new children, and the process is repeated over various generations. Each individual (or candidate solution) is assigned a fitness value (based on its objective function value) and the fitter individuals are given a higher chance to mate and yield more “fitter” individuals. This is in line with the Darwinian Theory of “Survival of the Fittest”.
In this way we keep “evolving” better individuals or solutions over generations, till we reach a stopping criterion.
Genetic Algorithms are sufficiently randomized in nature, but they perform much better than random local search (in which we just try various random solutions, keeping track of the best so far), as they exploit historical information as well.
Advantages of GAs
GAs have various advantages which have made them immensely popular. These include −
- Does not require any derivative information (which may not be available for many real-world problems).
- Is faster and more efficient as compared to the traditional methods.
- Has very good parallel capabilities.
- Optimizes both continuous and discrete functions and also multi-objective problems.
- Provides a list of “good” solutions and not just a single solution.
- Always gets an answer to the problem, which gets better over the time.
- Useful when the search space is very large and there are a large number of parameters involved.
Limitations of GAs
Like any technique, GAs also suffer from a few limitations. These include −
- GAs are not suited for all problems, especially problems which are simple and for which derivative information is available.
- Fitness value is calculated repeatedly which might be computationally expensive for some problems.
- Being stochastic, there are no guarantees on the optimality or the quality of the solution.
- If not implemented properly, the GA may not converge to the optimal solution.
Basic Structure
The basic structure of a GA is as follows −
We start with an initial population (which may be generated at random or seeded by other heuristics), select parents from this population for mating. Apply crossover and mutation operators on the parents to generate new off-springs. And finally these off-springs replace the existing individuals in the population and the process repeats. In this way genetic algorithms actually try to mimic the human evolution to some extent.
Each of the following steps are covered as a separate chapter later in this tutorial.

One of the most important decisions to make while implementing a genetic algorithm is deciding the representation that we will use to represent our solutions. It has been observed that improper representation can lead to poor performance of the GA.
Therefore, choosing a proper representation, having a proper definition of the mappings between the phenotype and genotype spaces is essential for the success of a GA.
In this section, we present some of the most commonly used representations for genetic algorithms. However, representation is highly problem specific and the reader might find that another representation or a mix of the representations mentioned here might suit his/her problem better.
Application-
Genetic Algorithms are primarily used in optimization problems of various kinds, but they are frequently used in other application areas as well.
In this section, we list some of the areas in which Genetic Algorithms are frequently used. These are −
- Optimization− Genetic Algorithms are most commonly used in optimization problems wherein we have to maximize or minimize a given objective function value under a given set of constraints. The approach to solve Optimization problems has been highlighted throughout the tutorial.
- Economics− GAs are also used to characterize various economic models like the cobweb model, game theory equilibrium resolution, asset pricing, etc.
- Neural Networks− GAs are also used to train neural networks, particularly recurrent neural networks.
- Parallelization− GAs also have very good parallel capabilities, and prove to be very effective means in solving certain problems, and also provide a good area for research.
- Image Processing− GAs are used for various digital image processing (DIP) tasks as well like dense pixel matching.
- Vehicle routing problems− With multiple soft time windows, multiple depots and a heterogeneous fleet.
- Scheduling applications− GAs are used to solve various scheduling problems as well, particularly the time tabling problem.
- Machine Learning− as already discussed, genetics based machine learning (GBML) is a niche area in machine learning.
- Robot Trajectory Generation− GAs have been used to plan the path which a robot arm takes by moving from one point to another.
- Parametric Design of Aircraft− GAs have been used to design aircrafts by varying the parameters and evolving better solutions.
- DNA Analysis− GAs have been used to determine the structure of DNA using spectrometric data about the sample.
- Multimodal Optimization− GAs are obviously very good approaches for multimodal optimization in which we have to find multiple optimum solutions.
- Traveling salesman problem and its applications− GAs have been used to solve the TSP, which is a well-known combinatorial problem using novel crossover and packing strategies.
Syntax for ga-
[x,fval,exitflag,output,population,scores]=ga(func,nvars,A,b,Aeq,Beq,lb,ub,nonlcon,intcon,options)
x=optimum solution
exitflag=reason ga stopped
output=information about optimization process
population=final population
scores=final score
f=ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,intCon,options)
fun=fitness function
nvars=number of varaible
A,b=linear inequality constraints
Aeq,beq=linear equality constraint
lb=lower bound(limit)
ub=upper bound(limit)
nonlcon=use for nonlinear constraint
intCon=to get solution integer
option=to set population size minimize with non-default option
Body of the content-
- Fitness Function
`f(x,y)=f1,xf2,xf1,yf2,y`
`f1.x=[sin(5.1pix+0.5)]^6`
`f2,x=exp[-4ln(2)frac{(x-0.667)^2}{0.64}]`
`f2,y=exp[-4ln(2)frac{(x-0.667)^2}{0.64}]`
The above equation are used in the stalagmite function to plot the graph in main function.
function [f]=stalagmite(input_vector)
x=input_vector(1);
y=input_vector(2);
f1_x=(sin(5.1*pi*x+0.5)).^6;
f1_y=(sin(5.1*pi*y+0.5)).^6;
t1=((x-0.0667).^2)/(0.64);
t2=((y-0.0667).^2)/(0.64);
f2_x=exp(-4.*log(2)*t1);
f2_y=exp(-4.*log(2)*t2);
f=-(f1_x.*f2_x.*f1_y.*f2_y);
end
- Statical study without boundation
clear all
close all
clc
%define the search space
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
% ga run for 50 iteration
iteration=50;
%creating 2d dimensional mesh
[xx yy]=meshgrid(x,y);
%stalgamite 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
% statical behavior (unbounded)
% iteration time count start
tic
for i=1:iteration;
[inputs,f_opt(i)]=ga(@stalagmite,2);
x_opt(i)=inputs(1);
y_opt(i)=inputs(2);
end
%iteration time count end
iteration_2_time=toc
%plotting the graph
figure(1)
subplot(2,1,1)
surfc(xx,yy,-f)
hold on
shading interp
plot3(x_opt,y_opt,-f_opt,'marker','o','markersize',5,'markerfacecolor','r');
title('unbounded inputs')
xlabel('x')
ylabel('y')
grid on
subplot(2,1,2)
plot(-f_opt);
xlabel('iteration')
ylabel('function min value')
2. Statical Study with Boundation
clear all
close all
clc
%define the search space
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
% ga run for 50 iteration
iteration=50;
%creating 2d dimensional mesh
[xx yy]=meshgrid(x,y);
%stalgamite 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
% iteration time count start
tic
for j=1:iteration;
[inputs,f_opt(j)]=ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6]);
x_opt(j)=inputs(1);
y_opt(j)=inputs(2);
end
%iteration time count end
iteration_2_time=toc
subplot(2,1,1)
hold on
surfc(xx,yy,-f);
shading interp
plot3(x_opt,y_opt,-f_opt,'marker','o','markersize',5,'markerfacecolor','r');
title('bounded inputs')
xlabel('x')
ylabel('y')
grid on
subplot(2,1,2)
plot(-f_opt);
xlabel('iteration')
ylabel('function max value')
3. Bounded input with modified Population size
clear all
close all
clc
%define the search space
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
% ga run for 50 iteration
iteration=50;
%creating 2d dimensional mesh
[xx yy]=meshgrid(x,y);
%stalgamite 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
%increse the size of population by using optimaization function
options=optimoptions('ga');
% iteration time count start
tic
for j=1:iteration;
[inputs,f_opt(j)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
x_opt(j)=inputs(1);
y_opt(j)=inputs(2);
end
%iteration time count end
iteration_3_time=toc
subplot(2,1,1)
hold on
surfc(xx,yy,-f);
shading interp
plot3(x_opt,y_opt,-f_opt,'marker','o','markersize',5,'markerfacecolor','r');
title('bounded input with modified population size')
xlabel('x')
ylabel('y')
grid on
subplot(2,1,2)
plot(-f_opt);
xlabel('iteration')
ylabel('function min value')
Output-



Conclusion-
ga is useful for optimizing the function but still the result are not accurate as the maximum value for the function lies at-1 to overcome that using the given programme .