Project

Genetic Algorithm

AIM :

To calculate the global maxima of a stalagmite function.

OBJECTIVE :

  • To write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
  • To plot graphs for all 3 studies and for F maximum vs no. of iterations.

EQUATIONS:

Equations that are used in the stalagmite functions are:

THEORY :

STALAGMITE FUNCTION:

A stalagmite function is used to get local minima or maxima. For this particular problem we need to optimise it to get the maximum value. This is done with the help of genetic algorithm.

ABOUT GENETIC ALGORITHM:

Genetic Algorithm (GA) is a search-based optimization technique based on the principles of Genetics and Natural Selection, in line with the Darwinian Theory of “Survival of the Fittest”. It is frequently used to find optimal or near-optimal solutions to difficult problems.

Optimization is the process of making something better. 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.

Five phases are considered in a genetic algorithm.

  1. Initial population
  2. Fitness function
  3. Selection
  4. Crossover
  5. Mutation

The process begins with a set of individuals which is called a Population. Each individual is a solution to the problem you want to solve. An individual is characterized by a set of parameters (variables) known as Genes. Genes are joined into a string to form a Chromosome (solution).

For example if an individual A = 1 0 1 1 0

Each 1 or 0 are a gene, but the string of 1 0 1 1 0 together is a chromosome. And a group of individuals is called a population.

The fitness function determines how fit an individual is (the ability of an individual to compete with other individuals). It gives a fitness score to each individual. The probability that an individual will be selected for reproduction is based on its fitness score.

Two pairs of individuals (parents) are selected based on their fitness scores. Crossover is for each pair of parents to be mated, a crossover point is chosen at random from within the genes. Offspring are created by exchanging the genes of parents among themselves until the crossover point is reached.

Example

A1 = 0 0 0 0 0 0

A2 = 1 1 1 1 1 1

if cross over point is 2 then their children will be

A3 = 1 1 0 0 0 0

A4 = 0 0 1 1 1 1

In certain new offspring formed, some of their genes can be subjected to a mutation with a low random probability. This implies that some of the genes in their chromosome can be flipped. Finally, the new children are recombined amongst themselves creating a new population.

The algorithm terminates if the population has converged (does not produce offspring which are significantly different from the previous generation). Then it is said that the genetic algorithm has provided a set of solutions to our problem.

Advantages of GA's :

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 GA's :

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.

STALAGMITE FUNCTION:

A stalagmite function is used to get local minima or maxima. For this particular problem we need to optimise it to get the maximum value. This is done with the help of genetic algorithm.

SYNTAX OF "ga" :

