Project

Optimization using Genetic Algorithm - MATLAB

Objective - Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function using Genetic Algorithm (ga) for 3 studies follow as:

  • Study 1 - Statistical Behaviour
  • Study 2 - Statistical Behaviour - with upper and lower bounds
  • Study 3 - Increasing GA iterations

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

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.

\"\"

In general, the performance parameters of any component depends upon more than one variable. In order to have the best suited value of any parameter, we need to find the best combination of the variables, it depend upon. If we go by hidden trial method then the process is tedious, not reliable and also do not guarantee the best outcome. Thus, we need to perform optimization. There are many techniques available to perform optimization, genetic algorithm is one of them.

Basic Structure - We start with an initial population (which may be generated at random or seeded by other heuristics), select parents from this population for mating. Apply crossover and mutation operators on the parents to generate new off-springs. And finally these off-springs replace the existing individuals in the population and the process repeats. In this way genetic algorithms actually try to mimic the human evolution to some extent.

Five phases are considered in a genetic algorithm.

\"\"

Genetic Algorithm (ga) in MATLAB

The ga command in MATLAB uses genetic algorithm technique to optimize the function. It return global minima. It will first generate an initial population using random sampling technique. The bounds and constraints can also be provided to initial population selection. The fitness function is provided which help to evaluate fitness scores and selecting the fittest individual. After crossover and mutation, a new generation is found and the iteration continues till the optimized solution is found. MATLAB puts a limit to the number of iterations and if the optimized solution is not found within that limit, it will return the result of the last iteration with warning sign.

Command Syntax :

\"\"

Stalagmite Function is used to generate minimum value.

\"\"

This is stalagmite function equation.

Function Code

function [f] = stalagmite(input)

x = input(1);
y = input(2);

f1 = (sin(5.1*pi*x + 0.5)).^6;
f2 = (sin(5.1*pi*y + 0.5)).^6;
f3 = exp(-4*log(2)*((x - 0.0667).^2)./0.64);
f4 = exp(-4*log(2)*((y - 0.0667).^2)./0.64);

f = -(f1.*f2.*f3.*f4);

end

Here I am putting negative sign to obtain maximum value.

Main Code

Study 1 - Statistical Behaviour

Code

clear all
close all
clc

x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 100;

[xx, yy] = meshgrid(x, y);

%Evaluating stalagmite function

for i = 1:length(xx)
    for j = 1:length(yy)
        input(1) = xx(i,j);
        input(2) = yy(i,j);
        f(i,j) = stalagmite(input);
    end
end


%Study 1 - Statistical Behaviour
tic

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

study1_time = toc

figure(1)
subplot(2,1,1)
hold on
surfc(x,y,-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\')

Explanation

  • Stalagmite function values are generated and assigned to a new variable f
  • If no bounds and constraints are provided to create an initial population , we get different results each time.
  • Use for loop to capture the various results.
  • In surface plot, put negative sign before f because we need the inverted surface plot for maxima.
  • Using subplot command plot the stalagmite function and location result.
  • Another part of the subplot shows the value of global maxima obtained in each case.
  • Study time is calculated using tic and toc command to compare with other studies.

Output Graph

\"\"

Study Time

\"\"

Study 2 - Statistical Behaviour - with upper and lower bounds

Code

clear all
close all
clc

x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 100;

[xx, yy] = meshgrid(x, y);

%Evaluating stalagmite function

for i = 1:length(xx)
    for j = 1:length(yy)
        input(1) = xx(i,j);
        input(2) = yy(i,j);
        f(i,j) = stalagmite(input);
    end
end

%Study 2 - Statistical Behaviour - with upper and lower bounds
tic

for i = 1:num_cases
    [inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0 0],[1 1]);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end

study2_time = toc

figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-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\')

Explanation

  • Same procedure is followed in this study.
  • For improving the results the lower bound and upper bound are provided to the variables.
  • Similarly tic and toc command use to calculate the study time.

Output Graph

\"\"

Study Time

\"\"

Study 3 - Increasing GA iterations

Code

clear all
close all
clc

x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 100;

[xx, yy] = meshgrid(x, y);

%Evaluating stalagmite function

for i = 1:length(xx)
    for j = 1:length(yy)
        input(1) = xx(i,j);
        input(2) = yy(i,j);
        f(i,j) = stalagmite(input);
    end
end

%Study 3 - Increasing GA iterations
options = optimoptions(\'ga\');
options = optimoptions(options,\'populationsize\',250);

tic

for i = 1:num_cases
    [inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0 0],[1 1],[],[],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)
shading interp
plot3(xopt,yopt,-fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\')
title(\'Bounded inputs with increased population size\')
subplot(2,1,2)
plot(-fopt)
xlabel(\'Iterations\')
ylabel(\'Function Minimum\')

Explanation

  • Same procedure is also followed in this study.
  • To further improve the results, initial population size increased using optimoptions and options command in ga.
  • Similarly tic and toc command use to calculate the study time.

Output Graph

\"\"

Study Time

\"\"

Conclusion -

  • Optimization of stalagmite function performed using MATLAB and global maxima values obtained.
  • Study 1 - Statistical Behaviour - If no bounds and constraints are provided to create an initial population , we get different results each time.
  • Study 2 - Statistical Behaviour - with upper and lower bounds - as the lower and lower bound provide the variation in global maxima is less but the study time increased.
  • Study 3 - Increasing GA iterations - increasing the initial population size study time increased but the value obtained is almost near to global maxima.
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.