Project

Genetic Algorithm using MATLAB.

AIM - To analyse the Stalagmite function using Genetic Algorithm using MATLAB.

Theory -

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.

The process of natural selection starts with the selection of fittest individuals from a population. They produce offspring which inherit the characteristics of the parents and will be added to the next generation. If parents have better fitness, their offspring will be better than parents and have a better chance of surviving. This process keeps on iterating and at the end, a generation with the fittest individuals will be found. This notion can be applied to a search problem. We consider a set of solutions for a problem and select the set of best ones out of them.

Five phases are considered in a genetic algorithm.

  1. Initial population
  2. Fitness function
  3. Selection
  4. Crossover
  5. Mutation
  1. Initial Population

The process begins with a set of individuals which is called a Population. Each individual is a solution to the problem you want to solve.

  1. Fitness Function

The fitness function determines how fit an individual is (the ability of an individual to compete with other individuals). It gives a fitness score to each individual. The probability that an individual will be selected for reproduction is based on its fitness score.

3. Selection

The idea of selection phase is to select the fittest individuals and let them pass their genes to the next generation.

  1. Crossover

Crossover is the most significant phase in a genetic algorithm. For each pair of parents to be mated, a crossover point is chosen at random from within the genes.

  1. Mutation

In certain new offspring formed, some of their genes can be subjected to a mutation with a low random probability. This implies that some of the bits in the bit string can be flipped.

  1. Termination

The algorithm terminates if the population has converged (does not produce offspring which are significantly different from the previous generation). Then it is said that the genetic algorithm has provided a set of solutions to our problem.

Syntax

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

2) 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').

3) 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').

4) 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.)

5) x=ga(fun,nvars,A,b,Aeq,beq,lb,ub,noncolon):

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

6) x=ga(fun,nvars,A,b,Aeq,beq,lb,ub,noncolon,options):

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

7) x=ga(fun,nvars,A,b,Aeq,beq,lb,ub,noncolon,IntCon):

Requires that the variables listed in IntCon take integer values.

8) x = ga(problem):

Finds the minimum for problem, where problem is a structure.

9) [x,fval] = ga(___):

For any previous input arguments, also returns fval, the value of the fitness function at x.

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

11) [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 -

  • It is a mathematical model that helps to generate local minima and maxima.
  • It is a three-dimensional function and the way the function is designed is that it has a lot of local maxima or minima and one global maxima or minima.
  • The name stalagmite is actually a type of rock formation that rises from the floor of a cave due to the accumulation of material deposited on the floor from ceiling drippings.

Function formula -

`f_(1x) = [sin(5.1*pi*x+0.5)]^6`

`f_(1y) = [sin(5.1*pi*y+0.5)]^6`

`f_(2x) = exp(-4*log(2).*(((x-0.0667).^2)/0.64)`

`f_(2x) = exp(-4*log(2).*(((y-0.0667).^2)/0.64)`

`Function = F = (f_(1x)*f_(1y)*f_(2x)*f_(2y))`

Procedure -

Inputs -

Function Code -

function [F] = stalagmite(input_vector)

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

f1x = [sin((5.1*pi*x)+0.5)]^6
f1y = [sin((5.1*pi*y)+0.5)]^6
f2x = exp(-4*log(2).*(((x-0.0667).^2)/0.64))
f2y = exp(-4*log(2).*(((y-0.0667).^2)/0.64))

F = -[f1x*f1y*f2x*f2y]

end

Main Program

clear all
close all
clc

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

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

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_1 statistical behaviour
tic
for i = 1:num_cases
    [inputs1,Fopt1(i)] = ga(@stalagmite,2);
    xopt1(i) = inputs1(1);
    yopt1(i) = inputs1(2);
end

study_time_1 = toc;

figure(1)
subplot(2,1,1);
hold on
surfc(xx,yy,-F);
shading interp;
xlabel('X value');
ylabel('Y value');
plot3(xopt1,yopt1,-Fopt1,'marker','o','markersize',5,'markerfacecolor','r');
title('unbounded inputs');
subplot(2,1,2);
plot(-Fopt1);
xlabel('Iterations');
ylabel('Function maximum');


%study 2 Statistical behaviour with upper and lower bonds
tic
for i = 1:num_cases
    [inputs2,Fopt2(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
    xopt2(i) = inputs2(1);
    yopt2(i) = inputs2(2);
end
study_time_2 = toc;

figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,-F)
shading interp;
plot3(xopt2,yopt2,-Fopt2,'marker','o','markersize',5,'markerfacecolor','r');
xlabel('X value')
ylabel('Y value')
title('Statistical behaviour wih upper and lower bonds')
subplot(2,1,2)
plot(-Fopt2)
xlabel('Iterations')
ylabel('Function maximum')

%Study 3 Statistical behaviour with increase population and with upper and lower bonds
tic
options = optimoptions('ga');
options = optimoptions(options,'Populationsize',250);

for  i = 1:num_cases
    [inputs3,Fopt3(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
    xopt3(i)=inputs3(1);
    yopt3(i)=inputs3(2);
end
Study_time_3 = toc;

figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,-F)
shading interp;
plot3(xopt3,yopt3,-Fopt3,'marker','o','markersize',5,'markerfacecolor','r')
xlabel('X value')
ylabel('Y value')
title( 'Statistical behaviour with increased population and with upper amd lower bonds')
subplot(2,1,2)
plot(-Fopt3)
xlabel('Iterations')
ylabel('Function maximum')




Explanation -

1. Solving the Genetic algorithm for stalagmite function gives us a minimum value of the function. To predict the maximum value of the function, we have to change the sign of function, so that it will give us the value for the reverse case of minima, i.e. maxima.

2. As 'linspace()' creates the series of nos, x & y will be the column vector. As input to the stalagmite function, we need to convert the column vector to a row vector. The first step of this conversion is to create the 'meshgrid(x,y)'. Next, we use 'for' loop for conversion.

3. The next step is to obtain the results for 3 cases of the genetic algorithm in Matlab. In the first case, maximum value of the function is found out with no restrictions, no bounds, and no limits. Genetic algorithm is free to choose the population then reproduce and mutate.

4. To get the same value each time we run the program, we need to restrict the program within specific bounds namely upper bound and lower bound. The whole procedure for programming this case the same as that of the previous one, exception being for genetic algorithm.

5. We now need to increase GA iterations. We can raise it to any value, we chose it as 250. In this case, syntax for GA is different than the previous case. GA has to give this population size via an option.

Results-

Outputs -

Study 1 Outputs-

Study 2 Outputs -

Study 3 Outputs -

Connclusion -

1. The optimum value i.e., xopt = 0.06684 and yopt = 0.06683.

2. Therefore the Stalagmite Function is optimised in Study 3 by increasing GA 50 (default) to 250 where on only one peak is visible which is a global Maxima.

3. Previous 2 studies were failed due to more than 1 peak were visible in the plots.

4. This project gave the immense knowledge about 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.