Challenge

Week 4.1 - Genetic Algorithm

ga_satistics:


clear all
close all
clc


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

% Evaluating 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) =get_stalagmite(input_vector);
    end
end
num_cases = 50
tic
% Study 1 - Satistical behavior
for i = 1:num_cases
    [inputs,fopt(i)] = ga(@stalagmite,2);
    xopt = inputs(1);
    yopt = inputs(2);
end
study_time = toc

figure(1)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','MarkerSize',5,'MarkerFaceColor', 'r')
title('Unbound inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function minimum')

tic
% Study 2 - Satistical behavior - with upper and lower bounds
for i = 1:num_cases
    [inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
    xopt = inputs(1);
    yopt = inputs(2);
end
study_time = toc

figure(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 3 increasing GA iterations
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',170);

tic
for i = 1:num_cases
    [inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
    xopt = inputs(1);
    yopt = inputs(2);
end
study3_time = toc

figure(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 modified population size')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function minimum')

Get_Stalagmite:

function[input_vector] = get_stalagmite(input)
x = input(1);
y = input(2);

f1.x = (sin(5.1*3.14*x+0.5))^2;
f1.y = (sin(5.1*3.14*y+0.5))^2;

f2.x = exp(-4*log(2)*(x-0.0667)^2/0.64);
f2.y = exp(-4*log(2)*(y-0.0667)^2/0.64);

input_vector = -1*(f1.x*f2.x*f1.y*f2.y);

end


Explination:

             If you are genrating a genetic algorithm it will give you different values  every time so we have to find out a single values that how many times it genrate the 'ga' it has to show that same vlaue again and again.

            So for genrating that value we have to run number of studys by altering some boundarys.So i genrate three study and i get that value in the final study as you can see.

           Study 1 is Satistical behavior in the plot we can see number of values because we did't given any boundarys.

           Study 2 is Satistical behavior - with upper and lower bounds.In this we provided some bounds so the value is resticted with in some points.

           Study 3 is increasing GA iterations.In study 3 we increases number of iterations to 170 in this we get that required value.

           
           For genrating 'ga satistics' we should write Get_Stalagmite code.
           
Genetic alogorithm:

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);

% Evaluating 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

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

Stalagmite function:

function[input_vector] = stalagmite(input)
x = input(1);
y = input(2);

f1 = (sin(5.1*3.14*x+0.5))^2;
f2 = (sin(5.1*3.14*y+0.5))^2;

f3 = exp(-4*log(2)*(x-0.0667)^2/0.64);
f4 = exp(-4*log(2)*(y-0.0667)^2/0.64);

input_vector = -1*(f1*f2*f3*f4);

end


Expilination:

              Genetic algorithms are commonly used to generate high-quality solutions to optimization and search problems by relying on biologically inspired operators such as mutation, crossover and selection.

              But 'ga' genrally produces minimum value (ex=0)to get the maximum value or global maxima we should multiply input_vector values with '-1' (input_vector = -1*(f1*f2*f3*f4)).
              To genrate 'ga' we should write stalagmite function. For writing it we require set of formulas that are shown in the program.
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.