Challenge

Week 4.1 - Genetic Algorithm

Genetic Algorithm

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.

  • Genetic Algorithm is based on the Darwin's Evolution theory.
  • Genetic algorithm is the optimization function in Matlab which is used to find the Global minima in the function.

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.

The syntax for ga in MATLAB

There are many syntax used for Genetic algorithm some of them are follows

x = ga(fun,nvars)

x = ga(fun,nvars,A,b)

x = ga(fun,nvars,A,b,Aeq,beq)

x = ga(fun,nvars,A,b,Aeq,beq,lb,ub)

x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon)

x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options)

Here

ga is the syntax for Genetic Algorithm

fun refers to the Function or Equation which need to be optimised. For exapmle sttalgmite function, ode45 etc.

nvars is the no of variables in the Function.

x finds a local minimum to function.

lb and ub refers to the lower and upper limit of the function.

A and B are subjected to linear inequalities A*x ≤ b.

The function 'nonlcon' accepts x and returns vectors C and Ceq, representing the nonlinear inequalities and equalities respectively.

options refers to find the optimum solution of the functions.

Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.

function [f] = stalagmite(input_vector)
x = input_vector(1);
y = input_vector(2);
f1x = (sin(5.1*pi*x+0.5)).^6;
f1y = (sin(5.1*pi*y+0.5)).^6;
f2x = exp(-4*log(2)*(((x-0.0667).^2)/0.64));
f2y = exp(-4*log(2)*(((y-0.0667).^2)/0.64));
f = -(f1x*f2x*f1y*f2y);
end
clear all
close all
clc
%Defining our search space
num_cases = 150;
x = linspace(0,0.6,200);
y = linspace(0,0.6,200);
% Creating a 2-D dimensional 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
% Study 1 statistical behaviour
for i= 1:num_cases
[inputs, fopt(i)] = ga(@stalagmite, 2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study1_time = toc
fopt = -fopt;
figure(1)
subplot(2,2,1)
hold on 
surfc(x,y,-f)
xlabel('x value')
ylabel('y value')
shading interp 
plot3(xopt,yopt,fopt,'Marker','o','MarkerSize',5,'MarkerFaceColor','r')
title ('Unbounded inputs')
subplot(2,2,2)
plot(fopt)
xlabel('iterations')
ylabel('maximum function')
tic
% Study 2- Statistical behaviour with upper and lower bound
for i = 1:num_cases
[inputs, fopt(i)] = ga(@stalagmite, 2,[],[],[],[],[0;0],[0.6;0.6]);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study2_time =toc
figure(2)
subplot(2,1,1)
hold on 
surfc(x,y,-f)
xlabel('x value')
ylabel('y value')
shading interp 
plot3(xopt,yopt,fopt,'Marker','o','MarkerSize',5,'MarkerFaceColor','r')
title ('Bounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function minimum ')
% Study 3 increasing GA iterations
options = optimoptions("ga");
options = optimoptions(options,'PopulationSize',200);
tic
for i = 1: num_cases
[inputs, fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study3_time = toc
figure(3)
subplot(2,1,1)
hold on 
surfc(x,y,-f)
xlabel('x value')
ylabel('y value')
shading interp 
plot3(xopt,yopt,fopt,'Marker','o','MarkerSize',5,'MarkerFaceColor','r')
title ('Bounded inputs with population size')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function minimum ')
  1. Firstly we have take out the Stalagmite formula into the function 'f' and put the x & y equtions into the function.
  2. As the function will get and x & y as an input vector it will give the results accordingly.
  3. To find the Global Maxima of the stalagmite function we will use Genetic Algorithm. But GA is used to find the Global minima of the function.
  4. To find the Global Maxima of the function we will put a minus sign in the stalagmite function so that the GA will calculate the maxima of stalgmite function.
  5. While calculating the Global maxima for the stalagmite function we have performed 3 case studies:- Case study 1- with No lower and upper Bound. Case study 2- with lower and upper Bound. Case study 3- with lower and upper Bound along with Optimum options.
  6. In case study 1 with unbounded input we are getting multiple Global maxima with multiple iterations.(Graphs Attached below for Reference).This is happenening because we have not Bounded the input variable and GA is taking Different Values in each iteration and Finding Maxima for those Values.
  7. In case study 2 we have Bounded the Input variables with upper and lower Boundaries(Graphs Attached below for Reference). Even after that we are getting 4-5 Maxima values for Stalagmite Function.
  8. In Case study 3 we have add the optimum option with upper and lower Boundaries in GA function to avoid multiple results and Hence we are getting same results with Multiple Iterations.

Output Graphs

  • Study 1
  • Study 2
  • Study 3

Refernce

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.