Project

Genetic Algorithm

Aim:

To understand the concept of the genetic algorithm and write code in MATLAB to optimize the stalagmite function and find the global maxima of the function.

Theory:

The stalagmite function is a function with 4 components :

  1. 2 sine components in each of the axis direction
  2. 2 normal Gaussian distribution in each of the axis direction

The stalagmite function is given theoretically as:

As can be seen clearly that the stalagmite function is a combination of 4 function components namely `f_(1x)\ & f_(1y)` representing the sine components in each of the axis direction and the `f_(2x)\ & f_(2y)` representing the gaussian(normal) function in the each of the axis direction.

For our purposes, we will consider the domain of the function as a rectangle in `R^2` space that maps the elements from the `R^2` space onto the `R` space. We will consider only the real-valued functions for theoretical simplicity.

Genetic Algorithm :

A genetic algorithm is a search 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.

A genetic algorithm (GA) in the mathematical and computer science terms is a method for solving both constrained and unconstrained optimization problems based on a natural selection process. The algorithm repeatedly modifies a population of individual solutions. At each step, the genetic algorithm randomly selects individuals from the current population and uses them as parents to produce the children for the next generation. Over successive generations, the population "evolves" toward an optimal solution. Genetic algorithms are commonly used to generate high-quality solutions to optimization and search problems by relying on biologically inspired operators such as mutation, crossover, and selection.

Genetic Algorithm is used to find the global minima according to the random sampling. We might tend to think as to why we need the genetic algorithm when we already have the differential calculus aid with us. The answer is fairly simple. If the problems tend to get highly coupled and non-linear with sophisticated constraints then it becomes very hard to apply the differentiability rules to find the Global minima. Also, one major advantage of the genetic algorithm over the conventional algorithm to find the minima is that it works well for discontinuous, nondifferentiable, stochastic, or highly nonlinear functions which pose a difficulty in the theoretical differential calculus.

The Genetic Algorithm differs in the way of action from the conventional method in two ways:

  1. The genetic algorithm generates a population of points at each iteration. The best point in the population approaches an optimal solution whereas the classical algorithm generates a single point at each iteration. The sequence of points approaches an optimal solution.
  2. The genetic algorithm selects the next population by computation which uses random number generators whereas the classical algorithm selects the next point in the sequence by a deterministic computation.

The genetic algorithm solves smooth or nonsmooth optimization problems with any types of constraints, including integer constraints. It is a stochastic, population-based algorithm that searches randomly by mutation and crossover among population members. MATLAB has an in-built solver with the code as `ga`.

How does the Genetic Algorithm Work?

The main outline of the program methodology is "First of all the algorithm begins by creating a random initial population. Then it creates a sequence of new populations. At each step, the algorithm uses the individuals in the current generation to create the next population."

The initial population is set to 50 in case the number of independent variables is less than or equal to 5 otherwise it is set to 200.

Now to create a new population at the new step the algorithm selects the so-called parents and computes the new population(children) from it. It can be thought of like the parents each contribute their genes and their hybrid is a mixture of genes from both its parents. The algorithm usually selects individuals that have better fitness values as parents which makes sense also as the children should be healthy enough so that they can reproduce and produce further healthy offsprings.

The genetic algorithm creates three types of children for the next generation:

1.) Elite:

Elite are the individuals in the current generation with the best fitness values. These individuals automatically survive to the next generation and will produce healthy offsprings which further propagate and produce another set of healthy offsprings and so on.

2.) Mutation

As the name suggests the mutation happens when you alter something for the better good. The same thing is done by MATLAB for bounded or linearly constrained problems. For highly coupled problems and complex equations, it becomes a bit skeptical to rely on the healthy offsprings only and hence some variation must be introduced. The algorithm creates mutation children by randomly changing the genes of individual parents.

3.) Crossover:

s from the two species storing dominant information from each of its parents. Then using these genes the new offsprings are produced. With respect to MATLAB, the algorithm creates crossover children by combining pairs of parents in the current population. At each coordinate of the child vector, the default crossover function randomly selects an entry, or gene, at the same coordinate from one of the two parents, and assigns it to the child. Then these children are ready to produce the offspring further using their genes in any of the discussed manners.

Finally, the algorithm stops according to some of the default constraints in MATLAB which can be modified according to the needs of the user. These are :

