Challenge

Week 4.1 - Genetic Algorithm

Aim : To optimise the stalagmite function and to find the global maxima of the function.

Introduction :

The genetic algorithem is a methode for solving both constrained and un comstrained optamization problems that is based on nature selection. The genetic algorithem repeatdly modifies a population of individual solutions. A genetic algorithm is a search heuristic that is inspired by Charless Darwin's theory of natural evaluation.

Code to stalagmite function and to find global maximx of the function :

code 1 :

clear all
close all
clc

% defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);

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

% evaluating the stalagmite function
for i = 1:length(xx)
    for i =  1:length(yy)
        input_vector(1)=xx(i,j);
        input_vector(2)=yy(i,j);
        f(i,j)=stalagmite(input_vector);
    end
end

surfc(xx,yy,f)
xlabel('x value')
ylabel('y value')

shading interp

[inputs, fval] = ga(@stalagmite,2)

The above program is used to creat a stalagmite function by passing the input arrays as the argument for the variables x and y. The output is obtained bu using the function.

  • We start with the clear all, close all, clc command.
  • 2 dimensional mesh is created by using meshgrid.
  • First loop is for the xx variables i = 1: length(xx).
  • second loop is for the yy variables = j = 1 : length(yy).
  • 'surf' command is used for getting the smooth plot.
  • 'shading interp' is used for the plot to get clear plot.

Function code :

function [f] = stalagmite_func(input_vector)

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

We give the formulas as the inputs as f1,f2,f3,f4.

Main code :

clear all
close all
clc

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

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

% 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_func(input_vector);
    end
end

% study 1 - staistical behaviour
tic

for i = 1:num_cases
    [inputs,fopt(i)] = ga(@stalagmite_func,2);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
fopt = -fopt
study_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')

tic
%study 2 - statistical behaviour with upper and lower bounds 

for i = 1:num_cases
    [inputs2,fopt2(i)] = ga(@stalagmite_func,2,[],[],[],[],[0;0],[1;1]);
    xopt2(i) = inputs2(1);
    yopt2(i) = inputs2(2);
end
fopt2 = -fopt2
study2_time = toc

figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt2,yopt2,fopt2,'marker','o','MarkerSize',5,'MarkerFaceColor','r')
title('Bounded inputs')
subplot(2,1,2)
plot(fopt2)
xlabel('Iterations')
ylabel('Function minimum')

% Study 3 increasing ga iterations
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',300);

tic
for i = 1:num_cases
    [inputs3,fopt3(i)] = ga(@stalagmite_func,2,[],[],[],[],[0,0],[1,1],[],[],options);
    xopt3(i) = inputs3(1);
    yopt3(i) = inputs3(2);
end

study3_time = toc

figure(3)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt3,yopt3,-fopt3,'marker','o','MarkerSize',5,'MarkerFaceColor','r')
title('Bounded inputs')
subplot(2,1,2)
plot(-fopt3)
xlabel('Iterations')
ylabel('Function minimum')

Here we use the 3 study cases:

  • Study 1 as a statistical behaviour.
  • study 2 as a statistical behaviour with upper and lower boundaries.
  • study 3 as a increasing ga interactions.

Study 1 :

The programe in study 1 is for statistical behaviour of the unbounded inputs.

xopt is the input(1)

yopt is the input (2)

syntax of GA = ga(@stalagmite,2).

Tic and toc is used for creating the study time.

Study 2 :

The study 2 deals with the statistical behaviour of the bounded inputs.

syntax for GA = ga(@stalagmite,2,[],[],[],[],[]).

Tic and toc is used for creating the study time.

Study 3 :

The global maxima is obtained by increasing the populations size.

Syntax of GA = ga(@stalagmite,2,[],[],[],[],[],[],[],options).

Tic and toc is used for creating the study time.

Plot 1 (or) figure 1 :

Plot 2 (or) figure 2 :

Plot 3 (or) figure 3 :

Work space :

Conclusion :

The global maxima is calculated by population sizing and the plots provided a visual context of the solution and verifies the authentical of the obtained value.

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.