Project

Optimization of stalagmite function using genetic algorithm Using MATLAB

AIM: Optimization of stalagmite function using genetic algorithm

INTRODUCTION: Genetic algorithm heuristic that is inspired by Charles Darwin\'s theory of natural evolution. This algorithm reflects the process of natural selection where the fittest individuals are selected for reproduction in order to produce offspring of the next generation.

Natural selection: The process of natural selection starts with the selection of fittest individuals from a population. They produce offspring which inherit the characteristics of the parents and will be added to the next generation. If parents have better fitness, their offspring will be better than parents and have a better chance at surviving. This process keeps on iterating and at the end, a generation with the fittest individuals will be found.

There are five phases to genetic algorithmh

1) Population

2)Fitness function

3)selection

4)crossover

5 mutation

1) Population problem Set of individuals are called a population Every individual is a solution to the you want to solve.

2)Fitness function This function determines the fitness of each individual and the individual will be selected based on their fitness score

3)Selection Genetic algorithm selects the fittest individual and passes there genes to the the next generation. Two pairs of individuals(parents) are selected based on their fitness scores and there genes are passed to the next generation

4)Crossover Offspring is created by exchanging the genes of the parents and now the offspring is the fittest and he is added to the population

5)mutation the newly created offspring is mutated that is there genes are flipped a bit to maintain diversity within the population and to prevent premature convergence.The algorithm gets terminated once no more offsprings are created then we are left with set of offspring who are the fittest of all othere previous generations,The same is used in various programming language like java matlab,etc.

THEORY The genetic algorithm is a method for solving both constrained and unconstrained optimization problems that are based on natural selection, the process that drives biological evolution. The genetic algorithm repeatedly modifies a population of individual solutions. At each step the genetic algorithm selects individuals at random from the current population to be parents and uses them to produce the children for the next generation. Over successive generations, the population \"evolves\" toward an optimal solution. You variety of optimization problems that are not well suited for standard optimization algorithms, including problems in which the objective function is discontinuous, nondifferentiable, stochastic, or highly nonlinear. The genetic algorithm can address problems of mixed integer programming, where some components are restricted to be integer-valued.

Structure of the MATLAB program

The program has two User Defined Stalagmite Functions and one primary body. Three studies are performed using the same function but with alterations in function \'ga\'.

Part:1 \'stalagmite1\' function

• A function \'stalagmite 1\' is created with return value being f(x, y)

• The inputs of the function should be a vector of x and y.

• The equations as shown in \'Stalagmite Equations are entered but f(a, y)is multiplied by -1 which generates a downward stalagmite.

Function Body:

function [product] = stalagmite(input_vector)
             xpy = input_vector(1)*pi; 
             ypy = input_vector(2)*pi;

             f1x = (sin(5.1*xpy+0.5))*6; 
             f1y = (sin(5.1*ypy+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));

            product = -1*fix*fly*f2x*f2y ;
end

Part 2: \'stalagmite\' function

• A function \'stalagmite\' is created with return value being f(x, y)

• The inputs of the function should be a vector of x and y.

• The equations as shown in \'Stalagmite Equations\' are entered and f(x, y) is kept as shown.

Function Body:

function [product] = stalagmite(input_vector) 
            Xpy = Input_vector(1)*pi;
            ypy = input_vector(2)*pi;

            f1x = (sin(5.1*xpy+0.5))^6; 
            fly = (sin(5.1*ypy+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));

            product = f1x*f1y*f2x*f2y;
end

Part 3: Implementing the UDFs and \'ga\' in the body

The body is divided into 3 studies in which certain commands are altered.

• x and y values are initialized and another value num case is initialized to any number which is used to iterate the \'ga\' function so many times.

•A meshgrid is created using x and y values using \'meshgrid\' function which gives two square matrices, • A new matrix is assigned to the \'stalagmite\' function whose inputs are set of coordinates which are generated by keeping this in two for loops. The outer for loop navigates the first square matrix generated by \'meshgrid\' and the second for loops navigates the second square matrix. This generates a set of values in the 3rd dimension

• Now a similar operation as the previous step is performed using \'stalagmite\' function. \'stalagmite\' function generates values in the positive z-direction.

• Now \'ga\' function is implemented to \'stalagmite\' because \'stalagmite 1 generates values in the negative z direction. The \'ga\' function returns the minimum of the \'stalagmite function and their corresponding dependent variables

• The minimum value van be multiplied with -1 to get a positive number and ca be plot on the function surface of \'stalagmite\' function and hence the maximum and their corresponding x and y values can be shown.times to get

• function is iterated several a range of maximum values and the most maximum value is chosen The similar procedure is followed in study 2 where the limiting values of x and y axes are shown on the plot • In the 3rd study, the number of experimental subjects which the \'ga\' function picks at a time is increased to increase the accuracy.

