Challenge

Week 4 - Genetic Algorithm

AIM: -Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.

  • Plot graphs for all 3 studies and for F maximum vs no. of iterations
  • Clearly explain the concept of genetic algorithm in your own words and also explain the syntax for gain MATLAB

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.

Find global minima for highly nonlinear problems

A genetic algorithm (GA) is a method for solving both constrained and unconstrained optimization problems based on a natural selection process that mimics biological evolution. 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.

The genetic algorithm to solve problems that are not well suited for standard optimization algorithms, including problems in which the objective function is discontinuous, non-differentiable, stochastic, or highly nonlinear.

What is classical algorithm and genetic algorithm

Classical Algorithm: -

Generates a single point at each iteration. The sequence of points approaches an optimal solution. Selects the next point in the sequence by a deterministic computation.

Genetic Algorithm: -

Generates a population of points at each iteration. The best point in the population approaches an optimal solution. Selects the next population by computation which uses random number generators.

Stalagmite Function: -

Five phases are considered in a genetic algorithm.

  • Initial population.
  • Fitness function.

MAIN MATLAB PROGRAMMING

clear all

close all

clc

%Defining our search space

x = linspace(0,0.6,200);

y = linspace(0,0.6,200);

iterations = 75;

% creating = 2D mesh

% converting one dimensional array into two dimensional array

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

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

end

end

% 1st iteration = statistical behaviour

% timmer command

tic

for i=1:iterations

% this is a genetic alorithm or it is builtup function of matlab

[inputs , fopt(i)] = ga(@stalagmite,2);

xopt(i) = inputs(1);

yopt(i) = inputs(2);

end

iteration_1_time = toc;

% surface plotting

figure (1)

% It use to multiple plot in same plot window

subplot(2,1,1)

hold on

surfc(xx,yy,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')

%2nd iteration = statistical behavior with upper and lower limits

tic

for i=1:iterations

[inputs , fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);

xopt(i) = inputs(1);

yopt(i) = inputs(2);

end

iteration_2_time = toc

%plotting

figure (2)

subplot(2,1,1)

hold on

surfc(xx,yy,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')

%3rd iteration - increasing GA the size of population

options = optimoptions('ga');

options = optimoptions(options,'populationsize',200)

tic

for i=1:iterations

[inputs , fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);

xopt(i) = inputs(1);

yopt(i) = inputs(2);

end

iteration_3_time = toc

%plotting

figure (3)

subplot(2,1,1)

hold on

surfc(xx,yy,f)

shading interp

plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')

title('Bounded inputs with modified population size')

subplot(2,1,2)

plot(fopt)

xlabel('Iterations')

ylabel('Function maximum')

STALAGMITE FUNCTION PROGRAMME

function [F] = stalagmite(input_vector)

x = input_vector(1);

y = input_vector(2);

f1=(sin(5.1*pi*x + 0.5)).^6;

f2=(sin(5.1*pi*y + 0.5)).^6;

f3= exp((-4*log(2)*(x-0.0667).^2)/(0.64));

f4= exp((-4*log(2)*(y-0.0667).^2)/(0.64));

F = -f1*f2*f3*f4;

end

PROGRAM EXPLANATION

  • To provide the input data and to search space required to execute the program is defined which varies from 0 to 0.6 with 200 divisions in between.
  • meshgrid command use for, row vectors x and y are converted into matrices xx and yy.
  • With the help of loop, input vectors required for stalagmite function are defined using the matrices xx and yy.

Syntax of the genetic algorithm in MATLAB:

Syntax - 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 - local minimum point
  • fun - objective function
  • nvars - Number of variables used
  • A & b - they are used if the inequality "A*x ≤ b" exists in the function
  • Aeq & beq - they are used if the inequality "Aeq*x = beq" exists in the function
  • lb - It is lower bound limit of the function
  • ub - It is upper bound limit of the function
  • nonlcon - The function nonlcon accepts x and returns vectors C and Ceq, representing the nonlinear inequalities and equalities respectively.
  • IntCon - Takes integer values
  • options - This argument allows add extra parameters like increasing population size.The options argument is created by the command "optimoptions."

There are three study iteration plot in a graph, given on below,

1ST ITERATION

In first iteration of graph we find that the behaviour of algorithm not define the limit.

2ND ITERATION

The lower and upper bounds are defined so as to know the boundaries within which the global maxima lie.

3RD ITERATION

The population size is increased so as to converge to the global maxima.

ITERATION TIME

Workspace out put

Error finding at the time of creating a programming

To identify the error and to solve the programming.

CONCLUSION

To study the all iteration a successfully find the best result of genetic algorithm.

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.