Challenge

Week 4.1 - Genetic Algorithm

Question :- Try to understand the concept of genetic algorithm by referring to varius sources before you attend this challenge. Look out for information regarding stalagmite function and ways to optimise it.

Answer:-

Theory:-

Genetic Algorithm:-

A genetic algorithm is a search heuristic that is inspired by Charles Darwin's theory of natural evolution. This algorithm reflects the process of natural selection where the fittest individuals are selected for reproduction in order to produce offspring of the next generation.

The process of natural selection starts with the selection of fittest individuals from a population . They produce pffspring 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 parentsand have a better chance of sueviving. This process keeps on iterating and at the end a generation with the fittest individuals will be found. This notion can be applied to a search problem. We consider a set of solutions for a problem and select the set of best ones out of them.

*Five phases are considered in a genetic algorithm:-

1:- INITIAL POPULATION = The process begins with a set of individuals which is called a population.Each individuals is a solution to the problem you want to solve.

2:- FITNESS FUNCTION = The fitness function determines how fit an individulas is .It gives a fitness score to each individual.The probability that probability that an individual will be selected for reproduction is based on its fitness score.

3:- SELECTION = The idea of selection phase is to select the fittest individuals and let them genes to the next generation.

4:- CROSSOVER:- Crossover is the most significant phase in a genetic algorithm.For each pair of parents to be mated,a crossover point is chosen at random from within the genes.

5:- MUTATION = In certain new offspring formed some of their genes can be subjected to a mutation with a low random probability.This implies that some of the bits in the bits string can be flipped.

* STALAGMITE FUNCTION:-

>> It is a mathematical model that helps to generate local minima and maxima.

>> It is a three-dimentional function and the way the function is designed is that it has a lot of local maxima or minima and one global maxima or minima.

>> The name stalagmite is actually a type of rock formation that rises from the floor of a cave due to the accumulation of material deposited on the floor from celling drippings.

* THE PROGRAMING PART

THE FUNCTION CODE:-

function [F] = stalagmite(input_vector)

x = input_vector(1)
y = input_vector(2)

f1x = [sin((5.1*pi*x)+0.5)]^6
f1y = [sin((5.1*pi*y)+0.5)]^6
f2x = exp(-4*log(2).*(((x-0.0667).^2)/0.64))
f2y = exp(-4*log(2).*(((y-0.0667).^2)/0.64))
F = -[f1x*f1y*f2x*f2y]
end

THE MAIN CODE:-

close all
clear all
clc

x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 50;
[xx,yy] = meshgrid(x,y);

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

% Study_1 statistical behavior
tic
for i = 1:num_cases
[inputs1,Fopt1(i)] = ga(@stalagmite,2)
xopt1(i) = inputs1(1);
yopt1(i) = inputs1(2);
end

study_time_1 = toc;

figure(1)
subplot(2,1,1);
hold on
surfc(xx,yy,-F);
shading interp;
xlabel('X value');
ylabel('Y value');
plot3(xopt1,yopt1,-Fopt1,'marker','o','MarkerSize',5,'MarkerFaceColor','r')
title('unbounded inputs');
subplot(2,1,2);
plot(-Fopt1);
xlabel('Iterations');
ylabel('Function maxima')

%study 2 Statistical behaviour with upper and lower bonds
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
study_time_2 = 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');
xlabel('X value');
ylabel('Y value');
title('Statistical behaviour with upper and lower bonds')
subplot(2,1,2)
plot(-Fopt2)
xlabel('Iterations');
ylabel('Function maxima')
%Study 3 Statistical behaviour with increase population and with upper and
%lower bonds
tic
options = optimoptions ('ga');
options = optimoptions(options,'Populationsize',250);
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
study_time_3 = 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');
xlabel('X value');
ylabel('Y value');
title('Statistical behaviour with increase population and with upper and lower bonds')
subplot(2,1,2)
plot(-Fopt3)
xlabel('Iterations');
ylabel('Function maxima');

CODE EXPLANATION:-

1>> Solving the GA for stalgmite function gives us aminimum value of the function .

2>> As 'linspace()' creates the series of nos,x and y will be the column vector. As input to the stalagmite function ,we need to convert column vector to a row vector.

3>> The next step is to obtain the results for 3 cases of the genetic algorithm in matlab.

4>> TO get the same value each time we run the program,we need to restrict the program within specific bounds namely upper and lower bound.

5>> We need to increase GA iterations.We can raise it to any value , we choseit as 250.

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.