1.) Generations: It specifies the number of generations that MATLAB should produce and will stop after the number of generations is reached. It is not a very strong stopping criterion as we may provide the number of generations to be a very large value because of which the MATLAB program can keep on running extensively jeopardizing both the computational power and the precious time in hand. It is also possible that the user may provide quite less number of generations because of which the genetic algorithm might not reach the desired answer.

2.) Time: The genetic algorithm will stop after running for a certain time set by the user. This comes in handy as the time may be limited and so if the algorithm is taking too much time it is better to have some compromise on the answer along with the shortening of the time.

3.) Tolerance: This is a very strong stopping criterion. It specifies the convergence limit between two populations and when they differ by less than this amount then the algorithm will stop.

The algorithm stops as soon as any one of these conditions is met. There are several other nuances regarding the selection and the reproduction procedure which are out of the scope of this report and hence are not discussed here.

Imp Point: It is important to note that the genetic algorithm solves the optimization problem i.e. it can only find the minima. To find the maxima we will apply a bit complex approach. First of all, if a function has maxima it is equivalent to saying that the negative of the function will have minima at the same point. This can be easily visualized with the help of the following diagram:

As is seen clearly in the following diagram the graph of the function `f(x)` has a maximum at the point `x_0` and in the same graph we plot the negative of the function i.e. the function `-f(x)` and we can see visually that it has a minimum at the point `x_0`. This visualization can be extended to the 3-D and higher dimension graphs with myriad independent variables.

Visualization of stalagmite function in MATLAB

In this section, we tend to see how to implement the stalagmite function in MATLAB.

First of all, we will define the function stalagmite in MATLAB. The following code is implemented:

function value = stalagmite_function(inputs_1)

%
% This function will take in input a two dimensional matrix inputs_1 and
% then produce a value at the specified x and y point
%
%
% Note that this function will return the negative of the opriginal value
% and hence in the main program while calling the function we must multiply
% it by -1
%

% Segregating the inputs
x = inputs_1(1);
y = inputs_1(2);

% computing the stalagmite function symbollically
f1x_1  =  sin(5.1*pi*x + 0.5).^6;
f1y_1  =  sin(5.1*pi*y + 0.5).^6;
f2x_1  =  exp(-4*log(2)*(x-0.0667).^2/0.64);
f2y_1  =  exp(-4*log(2)*(y-0.0667).^2/0.64);

% Returing the value
value = (-1) * f1x_1 * f1y_1 * f2x_1 * f2y_1;

end

Explanation: This code will take in the input as a 2-D vector of the X and the Y values and will return a scalar value corresponding to the value of the stalagmite function at that point but with a negative sign. The use of this negative sign will become evident once we proceed towards the optimization.

In the MAIN MATLAB Program the following code is implemented to generate the stalagmite function:

clear all;
close all;
clc;

% Creating the x and the y vectors
x = linspace(0,0.6,100);
y = x;

% Creating the meshgrid
[XX,YY] = meshgrid(x,y);

% Assigning the value at each grid point
for i=1:length(x)
    
    % Inner loop
    for j = 1:length(y)
        
        % Computing the value
        f(i,j) = (-1) * stalagmite_function([x(i),y(j)]);
        
    end
    
end


Explanation: This Code will create the vectors x and y in the x and the y-direction and then using each corresponding value between x and y it will compute the value at that particular grid point and will store it in a variable called f.

After the computation of the stalagmite function, it is important that we visualize it. To plot a graph in MATLAB the following command is implemented:

% Plotting the stalagmite function
figure(1); clf;
set(gcf,'Position',[100,100,1000,1000])
surf(YY,XX,f);
shading interp;
axis tight;

% Adding the x and the y labels
xlabel('X - Values');
ylabel('Y - Values');
zlabel('Function Values');
title('Stalagmite Function'); 

% Adjusting the parameters
rotate3d on;
colormap jet;
colorbar;

% Increasing the Fontsize Property
set(gca,'Fontsize',20)

On the execution of the above MATLAB command the following output was produced:

Some more of the same plot but from different views:

Now as it can be seen from the graphs the stalagmite function has several local maxima that occur in the form of the mountain peaks. To find the global maxima using the Genetic Algorithm we will take the negative of this function and find its global minima which will be the original functions global maxima as stated in the theory.

We multiply the function with negative 1 and plot it which is done with the help of the following MATLAB Command:

% Plotting the stalagmite function
figure(1); clf;
set(gcf,'Position',[100,100,1000,1000])
surf(YY,XX,-f);
shading interp;
axis tight;

