Project

Function optimization using Genetic Algorithm in MATLAB

Genetic Algorithm:

This is an optimization method based on the biological evolution process. Genetic algorithm can be used to solve both constrained and unconstrained optimization problems based on natural selection. 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. After several generations (iterations), the population converge to an optimal solution.

Types of children created by genetic algorithm:

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

  • Elite are the individuals in the current generation with the best fitness values.

  • Crossover are created by combining the vectors of a pair of parents.

  • Mutation children are created by introducing random changes, or mutations, to a single parent.

Working of genetic algorithm:

The following outline summarizes how the genetic algorithm works

a. The algorithm begins by creating a random initial population.

b. The algorithm then creates a sequence of new populations. At each step, the algorithm uses the individuals in the current generation to create the next population. To create the new population, the algorithm performs the following steps:

  1. Fitness value of the current population is calculated. These values are called the raw fitness scores.

  2. Converts the raw scale to a more usable range of values called expectation values.

  3. Selects members, called parents, based on their expectation.

  4. Some of the individuals in the current population that have lower fitness are chosen as elite. These elite individuals are passed to the next population.

  5. Produces children from the parents. Children are produced either by making random changes to a single parent—mutation—or by combining the vector entries of a pair of parents—crossover.

  6. Replaces the current population with the children to form the next generation.

Programming approach:

Stalagmite function:

Programming the stalagmite function:

% Stalagmite function to find the maxima
function[inputs]=stalagmite(input_vector)
x1=input_vector(1);
y1=input_vector(2);
f1_x=(sin(5.1*pi*x1+0.5))^6;

f1_y=(sin(5.1*pi*y1+0.5))^6;

f2_x=exp(-4*log(2)*(x1-0.0667)^2/0.64);

f2_y=exp(-4*log(2)*(y1-0.0667)^2/0.64);

[inputs]=-(f1_x.*f1_y.*f2_x.*f2_y);

end

Main code:

% Program to give a plot of the stalagmite function- And implementation of
% genetic algorothim

clear all
close all
clc

% Generating x and y values
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);

% Creating 2D arrays of the x and y values
[xx yy]= meshgrid(x,y);

% Using for loops to calculate the value of the matrix f, which is the
% stalagmite function of input
for i=1:length(xx)
  for j=1:length(yy)
      input(1)=xx(i,j);
      input(2)=yy(i,j);
      f(i,j)=stalagmite(input);
  end
end

%surfc(X,Y,Z) creates a three-dimensional surface plot with a contour plot underneath
surfc(xx,yy,f)

% Specifying shading type as interpolated so as to obtain a smooth looking
% plot
shading interp

% Finding the co-ordinates of the point where the function attains a
% maximum value

xlabel('x value')
ylabel('y value')
[inputs_1, fval]=ga(@stalagmite,2)


num_cases = 50;


tic

% Study 1- Statistical behaviour - Unbounded search space
for i=1:num_cases
    [inputs, fopt(i)]=ga(@stalagmite,2);
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end

% Generating plot 2
figure(2)
subplot(2,1,1)
hold on
surfc(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 maximum')

study_time1=toc

tic

% Study 2 - With upper and lower bounds
for i=1:num_cases
    [inputs, fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end

% Generating plot 3
figure(3)
subplot(2,1,1)
hold on
surfc(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 maximum')

study_time2=toc

% Study 3 - Increasing the iterations

% Specifying population size
options=optimoptions('ga');
options=optimoptions(options,'PopulationSize',170);

tic

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

study_time3=toc

% Generating plot 4
figure(4)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs and Increased population size')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('Function maximum')

Explanation for the code:

1. Initially, a vector of x and y values are created. Using the 'meshgrid' command, these vectors are transformed into a 2D array.

2. Then 'for' loop is used to calculate the value of the matrix f, which is the stalagmite function of input.

3. Then using the 'surfc' command, a contour over the surface of the stalagmite funciton is created.

Fig1. Initial surface created by stalagmite function

Various Studies:

a. Statistical behaviour - Unbounded search space

Here, the search space where the genetic algorithim has to work is not mentioned. So, the genetic algorithim spreads its population all over the limits of the bounds.

This is carried out for the numeber of times that is already metioned under the variable num_cases. This specifies the number of iterations for which the genetic algorithm has to work. Specifying, such numbers, helps in mentioning the stopping critera for the genetic algorithm.

A sub-plot of the maximum value of the function for each iteration is also generated along with the surface plot.

The genetic algorithim is visualized with the help of 'markers'. This gives a visual idea of how a population is created initially and after successive generations they converge towards the maximum or minimum value.

Fig 2. Unbounded search space

b. Upper and lower bounded search space

In this study, the syntax of the genetic algorithm is changed in order to mention the upper and lower bounds of the search space.In this way, the genetic algorithim looks for the maximum value in a more confined space which gives more accurate results.

Syntax for ga

(i) While specifying the upper and lower bounds:

x = ga(fun,eq,A,b,Aeq,Beq,ub,lb)

This defines a set of lower and upper bounds on the design variables, x, so that a solution is found in the range lb ≤ x ≤ ub. (Set Aeq=[] and beq=[] if no linear equalities exist.)

Fig 3. Bounded search space

c. Bounded limits with increased population size

In this study, the syntax of the ga is further altered to increase the population size. Increasing the population size increases the accuracy of the GA. Increasing population size also causes the number of generations to converge to increase. The optimal population for a given problem is the point of inflection where the benefit of quick convergence is offset by increasing inaccuracy.

Syntax for ga

(ii) While specifying the upper and lower bounds with increased population size:

x = ga(fun,eq,A,b,Aeq,Beq,ub,lb,nonlcon,option)

This minimizes with the default optimization parameters replaced by values in options. (Set nonlcon=[] if no nonlinear constraints exist.) Create options using optimoptions.

Fig 4. Bounded limits with increased population size

References:

1. https://in.mathworks.com/help/gads/how-the-genetic-algorithm-works.html

2. https://in.mathworks.com/discovery/genetic-algorithm.html

3. MATLAB documentation

4. http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.105.2431&rep=rep1&type=pdf#:~:text=We%20show%20that%20increasing%20the,is%20offset%20by%20increasing%20inaccuracy.

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.