Project

GENETIC ALGORITHM USING MATLAB

Aim:

To write a program to find the Global Maxima of a Stalagmite function.

Theory:

Genetic Algorithm:

Genetic Algorithm works on the theory of "Natural Selection" proposed by Charles Darwin.They are used to generate high quality optimisation on the basis of the "Natural Selection" theory.As the name suggests,the optimisation is done on the basis of genes.The three processes that are involved in Genetic Algorithm are,

  1. Selection.
  2. Cross Over.
  3. Mutation.

In the process of selection,a certain number of individuals are selected from thr population.A fitness function is used to check the fitness of the individuals.The fit individuals are then crossed over amongst them.This gives new offsprings and are left over for mutation.The process of selection,cross over and mutation repeats itself unless the best results are obtained.

Syntax:

The syntax of Genetic algorithm used are as follows:

  • x = ga(func,nvars.)

where,

func is the Function name,

nvars is the Number of Variables.

  • x = ga(func,nvars,A,b,Aeq,beq,l.bound,U.bound)

where,

func is the Function name,

nvars is the Number of Variables

A is linear inequality constraints as real matrix,

b is linear inequality constraints as real vector,

Aeq is linear equality constraints as real matrix,

beq is linear equality constraints as real vector.

l.bound is the Lower boundary,

U.bound is the Upper boundary.

  • x = ga(func.,no. of variables,A,b,Aeq,beq,l.bound,U.bound,nonlcon,options)

where,

func is the Function name,

nvars is the Number of Variables

A is linear inequality constraints as real matrix,

b is linear inequality constraints as real vector,

Aeq is linear equality constraints as real matrix,

beq is linear equality constraints as real vector.

l.bound is the Lower boundary,

U.bound is the Upper boundary,

noncoln is the Non linear constraints,

options is creating optimisation options using "optimoptions".

Description of Program:

  1. The inputs are given and a 2d array is created using the "meshgrid" command.
  2. A Stalagmite function is created with the name stag.
  3. Two "for" loops are used of length as same as the length of the x and y respectively.
  4. The inputs to the stalagmite function are assigned through these loops.
  5. The output of the stalagmite function is saved and the and are plotted.

Study 1-Statistical Behaviour:

  1. A clock is assigned to clock the time taken for optimisation.
  2. A "for" loop is used to run the optimisation only for 20 times.
  3. The outputs from "ga" function are stored as x_o1,y_o1 and f_o1.
  4. The syntax of ga used is "x=ga(func,no.of variables)".
  5. The F maximum values are obtained by inverting the plot and storing the negative of optimised values,as "ga" function gives the global minima by its virtue.
  6. The main plot has the stalagmites and the optimised F-maximum values.
  7. A subplot is made inside the main plot with F-maximum and No. of Iterations.

Study 2-Statistical Behaviour with Upper and Lower bounds:

  1. A clock is assigned to clock the time taken for optimisation.
  2. A "for" loop is used to run the optimisation only for 20 times.
  3. The outputs from "ga" function are stored as x_o2,y_o2 and f_o2.
  4. The syntax of ga used is "x = ga(func,no. of variables,A,B,aeq,beq,l.bound,U.bound)".
  5. The F maximum values are obtained by inverting the plot and storing the negative of optimised values,as "ga" function gives the global minima by its virtue.
  6. The main plot has the stalagmites and the optimised F-maximum values.
  7. A subplot is made inside the main plot with F-maximum and No. of Iterations.
  8. It is observed that the results obtained are more stable than last time.

Study 3-Statistical Behaviour by increasing the number of Population:

  1. A clock is assigned to clock the time taken for optimisation.
  2. x = ga(func.,no. of variables,A,B,aeq,beq,l.bound,U.bound,nonlcon,options) is the syntax used.
  3. Optimised options are made to increse the number of population to get the global maxima.
  4. A "for" loop is used to run the optimisation only for 20 times.
  5. The outputs from "ga" function are stored as x_o3,y_o3 and f_o3.
  6. The F maximum values are obtained by inverting the plot and storing the negative of optimised values,as "ga" function gives the global minima by its virtue.
  7. The main plot has the stalagmites and the optimised F-maximum values.
  8. A subplot is made inside the main plot with F-maximum and No. of Iterations.
  9. This gives the Global maxima of the stalagmite function.

Program:

%GA:
clear all 
close all
clc

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

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

for j=1:length(x)
    for k = 1:length(y)
        inputs(1) = xx(j,k);
        inputs(2) = yy(j,k);
        f(j,k) = stag(inputs);
    end
end

%Study 1:

tic
 for i = 1:n_c
     [inputs, f_o1(i)] = ga(@stag,2);
     x_o1(i) = inputs(1);
     y_o1(i) = inputs(2);   
 end
 
study_1_time = toc
figure(1)
subplot(2,1,1)
surfc(xx,yy,-f);
shading interp
xlabel('X');
ylabel('Y');
hold on
plot3(x_o1,y_o1,-f_o1,'marker','.');
subplot(2,1,2)
plot(f_o1);
xlabel('No. of Iteration');
ylabel('F maximum');

%Study 2:

 tic
 for i = 1:n_c
     [inputs, f_o2(i)] = ga(@stag,2,[],[],[],[],[0,0],[0.6,0.6]);
     x_o2(i) = inputs(1);
     y_o2(i) = inputs(2);   
 end
 
study_2_time = toc
figure(2)
subplot(2,1,1)
surfc(xx,yy,-f);
shading interp
xlabel('X');
ylabel('Y');
hold on
plot3(x_o2,y_o2,-f_o2,'marker','.');
subplot(2,1,2)
plot(f_o2);
xlabel('No. of Iteration');
ylabel('F maximum');

%Study 3:

options = optimoptions('ga')
options = optimoptions('ga','populationsize',300)

tic
for i = 1:n_c
     [inputs, f_o3(i)] = ga(@stag,2,[],[],[],[],[0,0],[0.6,0.6],[],[],options);
     x_o3(i) = inputs(1);
     y_o3(i) = inputs(2);    
end
 
study_3_time = toc
figure(3)
subplot(2,1,1)
surfc(xx,yy,-f);
shading interp
xlabel('X');
ylabel('Y');
hold on
plot3(x_o3,y_o3,-f_o3,'marker','.');
subplot(2,1,2)
plot(f_o3);
xlabel('No. of Iteration');
ylabel('Function max');

Function:

function [f] = stag(inputs)
x = inputs(1);
y = inputs(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

Output:

Study 1:

Study 2:

Study 3:

Errors and Corrections:

  1. An error in the function was corrected by replacing "log" with "ln".

Results and Conclusion:

The Global Maxima of a stalagmite function was found to be 1 at the (0.06683,0.06683) co-ordinates and the outputs were plotted for the all the three studies.The graphs with F maximum and number of iterations were also plotted.It was observed that,when the number of iterations was increased,the time taken for optimisation also increased.

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.