WEEK 4- GA
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 Genetics 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 used to solve optimization problems, in research, and in machine learning.
Nature has always been a great source of inspiration to all mankind. Genetic Algorithms (GAs) are search based algorithms based on the concepts of natural selection and genetics. GAs are a subset of a much larger branch of computation known as Evolutionary Computation.
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. GAs were developed by John Holland and his students 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 generations. Each individual (or candidate solution) is assigned a fitness value (based on its objective function value) and the fitter individuals are given a 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 individuals or solutions over generations, till we reach a stopping criterion.
Genetic Algorithms are sufficiently randomized in nature, but they perform much better than random local search (in which we just try various random solutions, 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 continuous and discrete functions and also multi-objective problems.
- Provides a list of “good” solutions and not just a single solution.
- Always gets 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 parameters involved.
Limitations of GAs : Like any technique, GAs also suffer from a few limitations. These include −
- GAs are not suited for all problems, especially problems 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 guarantees 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 from the current population:
- Selection rulesselect the individuals, called parents, that contribute to the population at the next generation.
- Crossover rulescombine two parents to form children for the next generation.
- Mutation rulesapply random changes to individual parents to form children.
SOLUTION STEPS :
- 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.
- Another function named stalagmite was created separately which stores the two input variables and contains equations for output and was called in main program.
- The results from stalagmite function was stored in another variable (f) and plotted as surface usinf surfc command in MATLAB.
- "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 values from "ga" command.
- Then our code was made to iterate 50 times using 'for loop' to study the statistical behaviour of genetic algorithm with unbounded inputs,bounded inputs, bounded inputs with increasing populations and all these studies are plotted individually using subplot,plot3 and sufc command.
- The results obtained from all three studies differ from each other as in first study of unbounded inputs, during each iterations the maximum values changes from 0 to 1, so in second study of bounded inputs "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.
- Hence, in third study of bounded inputs with increasing population size which was 50 by default, it was incresed to 300. The output results obtained were confined in very small range of 0.99 and it was approximately accuate results. If the population size was increased further, more accurate results 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 ≤ b. ga evaluates 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 ≤ b. (Set 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,lb,u defines a set of lower and upper bounds on the design variables, b)x, so that a solution is found in the range lb ≤ x ≤ ub. (Set Aeq=[] and beq=[] if no linear equalities exist.)
x = ga(fun,nvars,A,b,A subjects the minimization to the constraints defined in eq,beq,lb,ub,nonlcon)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.)
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 = ga(fun,nvars,A,b,[],[],lb,ub,non or lcon,IntCon) requires that the variables listed in x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options)IntCon take integer values.
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)*((y-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
1ST :
% 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')
2ND :
% 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','r','markersize',5)
title('Bounded inputs')
subplot(2,1,2)
plot(-fopt_2)
xlabel('Iterations')
ylabel('Function maximum')
3RD :
% 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 population size')
subplot(2,1,2)
plot(-fopt_3)
xlabel('Iterations')
ylabel('Function maximum')



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