% Adding the x and the y labels
xlabel('X - Values');
ylabel('Y - Values');
zlabel('Function Values');
title('Stalagmite Function'); 

% Adjusting the parameters
rotate3d on;
colormap jet;
colorbar;

% Increasing the Fontsize Property
set(gca,'Fontsize',20)

This code plots the function with a negative multiplication by unity. The following graph is obtained:

From a different angle :

As it is evident from the graph of the negative function, co-ordinates of the local minima of the negative function coincide with the coordinates of the local maxima.

Before we move towards the optimization using the genetic algorithm it is imperative to understand the syntax of the genetic algorithm and how it works:

Syntax Explanation of the Genetic Algorithm:

The syntax of the genetic algorithm is indeed its acronym i.e. ga.

1.) The simple syntax of ga( function, n_variables ) will give the local unconstrained minimum, to the objective function. Here n_variables are the number of independent variables in the function.

2.) The syntax: ga(@stalagmite_function, num_variables, A, b) finds a local minimum x to fun, subject to the linear inequalities Ax ≤ b.

3.) The syntax: ga(@stalagmite_function,num_variables, A, b, Aeq, beq) finds a local minimum, subject to the linear equalities Aeq*x = beq and Ax ≤ b.

4.) The syntax: ga(@stalagmite_function,num_variables, A,b, Aeq, beq, X, Y) defines a set of lower and upper bounds on the design variables in the terms of the column matrices.

5.) The syntax: ga(@stalagmite_function,num_variables, A,b, Aeq, beq, X, Y,non_linear, int, options) will find the minimum subject to the non-linear constraints in the non_linear and int and according to the options which can contain several initializations like the Population size, Fitness Scaling function, etc.

Now with a basic knowledge of the syntax if the genetic Algorithm lets try and apply it to our stalagmite model in MATLAB.

Application of the GA without constraints:

In the first case, we will apply the algorithm directly without any constraints:

%% ---------------------------------------------------------------------

% Case 1
% Applying the algorithm directly

% Initialising the rng to be 0 for uniform results
rng(0);

% Number of test cases
num_cases = 50;

% starting the time clock
tic;

% Running the ga algorithm
for i = 1:num_cases
    
    % Apply the algorithm for these many cases
    [coordinates_to_plot,output_1(i)] = ...
        ga(@stalagmite_function,2);
    
    % Extracting the co-ordinates
    case1_x(i) = coordinates_to_plot(1);
    case1_y(i) = coordinates_to_plot(2);
    
end

% Ending the case 1 study time
end_time_case_1 = toc

The code basically first initializes the random number generation count to 0 so that whenever we run the simulation we start from the same random sampling and get the same result. Here we run the Genetic algorithm 50 times and each time we will get a different output as in each case it will start sampling from a different number. The results of all the 50 steps are stored in the output_1 function and the corresponding minimum co-ordinates are stored in case1_x and case1_y respectively. Also, it is important to note that the ga will work and will find the local minima for the function provided as per our definition.

Now after the Computation we shall plot the results. It is achieved with the help of the following MATLAB Code:

% Plotting the output values for the minimum wrt the iterations
figure(2);clf;
plot(output_1)
set(gcf,'Position',[100,100,1000,1000])
xlabel('Iterations');
ylabel('Min Value according to the GA');

title('Min-Value at different iterations ');
grid on;
grid minor;
set(gca,'FontSize',15)

The following results were obtained:

As is seen clearly the value of the output is all over the place and is not a fixed value. This is because of the fact of how the genetic algorithm works and it reaches a different minimum if it starts with a different random number.

However, it is important to note that this is the minimum value for the negative of the stalagmite function and hence the negative of these values will be the maximum to the original stalagmite function. We shall plot the maximum values on the top of the original function by multiplying these output values by negative 1. The following code does the plotting in MATLAB:

% Plotting on top of one another i.e on the top of the stalagmite
figure(3); clf;
set(gcf,'Position',[100,100,1000,1000]);
surfc(YY,XX,f);
hold on;
plot3(case1_x,case1_y,-output_1,'ko-',...
    'markersize',7,'linew',2)
shading interp;
axis tight;

% Adding the x and the y labels
xlabel('X - Values');
ylabel('Y - Values');
zlabel('Function Values');
title({['Maximum Values on the top of the function'];  ...
    ['with the help of genetic algorithm']})

% Adjusting the parameters
rotate3d on;
colormap jet;
colorbar;

set(gca,'FontSize',20)

The following graph was obtained:

Some more views of the same graph:

