Challenge

Week 4.1 - Genetic Algorithm

Introduction to GA algorithm:

The genetic algorithm is inspired by Darwin's theory of survival of the fittest in a population. Eventually, the offsprings of the fittest will inherit the characteristics of the parents i.e. passed onto the next generation. Hence, the fitter the parents, the stronger the offsprings, stronger generations. Through the process of iteration, the fittest individuals will survive, in this case, be found. This concept can be utilized here where the algorithm selects a random and then is allowed to populate through iterations and over successive interactions, the population can evolve towards a successive optimal solution.

Syntax explanation in MATLAB:

- For the unbounded study

Syntax: [inputs, f_opt] = ga(@fun, nvars)

inputs - values of the parameters i.e. x and y

f_opt - least value of the function

fun - function which needs to be optimized

- For the defined upper and lower bound study

Syntax: [inputs,f_opt] = ga(@fun,nvars,a,b,eq1,eq2,low,up)

a,eq1 - linear inequality constraints

b,eq2 - linear inequality vectors

low - lower bound

up - upper bound

- For the increasing population size

Syntax: [inputs,f_opt] = ga(@fun, nvars,a,b,eq1,eq2,low,up,non_lin,int_var,options)

non_lin - no linear constrains

int_var - interger variables

options - more optimization options

Code:

close all
clear all
clc

%Defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 50; %Total number of cases to run

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

f = zeros(meshgrid(length(xx),length(yy))); %Pre-allocating the meshgrid for better memory allocation

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

xopt1 = zeros(1,num_cases);
yopt1 = zeros(1,num_cases);
fopt1 = zeros(1,num_cases);


%% Study 1 - Statistical Behavior
tic
for i = 1:num_cases
    [inputs1, fopt1(i)] = ga(@stalagmite,2); %Running the genetic algorithm for the cases
    xopt1(i) = inputs1(1);
    yopt1(i) = inputs1(2);
end
study1_time = toc;

%Plotting
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
plot3(xopt1,yopt1,-fopt1,'marker','o','markersize',5,'markerfacecolor','r')
title('Unbounded Inputs')
subplot(2,1,2)
plot(-fopt1)
xlabel('Interations')
ylabel('Function Minimum')

%% Study 2 - Statistical behavior with upper and lower limits

xopt2 = zeros(1,num_cases); %Pre-allocation
yopt2 = zeros(1,num_cases);
fopt2 = zeros(1,num_cases);
tic
for i=1:num_cases
    [inputs2, fopt2(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
    xopt2(i) = inputs2(1);
    yopt2(i) = inputs2(2);
end
study2_time = toc;

figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
plot3(xopt2,yopt2,-fopt2,'marker','o','markersize',5,'markerfacecolor','r')
title('Inputs with upper and lower limits')
subplot(2,1,2)
plot(-fopt2)
xlabel('Interations')
ylabel('Function Minimum')

%% Study 3 - Increasing GA interations
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',170);

xopt3 = zeros(1,num_cases); %Pre-allocation
yopt3 = zeros(1,num_cases);
fopt3 = zeros(1,num_cases);

tic
for i=1:num_cases
    [inputs3, fopt3(i)] = ga(@stalagmite,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(xx,yy,-f)
shading interp
plot3(xopt3,yopt3,-fopt3,'marker','o','markersize',5,'markerfacecolor','r')
title('Inputs with increasing GA interations')
subplot(2,1,2)
plot(-fopt3)
xlabel('Interations')
ylabel('Function Minimum')

Function Code:

function [f] = stalagmite(in_vec)

a = in_vec(1);
b = in_vec(2);

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

func = (f1*f2*f3*f4);
f = -func;
end

Solutions:

Study1: Unbounded

Study2: Bounded

Study3: Increasing population size

Final Results:

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.