Challenge

Week 4.1 - Genetic Algorithm

AIM: To calculate the global maxima of a stalagmite function in MATLAB.

OBJECTIVE:

  • To write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
  • To plot graphs for all 3 studies and for F maximum vs no. of iterations.

THEORY: Genetic Algorithm (GA) is a search-based optimization technique based on the principles of Genetic and natural selection. it is frequently used to find optimal or near-optimal solutions to difficult problems which otherwise would take a lifetime to solve. it is frequently to solve optimization problems, in reserach, and in machine learning.

Nature has always been a great source of inspiration to all mankind. Genetic Algorithm (GAs) are search based algorithm based on the concepts of natural selection and genetic. GAs are a subset of a much larger branch of computation known as Evolutionary computation.

A denetic 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. GAs were developed by john Holland and his student and colleagues at the university of michigan, most notably david E. Goldberg and has since been tried on various optimization problems with a high degree of success.

In GAs, we have a pool or a population of possible solutions to the given problem.These solutions then undergo recombination and mutation( like in natural genetics), producing new children, and the process is repeated over various generation. Each individual (or candidate solution) is assigned a fitness value(based on its objective function value) and the fitter individual are given higher chance to mate and yield more''fitter'' individuals. This is in line with the Darwinian Theory of ''survival of the fittest''. In this way we keep ''evolving'' better individual or solution over generation, till we reach a stopping criterion.

Genetic Algorithm are sufficiently randomized in nature, but they perform much better than random local search (in which we just try various random solution, keeping track of the best so far), as they exploit historical information as well.

Advantages of GAs: GAs have various advantages which have made them immensely popular.These include-

  • Does not require any derivative information(which may not be available for many real-world problems).
  • Is faster and more efficient as compared to the traditional methods.
  • Has very good parallel capabilities.
  • optimizes both continous and discreate functions and also multi-objective problems.
  • Provides a list of ''good''solutions and not just a single solution.
  • Always get an answer to the problem, which gets better over the time.
  • useful when the search space is very large and there are a large number of parameter involved.

Limitations of GAs: Like any technique, GAs also suffer from a few limitations.

These include-

  • GAs are not suited for all problems, especially problem which are simple and for which derivative information is available.
  • fitness value is calculated repeatedly which might be computationally expensive for some problems.
  • Being stochastic, there are no guarantee on the optimality or the quality of the solution.
  • If not implemented properly, the GA may not converge to the optimal solution.

The genetic algorithm uses three main types of rules at each step to create the next generation fron the current population:

  • Selection rulesselect the individuals, called parants, that contributes to the population at the next generation.
  • crossover rulescombine two parents to form children for the next generation.
  • mutation rulesapply random change to individual parents to form children.

The genetic algorithm differs from a classical, derivative-based, optimization algorithm in two main ways, as summarized in the following table.

Classical Algorithm and Genetic Algorithm

  • Generates a single point at each iterations. The sequence of points approaches an optimal solution.
  • Generates a population of points at each iteration. The best point in the population approaches an optimal solution.
  • selects the next point in the sequence by a deterministic computation.
  • selects the next population by computation which uses random number generators.

GOVERNING EQUATIONS:

=`f(x,y)=f1,x.f2,x.f1,y.f2,y`

=`f1,x=(sin(5.1pix+0.5))^6`

=`f1,y=(sin(5.1piy+0.5))^6`

=`f2,y=exp(-4ln(2)((x-0.0667)^2/0.64))`

=`f2,y=exp(-4ln(2)((y-0.0667)^2/0.64))`

SOLUTION STEPS:

1.First two input variables (x and y)were created which were one dimensional arrays of 150 equally spaced values between 0 and 0.6 using linspace command and then two dimensional matrix of these variables were created using meshgrid command.

2. Another function named stalagmite was created separately which store the two input variables and contains equations for output and was called in main program.

3.The results from stalagmite function was stored in another variable (f) and plotted as surface usinf surface command in MATLAB.

4.''ga''function was used to calculate the maximum value of stalagmite function, but since ''ga''command gives minimum value of function provided so some changes had to be made to get desired results, so a '-' sign was used before (f) to get maximum value for ''ga'' command.

5.Then our code was made to iterate 50 times using 'for loop' to stydey the statistical behaviour or genetic algorithm with unbounded input, bounded input, bounded input with increasing population and all these studies are plotted individually using sublot,plot3 and sufc command.

6.The results obtained from all three studies differ from each other as in first study of unbounded input,during each iteration the maximum value change from 0 to 1 so in second study of bounded input''ga'' command is changed and upper and lower bounds of 0 and 1 are set respectively. In this case, the results obtained are much sophisticated as compared to first one.

7.Hence, in third study of bounded inputs with increasing population size which was 50 by default , it was increased to 300. The output results obtained were confined in very small range of 0.99 and it was approximately accurate result. if the population size was increased further, more accurate result will be obtained.

