Project

Stalagmite function using Genetic algorithm to find global maxima

This program optimises the stalagmite function to find the global maximum of the function using genetic algorithm optimization.

Genetic algorithim (ga) is a inbuilt minimization funtion in MATLAB and Octave. This function works on the natural selection theory of evolution. This function starts with a random population size and then selects parents based on fitness values of each individual. Fitness values are evaluated using the fitness function which is taken as one of the inputs in ga. These parents are then used to produce off springs using cross over operator. This operator combines the solution of parents to generate a new solution (off spring) which is usually mutated before adding to the population. This process is repeated untill an optimum (minimum) solution is reached or the number of iteration specified by user are complete.

The syntax for ga used in MATLAB is as follows:

x = ga(fun,nvars)
x = ga(fun,nvars,A,b)
x = ga(fun,nvars,A,b,Aeq,beq)
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub)
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon)
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options)
x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon)
x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options)
x = ga(problem)
[x,fval] = ga(__)
[x,fval,exitflag,output] = ga(__)
[x,fval,exitflag,output,population,scores] = ga(__)

where

  • x = local minimum
  • fun = objective function
  • nvars = number of variables in objective function
  • A,b = linear inequalities such that A*x<=b
  • Aeq,beq = liner equality such that Aeq*x = beq
  • lb,ub = lower and upper bounds for the solution
  • nonlcon = non linear constraints
  • options = optimization options
Explation of code:
Step 1: Search space for the ga function is specified and also the number of times the function should run to reach optimization is specified (40 in this case)
Step 2: A 2D matrix is created using two 1D matrices which is used to provide input vectors for the stalagmite function.
Step 3: Stalagmite functuion is defined using the given formulae and inverted by multiplying the output by -1.
Step 4: Study 1 is plotted without specifying the boundaries of solution in ga function. The plot includes a 3D line plot (using plot3 command) on the stalagmite function and a 2D plot of F maximum vs number of iterations.
Since the ga evaluates the minimum value of function hence the optimum values found by the ga function are multiplied by -1 to get the maximum values.
Step 5: Similarly study 2 is plotted with the specified search boundary for the ga function and study 3 is plotted by increasing the population size in study 2 to get closer to the optimum value.
clear all
close all
clc

% defining search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 40;   % number of times ga runs

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

% stalagmite function

function [output] = stalagmite(input_vector)
  a = input_vector(1);
  b = input_vector(2);
  
  f1x = (sin(5.1*pi*a+0.5))^6;
  f1y = (sin(5.1*pi*b+0.5))^6;
  f2x = exp(-4*log(2)*(a-0.0667)^2/0.64);
  f2y = exp(-4*log(2)*(b-0.0667)^2/0.64);
  
  [output] = -(f1x*f2x*f1y*f2y);
end

% evaluating the stalagmite function

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


% 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(xx, yy, -f)
shading interp
plot3(xopt,yopt,-fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\')
title(\'Unbounded inputs\')
xlabel(\'X values\')
ylabel(\'Y values\')
subplot(2,1,2)
plot(-fopt)
xlabel(\'Iterations\')
ylabel(\'Function Maximum\')


% 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(xx, yy, -f)
shading interp
plot3(xopt,yopt,-fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\')
title(\'Bounded inputs\')
xlabel(\'X values\')
ylabel(\'Y values\')
subplot(2,1,2)
plot(-fopt)
xlabel(\'Iterations\')
ylabel(\'Function Maximum\')

% study 3 : increasing GA population size

options = optimoptions(\'ga\');
options = optimoptions(options,\'PopulationSize\',450);

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(xx, yy, -f)
shading interp
plot3(xopt,yopt,-fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\')
title(\'Bounded inputs with different population size\')
xlabel(\'X values\')
ylabel(\'Y values\')
subplot(2,1,2)
plot(-fopt)
xlabel(\'Iterations\')
ylabel(\'Function Maximum\')
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.