Challenge

Optimise Stalagmite function and find global maxima using GA

Objective

> Concept of genetic algorithm and syntax for ga in Matlab.

> Code to optimise the stalagmite function and find the global maxima of the function.

Genetic algorithm

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 search 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 who can adapt to changes in their environment are able to survive and reproduce and go to next generation. In simple words, they simulate “survival of the fittest” among individual of consecutive generation for solving a problem. Each generation consist of a population of individuals and each individual represents a point in search space and possible solution. Each individual is represented as a string of character/integer/float/bits. This string is analogous to the Chromosome.

Syntax of GA

>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,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,favl] = ga(___) - for any previous input arguments, also returns fval, the value of the fitness function at x.
Stalagmite function
`f(x,y) = f1,xf2,xf1,yf2,y`
`f1,x = [sin(5.1pix+0.5)]^6`
`f1,y = [sin(5.1piy+0.5)]^6`
`f2,x = exp[-4ln(2)*((x-0.0667)^2/0.64)]`
`f2,y = exp[-4ln(2)*((y-0.0667)^2/0.64)]`
Program
Stalagmite function
function [f] = stalagmite(ip_vector)

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

f1 = (sin(5.1*pi*x+0.5)).^6;
f2 = exp(-4*log(2)*((x-0.0667).^2)/0.64);
f3 = (sin(5.1*pi*y+0.5)).^6;
f4 = exp(-4*log(2)*((y-0.0667).^2)/0.64);
f  = -(f1*f2*f3*f4);
The code to optimize the stalagmite function.
% Optimization_using_Genetic_algorithm
close all
clc

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

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

% Function call
for i =1:length(xx)
    for j = 1:length(yy)
        ip_vector(1) = xx(i,j);
        ip_vector(2) = yy(i,j);
        f(i,j) = stalagmite(ip_vector);  
    end
end

% Plotting stalagmite func
surfc(xx,yy,-f)
shading interp
xlabel('x values')
ylabel('y values')
title('Stalagmite Function')

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

% Study 1 - Statistical behaviour
tic
iter = 50;
for i = 1:iter
    [inputs,fopt(i)] = ga(@stalagmite,2);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
 Studytime1 = toc
 figure(2)
 subplot(2,1,1)
 hold on
 surfc(x,y,-f)
 shading interp
 title('Unbounded Inputs')
 plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
 subplot(2,1,2)
 plot(-fopt)
 xlabel('Iterations')
 ylabel('Function Maximum')
    
% ------------------------------------------------------------------------------

% Study 2 - With upper and lower bounds
tic
for i = 1:iter
    [inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.8;0.8]);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
 Studytime2 = toc
 figure(3)
 subplot(2,1,1)
 hold on
 surfc(x,y,-f)
 shading interp
 title('UB & LB Inputs')
 plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
 subplot(2,1,2)
 plot(-fopt)
 xlabel('Iterations')
 ylabel('Function Maximum')

 % ------------------------------------------------------------------------------
 
 % Study 3 - Increasing GA Iterations
tic
options = optimoptions('ga');
options = optimoptions(options,'populationsize',250)
for i = 1:iter
    [inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.8;0.8],[],[],options);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
 Studytime3 = toc
 figure(4)
 subplot(2,1,1)
 hold on
 surfc(x,y,-f)
 shading interp
 title('Optimized Solution')
 plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
 subplot(2,1,2)
 plot(-fopt)
 xlabel('Iterations')
 ylabel('Function Maximum')
Program explanation
> The Stalagmite function is written as a seperate code at first
> Since GA calculates the minimum of the a function by default, a minus(-) is multiplied in the stalgamite function to get the maximum value
> Then the x and y ranges are determined in another program which are converted into a 2d array
> The stalagmite function is called and plotted.
> To get the global maxima 3 studies are made
> Unbounded Inputs
> The variable "iter" is included in order to repeat the for loop to get optimal function value (fopt).
> The syntax "x = ga(fun,nvars)" is used, as the inputs are not bounded.
> Upper and Lower bounded inputs
> The syntax "x = ga(fun,nvars,A,b,Aeq,beq,lb,ub)" is used inorder to give lower bound(lb) and upper bound(ub) inputs.
> Since there is no linear equalities A, B, Aeq, beq are set to "[]".
> Increasing GA iterations
> The syntax "x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options)" is used inorder to increase the initial population size to get the global maxima.
>As there is no linear equalities and nonlinear constraints A, B, Aeq, beq, nonlcon are set to "[]".
Results
> The stalagmite function
> Study1 - Unbounded inputs
> Study2 - Lower & upper bounded inputs
> Study3 - Increasing GA iterations
> Workspace
Conclusion
> From the three studies it is concluded that with, increased initial population size and input bounds, we can derive optimal solution of the stalagmite function.
> But meanwhile, it will consume more time as it is seen in the workspace image.
> Study3 was 4 times slower than study2.
References
https://skill-lync.com/knowledgebase/genetic-algorithm-explained
https://www.geeksforgeeks.org/genetic-algorithms/
Find minimum of function using genetic algorithm - MATLAB ga - MathWorks India
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.