Challenge

Week 4.1 - Genetic Algorithm

Genetic Algorithm

Aim:

  • To optimize the stalagmite function using genetic algorithm techniques using Matlab.
THEORY:
Genetic Algorithm:
Genetic Algorithm (GA) is a branch of the field of study called evolutionary computation. It is based on the evolution theory by Darwin. GA is used to optimize a given function to find its maximum or minimum value. The basic components of a GA are
  • Fitness function: It is the function that GA is trying to optimize.
  • A population of chromosomes: It is the numerical values that represent all the solutions to the function. The selection operator chooses some of the chromosomes (with replacement) for reproduction based on probability distribution. the fitter the chromosome more likely it is to be selected.
  • Cross over: This operator swaps in random a subsequence of two of the chosen chromosomes to create two offspring. Usually, each chromosome is converted into binary code and then cross-over is done from one parent chromosome into another.
  • Mutation: The mutation operator randomly flips individual bits in the new chromosomes.

The GA syntax in Matlab is given by:
x=ga(fun,nvars,A,B.Aeq.Beq,LB,UB.nonlcon,intcon,options)
Where,
fun = objective function
nvars = number of variables
A and B = subject to linear inequalities of the form A*x <=B
Aeq and Beq = subject to linear equalities of the form A*x= B
LB = lower bound
UB = upper bound
nonlcon = non linear inequalities
intcon = integer constrained
options = helps to add contraint tolerance by increasing the population size
Some advantages of genetic algorithms in this method are the following:
  • They usually perform better than traditional feature selection techniques.
  • Genetic algorithms can manage data sets with many features.
  • They don't need specific knowledge about the problem under study.
  • These algorithms can be easily parallelized in computer clusters.

Working of Genetic Algorithm:
The following outline summarizes how the genetic algorithm works:
  1. The algorithm begins by creating a random initial population. The algorithm then creates a sequence of new populations.
  2. At each step, the algorithm uses the individuals in the current generation to create the next population.
To create the new population, the algorithm performs the following steps:
  • Scores each member of the current population by computing its fitness value. These values are called the raw fitness scores.
  • Scales the raw fitness scores to convert them into a more usable range of values. These scaled values are called expectation values.
  • Selects members, called parents, based on their expectations.
  • Some of the individuals in the current population that have lower fitness are chosen as elite. These elite individuals are passed on to the next population.
  • Produces children from the parents. Children are produced either by making random changes to a single parent mutation or by combining the vector entries of a pair of parents crossover.
  • Replaces the current population with the children to form the next generation. The algorithm stops when one of the stopping criteria is met.

CODE:

Function code:

function [f] = stalagmite(input_vector)
x = input_vector(1);
y = input_vector(2);
f1_x = (sin((5.1*pi*x)+0.5)).^6;
f1_y = (sin((5.1*pi*y)+0.5)).^6;
f2_x = exp(((-4*log(2))*(x-0.0667).^2)/0.64);
f2_y = exp(((-4*log(2))*(y-0.0667).^2)/0.64);
[f] = -(f1_x.*f1_y.*f2_x.*f2_y);
end

Main code:

% A program to genetic Genetic algorithm
clear all
close all
clc


% Defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 80;
% Creating a 2D mesh
[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

tic
for k = 1:num_cases;
    x_1(k)=input_vector(1);
    y_1(k)=input_vector(2);
  
end
study1_time = toc

tic
for s = 1:num_cases;
    [input,fval(s)] = ga(@stalagmite,2,[],[],[],[],[0,0],[.6,.6]);
    x_2(s) = input(1);
    y_2(s) = input(2);
end
Study2_time = toc

% 3rd case study increasing ga iterations
options = optimoptions('ga');
options = optimoptions(options,'populationsize',150);
tic
for m = 1:num_cases;
    [input,fval(m)] = ga(@stalagmite,2,[],[],[],[],[0,0],[.6,.6],[],[],options);
    x_3(m) = input(1);
    y_3(m) = input(2);
end
study3_time = toc

figure(1)
hold on
grid on
surfc(xx,yy,-f)
shading interp
title('stalagmite plot') 
xlabel('x')
ylabel('y')

figure(2)
subplot(2,1,1)
surfc(xx,yy,-f)
shading interp
hold on
plot3(x_1,y_1,-fval,'marker','o','markersize',5,'markerfacecolor','g')
title('unbounded inputs')
subplot(2,1,2)
plot(-fval)
xlabel('iterations')
ylabel('function minimum')

% 2nd case study statistical behavior
figure(3)
subplot(2,1,1)
surfc(xx,yy,-f)
shading interp
hold on
plot3(x_2,y_2,-fval,'marker','o','markersize',5,'markerfacecolor','g')
title('unbounded inputs')
subplot(2,1,2)
plot(-fval) 
xlabel('iterations')
ylabel('function minimum')

% 3rd case study increasing ga iterations
figure(4)
subplot(2,1,1)
surfc(xx,yy,-f)
shading interp
hold on
plot3(x_3,y_3,-fval,'marker','o','markersize',5,'markerfacecolor','g') 
title('unbounded inputs')
subplot(2,1,2)
plot(-fval)
xlabel('iterations')
ylabel('function minimum')

Explanation of the code:

  • The code starts with defining x and y as inputs using linspace.
  • A function known as Stalagmite_func is defined as shown in the 2nd stage of codes.
  • 2 for loops are incorporated in the main code so that all the combinations of x and y values are taken in the stalagmite_func function
  • In the 1st study, the GA function runs for 80 iterations. Plots of the stalagmites and maximum values generated in each iteration are shown.
  • In the 2nd study, the GA function is incorporated with lower and upper bounds and runs for 80 iterations. Plots of stalagmites and maximum values generated in each iteration are shown.
  • In the 3rd study, the GA function is incorporated with lower and upper bounds along with the population size of 200. Plots of the stalagmites and maximum values generated in each iteration are shown.

RESULT:

figure (1)
stalagmite basic
as we can see that there are more up-growing peaks, which basically are my stalagmite structure. However, the function tends to give the negative value which gives the local minima, so to make the function positive for getting the local and global maxima(upgrowing peaks). The highest peak is called global maxima and others are local maxima.
figure(2)
case study (1)
here, in the case study (1), the solutions are not bounded so the GA will calculate all the possible maxima in 'x' and 'y coordinates, and the time taken to calculate the possible solution is 4.4339
study (2)
As the solution is dispersed in space we are limiting it to the global maxima simply we are converging the are function for optimum values, iterating the for loop 80 times means we are evolving our solution to get b results for every generation to generation.
Figure(3)
As we can see that the time taken in the result two studies is 6.1952 for the reading of global maxima. Here, are using the genetic algorithm tool to bound the lower and upper values, because when we bound the upper and lower value then the ga tool will search in that range only. here, the lower and upper bound are taken as (0 and 0.6) for the x and y-direction.
Figure(4)
The resulted line from high peak to low peak is the critical point for my global maxima and the time taken by it is 36.4559 which is approx 3 times greater than the case study one. As the population size is large enough to carry out 80 iterations at the same time, however, we got our optimum maximized solution.

CONCLUSION:
By using basic commands in MatLab we got the stalagmite plot. The statistical behavior of the stalagmite plot is generated, and finally, we optimized our plot and go to its critical point to reach the global maxima by using the GA tool, we also came to know that increasing the population size and iterating it resulted in optimization of the stalagmite function generated to GA
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.