As can be seen from the graph the maximum values are all over the place. This is because of the fact that we have not provided any bounds for the search space of the domain. Hence MATLAB thinks of it in optimizing in every way possible and hence the maximum values reach out of the domain also.

The graph of the maximum values for different iterations is obtained by multiplying the outputs by -1. The following code is implemented for this job:

% Plotting the output values for the minimum wrt the iterations
figure(4);clf;
plot(-output_1)
set(gcf,'Position',[100,100,1000,1000])
xlabel('Iterations');
ylabel('Max Value according to the GA');

title('Max-Value at different iterations ');
grid on;
grid minor;
set(gca,'FontSize',20);

The following graph is obtained :

As is seen there is a lot of oscillations in the values of the local maxima that the genetic algorithm predicts which can be attributed to the reasons mentioned above. Finally, we see the runtime of the simulation is :

Clearly, this method of finding the maxima is not working out so well as each time we run the function we get different outputs. So now we move to the second case study using the constraints given on the search space.

Application of the GA with constraints on search space:

In the first case, we will apply the algorithm directly without any constraints:

%% ------------------------------------------------------------------------

% Case 2 
% Limiting the search space

a = input('Enter the search space you want to limit to between 0 and 1: ');
fprintf('\n');

% Initialising the rng to be 0 for uniform results
rng(0);

% starting the time
tic;

% Running the ga algorithm
for i = 1:num_cases
    
    % Apply the algorithm for these many cases
    [coordinates_to_plot,output_2(i)] = ga(@stalagmite_function,2,...
        [],[],[],[],[0;0],[a;a]);
    
    plotting_x_values(i) = coordinates_to_plot(1);
    plotting_y_values(i) = coordinates_to_plot(2);
    
end

% Ending the time
end_time_case_2 = toc

The code basically first initializes the random number generation count to 0 so that whenever we run the simulation we start from the same random sampling and get the same result. Here we run the Genetic algorithm 50 times and each time we will get a different output as in each case it will start sampling from a different number. The results of all the 50 steps are stored in the output_1 function and the corresponding minimum co-ordinates are stored in plotting_x_values and plotting_y_values respectively. Also, it is important to note that the ga will work and will find the local minima for the function provided as per our definition.

As can be seen in the syntax of the ga, we have introduced some square brackets [], which allows us to input to MATLAB that we don't want any linear inequality and equality constraints. Also when the code is run, MATLAB will ask the user to enter the search space domain. Then according to the syntax discussed above MATLAB will search for a local minimum in the space [0, a] in both the axis.

Now after the Computation we shall plot the results. It is achieved with the help of the following MATLAB Code:

% Plotting the output values for the minimum
figure(5);
set(gcf,'Position',[100,100,1000,1000]);
plot(output_2)
xlabel('Iterations');
ylabel('Min Value according to the GA');

title({['Min-Value at different iterations '];...
    ['for search space between 0 and ' num2str(a) ' on both axis']});
grid on;
grid minor;
set(gca,'FontSize',20);

We first limit our search space to [ 0, 1 ] in both the axis directions. The following results are obtained :

As can be seen in limiting the search space we tend to get fewer oscillations as compared to the one with no constraints. This is because we now explicitly tell MATLAB to search in the specified domain and hence many of the values will be repeated.

However, it is important to note that this is the minimum value for the negative of the stalagmite function and hence the negative of these values will be the maximum to the original stalagmite function. We shall plot the maximum values on the top of the original function by multiplying these output values by negative 1. The following code does the plotting in MATLAB:

% Plotting on top of one another
% Plotting the stalagmite function
figure(6); clf;
set(gcf,'Position',[100,100,1000,1000]);
surfc(YY,XX,f);
hold on;
plot3(plotting_x_values,plotting_y_values,-output_2,'ko-',...
    'markersize',10,'linew',3)
shading interp;
axis tight;

% Adding the x and the y labels
xlabel('X - Values');
ylabel('Y - Values');
zlabel('Function Values');
title({['Maximum Values on the top of the function'];  ...
    ['with the help of genetic algorithm'];
    ['for search space between 0 and ' num2str(a) ' in both axis']})


% Adjusting the parameters
rotate3d on;
colormap jet;
colorbar;

set(gca,'FontSize',20)

The following graph was obtained:

From some more different views:

As can be seen from the graph the maximum values are not all over the place. This is because of the fact that we have provided the bounds for the search space of the domain. Hence MATLAB will search for the minimum it in the given specified domain and hence we will have many recurring values within the constricted search space.

