Challenge

Week 5 - Genetic Algorithm

Aim: To write a code in MATLAB to optimize the stalagmite function and find the global maxima of the function.

Objectives:

  • Explain the concept of genetic algorithm (ga).
  • Explain the syntax for ga in MATLAB.
  • Plot graphs for all 3 studies.

Concept of Genetic Algorithms: Genetic Algorithms(GAs) are adaptive heuristic search algorithms that belong to the larger part of evolutionary algorithms. Genetic algorithms are based on the ideas of natural selection and genetics. These are intelligent exploitation of random searches provided with historical data to direct the search into the region of better performance in solution space. They are commonly used to generate high-quality solutions for optimization problems and search problems.

Genetic algorithms simulate the process of natural selection which means those species that can adapt to changes in their environment are able to survive and reproduce and go to the next generation. In simple words, they simulate “survival of the fittest” among individuals of consecutive generations for solving a problem. Each generation consists of a population of individuals and each individual represents a point in search space and a possible solution. Each individual is represented as a string of characters/integers/floats/bits. This string is analogous to the Chromosome.

The following outline summarizes how the genetic algorithm works:

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

  2. 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:

    • Scores each member of the current population by computing its fitness value. These values are called the raw fitness scores.

    • Scales the raw fitness scores to convert them into a more usable range of values. These scaled values are called expectation values.

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

    • 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.

    • 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.

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

  3. The algorithm stops when one of the stopping criteria is met.

  4. The algorithm takes modified steps for linear and integer constraints.

  5. The algorithm is further modified for nonlinear constraints.

Syntax for ga in MATLAB:

Description:

x = ga(fun,nvars) finds a local unconstrained minimum, x, to the objective function, fun. nvars is the dimension (number of design variables) of fun.

x = ga(fun,nvars,A,b) finds a local minimum x to fun, subject to the linear inequalities A*x ≤ b. ga evaluates the matrix product A*x as if x is transposed (A*x').

x = ga(fun,nvars,A,b,Aeq,beq) finds a local minimum x to fun, subject to the linear equalities Aeq*x = beq and A*x ≤ b. (Set A=[] and b=[] if no linear inequalities exist.) ga evaluates the matrix product Aeq*x as if x is transposed (Aeq*x').

x = ga(fun,nvars,A,b,Aeq,beq,lb,ub) 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.)

x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon) subjects the minimization to the constraints defined in nonlcon. The function nonlcon accepts x and returns vectors C and Ceq, representing the nonlinear inequalities and equalities respectively. ga minimizes the fun such that C(x) ≤ 0 and Ceq(x) = 0. (Set lb=[] and ub=[] if no bounds exist.)

x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options) minimizes with the default optimization parameters replaced by values in options. (Set nonlcon=[] if no nonlinear constraints exist.) Create options using optimoptions.

x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon) or x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options) requires that the variables listed in IntCon take integer values.

x = ga(problem) finds the minimum for problem, a structure described in problem.

[x,fval] = ga(___), for any previous input arguments, also returns fval, the value of the fitness function at x.

[x,fval,exitflag,output] = ga(___) also returns exitflag, an integer identifying the reason the algorithm terminated, and output, a structure that contains output from each generation and other information about the performance of the algorithm.

[x,fval,exitflag,output,population,scores] = ga(___) also returns a matrix population, whose rows are the final population, and a vector scores, the scores of the final population.

MATLAB CODE:

Stalagmite function:

function [f] = stalagmite_f(input_vector)

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

f_1_x = (sin(5.1*pi*x + 0.5))^6;
f_1_y = (sin(5.1*pi*y + 0.5))^6;
f_2_x = exp(-4*log(2)*((x-0.0667)^2/0.64));
f_2_y = exp(-4*log(2)*((y-0.0667)^2/0.64));
[f] = -((f_1_x)*(f_2_x)*(f_1_y)*(f_2_y));  % -ve sign is used to obtain the peak value.

end

Main code:

clear all
close all
clc

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

num_cases = 50; % to run ga 50 times

[xx, yy] = meshgrid(x,y); % creating 2D array

% 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_f(input_vector);
    end
end

% Study 1 - Statistical Behavior
tic
for i=1:num_cases
    [inputs, fopt(i)] = ga(@stalagmite_f,2);
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end
study1_time=toc;

figure(1)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',3,'markerfacecolor','r')
title('Unbounded Inputs')

subplot(2,1,2)
plot(fopt)
xlabel('Number of iterations')
ylabel('Function Maximum')

% Study 2 - Statistical Behaviour  with Upper & Lower Bounds
for i = 1:num_cases
    [inputs, fopti] = ga(@stalagmite_f,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(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',3,'markerfacecolor','r')
title('Bounded Inputs')

subplot(2,1,2)
plot(fopt)
xlabel('Number of iterations')
ylabel('Function Maximum')

% Study 3 - Bounded Inputs with Increased Population

options=optimoptions('ga','populationsize',620);
tic
for i = 1:num_cases
    [inputs, fopt(i)] = ga(@stalagmite_f,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(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',3,'markerfacecolor','r')
title('Bounded Inputs with Increased Population Size')

subplot(2,1,2)
plot(fopt)
xlabel('Number of iterations')
ylabel('Function maximum')

Code Explanation:

  • A Stalagmite function file is created, with the f value being -ve to determine the global maxima instead of the default global minima.
  • Inputs 'x' and 'y' have 150 values from 0 to 0.6.

  • Created a 2D array using meshgrid function.

  • Ran ga 50 times.

  • Using for loop, evaluated the stalagmite function.

  • Study 1 – Statistical Behavior

    tic toc command is used to determine the total execution time.

    Initiated a for loop.

    Plotted figure 1, which contains two subplots for unbounded inputs.

  • Study 2 – Statistical Behavior with Upper and Lower Bounds and Study 3 – with Increased Population were also conducted.

    Similarly, tic toc commands were used to determine the total execution time and figures, along with sub plots were plotted.

No major error occured during coding.

Results:

Study 1 plots:

Study 2 plots:

Study 3 plots:

Workspace:

Study time:

Conclusion:

  • The concept of genetic algorithm (ga) has been explained.
  • The syntax for ga in MATLAB has been explained.
  • Successfully executed a code in MATLAB to optimize the stalagmite function and find the global maxima of the function.
  • Successfully plotted graphs for all 3 studies.

References:

  • au.mathworks.com. (n.d.). Find minimum of function using genetic algorithm - MATLAB ga - MathWorks Australia. [online] Available at: https://au.mathworks.com/help/gads/ga.html#mw_46d5ecc6-7ae1-4400-821b-3a29c14be603 [Accessed 3 Feb. 2023].
  • au.mathworks.com. (n.d.). How the Genetic Algorithm Works - MATLAB & Simulink - MathWorks Australia. [online] Available at: https://au.mathworks.com/help/gads/how-the-genetic-algorithm-works.html [Accessed 3 Feb. 2023].
  • GeeksforGeeks. (2017). Genetic Algorithms. [online] Available at: https://www.geeksforgeeks.org/genetic-algorithms/.
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.