Body of the program

% Study 1
% defining your search space 
x = linspace(0,0.6,150); 
y = linspace(0,0.6,150); 
num_case = 100;

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

% Evaluating the downward stalagmite(stalactite) function 
for i = 1:length(xx) 
    for j = 1:length(yy) 
        input vector1(1) = xx(i,j); 
        input_vector1(2) = yy(i,j);
        f(i,j) = stalagmite(input_vector1);
    end

end

% 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(ij) = stalagmite (input_vector);
    end
end

tic
for i=1:num_case
    [inputs,For A(i)] = ga(@stalagmitel, 2);
    Xopt(i) = inputs(1); 
    Yopt(i) = inputs(2);

end

study1_time = toc; 
pFopt = -1. *Fopt;
%Fid is the maximum value
Fid = max(pFopt);

for q=1:num_case
    if pFpt(q)==Fid
    Xid = Xopt(q);
    Yid = Yopt(q);
    end
end


figure (1),clf

subplot(211)
surfc(xx,yy,f),
xlabel(\'X value\');
ylabel(\'Y value\');
axis square;
shading interp:
hold on;
scatter3(Xid, Yid, Fid, \'o\'); 
title(\'Without modifying ga\');
subplot(212) 
plot(pFopt)
xlabel(\'Iterations\') 
ylabel(\'Function Maximum\');

%% Study 2

% defining your search space
x = linspace(0,0.6,150); 
y =linspace(0,0.6,150); 
num_case = 100;

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

% Evaluating the downward stalagmite (stalactite) function 
for i = 1: length(xx)
    for j = 1: length(yy)
        input_vector1(1) = xx(i,j);  
        input_vector1(2) = yy(i,j); 
        F(i,j) = stalagmite1(input_vector1);
    end
end


Evaluating the stalagmite function
for i = 1:length(xx) 
    for j = 1:length(yy) 
        input vector(1)= xx(1,j); 
        input_vector(2) = i(i,j); 
        f(i,j) = stalagmite(input_vector)
    end
end


% Evaluating the downward stalagmite(stalactite) function 
tic
for i=1:num_case 
     [inputs, For A(i)] = ga(@stalagmitel, 2,[],[],[],[],[0;0],[1;1]);
     Xopt(i) = inputs(1);
     Yopt(i) = inputs(2);
end


study1_time = toc; 
pFopt = -1. *Foot; 
%Fid is the maximum value
Fid = max(pFopt);

for q=1: num_case 
    if pFopt(q)==Fid
        Xid = Xopt(q); 
        Yid = dopt(q);
    end
end


figure(2),clf
subplot(211)
surfc(xx,yy,f);
xlabel(\'x value\'); 
ylabel(\'Y value\'); 
axis square; 
shading interp; 
hold on; 
scatter3(Xid, Yid, Fid, \'o\');
title(\'Boundary Values Assigned\')

subplot(212)
plot(pFpt) 
xlabel(\'Iterations\') 
ylabel(\'Function Maximum\');

% Study 3 
close all 
clear all 
clc

% defining your search space 
x = linspace(0,0.6,150); 
y = linspace(0,0.6,150); 
num_case = 100;

% Creating a 2Dmesh
[xx,yy] = meshgrid(x,y);

% Evaluating the downward stalagmite (stalactite) function 
for i = 1:length(xx) 
    for j = 1:length(yy)
    input_vector(1) = xx(1,1); 
    input vector(2) = yy(1,1);
    input vector(2) - i(i,1);
    F(1,j) = stalagmite(input_vector);
    end
end


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(1,3) = stalagmite(input_vector);
    end
end

% Evaluating the downward stalagmite(stalactite) function 
option = optimoptions(\'ga\');

option = optimoptions (options, \'Population Size\',300);

for i=1:num_case 
    [inputs, Fopt(1)] = ga(@stalagmitel,2,[],[],[],[],[0;0],[1;1]) 
    Xopt(1) = inputs(1); 
    Yopt(1) - inputs(2);
end

study1_time = toc; 
pFopt = -1.*Fopt;
XFid is the maximum value
Fid = max(pFpt);

for q=1:num_case 
    if pFopt(q)==Fid
        Xid - Xopt(q); 
        Yid = Yopt(q);
    end
end

figure(3),clf

subplot(211) 
surfc(xx,yy,f);
xlabel(\'X value\'); 
ylabel(\'Y value\'); 
axis square;
shading interp;
hold on;
scatter3(Xid, Yid, Fid, \'o\'); 
title(\'With Boundary levels and Specified Population size\');

subplot(212) 
plot(pFopt) 
xlabel(\'Iterations\') 
ylabel(\'Function Maximum\' );

ERROR FACED : No Error Counter

RESULT

Study 1 Output

Study 2 Output

Study 3 Output

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.