The graph of the maximum values for different iterations is obtained by multiplying the outputs by -1. The following code is implemented for this job:

% Plotting the output values for the minimum
figure(7);
set(gcf,'Position',[100,100,1000,1000]);
plot(-output_2)
xlabel('Iterations');
ylabel('Max Value according to the GA');

title({['Max-Value at different iterations '];...
    ['for search space between 0 and ' num2str(a) ' on both axis']});
grid on;
grid minor;
set(gca,'FontSize',20);

The following graph is obtained :

As is seen there is are relatively stable oscillations in the values of the local maxima that the genetic algorithm predicts compared to the no-constraints case which can be attributed to the reasons mentioned above. Finally, we see the runtime of the simulation is :

For different search spaces the following results were obtained:

For search space : [ 0, 0.5 ] in both the axis directions:

And for the prediction of the maximum value we get per iteration we get:

Here we see that the oscillations are almost zero and in most of the iterations, the genetic algorithm predicts the maximum value of near about 1 which is clearly evident from the graph of the stalagmite function. Clearly we have improved our result and calculation somewhat.

But this method will work only when we know the effective search space. In case we don't know the search space we are in trouble. So now we resort to a new approach to find global maxima using the Population size parameter without having to specify any bounds for the search space.

Application of the GA with the change in the optimum search pattern:

As the genetic algorithm begins by creating a random initial population we change the number of the initial population and then feed this data into the 'ga' solver as an initial condition. The following MATLAB Code does this job:

%% -----------------------------------------------------------------------

% Case 3 
% changing the population size in the optimum options

a = input('Enter the search space you want to limit to between 0 and 1: ');
fprintf('\n');

% Initialising the rng to be 0 for uniform results
rng(0);

% Initialising the optimum options
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',200);

% starting the time
tic;

% Running the ga algorithm
for i = 1:num_cases
    
    % Apply the algorithm for these many cases
    [coordinates_to_plot,output_3(i)] = ga(@stalagmite_function,2,...
        [],[],[],[],[0;0],[a;a],[],[],options);
    
    plotting_x_values(i) = coordinates_to_plot(1);
    plotting_y_values(i) = coordinates_to_plot(2);
    
end

% Ending the time
end_time_case_3 = toc

The code first initializes the random number generation count to 0 so that whenever we run the simulation we start from the same random sampling and get the same result. Here we run the Genetic algorithm 50 times and each time we will get a different output as in each case it will start sampling from a different number. The results of all the 50 steps are stored in the output_1 function and the corresponding minimum co-ordinates are stored in plotting_x_values and plotting_y_values respectively. Also, it is important to note that the ga will work and will find the local minima for the function provided as per our definition.

As can be seen in the syntax of the ga, we have introduced some square bracket [], which allows us to input to MATLAB that we don't want any linear inequality constraints. Also when the code is run, MATLAB will ask the user to enter the search space domain. Then according to the syntax discussed above MATLAB will search for a local minimum in the space [0, a] in both the axis. As there are no non-linear constraints we set the brackets after [a, a] to be empty i.e. [], [] as per the syntax of the ga.

Also, we input the additional parameter of options after setting the non-linear constraints to be empty to be that of the desired population size. We shall see that this parameter of the Population size plays an important role in the convergence of the solution.

Now after the Computation we shall plot the results. It is achieved with the help of the following MATLAB Code:

% Plotting the output values for the minimum
figure(5);
set(gcf,'Position',[100,100,1000,1000]);
plot(output_3)
xlabel('Iterations');
ylabel('Min Value according to the GA');

title({['Min-Value at different iterations '];...
    ['for search space between 0 and ' num2str(a) ' on both axis']});
grid on;
grid minor;
set(gca,'FontSize',20);

We first limit our search space to [ 0, 1 ] in both the axis directions and the initial Population size to be 200. The following results are obtained :

As can be seen in limiting the search space we tend to get very fewer oscillations as compared to the above cases. This is because we have initialized the population size to be 200 instead of the default 50 which helps in giving better results.

However, it is important to note that this is the minimum value for the negative of the stalagmite function and hence the negative of these values will be the maximum to the original stalagmite function. We shall plot the maximum values on the top of the original function by multiplying these output values by negative 1. The following code does the plotting in MATLAB:

The same figure but from different views:

As can be seen from the graph the maximum values are not all over the place rather it is limited. This is because of the fact that we have provided the initial population quite high. Hence MATLAB will optimize it in a better way and hence we will have many recurring values.

