Challenge

Genetic Algorithm

AIM: To calculate the global maxima of a stalagmite function in MATLAB

OBJECTIVE:

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

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 that otherwise would take a lifetime to solve. It is frequently used to solve optimization problems, in research, and in machine learning.

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.

A genetic algorithm is a search heuristic that is 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. 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 that 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 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 that are simple and for which derivative information is available.
  • The 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.

The genetic algorithm uses three main types of rules at each step to create the next generation from the current population:

  • Selection rules select the individuals, called parents, that contribute to the population of the next generation.
  • Crossover rules combine two parents to form children for the next generation.
  • Mutation rules apply random changes to individual parents to form children.

GOVERNING EQUATIONS :

Syntax: ga

x = ga(fun,nvars) finds a local unconstrained minimum, x, to the objective function, fun. nvars is the dimension (number of design variables) of fun.

x = ga(fun,nvars,A,b,Aeq,beq) 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').

x = ga(fun,nvars,A,b,Aeq,beq,lb,ub) 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.)

x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options) minimizes with the default optimization parameters replaced by values in options. (Set nonlcon=[] if no nonlinear constraints exist.) Create options using optimoptions.

MATLAB CODE :

Main code

clearvars
close all
clc

%% Defining global maxima

x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
% creaating 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);
        input_vector;                                         
        f(i,j)  = stalagmite(input_vector);
      
    end
    
end

%% Study1-Statistical behaviour with unbounded input values
num_cases = 50;
for a=1:num_cases
    [inputs,fopt_1(a)]=ga(@stalagmite,2);
    xopt(a)=inputs(1);
    yopt(a)=inputs(2);
end
study1_time=toc

%% plotting our data


figure(1)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp

plot3(xopt,yopt,-fopt_1,'marker','o','markerfacecolor','r','markersize',5)
title('Unbounded inputs')
subplot(2,1,2)
plot(-fopt_1)
xlabel('Iterations')
ylabel('Function maximum')
saveas(figure(1),'global maxima Study 1.jpg')
%% Study2-Statistical behaviour with upper and lower bounded input values

for b=1:num_cases
    [inputs,fopt_2(b)]= ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
    xopt(b)=inputs(1);
    yopt(b)=inputs(2);
end
study2_time = toc


% plotting our data

figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp

plot3(xopt,yopt,-fopt_2,'marker','o','markerfacecolor','r','markersize',5)
title('Bounded inputs')
subplot(2,1,2)
plot(-fopt_2)
xlabel('Iterations')
ylabel('Function maximum')
saveas(figure(2),'global maxima Study 2.jpg')
%% Study3-Statistical behaviour with upper and lower bounded input values and increasing genetic algorithm iterations

option_1 = optimoptions('ga');
option_1 = optimoptions(option_1,'Populationsize',600);

tic
for c=1:num_cases
    [inputs,fopt_3(c)]= ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],option_1);
    xopt(c)=inputs(1);
    yopt(c)=inputs(2);
end
study3_time = toc

%% plotting our data

figure(3)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp

plot3(xopt,yopt,-fopt_3,'marker','o','markerfacecolor','r','markersize',5)
title('Bounded inputs with increasing population size')
subplot(2,1,2)
plot(-fopt_3)
xlabel('Iterations')
ylabel('Function maximum')
saveas(figure(3),'global maxima Study 3.jpg')

stalagmite function code

function [f]  = stalagmite(input_vector)
% Inputs

p = input_vector(1);
q = input_vector(2);
    f1x = (sin(5.1*pi*p + 0.5))^6;
    f1y = (sin(5.1*pi*q + 0.5))^6;
    f2x = exp(-4*log(2)*(((p-.0667)^2)/.64));
    f2y = exp(-4*log(2)*(((q-.0667)^2)/.64));
    
    f = -f1x*f1y*f2x*f2y;
   
end

Result:

1) Unbounded inputs study

2) Bounded input study

3) Bounded inputs with increased population size

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.

Reference :

  1. https://in.mathworks.com/help/gads/ga.html
  2. https://in.mathworks.com/help/gads/what-is-the-genetic-algorithm.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.