Challenge

Week 4.1 - Genetic Algorithm

code for genetic algorithm and stalagmite function:

clear all;
close all;
clc;
%independent variables
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
n=50 %number of iterations
% creating 2D array
[xx,yy]=meshgrid(x,y)
%Number of iteration for which the function will evaluate the fitness
%creating the surface plot for stalagmite function
%To remove the lines produced by the mesh grid
%STATISTICAL BEHAVIOUR (UNBOUNDED)
%Calculating the study time through 
%study 1-statistical behaviour

for i=1:length(xx)
    for j=1:length(yy)
        input(1)=xx(i,j);
        input(2)=yy(i,j);
        f(i,j)=stalagmite(input);
    end
end

%study 1-statistical behaviour
for k=1:n
[inputs,fopt(k)]=ga(@stalagmite,2);
xopt(k)=inputs(1)
yopt(k)=inputs(2)
end

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('Bounded inputs')
plot(-fopt)
xlabel('iteration')
ylabel('function Maxima')

tic % start of timer
%study 2-statistical behaviour

for k=1:n
[inputs2,fopt2(k)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1])
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end

study2_time=toc % stop timer

figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt2,yopt2,-fopt2,'marker','o','MarkerSize',5,'Markerfacecolor','g')
title('bounded inputs')
subplot(2,1,2)
plot(-fopt2)
xlabel('Iterations')
ylabel('function Maximum')

%study 3 - Incresing the population size
%by doing so we are increasing the probability of the function
%to converge at global maximum
tic
for k=1:n
    [inputs3,fopt3(k)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options)
    xopt(k)=inputs3(1)
    yopt(k)=inputs3(2)
end
study3_time=toc %gives the time taken for study plotting3

figure(3)
subplot(2,1,1)
hold on;
surfc(x,y,-f)     %creates a surface to visualise 3D
shading interp
plot3(xopt3,yopt3,-fopt3,'marker','o','MarkerSize',5,'Markerfacecolor','g')
title('Bounded inputs and increasing GA iteration')
subplot(2,1,2)
plot(-fopt)
xlabel('Iteration')
ylabel('function Maxima')


Stalagmite function


clc;
close all;
clear all;

function [f]=stalagmite(input_vector);

x=input_vector(1);
y=input_vector(2);
f1=(sin(5.1*pi*x+0.5)).^6;
f3=(sin(5.1*pi*y+0.5)).^6;
f2=exp(-4*log(2)*(x-0.0667)^2/0.64);
f4=exp(-4*log(2)*(y-0.0667)^2/0.64);

f=-((f1)*(f2)*(f3)*(f4));
end





























Results of the above code:

this is the error occured during the execution of program and i didn't find the solution of the problem.

so i couldn't do it further.

 
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.