Project

OPTIMISATION OF STALAGMITE FUNCTION USING GENETIC ALGORITHM

Aim

To optimise the stalagmite function using Genetic Algorithm (GA).

Genetic Algorithm

GA is one of the optimisation algorithms that is widely used in all engineering fields. It is a search heuristic inspired by Charles Darwin’s theory of natural evolution. In this algorithm, from initial population, the fittest individuals are selected by using fitness function to reproduce offspring for next generation.

Gene

Every individual in a population is characterised by set of parameters called by Genes.

Chromosome

All genes are joined to form chromosomes

Population

A batch of chromosome is called as population.

Working

GA usually works in five phases to complete an optimisation. The phases are:

  1. Initial Population
  2. Fitness function
  3. Selection
  4. Crossover
  5. Mutation

Initial Population

The process begins with a set of individuals which is called initial population

Fitness function

Fitness function determines how an individual is better than the other individuals in a population to optimise the function. It gives a fitness score to each individual. Based on the fitness score, individuals are selected to produce offspring for next generation.

Selection

In selection phase, based on the fitness score, the best individuals are selected and let them pass their genes to the next generation.

Cross over

It is the most significant phase in GA. In this phase, parents will exchange their genes and produce new individuals.

Mutation

The new offspring formed from cross over phase will mutate its genes by itself

GA in MATLAB

In MATLAB, GA minimises the function.

Syntax:

[x, fval] = ga(fun, nvars, A, b, Aeq, Beq, lb, ub, nonlcon, options)

x – value at which the function is having minimum value

fval – gives minimum function value

fun – function to be optimised

nvars – number of design variables in function

A, b represents inequality A*x<=b

lb, ub represents lower and upper bounds of design variables

nonlcon – non-linear constraint

Step-by-step explanation

  1. Define the stalagmite function
  2. Define input variables x and y for the stalagmite function
  3. Pass each and every values of mesh grid into stalagmite function to get the plot of stalagmite function
  4. Three cases are studied in this challenge,
    1. First case is that there is no restriction in the limits of design variables
    2. Upper and lower limits are defined for design variables
    3. More values are taken for creating the offsprings of next generation
  5. In each cases, GA is made to run for 50 times and the maximum value from each iterations are plotted against number of iterations.

Code

clc
clear vars
close all
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
[xx, yy] = meshgrid(x,y);
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

%Reproduceability of Genetic algorithm
%case1: No restrictions in design space
iterations=50;
tic
for i=1:iterations
    [output, fval(i)] = ga(@stalagmite,2);
    x_out(i)=output(1);
    y_out(i)=output(2);
end
study_time1 = toc;
figure(1)
subplot(2,1,1)
hold on
title('No restrictions on design space')
surfc(xx,yy,f)
shading interp
plot3(x_out,y_out,fval,'o','Color','b','MarkerSize',3,'MarkerFaceColor','r')
subplot(2,1,2)
plot(fval,'linewidth',4)
xlabel('No.of iterations');
ylabel('Fmax');
hold off

%case2: Input variables are restricted
iterations_2=50;
tic
for y=1:iterations_2
    [output_2, fval_2(y)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
    x_out_2(y)=output_2(1);
    y_out_2(y)=output_2(2);
end
study_time2 = toc;
figure(2)
subplot(2,1,1)
hold on
title('Inputs restricted')
surfc(xx,yy,f)
shading interp
plot3(x_out_2,y_out_2,fval_2,'o','Color','b','MarkerSize',3,'MarkerFaceColor','r')
subplot(2,1,2)
plot(fval_2,'linewidth',4)
xlabel('No.of iterations');
ylabel('Fmax');
hold off

%case3
options = optimoptions('ga','PopulationSize',100)
iterations_3=50;
tic
for y=1:iterations_3
    [output_3, fval_3(y)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
    x_out_3(y)=output_3(1);
    y_out_3(y)=output_3(2);
end
study_time3 = toc;
figure(3)
subplot(2,1,1)
hold on
title('More input values')
surfc(xx,yy,f)
shading interp
plot3(x_out_3,y_out_3,fval_3,'o','Color','b','MarkerSize',3,'MarkerFaceColor','r')
subplot(2,1,2)
plot(fval_3,'linewidth',3)
xlabel('No.of iterations');
ylabel('Fmax');
axis([1 50 -1 -0.7])
hold off

Function

function [values] = stalagmite(input)
v1 = (sin(5.1*pi*input(1)+0.5))^6;
v2 = (sin(5.1*pi*input(2)+0.5))^6;
v3 = exp((-4*log(2)*(input(1)-0.0667)^2)/0.64);
v4 = exp((-4*log(2)*(input(2)-0.0667)^2)/0.64);
values = -(v1*v2*v3*v4);
end

Results

Case1:

Since there is no limits in design variables, GA will randomly take 50 samples and use them to create next generations. Because of random sampling, results are different in each iterations.

Case2:

In this case, input variables are limited by the given upper and lower bounds. To create the next generation, GA will take random samples between 0 to 1. Hence the results are also obtained within the limits.

Case3:

GA usually takes 50 samples in all iterations. In this case, it is made to take 100 samples for all iterations.

Errors faced:

Initially, the inputs are passed as variables to stalagmite function. After discussing in support session, it is changed as a vector.

References

https://in.mathworks.com/help/gads/examples/genetic-algorithm-options.html

https://in.mathworks.com/help/gads/how-the-genetic-algorithm-works.html

https://in.mathworks.com/help/gads/ga.html

https://www.researchgate.net/post/How_can_you_find_the_max_function_with_Genetic_Algorithm

https://towardsdatascience.com/introduction-to-genetic-algorithms-including-example-code-e396e98d8bf3

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.