The graph of the maximum values for different iterations is obtained by multiplying the outputs by -1. The following code is implemented for this job:

As is seen there is are relatively stable oscillations in the values of the local maxima that the genetic algorithm predicts compared to the above 2 cases discussed case which can be attributed to the reasons mentioned above. Finally, we see the runtime of the simulation is :

We now change the Population Size parameter to a quite lower value and see the results. We set the population size value to 20. The following results were obtained:

As can be seen clearly from the graph the decrease in the Population Size value has increased the number of oscillations. This can be attributed to point discussed in the theory above. Finally we plot the Maximum Values on top of the stalagmite function.

As it is seen that there are a lot of fluctuations in the maximum value predicted by the genetic algorithm

The best way to find the Global maxima would be to increase the Population size even further. We increase the Population size to 500. The following results were obtained:

Here we see that we get all the predicted values well within the tolerance limit of 1e-6 which is evident from the graph. This is because of the fact that we have increased the Population size to 500.

Finally, the simulation time is:

It is seen that by nearly increasing the Population to twice we get the runtime also twice as what was obtained previously. It makes sense also that if we increase the population twice as much then the algorithm will be doing twice as many computations at each iteration step and hence overall taking twice as much time. We also see we get excellent convergence results with an increased population. In terms of simple biology, it can be thought like this. If there are more people living on a planet and are reproducing the offsprings then the probability of them going extinct is very less as compared to the planet where there are fewer people. The planet will higher population will thrive in terms of genetic mutations and reproduction which is exactly what happens in the genetic algorithm in MATLAB. A higher population implies that there would be better genes at each computation step as compared to the low population size. Hence we get an improved convergence for many iterations.

However, it is important to note that an unnecessary increase in the population size will only lead to wastage of the computation power and running time. So based upon the experience of the user a proper selection of the population size is a must.

Errors Faced While Programming

1.) One major error was the incorrect use of the syntax of the genetic algorithm. It is illustrated in the code below:

 % Apply the algorithm for these many cases
    [coordinates_to_plot,output_3(i)] = ga(@stalagmite_function,2,...
        [],[],[],[],[0;0],[a;a],[],[],options);
    

To a beginner's eye this may seem correct however there is a subtle mistake. While calling the stalagmite function the MATLAB function ga expects a function handle and so we get an error.

This problem is fairly easy to fix. We add the @ symbol before the stalagmite function to tell MATLAB that it is a function handle explicitly.

2.) Another error is the incorrect plotting of the outputs. It is shown in the code below:

% Plotting on top of one another i.e on the top of the stalagmite
figure(3); clf;
set(gcf,'Position',[100,100,1000,1000]);
surfc(YY,XX,f);
hold on;
plot3(case1_x,case1_y,output_1,'ko-',...
    'markersize',7,'linew',2)
shading interp;
axis tight;

% Adding the x and the y labels
xlabel('X - Values');
ylabel('Y - Values');
zlabel('Function Values');
title({['Maximum Values on the top of the function'];  ...
    ['with the help of genetic algorithm']})

% Adjusting the parameters
rotate3d on;
colormap jet;
colorbar;

set(gca,'FontSize',20)

This code by itself won't give any error but will produce the wrong result

As can be seen clearly the output values are plotted in the negative sign. This is because the user has forgotten to change the sign of the outputs. It can be fixed easily by providing a minus sign.

3.) One more error is the wrong syntax of the ga. See the code below:

 % Apply the algorithm for these many cases
    [coordinates_to_plot,output_1(i)] = ...
        ga(@stalagmite_function);
    

The user has forgotten to specify the number of variables. Without this specification, MATLAB can't understand how may variables are there and how to optimize the function. It produces an error as shown below:

It can be corrected by providing the right set of inputs

Conclusion:

In this report, we studied the genetic algorithm, how it has been constructed from the theory of evolution considering all the factors of reproduction at each stage that can lead to better production of the offsprings in our case the data and hence give better results. The genetic algorithm for a particular case study has been applied several times as each time we would get a new result as the algorithm selects the initial population at random. These small and tiny changes can lead to drastic variations in the computation of the minimum. In some cases, it may give local minima and in some, it may give global. For problems with less number of independent variables, it is not much of a problem as they can be verified in the manual calculations and the visualizations. However, for an engineering level optimization problem with several independent variables, it is a must to select the conditions of the genetic algorithm properly to give accurate convergence results.

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.