Challenge

Week 4 - Genetic Algorithm

GENETIC ALGORITHM USING MATLAB

AIM :

Aim of this project is to understand the concept of genetic algorithm and writing a code in MATLAB to optimize the stalagmite function and the find the global maxima of the function .

INTRODUCTION :

Genetic algorithm solver for mixed-integer or continuous-variable optimization, constrained or unconstrained .Genetic algorithm solves smooth or nonsmooth optimization problems with any types of constraints, including integer constraints. 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.

OPTIMISATION :

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.

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 GENETIC ALGORITHM:

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 GENETIC ALGORITHMS :

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.\

GA MOTIVATION :

Genetic Algorithms have the ability to deliver a “good-enough” solution “fast-enough”. This makes genetic algorithms attractive for use in solving optimization problems. The reasons why GAs are needed are as follows :

SOLVING DIFFICULT PROBLEMS :

In computer science, there is a large set of problems, which are NP-Hard. What this essentially means is that, even the most powerful computing systems take a very long time (even years!) to solve that problem. In such a scenario, GAs prove to be an efficient tool to provide usable near-optimal solutions in a short amount of time

FAILURE OF GRADIENT BASED METHODS:

Traditional calculus based methods work by starting at a random point and by moving in the direction of the gradient, till we reach the top of the hill. This technique is efficient and works very well for single-peaked objective functions like the cost function in linear regression. But, in most real-world situations, we have a very complex problem called as landscapes, which are made of many peaks and many valleys, which causes such methods to fail, as they suffer from an inherent tendency of getting stuck at the local optima as shown in the following figure.

CODE:

%Genetic algorithm 
clear all 
close all
clc

% independent variables

x=linspace(0,0.6,150);
y=linspace(0,0.6,150); 
% matrix 
[xx,yy]= meshgrid(x,y); 
%Number of iterations
num_cases=50;

% starting the loop
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

% creates a three dimensional surface 
figure(1)

surfc(xx,yy,-f)
shading interp %makes the surface look more smoother
xlabel('x value')
ylabel('y value')
title('Stalagmite Function')

%study 1
tic

for i=1:num_cases

[inputs,fopt(i)]=ga(@stalagmite, 2);
xopt(i)= inputs(1);
yopt(i)= inputs(2);

end

study_1=toc
figure(2)
subplot(2,1,1)
hold on
surface(x,y,-f) 
title('Unbounded inputs')
xlabel('x value')
ylabel('y value')

shading interp
hold on

plot3(xopt,yopt, - fopt, 'marker','o', 'markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maximum')

% case 2 with upper and lower bounds

tic;

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

study_2_time=toc
figure(3) 
subplot(2,1,1)
surface(x,y,-f)
shading interp 
title('Bounded inputs')
xlabel('x value')
ylabel('y value')

hold on

plot3(xopt, yopt, -fopt, 'marker','o', 'marker size',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maximum')

%study 3 increasing the population size
% by doing so we are increasing the probability of 
% to converge at global maximum

options=optimoptions(@ga) 
options=optimoptions (@ga, 'PopulationSize',500)%increasing the population size 

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
study_3_time =  toc %gives the time taken for study
figure(4)
subplot(2,1,1)
surface(x,y,-f) %creates a surface to visualise 3D 
shading interp
title('bounded inputs with increased population size')
xlabel('x value')
ylabel('y value')

hold on
plot3(xopt, yopt, -fopt, 'marker', 'o', 'marker size',4,'markercolor','r')
subplot(2,1,2)
plot(-fopt) %plotting -f vs number of iterations
xlabel('iterations')
ylabel('function maximum')
function [f] = stalagmite(input_vector)

a=input_vector (1);
b=input_vector(2);

term1=(sin((5.1*pi*a)+0.5))^6;
term 2=exp(-4*log(2)*((a-0.0667)^2)/0.64); 
term3=(sin((5.1*pi*b)+0.5))^6; 
term4=exp((-4)*log(2)*((b-0.0667)^2)/0.64);

[f]=-(term1*term2*term3*term4);%inverts the objective function

end

OUTPUT:

REFERENCES:

http://www.matlabtrading.com/2016/12/testing-and-analysis-of-algorithmic-trading-strategies-in-matlab-part4-genetic-algorithms.html

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

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.