Challenge

Week 4 - Genetic Algorithm

GENETIC ALGORITHM USING MATLAB

AIM - To calculate the global maxima of a stalagmite function.

OBJECTIVE -

  • Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
  • Clearly explain the concept of genetic algorithm in your own words and also explain the syntax for GA in MATLAB.
  • Plot graphs for all 3 studies and for F maximum vs no. of iterations.

RELATED THEORY :-

Genetic Algorithms (GAs) come under the category of evolutionary algorithms with working principle based on the mechanics of natural genetics. The basic objective in natural genetics is the retention of the fit genes & discard of the redundant. New generations created by manipulating the genetic code using the tools such as selection, crossover & mutation. GA also works in similar manner with the objective to search appropriate solution for the problems involving either minimization or maximization of the objective function. GA use similar tools as selection, crossover & mutation applied to a population of binary strings generated randomly. In every generation, a new set of artificial species or strings is created using bits and pieces of the fittest among old; an occasional new part may be tried for good measure. Genetic algorithms are proven to yield robust search in complex spaces.

The stalagmite Function used in the program is written below

f(x,y)=f1x⋅f2x⋅f1y⋅f2yf(x,y)=f1x⋅f2x⋅f1y⋅f2y

f1x=(sin(5.1πx+0.5))6f1x=(sin(5.1πx+0.5))6

f1y=(sin(5.1πy+0.5))6f1y=(sin(5.1πy+0.5))6

f2x=exp(−4ln2(x−0.0667)20.64)

SYNTAX OF GENETIC ALGORITHM

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 rangelb ≤ 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=[] andub=[] 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 (recommended) or gaoptimset.

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, where problem is a structure.

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

STALAGMITE FUNCTION CODE

function [f]=stalagmite(input_vector)

x = input_vector(1);
y = input_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);

end

GENETIC ALGORITHM CODE

clear all
close all
clc


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



% converting 1D array into 2D array

[xx yy] = meshgrid(x,y);
rep = 50;

tic

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

study_time = toc

surfc(xx,yy,-f)
shading interp
xlabel('x value')
ylabel('y value')
title('stalagmite function')

% STUDY 1- Unbounded Inputs

tic

for i = 1:rep
    
    [inputs,fopt(i)] = ga(@stalagmite,2);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
    
end

study_time1 = toc

figure(1)
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 2- Bounded Inputs - restricted with upper and lower bounds

tic

for i = 1:rep
    
    [inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
    
end

study_time2 = toc

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('optimum solution')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maximum')

% STUDY 3 -  Bounded inputs with options - increasing no of iterations by giving population size

options = optimoptions(@ga)
options = optimoptions(options,'populationsize',170);


tic
for i=1:rep
    
    [inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
    
end


study_time3 = toc


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('optimum solution')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maximum')

global_maxima = -fopt

EXPLANATION

  1. The linspace set the range of data between the two number.
  2. The meshgrid command is used to create an array of m*n matrix
  3. The for command is used for loop operation. The input vector stored the value of matrix XX and YY. The output stored in the F matrix after evaluated from the stalagmite operation
  4. surfc (x, y, z) creates a three-dimensional surface plot with a contour plot underneath. A surface plot is a three-dimensional surface that has solid edge colours and solid face colours. The function plots the values in matrix Zas heights above a grid in the x-y plane defined by X and Y. The colour of the surface varies according to the heights specified by Z.
  5. In study1- Genetic Algorithm for random sampling
  6. In study 2 - statistical behaviour with upper and lower bounds
  7. In study 3- increasing the GA iterations.

ERRORS – Undefined function “stalagmite”

Error was rectified by correctly naming the stalagmite function file.

OUTPUT –

CONCLUSION- Thus a code was written in MATLAB to optimise the stalagmite function and find the global maxima of the function.

References

https://www.google.com/search?q=genetic+algorithm&oq=genetic+algorithm&aqs=chrome..69i57.6734j0j7&sourceid=chrome&ie=UTF-8

https://www.mathworks.com/discovery/genetic-algorithm.html

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.