Week 4.1 - Genetic Algorithm
Genetic Algorithm
Aim:
- Write a code in MATLAB to optimize the stalagmite function and find the global maxima of the function.
- Clearly explain the concept of genetic algorithm in your own words and also explain the syntax for ga in MATLAB in your report.
- Make sure that your code gives the same output, even if it is made to run several times.
- Plot graphs for all 3 studies and for F maximum vs no. of iterations. Title and axes labels are a must, legends could be shown if necessary.
Input function:
f(x,y)=f1,xf2,xf1,yf2,y
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×ln(2) (x-0.0667)^2/0.64]
f_(2,y)=exp[-4×ln(2) (y-0.0667)^2/0.64]

Objectives:
1. unbounded input

2. bounded input

3. bounded input with increase iteration

Genetic algorithm:
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.
Introduction to Optimization:- Optimization is the process of making something better. In any process, we have a set of inputs and a set of outputs, Optimization refers to finding the values of inputs in such a way that we get the “best” output values. The definition of “best” varies from problem to problem, but in mathematical terms, it refers to maximizing or minimizing one or more objective functions, by varying the input parameters.
The set of all possible solutions or values which the inputs can take make up the search space. In this search space, lies a point or a set of points that gives the optimal solution. The aim of optimization is to find that point or set of points in the search space.
Introduction to Genetic algorithm:-
Genetic Algorithms (GAs) are search-based algorithms based on the concepts of natural selection and genetics. Genetic Algorithms are a subset of a much larger branch of computation known as Evolutionary Computation. A genetic algorithm was 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 Genetic Algorithms, 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 Genetic Algorithm:-
- 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 time.
- Useful when the search space is very large and there are a large number of parameters involved.
Limitations of Genetic Algorithm:-
- Genetic Algorithms are not suited for all problems, especially problems that are simple and for which derivative information is available.
- The 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.
Syntax for Genetic Algorithm:
X=ga (fun, nvars)
X=ga (fun, nvars, A, b)
X=ga (fun, nvars, A, b, Aeq, beq)
X=ga (fun, nvars, A, b, Aeq, beq, lb, ub)
X=ga (fun, nvars, A, b, Aeq, beq, lb, ub, nonlcon)
X=ga (fun, nvars, A, b, Aeq, beq, lb, ub, nonlcon, options)
X=ga (fun, nvars, A, b, Aeq, beq, lb, ub, nonlcon, intcon)
X=ga (fun, nvars, A, b, Aeq, beq, lb, ub, nonlcon, intcon, options)
X=ga (problem)
[x, fval] = ga ( __ )
[x, fval, exitflag, output, population, scores]=ga ( __ )
Procedure:
- First, the linspace command is used to define x and y with 150 elements between 0 and 0.6 including the bounds.
- Num_cases is set which will be used as a counter for iterations to 50.
- 2D grid is made using mesh grid command is created from x and y input to be outputted to xx and yy.
- For loop is used to solve the genetic algorithm for every element in the 2D mesh grid.
- The length of rows and columns are used as counters.
- Output stalagmite function stored in a matrix.
- The Tic command is used to start a timer.
- Then for loop bounded by the number of iterations is formed.
- Then GA code is entered for unbounded statistical study.
[input,fopt]=ga(fun,nvars)
Input represents the value of parameters x and y for which the function has the least value(fopt)
Fun-the function to be optimized, in this case, stalagmite
nvars-the number of design variables, in this case only x and y are the variable to be plotted
- The smallest value of the function for each iteration is outputted and its x and y coordinates are stored in a separate variable.
- Toc command is used to stop the timer and the output is stored in study_time1.
- Subplot command is used to partition the plot widow in order to draw more than one plot in the same windows, two rows and one column are specified and then the position of each plot is selected.
- Hold-on command is used to prevent the plot from being removed.
- Surf command is used to plot contour with x value and y value.
- x, y-axis is labeled.
- Shading interp is used to smoothen the function plot and to remove the grid lines.
- Plot3 command is used to plot a point in 3 dimensions, plot fopt because we need the maximum value of the function.
- Put the title for the graph.
- X and y-axis labeled.
- Then the same study is made but this time it is bounded (has a lower and upper limit) which requires the same commands and steps except for the ga command.
[inputs, fopt]=ga(fun, nvars, A, b, Aeq, beq, lb, ub)
A, b-linear inequality constraints: real matrix, if there are none,then []
Aeq,beq- linear equality constraints: real matrix, if there are none,then []
Lb- lower bounds, specified as a real vector or array.
Ub- upper bounds, specified as a real vector or array.
- The range of the genetic algorithm is bounded by introducing values for lower and upper bounds and hence the values of global maxima and the values of x and y will be within the defined search space in input.
- Then increasing population size study was done which is the same as before but it had a different ga code.
[inputs,fopt]=ga(fun,nvars,A,b,Aeq,lb,ub,nonlcon,IntCon,options)
Inputs, fopt, fun, nvars, A, b, Aeq, beq, lb, ub are the same as study 1 and study 2 respetively.
Nonlcon- nonlinear constraints, specific as a function handle or function name. if there are none, then[].
IntCon- Integer variable, specified as a vector of positive integers, if there are none, then [].
Options- Optimization options, specified as the output of optimoptions or a structure.
- The range of the genetic algorithm is bounded as in study 2 but to improve the accuracy, the commands optimoptions is induced to increase the population size.
- Then total time is computed.
Errors:
1.

runtime error-while run the code we get an error like not enough input arguments. Put the z coordinate value fopt and also change xopt(i) into xopt and yopt(i) into yopt.
2.

Getting all plots are the same. the error that happened was to put the value 'l' mistakenly instead of 'i' in forloop .
Conclusion:
- Wrote a code in MATLAB to optimize the stalagmite function and found the global maxima of the function.
- Clearly explained the concept of genetic algorithm in own words and also explained the syntax for ga in MATLAB.
- Plotted graphs for all 3 studies and for F maximum vs no. of iterations.