Challenge

Week 4.1 - Genetic Algorithm

Genetic Algorithm:

The genetic algorithm is inspired by the process that drives biological evolution. The process of evolution starts with the selection of fittest individuals from a population. Then they produce offspring which inherit the characteristics of the parents and will be added to the next generation. If parents have better fitness, their offspring will be better than parents and have better chance at surviving. This process keeps on iterating and at the end, a generation with the fittest individuals will be found. And this is the main working principle for the algorithm because at each step, the genetic algorithm selects randomly from the current population to be parents and uses them to produce the children for the next generation. Over successive generations the population evolves toward an optimal solution. The genetic algorithm can be applied to solve a variety of optimization problems that are not well suited for standard optimization problems in which the objective function is discontinuous, nondifferentiable, stochastic, or highly non-linear.

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

  • Selection rules select the individuals, called parents, that contribute to the population at the next generation.
  • Crossover rules combine two parents to form children for the next generation.
  • Mutation rues apply random changes to individual parents to form children.

Main code:

clear all

close all

clc

x = linspace(0,0.6,150);

y = linspace(0,0.6,150);

num_cases=50;

% Creating a 2D mesh

[xx yy] = meshgrid(x,y);

% Evaluating Stalagmite Function

for i = 1:length(xx)

for j = 1:length(yy)

input_value(1) = xx(i,j);

input_value(2) = yy(i,j);

f(i,j) = stalagmite(input_value);

end

end

% unbounded statistical behaviour

tic

for i = 1:num_cases;

[inputs1,fopt1(i)] =ga(@stalagmite,2);

xopt(i) = inputs1(1);

yopt(i) = inputs2(2);

end

study1_time=toc;

figure(1)

subplot(2,1,1)

hold on

surfc(xx,yy,-f)

xlabel('x-value')

ylabel('y-value')

zlabel('function value')

shading interp

plot3(xopt,yopt,-fopt1,'marker','o','markersize',5,'markerfacecolor','r')

title('Unbounded Statistical Behaviour')

subplot(2,1,2)

plot(-fopt1)

xlabel('iterations')

ylabel('function maximum')

%statistical behaviour with upper and lower bound

tic;

for i = 1:num_cases;

[inputs2,fopt2(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);

xopt(i) = inputs2(1);

yopt(i) = inputs2(2);

end

study2_time = toc;

figure(2)

subplot(2,1,1)

hold on

surfc(xx,yy,-f)

xlabel('x-value')

ylabel('y-value')

zlabel('function value')

shading interp

plot3(xopt,yopt,-fopt2,'marker','o','markersize',5,'markerfacecolor','r')

title('Bounded Statistical Behaviour')

subplot(2,1,2)

plot(-fopt2)

xlabel('iterations')

ylabel('function maximum')

%increasing Ga population size

options = optimoptions('ga');

options = optimoptions(options,'populationsize',170);

tic

for i = 1:num_cases;

[input3,fopt3(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);

xopt(i) = inputs3(1);

yopt(i) = inputs3(2);

end

study3_time = toc;

figure(3)

subplot(2,1,1)

hold on

surfc(xx,yy,-f)

xlabel('x-value')

ylabel('y-value')

zlabel('function value')

shading interp

plot3(xopt,yopt,-fopt2,'marker','o','markersize',5,'markerfacecolor','r')

title('Bounded Statistical Behaviour with Increased Population size')

subplot(2,1,2)

plot(-fopt3)

xlabel('iterations')

ylabel('Function maximum')

Total_Time = study1_time + study2_time + study3_time;

fprintf('n The Results are Below:')

fprintf('nn Study1: Unbounded statistical behaviour')

fprintf('nn The time taken for study1 is : %f', study1_time)

fprintf('n The Co-ordinates of x and y for the global maxima of the function are : %f,%f',inputs1(1),inputs1(2))

fprintf('nn The Global Maxima of the Function is :%f',-(mean(fopt1)))

fprintf('nn Study1: Unbounded statistical behaviour')

fprintf('nn The time taken for study1 is : %f', study2_time)

fprintf('n The Co-ordinates of x and y for the global maxima of the function are : %f,%f',inputs2(1),inputs2(2))

fprintf('nn The Global Maxima of the Function is :%f',-(mean(fopt2)))

fprintf('nn Study1: Unbounded statistical behaviour')

fprintf('nn The time taken for study1 is : %f', study3_time)

fprintf('n The Co-ordinates of x and y for the global maxima of the function are : %f,%f',inputs3(1),inputs3(2))

fprintf('nn The Global Maxima of the Function is :%f',-(mean(fopt3)))

fprintf('nn The total Time taken to complete the total Study = %f', Total_time)

Output:

Unbounded statistical behaviour plot

  • Bounded statistical behaviour plot

  • Modified population size plot

Stalagmite function output print in command window

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.