Challenge

Week 4.1 - Genetic Algorithm

Aim :- To understand Genetic Algorithm (ga) function and stalagmite function using matlab code.

Genetic Algorithm :- The genetic algorithm is a method for solving both constrained and unconstrained optimization problems that is based on natural selection, the process that drives biological evolution. The genetic algorithm repeatedly modifies a population of individual solutions.

Flow chart of GA :-

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. The selection is generally stochastic, and can depend on the individuals scores.

  • cross combine two parents to form children for the next generation.

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

Styntax for GA :-

function code for stalagmite function

function f = stalagmite(input_vector)
x = input_vector(1);
y = input_vector(2);

a=(5.1*pi*input_vector(1)+0.5);
b=(5.1*pi*input_vector(2)+0.5);
c=(-(4*log(2))*(input_vector(1)-0.0667).^2)/(0.64);
d=(-(4*log(2))*(input_vector(2)-0.0667).^2)/(0.64);

f_x1=(sin(a)).^6;
f_y1=(sin(b)).^6;
f_x2=exp(c);
f_y2=exp(d);

f=-f_x1.*f_y1.*f_x2.*f_y2;

end

To find global maxima codes are given below:-

clear all
close all
clc

% defining our search space

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

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

% evaluting 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)

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

output :-

For first iteration num cases = 50 and its statical behavior:-

clear all
close all
clc

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


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

% for first iteration
num_cases = 50;
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

% f =  stalamite(x,y);
% study 1 statical behavior
for i = 1:num_cases
    [inputs,fopt(i)] = ga(@stalamite,2);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
% fig 1
subplot(2,1,1)
hold on
surfc(x,y,-F)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('unbounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('Function Minimum')



Output :-

Study 2 statical behavior and code for it :-

% study 2 statical behavior
tic
for i = 1:num_cases
     [inputs,Fopt(i)] = ga(@stalamite,2,[],[],[],[],[0,0],[1,1]);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
study_time_2 = toc
% fig 2
subplot(2,1,1)
hold on
surfc(x,y,F)
shading interp
plot3(xopt,yopt,Fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputs')
subplot(2,1,2)
plot(Fopt)
xlabel('Iterations')
ylabel('Function Minimum')

output :-

Study 3 statical behavior with increase in population size and code for it :-

% study time_3 bounding the values and increase popukation size
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',400);
tic
for i = 1:num_cases;
     [inputs,Fopt(i)] = ga(@stalamite,2,[],[],[],[],[0,0],[1,1],[],[],options);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
study_time_3 = toc
% fig 3
subplot(2,1,1)
hold on
surfc(x,y,F)
shading interp
plot3(xopt,yopt,Fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputs with increase in population size')
subplot(2,1,2)
plot(Fopt)
xlabel('Iterations')
ylabel('Function Minimum')

Output :-

Main Program :-

clear all
close all
clc

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


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

% for first iteration
num_cases = 50;
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

% f =  stalamite(x,y);
% study 1 statical behavior
for i = 1:num_cases
    [inputs,fopt(i)] = ga(@stalamite,2);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
% fig 1
subplot(2,1,1)
hold on
surfc(x,y,-F)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('unbounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('Function Minimum')

% study 2 statical behavior
tic
for i = 1:num_cases
     [inputs,Fopt(i)] = ga(@stalamite,2,[],[],[],[],[0,0],[1,1]);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
study_time_2 = toc
% fig 2
subplot(2,1,1)
hold on
surfc(x,y,F)
shading interp
plot3(xopt,yopt,Fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputs')
subplot(2,1,2)
plot(Fopt)
xlabel('Iterations')
ylabel('Function Minimum')

% study time_3 bounding the values and increase popukation size
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',400);
tic
for i = 1:num_cases;
     [inputs,Fopt(i)] = ga(@stalamite,2,[],[],[],[],[0,0],[1,1],[],[],options);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
study_time_3 = toc
% fig 3
subplot(2,1,1)
hold on
surfc(x,y,F)
shading interp
plot3(xopt,yopt,Fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputs with increase in population size')
subplot(2,1,2)
plot(Fopt)
xlabel('Iterations')
ylabel('Function Minimum')





References :-

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.