Challenge

Week 4.1 - Genetic Algorithm

Stalagmite function: it is used to generate the global maximas and minimas along with local maximas and minimas.

Optimisation: it is to optimize the genetic algorithm to find global maximas.

Genetic algorithm: it helps in finding the individual, doing crossover and mutating the end result.

Syntax of ga:

[x]=ga(fun,nvars,A,B,[],[],lb,ub,nonlcon,Intcon,options);

[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],(0:0),(0.6:0.6),[],[],options);

fun-function

nvars-number of design variables

A- A matrix

B- B vector

lb-lower bound

ub-upper bound

Main program:

close all
clear all
clc

%defining our search space to find global maxima

x = linspace(0,0.6,200);
y = linspace(0,0.6,200);

%creating 2D array

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

surfc(xx,yy,-f);
xlabel('x value');
ylabel('y value');
shading interp;
[inputs,fval]=ga(@stalagmite,2);


%study of statistical behaviour for unbounded inputs

num_case=50;

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

study_time_1=toc;

figure(1)
subplot(2,1,1);
hold on
surfc(x,y,-f);
shading interp;
plot3(xopt,yopt,-fopt,'Marker','o','MarkerSize',5,'MarkerFaceColor','g');
title('unbounded inputs');
subplot(2,1,2);
plot(fopt)
xlabel('iterations');
ylabel('function maximum');


%study of statistical behaviour for upper and lower bound inputs

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

study_time_2=toc;

figure(2)
subplot(2,1,1);
hold on
surfc(x,y,-f);
shading interp;
plot3(xopt,yopt,-fopt,'Marker','o','MarkerSize',5,'MarkerFaceColor','g');
title('bounded inputs');
subplot(2,1,2);
plot(fopt)
xlabel('iterations');
ylabel('function maximum');

%study of statistical behaviour by increasing the iterations 
options=optimoptions('ga');
options=optimoptions(options,'PopulationSize',500);
tic
for i=1:num_case
    [inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],(0:0),(0.6:0.6),[],[],options);
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end

study_time_3=toc;

figure(3)
subplot(2,1,1);
hold on
surfc(x,y,-f);
shading interp;
plot3(xopt,yopt,-fopt,'Marker','o','MarkerSize',5,'MarkerFaceColor','g');
title('bounded inputs with increased populationsize');
subplot(2,1,2);
plot(fopt)
xlabel('iterations');
ylabel('function maximum');

Stalagmite function:

function[f]=stalagmite(input_vector)
x=input_vector(1);
y=input_vector(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.667)^2)/(0.64)));
f4=exp(-4*log(2)*(((y-0.667)^2)/(0.64)));

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

Explanation:

To give the algorithm we need to provide search space so x and y axis is given by linspace.

And use meshgrid to create 2D matrix

Stalagmite function is created using formulas and following three case studies is made.

Case study 1:

To increase the accuracy and get maximas number of cases is given as numcase=50.

Statistical behaviour is calculated by using for loop with ga(@stalagmite) time is calculated by tic and toc.

Surface model is created using surfc(x,y,-f) and shading is done shading interp.

Then title and label of axis is given.

Case study 2:

In this case we gave the upper and lower bounds to study (0:0),(0.6:0.6).

Statistical behaviour is calculated by using for loop with ga(@stalagmite) time is calculated by tic and toc.

Surface model is created using surfc(x,y,-f) and shading is done shading interp.

Then title and label of axis is given.

Case study 3:

In this case we gave the population size as 500 to further increase the accuracy of the study

Statistical behaviour is calculated by using for loop with ga(@stalagmite) time is calculated by tic and toc.

Surface model is created using surfc(x,y,-f) and shading is done shading interp.

Then title and label of axis is given.

Result:

After all the case study running the consistency of output is checked.

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.