Challenge

Week 4.1 - Genetic Algorithm

Basic code for Stalagmite

%code for basic stalagmite
clear all 
close all
clc

%Defining the search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);

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

%creating 3D stalagmite fuction

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)
xlabel('x values')
ylabel('y values')

shading interp

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

code for stalagmite function

%code for stalagmite function
function [f]= stalagmite(input_vector)
f1x = (sin(5.1*pi*input_vector(1)+0.5))^6;
f1y = (sin(5.1*pi*input_vector(2)+0.5))^6;
f2x = exp((-4)*log(2)*(input_vector(1)-0.0667)^2/0.64);
f2y = exp((-4)*log(2)*(input_vector(2)-0.0667)^2/0.64);
f = -f1x*f1y*f2x*f2y;
end

Output for basic stalagmite function

Code for optimization of random values and checking the stalagmite function study1(unbounded input)

clear all 
close all
clc

x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
[xx yy]=meshgrid(x,y);
num_cases = 150;
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(x,y,f)
shading interp


tic 
%study1-satisticial behaviour
for i=1:num_cases
    [input,fopt(i)]=ga(@stalagmite,2);
    xopt(i)=input(1);
    yopt(i)=input(2);
end
study1_time=toc


figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('unbounded input')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function minium')

output for study1

and the study time is 7.4528sec

Code for optimization of random values and checking the stalagmite function study2(bounded input)

clear all 
close all
clc

x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
[xx yy]=meshgrid(x,y);
num_cases = 150;
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(x,y,f)
shading interp

tic
%study2-statisticial behaviour-with upper bond and lower bond
for i=1:num_cases
    [input2,fopt2(i)]=ga(@stalagmite,2,[],[],[],[],[0,0],[1,1]);
    xopt2(i)=input2(1);
    yopt2(i)=input2(2);
end
study2_time=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')
title('bounded inputs')
subplot(2,1,2)
plot(-fopt2);
xlabel('iterations')
ylabel('function minimum')

output for study 2

time for study 2 is 10.874secs

Code for optimization of random values and checking the stalagmite function study3(population bound)

clear all 
close all
clc

x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
[xx yy]=meshgrid(x,y);
num_cases = 150;
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(x,y,f)
shading interp

%study3-increasing GA iterations
options = optimoptions('ga');
options=optimoptions(options,'populationsize',500);

tic
for i=1:num_cases
    [input3,fopt3(i)]=ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6],[],[],options);
    xopt3(i) = input3(1);
    yopt3(i) = input3(2);
end
study3_time = 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');
title('populations bound')
subplot(2,1,2)s
plot(-fopt3)
xlabel('iterations')
ylabel('function minimum')

output for study 3

study time is74.3123sec

Combined code for all three studies

 %A code to optimise the stalagmite function and find the global maxima of the function

clear all 
close all
clc

x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
[xx yy]=meshgrid(x,y);
num_cases = 150;
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(x,y,f)
shading interp


tic
%study1-satisticial behaviour
for i=1:num_cases
    [input,fopt(i)]=ga(@stalagmite,2);
    xopt(i)=input(1);
    yopt(i)=input(2);
end
study1_time=toc


figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('unbounded input')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function minium')



tic
%study2-statisticial behaviour-with upper bond and lower bond
for i=1:num_cases
    [input2,fopt2(i)]=ga(@stalagmite,2,[],[],[],[],[0,0],[1,1]);
    xopt2(i)=input2(1);
    yopt2(i)=input2(2);
end
study2_time=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')
title('bounded inputs')
subplot(2,1,2)
plot(-fopt2);
xlabel('iterations')
ylabel('function minimum')



%study3-increasing GA iterations
options = optimoptions('ga');
options=optimoptions(options,'populationsize',500);

tic
for i=1:num_cases
    [input3,fopt3(i)]=ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6],[],[],options);
    xopt3(i) = input3(1);
    yopt3(i) = input3(2);
end
study3_time = 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');
title('population bound')
subplot(2,1,2)
plot(-fopt3)
xlabel('iterations')
ylabel('function minimum')

Output for all three studies

output for study 1 :- Unbounded input plot

Output for study2:- Bounded input plot

Output for study3:- population bound

Code for stalagmite Function

%code for stalagmite function
function [f]= stalagmite(input_vector)
f1x = (sin(5.1*pi*input_vector(1)+0.5))^6;
f1y = (sin(5.1*pi*input_vector(2)+0.5))^6;
f2x = exp((-4)*log(2)*(input_vector(1)-0.0667)^2/0.64);
f2y = exp((-4)*log(2)*(input_vector(2)-0.0667)^2/0.64);
f = -f1x*f1y*f2x*f2y;
end

Results,

To solve stalagmite first we should understand some concepts like

  • What is optimization
  • What is Genetic algorithm(Ga)
  • Where Genetic algorithm is used

Optimization:-

Optimization is nothing but its an decision making process and helps to find the best results and it compares various solutions till an optimum or satisfactory solution is found

Genetic algorithm:-

Genetic algorithm is one of the optimization process,The genetic algorithm is a method for solving both constrained and unconstrained optimization problems that is based on natural selection and Genetic algorithm is basically works on Darwin's theory of evloution

Applications of Genetic algorithm :-

Genetic algorithm are basically used in optimization process/problems of various kinds and they are also used in Vehicle routing problem (VRP),Financial markets,Manufacturing system, Mechanical engineering design,Economics,neural networks and many more

Now the stalagmite function is given by

Errors faced during code

Error1:- in the above screen shots i got the error in function program because i had called some other function instead of stalagmite function

Error2:- In the above screenshot i have used "markers" insted of "marker" and i had not used for loops

Reffrences:-

https://skill-lync.com/knowledgebase/genetic-algorithm-explained

https://in.mathworks.com/search.html?c%5B%5D=entire_site&q=Genetic%20algorithm&page=1

https://in.mathworks.com/search.html?c%5B%5D=entire_site&q=optimization&page=1

https://mech.iitm.ac.in/nspch52.pdf

https://www.tutorialspoint.com/genetic_algorithms/genetic_algorithms_introduction.htm

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.