Project

Optimization Maximization of a Stalagmite function using Genetic Algorithm

GENETIC ALGORITHM: Genetic Algorithm defines how to an algorithm can generate a modified copy of itself in order to achieve an objective. The next generation will also match itself against the criteria to ensure that the objective will eventually be achieved.

A genetic algorithm is a heuristic search method used in artificial intelligence and computing. It is used for finding optimized solutions to search problems based on the theory of natural selection and evolutionary biology. Genetic algorithms are excellent for searching through large and complex data sets. They are considered capable of finding reasonable solutions to complex issues as they are highly capable of solving unconstrained and constrained optimization issues.

A genetic algorithm makes uses of techniques inspired from evolutionary biology such as selection, mutation, inheritance and recombination to solve a problem. The most commonly employed method in genetic algorithms is to create a group of individuals randomly from a given population. The individuals thus formed are evaluated with the help of the evaluation function provided by the programmer. Individuals are then provided with a score which indirectly highlights the fitness to the given situation. The best two individuals are then used to create one or more offspring, after which random mutations are done on the offspring. Depending on the needs of the application, the procedure continues until an acceptable solution is derived or until a certain number of generations have passed.

A genetic algorithm differs from a classical, derivative-based, optimization algorithm in two ways:

  • A genetic algorithm generates a population of points in each iteration, whereas a classical algorithm generates a single point at each iteration.
  • A genetic algorithm selects the next population by computation using random number generators, whereas a classical algorithm selects the next point by deterministic computation.

Compared to traditional artificial intelligence, a genetic algorithm provides many advantages. It is more robust and is susceptible to breakdowns due to slight changes in inputs or due to the presence of noise. With respect to other optimization methods like praxis, linear programming, heuristic, first or breadth-first, a genetic algorithm can provide better and more significant results while searching large multi-modal state spaces, large state spaces or n-dimensional surfaces.

Genetic algorithms are widely used in many fields such as robotics, automotive design, optimized telecommunications routing, engineering design and computer-aided molecular design.

The Code to maximize the stalagmite function 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 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_func(input_vector);
    end
end

surfc(xx,yy,f)

shading interp

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

num_cases=50
f=stalagmite_func(input_vector);

tic
%Study 1 - Unbound Inputs
for i=1:num_cases
    [inputs,fopt(i)]=ga(@stalagmite_func,2);
    xopt(i)=inputs(1);
    yopt(i)=inputs(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 inputs\')
subplot(2,1,2)
plot(fopt)
xlabel(\'Iterations\')
ylabel(\'Function minimum\')

tic
%Study2 - Upper and Lower Bounds
for i=1:num_cases
   [inputs,fopt(i)]=ga(@stalagmite_func,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
surfc(xx,yy,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 - Increasing GA Iterations
options=optimoptions(\'ga\');
options=optimoptions(options,\'Populationsize\',170)

tic
for i=1:num_cases
    [inputs,fopt(i)]=ga(@stalagmite_func,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
surfc(xx,yy,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\')

In the above code, we are running ourstalagmite function for 3 different scenarios:

1. Unbound Inputs where the inputs do not have any resrictions i.e., the inputs are randomly distributed even outside the working space

2. Bound Inputs where the inputs are restricted and are not allowed to go beyond our working space

3. Bounded Inputs with increased iterations: Here we are increasing the number of iterations and try out dufferent iterations.

Stalagmite Function:

function[f]=stalagmite_func(input_vector)

x=input_vector(1);
y=input_vector(2);

f1=(sin(5.1*pi*x+0.5))^6;
f2=(sin(5.1*pi*y+0.5))^6;
f3=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);
f=1/(1+f);
end

\"\"

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.