Challenge

Week 4.1 - Genetic Algorithm

Aim:

  • To write a code in MATLAB to optimise the given Stalagmite function and plot it.
  • To use function ga(Genetic Algorithm) for optimisation and find the global maxima of the Stalagmite function.

General Procedure:

  • Open a Script file in MATLAB.
  • Use basic commands to close,clear all variables and clear command window.
  • Save the file in directory path.If function file is used ,Save the function file in the function name and save it in the same directory path.

Explanation:

Optimisation:

Optimisation is method to optimise i.e., to maximise or minimise the given function and find the optimial solution for the given function in allowed sets.It is also known as mathematical Programming.

Applications:

Optimisation used in many engineering,Research and development fields.

  • In Economics,Optimisation has been used to analyse,maximise the profit and utility,and minimise the expenditure.
  • Its used in machine learning,Image/signal processing,data classification.
  • Other applications in aerospace engineering are optimial space trajectory.
  • In mechanical engineering,its used in design,Planning,Control & manufacturing, and mathematical model.

Genetic algorithm method for optimisation:

  • A genetic algorithm (GA) is a method for solving both constrained and unconstrained optimization problems based on a natural selection process that mimics biological evolution.
  • GA is a metaheuristic search and optimization technique based on principles present in natural evolution. It belongs to a larger class of evolutionary algorithms.
  • GA maintains a population of chromosomes—a set of potential solutions for the problem.
  • The idea is that “evolution” will find an optimal solution for the problem after a number of successive generations—similar to natural selection.

The genetic algorithm is a random-based classical evolutionary algorithm. By random here we mean that in order to find a solution using the GA, random changes applied to the current solutions to generate new ones. Note that GA may be called Simple GA (SGA) due to its simplicity compared to other EAs.GA is based on Darwin’s theory of evolution. It is a slow gradual process that works by making changes to the making slight and slow changes. Also, GA makes slight changes to its solutions slowly until getting the best solution.

  • GA works on a population consisting of some solutions where the population size (popsize) is the number of solutions.
  • Each solution is called individual. Each individual solution has a chromosome.
  • The chromosome is represented as a set of parameters (features) that defines the individual. Each chromosome has a set of genes.
  • Each gene is represented by somehow such as being represented as a string of 0s and 1s
  • Each individual has a fitness value. To select the best individuals, a fitness function is used. The result of the fitness function is the fitness value representing the quality of the solution. The higher the fitness value the higher the quality the solution.

  • Selection of the best individuals based on their quality is applied to generate what is called a mating pool where the higher quality individual has higher probability of being selected in the mating pool.

  • The individuals in the mating pool are called parents. Every two parents selected from the mating pool will generate two offspring (children).

  • By just mating high-quality individuals, it is expected to get a better quality offspring than its parents.

Applications of Genetic algorithms:

  • Acoustics - GA is used to distinguish between sonar reflections and different types of object.
    Apart from that it is also used to design Active noise control systems which cancel out undesired
    sound by producing sound waves that destructively interfere the unwanted noise.
  • Aerospace Engineering - To design wing shape Super Sonic aircraft minimizing aerodynamic drag
    at supersonic cruising speeds, minimizing drag at subsonic speeds, and minimizing aerodynamic
    load .
  • Financial Markets – To predict the future performance of publicly traded stocks.
  • Geophysics - To locate earthquake hypocenters based on seismological data.
  • Materials Engineering - To design electrically conductive carbon based polymers known as
    polyanilines. To design exposure pattern for an electron lithography beam.
  • Routing and Scheduling - To find optimal routing paths in telecommunication networks which are
    used to relay data from sender to recipients .
  • Systems Engineering - To perform multi–objective task of designing wind turbines used to
    generate electric power.
  • Data mining - GA can also be used in data mining and pattern recognition. Given a large set of
    data, GA can perform a search for the optimum set of data which satisfies the conditions. Initial
    population in this case may be single objective conditions and at the end of the GA, we get a
    combined complex condition which when applied on the large data set can lead to finding the
    required set of data.

Stalagmite :

Stalactites and stalagmites are types of mineral deposits found in caves that accrue through the processes of solution and deposition. Stalactites hang downwards from the ceiling of caves, whereas stalagmites rise upwards from the floor of a cave.

Coding Explanation:

  • Given function is written as code in a separate function file.
  • From Given inputs,variables are created and allocated.
  • 2-D array has been created from given inputs using meshgrid command.
  • Function value has been calculated.
  • From the calculated values, Study 1 has been done using Statistical behaviour method.
  • In this method,no limitations has not been given i.e., no boundaries are applied for iterations.
  • From the outputs,plots are made for stalagmite function using surf,plot3 and plot commands.
  • Another study is done with given boundaries.
  • Here,optimal values has been used with given boundaries and optimal solution has been calculated.
  • Final study has been done using GA(genetic algorithm) method.
  • By increasing population size,optimal solution will be accurate but time taken for processing is too long.
  • Optimial solution has been calculated and finally global maxima is found.

CODING:

close all
clear all
clc

%X,Y values
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);

%Creating 2D mesh
[xx, yy]=meshgrid(x,y);

%Evaluating stalagmite
for k=1:length(xx)
    for j=1:length(yy)
        input_vector(1)=xx(k,j);
        input_vector(2)=yy(k,j);
        F(k,j)=stalagmitefunc(input_vector);
    end
end

num_cases=100;
tic
%study=1 Statistical behaviour
for k=1:num_cases
    [inputs,fopt(k)]=ga(@stalagmitefunc,2);
    xopt(k)=inputs(1);
    yopt(k)=inputs(2);
end
toc
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,-F)
shading interp
plot3(xopt,yopt,-fopt,'Marker','o','MarkerSize',5,'MarkerFaceColor','r')
title('Unbounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('function minimum')


%Study=2 Statistical behaviour with bounded inpuds
tic
for j=1:num_cases
    [input_vector,fopt(j)]=ga(@stalagmitefunc,2,[],[],[],[],[0;0],[0.6;0.6]);
    xopt(j)=input_vector(1);
    yopt(j)=input_vector(2);
end
toc
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,-F)
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',500);
tic
for i=1:num_cases
    [input_vector,fopt(i)]=ga(@stalagmitefunc,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
    xopt(i)=input_vector(1);
    yopt(i)=input_vector(2);
end
toc
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,-F)
shading interp
plot3(xopt,yopt,-fopt,'Marker','o','MarkerSize',5,'MarkerFaceColor','r')
title(' inputs')
subplot(2,1,2)
plot(-fopt)
disp(fopt)
xlabel('Iterations')
ylabel('function minimum')

Function Coding:

function [f]=stalagmitefunc(input_vector)
x=input_vector(1);
y=input_vector(2);
f1=(sin(5.1*pi*input_vector(1) + 0.5).^6);
f2=(sin(5.1*pi*input_vector(2) + 0.5).^6);
f3=exp(-4*log(2)*((input_vector(1)-0.0667).^2)/0.64);
f4=exp(-4*log(2)*((input_vector(2)-0.0667).^2)/0.64);
[f]=-(f1*f2*f3*f4);
end

RESULTS:

  • Optimal Solution has been calculated for given stalagmite function.
  • Graphs are plotted with all iterations for given all studies.
  • Global maxima is found.Global maxima is 1.000000 i.e, 0.999999985.

PLOTS:

Study 1:

Study 2:

Study 3:

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.