Challenge

Week 4 - Genetic Algorithm

Aim:-

To Study about the Genetic Algorithm and calculate the global maxima using stalagmite function in matlab

Objective :-

1.write code in matlab to find the global maxima using genetic algorithm and stalagmite function.

2.Explain Genetic Algorithm in your own words and also explain syntax of GA

3.plot graph for 3 cases, with statistical behaviour, with upper and lower bound and with increasing GA iteration.

Genetic Algorithm :-

1.Genetic algorithm is based upon the Charles Darwin theory.

2.Genetic algorithm is used to solve the constrained and unconstrained problem using natural selection process

3.Genetic algorithm selects from a random individual of a fittest parent which are used to generate the next generation.

4.After multiple generation, Genetic algorithm reaches to an optimal solution.

5.there are five phases considered at the Genetic Algorithm.

a.Initial Population

b.Fittest function

c.Selection

d.crossover

e.Mutation

Initial Population :- It is a population of set of individual. Every individual is a solution of a problem that we want to solve.

Fittest function :- It gives the fitness score to every individual. The probability of the individual to be selected to create next generation depends upon the fitness score.

Selection :- Selection phase is to select the fittest individual and to create the next generation.

Crossover :- From each pair of individual, a crossover point is chosen at random from within the genes. Offsprings are created by exchanging the genes of parents among themselves until the crossover point is reached.

Mutation :- Mutation implies that some of the bits in a bit string can be flipped. It occurs to maintain the diversity within the population and avoid premature convergence.

6.Genetic algorithm produces the off spring and also the individual which are fittest for the solution or they are very close to the solution.

7.Population size is fixed. As new off spring is formed, the least fit in the set of individual dies.

Equation :-

`fx_1=[sin⁡(5.1πx+0.5) ]^6`

`〖fy〗_1= 〖[sin⁡〖(5.1πy+0.5)]〗〗^6`

`〖fx〗_2=exp⁡[-4 log⁡(2) (x-0.0667)^2/0.64]`

`〖fy〗_2=exp⁡[-4 log⁡(2) (y-0.0667)^2/0.64]`

Syntax for GA:-

The syntax of GA is used to find the Local Minima of the function. There are several ways to define the ga function depending on the problem being solved

x = ga(fun,nvars)
x = ga(fun,nvars,A,b)
x = ga(fun,nvars,A,b,Aeq,beq)
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub)
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon)
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options)
x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon)
x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options)
x = ga(problem)
[x,fval] = ga(___)
[x,fval,exitflag,output] = ga(___)
[x,fval,exitflag,output,population,scores] = ga(___)

Body content :-

function [f] = stalagmite(input);

x = input(1);
y = input(2);

term1 = (sin((5.1*pi*x)+0.5))^6;
term2 = (sin((5.1*pi*y)+0.5))^6;
term3 = exp((-4*log(2)*((x-0.0667)^2)/0.64));
term4 = exp((-4*log(2)*((y-0.667)^2)/0.64));

f = -(term1*term2*term3*term4);


end

1. first of all we will start the program with clear all, close all and clc.

2. We have to write the stalagmite the function. Solving the stalagmite function using GA will give us the minimum value. So we have to change the sign to get the maximum value so that we will get the Global maxima.

clear all
close all
clc

%defining a search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);

%No. of iterations
pi = 3.14;
n = 50;             

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

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

tic
% case 1 = statistical behavoir

for k = 1:n;
    [ip1 fopt1(k)] = ga(@stalagmite,2);
    xopt1(k) = ip1(1);
    yopt1(k) = ip1(2);
end

study1_time = toc
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp

plot3(xopt1,yopt1,-fopt1,'marker','o','markersize',5,'markerfacecolor','r')
xlabel('x value')
ylabel('y value')
title('statistical behaviour')


subplot(2,1,2)
plot(-fopt1)
xlabel('iteration')
ylabel('fopt')
title('statistical behaviour')

%case -2 Statistical behaviour with upper bound and lower bound
for k1 = 1:n
    [ip2 fopt2(k1)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
    xopt2(k1) = ip2(1);
    yopt2(k1) = ip2(2);
    
end

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 values')
ylabel('y values')
title('statistical behaviour with upper and lower bound')

subplot(2,1,2)
plot(-fopt2)
xlabel('iteration')
ylabel('fopt2')
title('statistical behaviour with upper and lower bound')


% case - Statistical behaviour by increasing the GA iteration
options = optimoptions('ga');
options = optimoptions(options,'populationsize',300); 
for k2 = 1:n
    [ip3 fopt3(k2)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
    xopt3(k2) = ip3(1);
    yopt3(k2) = ip3(2);
    
end

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 values')
ylabel('y values')
title('statistical behaviour increasing GA Iteration')

subplot(2,1,2)
plot(-fopt3)
xlabel('iteration')
ylabel('fopt3')
title('statistical behaviour increasing GA Iteration')

3. Then we need to define the search space for finding the values of x and y in the stalagmite function. The search space is between 0 to 0.6 with 150 intervals.

4. we have used linspace to to create the series of nos for x and y values which will be column vector. So, to convert column vector to row vector we will use meshgrid vector

5. Then, we will obtain the result for 3 cases. in this case, maximum of value of the function is found out with no restriction, no limits and no bounds.

6. To get the same value each time we run the program, we get the upper bound and lower bound this time.

7. Even in the Bounded case the value is not fixed every time. Hence, we now increase the iteration by using GA iterations.

Conclusion :-

Hence, we got the results for 3 cases of ga using stalagmite function

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.