In MATLAB ‘ga’ is use for find minimum of function using genetic algorithm. Following syntaxes are use for ‘ga’ in MATLAB.

  1. x = ga(fun,nvars) [This finds a local unconstrained minimum, x, to the objective function, fun. nvars is the dimension (number of design variables) of fun.]
  2. x = ga(fun,nvars,A,b) [ This finds a local minimum x to fun, subject to the linear inequalities A*x ≤ b. ga evaluates the matrix product A*x as if x is transposed (A*x').]
  3. x = ga(fun,nvars,A,b,Aeq,beq) [ This finds a local minimum x to fun, subject to the linear equalities Aeq*x = beq and A*x ≤ b. (Set A=[] and b=[] if no linear inequalities exist.) ga evaluates the matrix product Aeq*x as if x is transposed (Aeq*x').]
  4. x = ga(fun,nvars,A,b,Aeq,beq,lb,ub) [ This defines a set of lower and upper bounds on the design variables, x, so that a solution is found in the range lb ≤ x ≤ ub. (Set Aeq=[] and beq=[] if no linear equalities exist.)]
  5. x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon) [ This subjects the minimization to the constraints defined in nonlcon. The function nonlcon accepts x and returns vectors C and Ceq, representing the nonlinear inequalities and equalities respectively. ga minimizes the fun such that C(x) ≤ 0 and Ceq(x) = 0. (Set lb=[ ] and ub=[ ] if no bounds exist.)]
  6. x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options) [ This subjects the minimization to the constraints defined in nonlcon. The function nonlcon accepts x and returns vectors C and Ceq, representing the nonlinear inequalities and equalities respectively. ga minimizes the fun such that C(x) ≤ 0 and Ceq(x) = 0. (Set lb=[ ] and ub=[ ] if no bounds exist.)]
  7. x = ga(fun,nvars,A,b,[ ],[ ],lb,ub,nonlcon,IntCon) or x = ga(fun,nvars,A,b, [ ], [ ],lb,ub,nonlcon,IntCon,options [ This requires that the variables listed in IntCon take integer values.]
  8. x = ga(problem) [ This finds the minimum for problem, where problem is a structure.]
  9. [x,fval] = ga(___) [ for any previous input arguments, also returns fval, the value of the fitness function at x.]
  10. [x,fval,exitflag,output] = ga(___) [ This also returns exitflag, an integer identifying the reason the algorithm terminated, and output, a structure that contains output from each generation and other information about the performance of the algorithm.
  11. [x,fval,exitflag,output,population,scores] = ga(___) [ This also returns a matrix population, whose rows are the final population, and a vector scores, the scores of the final population.]

SOLUTION STEPS :

  1. First two input variables (x and y) were created which were one dimensional arrays of 150 equally spaced values between 0 and 0.6 using linspace command and then two dimensional matrix of these variables were created using meshgrid command.
  2. Another function named stalagmite was created separately which stores the two input variables and contains equations for output and was called in main program.
  3. The results from stalagmite function was stored in another variable (f) and plotted as surface using surfc command in MATLAB.
  4. "ga" function was used to calculate the maximum value of stalagmite function, but since "ga" command gives minimum value of function provided so some changes had to be made to get desired results, so a '-' sign was used before (f) to get maximum values from "ga" command.
  5. Then our code was made to iterate 50 times using 'for loop' to study the statistical behaviour of genetic algorithm with unbounded inputs, bounded inputs, bounded inputs with increasing populations and all these studies are plotted individually using subplot,plot3 and sufc command.
  6. The results obtained from all three studies differ from each other as in first study of unbounded inputs, during each iterations the maximum values changes from 0 to 1, so in second study of bounded inputs "ga" command is changed and upper and lower bounds of 0 and 1 are set respectively. In this case, the results obtained are much sophisticated as compared to first one.
  7. Hence, in third study of bounded inputs with increasing population size which was 50 by default, it was incresed to 300. The output results obtained were confined in very small range of 0.99 and it was approximately accuate results. If the population size was increased further, more accurate results will be obtained.

MAIN CODE :

clear all
close all
clc

% SET OF VALUES FOR STALGAMITE FUNCTION
% or defining the search space
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);

[xx yy]=meshgrid(x,y);
num_case=150;

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

%study 1
tic
for i=1:num_case
  [inputs,Fopt(i)]=ga(@stalagmite,2);
  xopt(i)=inputs(1);
  yopt(i)=inputs(2);
end

Study_time_1=toc

figure(1)
subplot(2,1,1);
hold on
surfc(xx,yy,F);
shading interp;
ylabel('y data');
xlabel('x data');
plot3(xopt,yopt,Fopt,'marker','o','markersize',5,'markerfacecolor','k');
title('unbounded inputs');
subplot(2,1,2);
plot(Fopt);
xlabel('iterations');
ylabel('Function maximum');

%study 2
tic
for i=1:num_case
  [inputs,Fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0:0],[1:1]);
  xopt(i)=inputs(1);
  yopt(i)=inputs(2);
end

study_time_2=toc

figure(2)
subplot(2,1,1);
hold on
surfc(xx,yy,F);
shading interp;
ylabel('y data');
xlabel('x data');
plot3(xopt,yopt,Fopt,'marker','o','markersize',5,'markerfacecolor','k');
title('bounded inputs');
subplot(2,1,2);
plot(Fopt);
xlabel('iterations');
ylabel('Function maximum');

%study 3
options=optimoptions('ga');
options=optimoptions(options,'populationsize',450);
tic
for i=1:num_case
  [inputs,Fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0:0],[1:1],[],[],options);
  xopt(i)=inputs(1);
  yopt(i)=inputs(2);
end
study_time_3=toc

figure(3)
subplot(2,1,1);
hold on
surfc(xx,yy,F);
shading interp;
ylabel('y data');
xlabel('x data');
plot3(xopt,yopt,Fopt,'marker','o','markersize',5,'markerfacecolor','k');
title('bounded inputs with increase population size');
subplot(2,1,2);
plot(Fopt);
xlabel('iterations');
ylabel('Function maximum');

max_value=Fopt

CODE FOR STALAGMITE FUNCTION:

function [f] = stalagmite(input_vector)

x = input_vector(1);
y = input_vector(2);

f_1_x = (sin(5.1*pi*x + 0.5))^6;
f_1_y = (sin(5.1*pi*y + 0.5))^6;
f_2_x = exp(-4*log(2)*((x-0.0667)^2/0.64));
f_2_y = exp(-4*log(2)*((y-0.0667)^2/0.64));
% - sign is used to return max values from ga command
[f] = -((f_1_x)*(f_2_x)*(f_1_y)*(f_2_y));     
end

OUTPUT:

study 1:

study 2:

study 3:

Study_time_1 = 127.53
Study_time_2 = 115.00
Study_time_3 = 215.00

CONCLUSION :

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. But if the population size is taken too large, the processing time taken by MATLAB also increases.

REFERENCES :

https://www.tutorialspoint.com/genetic_algorithms/genetic_algorithms_introduction.htm

https://in.mathworks.com/help/gads/what-is-the-genetic-algorithm.html

https://in.mathworks.com/help/gads/ga.html

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.