Challenge

Week 4.1 - Genetic Algorithm

PROGRAM TO GENERATE A CODE FOR STALAGMITE FUNCTION AND FIND THE GLOBAL MAXIMA

first of all create a new window,

type clear all,close all,clc

defining our search space

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

x point is having 150 divisions between 0 and0.6

y point is having 150 divisions between 0 and0.6

now we can create a 2D mesh

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

next step is looping the x and y,

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

the stalagmite is a function that ha to be formed sepaerately and then insert in this for loop.

so next step is creating a stalagmite function,

for that we have a equation given below,

f(x,y)=(f1,x)*(f2,x)*(f1,y)*(f2,y)

f1,x=(sin((5.1*pi*x)+0.5)).^6
f1,y=(sin((5.1*pi*y)+0.5)).^6
f2,x=exp((-4)*log(2)*((x-0.0667).^2)/(0.064))
f2,y=exp((-4)*log(2)*((y-0.0667).^2)/(0.064))

we take this as 'term1',' term2 ','term3 ','term4 'respectively for the order mentioned above.

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.064));
term4=exp((-4)*log(2)*((y-0.0667).^2)/(0.064));

and so we call it as [F]=(term1*term2*term3*term4)

so we run this inside a function code ,which starts with function'' and ends with 'end'

we call the function [F]=stalagmite_func(input)

and th file is saved as the same function name.

i am attaching the program function below.

function [F]=stalagmite_func(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.064));
term4=exp((-4)*log(2)*((y-0.0667).^2)/(0.064));

F=(term1*term2*term3*term4)
end

now this function is inserted in the for loop,

after statistical behaviour of ga is done.

and statistical behaviour with upper bound and lower bound if calculated,

next to this is to find the increasing number of iterations.

in each case above ,plotting is to be prepared.

markersize of 5
xlabel and ylabel is provided.


xlabel('iterations')
ylabel('functions maxima')

so the program is as follows,

clear all
close all
clc

%defining our search space

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

%creating a 2D mesh

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

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

surfc(x,y,f)

%study-1 statistical behaviour of ga

num_cases=50;
tic
for i=1:num_cases
    [input,fopt(i)]=ga(@stalagmite_func,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')
title('bounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('functions maxima')


%statistical behaviour with upper bound and lower bound
tic

for i=1:num_cases
    [inputs,fopt(i)]=ga(@stalagmite_func,2,[],[0;0],[.3;.3])
        xopt(i)=input(1);
        yopt(i)=input(2);
end

study2_time=toc;
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'MarkerFaceColor')
title('bounded inputs')
subplot(2,1,1)
plot(-fopt)
xlabel('iteration')
ylabel('functions maxima')


%case3 increasing no of iterations
tic

options=optimoptions('ga');
options=optomoptions(options,'populationsize',500);
for i=1:num_cases
    [input,fopt(i)]=ga(@stalagmite_func,2,[],[],[],[],[0;0],[.3;.3])
    xopt(i)=inputs(1);
    yopt(i)=input(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')
title('bounded inputs with increased population size')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('functions maxima')

so explained the concept of genetic algorithm(ga) and explained the syntax of ga in matlab.

defenition about ga;

the genetic algorithm(ga) is a method of solving both constrained and unconstrained optimization problems that is based on natural selection,the process that drives biological evolution.the ga repeatedly modifies a population of individual solutions.

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.