Description of syntax of ''ga'':

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`le`b.gaevaluates 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`le`b.(ser 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,1b,ub)defines a set of lower and upper bounds on the design variables x, so that a solution is found in the range 1b`le`x`le`ub.(ser Aeq=[]and beq=[] if no linear equalities exist).

x = ga(fun,nvars,A,b,Aeq,beq,1b,ub,nonlcon) subjects the minimization to the constraints defined in nonlcon. The function nonlcon accepts x and returns vector c and ceq, representing the nonlinear inequalities and equalities respectively.ga minimizes the fun such that c(x)`le`0 and ceq(x) = 0.(set 1b=[] and ub =[] if no bounds exist).

x = ga(fun,nvars,A,b,Aeq,beq 1b,ub,nonlcon,options) minimizes with the default optimization parameter replaced by values in options.(set nonlcon=[]if no nonlinear costraints exist).create option using optimoptions.

x = ga(fun,nvars,A,b,[],

[],1b,ub,nonlcon,Intcon)or x = ga(fun,nvars,A,b,[],[],1b,ub,nonlcon,Intcon,options) requires that the variables listed in Intcon take integer values.

MATLAB CODE:

code containing stalagmite function-

function [f] = stalagmite(input_vector)
x = input_vector(1);
y = input_vector(2);
f_1_x = (sin(5.1*pi*x + 0.5))^6;
f_1_y = (sin(5.1*pi*y + 0.5))^6;
f_2_x = exp(-4*log(2)*((x-0.0667)^2/0.64));
f_2_y = exp(-4*log(2)*((x-0.0667)^2/0.64));
[f] = -((f_1_x)*(f_2_x)*(f_1_y)*(f_2_y)); % -sign is used to return max values from ga command
end

Main code containing first study-
% A program to calculate the maxima of a function with unbounded inputs
clear all
close all
clc
% inputs for search space
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_cases=50; % number of iterations
% creating a 2_dimensional 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
% study1-statistical behaviour with unbounded input values
for a=1:num_cases
[inputs,fopt_1(a)]=ga(@stalagmite,2);
xopt(a)=inputs(1);
yopt(a)=inputs(2);
end
study1_time=toc
% plotting our data
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
colorbar
plot3(xopt,yopt,-fopt_1,'marker','o','markerfacecolor','r','markersize',5)
title('Unbounded inputs')
subplot(2,1,2)
plot(-fopt_1)
xlabel('Iterations'
ylabel('Function maximum')

Main code containing second study-
% A program to calculate the maxima of a function with bounded inputs
clear all
close all
clc
% inputs for search space
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_cases=50;
% creating a 2_dimensional 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
% Study2-Statistical behaviour with upper and lower bounded input values
for b=1:num_cases
[inputs,fopt_2(b)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1,1]);
xopt(b)=inputs(1);
yopt(b)=inputs(2);
end
study2_time = toc
% plotting our data
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
colorbar
plot3(xopt,yopt,-fopt_2,'marker','o','markerfacecolor','markersize',5)
title('Bounded inputs')
subplot(2,1,2)
plot(-fopt_2)
xlabel('Iterations')
ylabel('Function maximum')

Main code containing third study-
% A program to calculate the maxima of a function with bounded inputs and
% increasing iterations
clear all
close all
clc
% inputs for search space
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_cases=50;
% creating a 2_dimensional 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
% Study3-Statistical behaviour with upper and lower bounded input values
% and increasing genetic algorithm iterations
option_1 = optimoptions('ga');
option_1 = optimoptions(option_1,'Populationsize',300);
tic
for c=1:num_cases
[inputs,fopt_3(c)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],option_1);
xopt(c)=inputs(1);
yopt(c)=inputs(2);
end
study3_time = toc
% plotting our data
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
colorbar
plot3(xopt,yopt,-fopt_3,'marker','o','markerfacecolor','r','markersize',5)
title('Bounded inputs with increasing populationsize')
subplot(2,1,2)
plot(-fopt_3)
xlabel('Iterations')
ylabel('Function maximum')

RESULTS:

study 1-

study 2-

study 3-

Note:- Genetic algorithm with different results at each run so use mg('default')to set the random number generator to a known value.

ERRORS AND REMEDIES: There were no errors encountered except one mistake that i'd saved the main program under same name as ga and it results an error while solving, so to rectify if the main program was renamed under different file name.

CONCLUSION: The genetic algorithm repeatedly modifies a population of individual solutions. At each step, the genetic algorithm selects individual at random from the current population to be parent and uses them to produce the children for the next genration. over successive generations, the population ''evolves''towards an optimal solution. But if the population size is taken too large, the processing time taken by MATLAB also increases.

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.