Challenge

Week 4.1 - Genetic Algorithm

AIM:- To write a code in matlab to optimise the stalagmite function and find global maxima of the function

THEORY:-

(1) About GA:-

It is based upon evolution theory.

In operation research,genetic algorithm is defined as the process of natural selection that belongs to a larger class of evolutionary algorithms.

A Genetic Algorithm is a method for solving for both constrained and unconstrained optimisation problems based on a natural selection process that mimics biological evolution.The algorithm repeatedly modifies a population of individual solutions.

At each step genetic algorithm randomly selects individuals from the current population and uses them as parents to produce the children of next generation.

Genetic algorithm are commomly used to generate high quality solutions to optimisation and search problems by relying on biological factors.

(2) Optimisation:-

An area of study that deals with how to minimize or maximize a particular function which depend upon multiple input parameters.Optimization refers to finding the values of input in such a way that we get the best output values.The definition of best depends on problem to problem but in mathematical terms it refers to maximizing or minimizing one or more objective function.

The set of all possible solutions or values which the inputs can take up the search space.The aim of oprimisation is to find that point or set of points in search space.

There are several methods to go about optimisation:

One of the method is Stalagmite Function

(3) Stalagmite function:-

It is a mathematical model that actually help us generate local minima and maxima.

Local maxima:- Tiny peaks

Local minima:- huge peaks=point at corner

(4) Stopping criteria:-

The termination condition in GA is important in determining when GA run will end. It has been observed that initially , the GA progress very fast with better solutions coming in very few iterations but this tend to saturate in the later stages where the improvements are very small.We usually want a termination condition such that our solution is close to the optimal, at the end of the run.

Usually we keep one of the termination conditions:-

1) When there has been no improvement in the population for X-number of generations.

2) When we reach an absolute number of generation

3) When the objective function has reached a certain predefined value.

(5) Synatx for GA:-

The syntax used for genetic algorithm is {X=ga(fun,nvars)} finds a local unconstrained minimum where

x is the objective function

nvars is the dimension of fun.

CODE :-

(1) Function code:-

function[f] = getstalagmite (inputs)
x = inputs(1);
y = inputs(2);
f1 = (sin(5.1*pi*x+0.5))^6;
f2 = (sin(5.1*pi*x+0.5))^6;
f3 = exp(-4*log(2)*(x-0.0667)^2/0.64);
f4 = exp(-4*log(2)*(x-0.0667)^2/0.64);
[f] = -(f1*f2f3*f4)
end
(2) Main code:-
clear all
close all
clc
% Defining our surface space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 50;
%Creating a 2D mesh
[xx,yy] = meshgrid(x,y);
for i = 1:length(xx)
for j = 1:length(yy)
inputs(1) = xx(i,j);
inputs(2) = yy(i,j);
f(i,j) = getstalagmite(inputs);
end
end
tic
% Study1 - Statistical Behaviour
for k = 1:num_cases
[inputs,fopt(k)] = ga(@getstalagmite,2);
xopt(k) = inputs(1);
yopt(k) = inputs(2);
end
figure(1)
subplot(2,1,1)
surfc(xx,yy,-f)
hold on
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')
study1_time = toc;
tic
% Study2 - Statistical behaviour with upper and lower bounds
for i = 1:num_cases
[inputs,fopt(i)] = ga(@getstalagmite,2,[],[],[],[],[0,0],[0.3,0.3]);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
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')
study2_time = toc;
tic
% Study3 - Increasing GA iteration
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',150);
for i = 1:num_cases
[inputs,fopt(i)] = ga(@getstalagmite,2,[],[],[],[],[0,0],[0.3,0.3],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
figure(3)
subplot(2,1,2)
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')
study3_time = toc;
Procedure for function code:-
In this we have written the for loop code to put all the formulas into it which is going to be used as formulae used in GA.
We have taken many functions to simulate the formulae and have place it in the main function itself.
The inputs x and y are provided into it
Procedure for the main code:-
  1. First write all the basic commands
  2. Then a one dimensional array created with inbuilt values using linspace command
  3. For the 1D array a 2D mesh is created
  4. getstalagmite function is called
  5. surfc(xx,yy,-f) here -f, is taken to calculate teh maxima because it will always give you the value of minima
  6. shadind interp:- code is used to perform shading instead of lines
  7. tic&toc :- this is used as a start stopwatch timer.
  8. the graph is labelled as x and y axis
  9. title has been given for the particular graph
  10. the same method is followed for all the 3 study cases

But even though the code is complete there is an error in the 17th line and I couldnt rectify tus a sample program with graph that i have written to understand the function of stalagmite function am attaching it below:

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.