Challenge

Week 4.1 - Genetic Algorithm

 Stalagmite function

%function  to define the equations of stalagmite function
function [f] = stalagmite(input_vector)
x=input_vector(1);
y=input_vector(2);
f1_x=(sin(5.1*pi*x+0.5))^6;
f1_y=(sin(5.1*pi*y+0.5))^6;
f2_x=exp(-4*log(2)*(((x-0.667)^2)/(0.64)));
f2_y=exp(-4*log(2)*(((y-0.667)^2)/(0.64)));
f= -((f1_x*f1_y)+(f2_x*f2_y));
end


Code

clear all
close all
clc

%defining search space
x=linspace(0,0.8,200);
y=linspace(0,0.8,200);

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

%evaluating 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
figure(1)
surfc(xx,yy,-f)
xlabel('xvalue')
ylabel('yvalue')

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

%study1-statistical behaviour
num_case=80;
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(2)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp 
plot3(xopt,yopt,-fopt)
title('Inputs without bounds')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('functions maximum')

%study-2 statistical behaviour with upper and lower bounds
tic
for i=1:num_case
    [inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0:0],[0.8:0.8]);
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end
study_time_2=toc
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp 
plot3(xopt,yopt,-fopt)
title('Bounded Inputs')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('functions maximum')

%study-3 statistical behaviour increasing ga iterations
tic
options=optimoptions('ga');
options=optimoptions(options,'populationsize',500)
for i=1:num_case
    [inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0:0],[0.8:0.8],[],[],options);
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end
study_time_3=toc
figure(4)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp 
plot3(xopt,yopt,-fopt)
title('Bounded Inputs with increased population size')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('functions maximum')

Code Explanation

The program has been executed in three different steps. The number of steps being 80, xand y values are taken between 0.8 and 200.

The first step being to study the statistical behaviour of the stalagmite command with the tic and toc command of time and plot it in 2D and 3D.

The second step being the the study of statistical behaviour with upper and lower bounds and we plot thee desired results. We can see that there is a decrement in study time.

the third step being statistical behaviour with GA iterations. The population size was upped till 400. The GA starts with random variables and there are 70 odd steps of 2 random variables.

 Errors

Stalagmite Function - It is a mathematical model that helps to generate maximas and minimas. Global maxima is a maximum peak value in entire function and global minima is a minimum peak value in the entire function. But local maxima and local minima are multiple number of values.

Genetic Algorithm - It is inspired by Charles Darwin's theory of natural evolution. It is a search heuristic. The algorithm reflects the process of natural selection of where fittest individuals are selected for the reproduction of offsprings of next generation. 

 In MATLAB, genetic algorithm generates a cluster of points at each iteration. The best point in the cluster approaches an optimal solution.

It uses three main types of rules at each step to create the next generation from the current population :

1. Selection rules select individuals, called parents that contribute to the population at the next generation

2. Crossover rule combines two parents to form children from next generation.

3. Mutation rules apply random changes to individual parents to form children.

Syntaxe for GA in MATLAB :

a = ga(fun,nvars)

a=ga(fun,nvars,X,y)

a=ga(fun,nvars,X,y,Xeq,yeq)

a=ga(fun,nvars,X,y,Xeq,yeq,lb,ub)

a=ga(fun,nvars,X,y,[],[],lb,ub,nonlcon,IntCon,options)

Stopping Criteria 

GA uses the following conditions to determine when to stop

-Generations - The algorithm terminates when number of generations reaches the value.

-Time limit - It stops after an amount of time when it reaches the limit.

-Fitness limit - the algorithm stops when the value of the fitness function for the best point in the current population is less than or equal to the fitness limit.

 If any of the above conditions are met, the algorithm stops.

Significance of Multiple Iterations - When multiple number of iteration runs, GA becomes linear or optimum. The significance of multiple iterations is to check over consistency of the output values of GA whether it gives same output for all iterations so that it can gove output value i.e. 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.