Project

Genetic Algorithm (Using Genetic Algorithm find global maxima)

In this project we willbe calculating global maxima using genetic algorithm.

Genetic Algorithm:

Genetic Algorithm (ga) is a optimization technique used to solve non-linear or non-differentiable optimization problems. This use concepts from biology to find the global minimum from a optimization problem.

It starts with an initial generation of candidate solutions that are tested against the objective function. Subsequent generations evolve from the 1st through selection, crossover and mutation.

Syntax for ga:

f=ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options)

fun - fitness function. We will use fitfun as @stalagmite,

nvars- number of variables,

lb- Lower bound(limit),

ub- Upper bound(limit),

options- We will use to set population size.

Now, here are the equations to find global maxima from:

Problem here is, we can find global minima through ga. So, to find global maxima we have to invert the equations, so to do that just multiply with -1.

Here is the code for the given equation, please check:

clc,close all,clear all

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

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

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

surf(xx,yy,f)
shading interp

Stalagmite function:

function f= stalagmite(vector)
x=vector(1);
y=vector(2);
f1x = (sin(5.1 * pi * x + 0.5))^6;
f2x = exp((-4) * log(2) * ((x - 0.0667)^2/0.64));
f1y = (sin(5.1 * pi * y + 0.5))^6;
f2y = exp((-4) * log(2) * ((y - 0.0667)^2/0.64));
f=(-1)*f1x*f2x*f1y*f2y;

end

So to find global maxima we will use ga:

Study1- Unbounded Iterations:

clc,close all,clear all

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

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

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

num_cases=50;

%study1-unbound calculations
tic
for i=1: num_cases
    [inputs,fopt(i)]= ga(@stalagmite,2);
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end
study1_time= toc
figure(1)
subplot(2,1,1)
hold on
surf(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')

study1_time = 4.6822

In this code, the genetic algorithm doesn't have a spcific search space. Even though we have values from 0 to 0.6, genetic algorithm tries to find minimum value outside the 0 to 0.6 limit.

Check the plot, we have found local minima outside our problem's specified limit.

Study2- Bounded Iterations, UB and LB:

clc,close all,clear all

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

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

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

num_cases=50;



%study2-bound calculations
tic
for i=1: num_cases
    [inputs,fopt(i)]= ga(@stalagmite,2,[],[],[],[],[0;0],[1;1])
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end
study2_time= toc
figure(2)
subplot(2,1,1)
hold on
surf(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')

In this case study2, we have provided UB as [1;1] and LB as [0;0].

study2_time = 6.6382 sec

So, now we have specifies search space so that genetic algorithm can find global and local minimas efficiently. But, we need more robust answer. In this we have narrowed down the search space, now we can increase population size to get global minima(In our case global maxima, because we have multiplied equation with -1).

Study3- Bounded Iterations, UB and LB with increased population to 200:

clc,close all,clear all

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

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

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

num_cases=50;

%study3-bound calculations with modified population size
tic
options=optimoptions('ga');
options=optimoptions(options,'PopulationSize',200);
for i=1: num_cases
    [inputs,fopt(i)]= ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options)
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end
study3_time= toc
figure(3)
subplot(2,1,1)
hold on
surf(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputs with increased population')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function Minimum')

In this we can see, there are only two instances where the values went to -0.85 but for all other iterations it was constantly at -1.

study3_time = 30.7607

Study4- Bounded Iterations, UB and LB with increased population to 500:

clc,close all,clear all

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

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

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

num_cases=50;

%study4-bound calculations with modified population size(500)
tic
options=optimoptions('ga');
options=optimoptions(options,'PopulationSize',500);
for i=1: num_cases
    [inputs,fopt(i)]= ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options)
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end
study4_time= toc
figure(4)
subplot(2,1,1)
hold on
surf(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputs with increased population(500)')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function Minimum')

In this graph we can clearly see that the global minima is at appx -0.9999.

study4_time = 36.5761

In this problem global minima is our global maxima as we have multiplied by a minus sign to main equatoion.

This is how we can find Global Maxima.

Here is the final and complete code:

clc,close all,clear all

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

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

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

num_cases=50;

%study1-unbound calculations
tic
for i=1: num_cases
    [inputs,fopt(i)]= ga(@stalagmite,2);
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end
study1_time= toc
figure(1)
subplot(2,1,1)
hold on
surf(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')

%study2-bound calculations
tic
for i=1: num_cases
    [inputs,fopt(i)]= ga(@stalagmite,2,[],[],[],[],[0;0],[1;1])
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end
study2_time= toc
figure(2)
subplot(2,1,1)
hold on
surf(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')

%study3-bound calculations with modified population size
tic
options=optimoptions('ga');
options=optimoptions(options,'PopulationSize',200);
for i=1: num_cases
    [inputs,fopt(i)]= ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options)
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end
study3_time= toc
figure(3)
subplot(2,1,1)
hold on
surf(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputs with increased population')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function Minimum')

%study4-bound calculations with modified population size(500)
tic
options=optimoptions('ga');
options=optimoptions(options,'PopulationSize',500);
for i=1: num_cases
    [inputs,fopt(i)]= ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options)
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end

study4_time= toc
figure(4)
subplot(2,1,1)
hold on
surf(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputs with increased population(500)')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function Minimum')
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.