Week 4.1 - Genetic Algorithm
AIM:- To Write a code using ga Syntax so as to find the Global Maxima of a Stalagmite Function.
Introduction:- 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.
Theory Required:-
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.
Genetic algorithms are general-purpose search algorithms that use principles inspired by natural population genetics to evolve solutions to problems. The basic idea is that over time, evolution will select the 'fittest species'.
How the Genetic Algorithm Works
Outline of the Algorithm
The following outline summarizes how the genetic algorithm works:
- The algorithm begins by creating a random initial population.
- The algorithm then creates a sequence of new populations. At each step, the algorithm uses the individuals in the current generation 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 expectation.
- Some of the individuals in the current population that have lower fitness are chosen as elite. These elite individuals are passed 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—
- Replaces the current population with the children to form the next generation.
- The algorithm stops when one of the stopping criteria is met.
Objective:
- To Understand the concept of genetic algorithm
- To Understand the syntax “ga” in MATLAB
- To find the Global Maxima of the stalagmite function using GA Syntax.
Syntax for genetic algorithm-
we can give the inputs by any one of the following ways-
Description of input variables-
These are the various input syntaxes that can be given-
finds a local unconstrained minimum, x = ga(fun,nvars)x, to the objective function, fun. nvars is the dimension (number of design variables) of fun.
Passing Extra Parameters (Optimization Toolbox) explains how to pass extra parameters to the objective function and nonlinear constraint functions, if necessary.
subjects the minimization to the constraints defined in x = ga(fun,nvars,A,b,Aeq,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.)
or x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon) requires that the variables listed in x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options)IntCon take integer values.
When there are integer constraints, ga does not accept linear or nonlinear equality constraints, only inequality constraints.
[, for any previous input arguments, also returns x,fval] = ga(___)fval, the value of the fitness function at x.
- First we initialise the values of x and y taking 150 values between the interval 0 and 0.6,we consider no of iterations num_spaces=50.
- Since we need to plot a 3D surface plot we take the 2d array of x and y using meshgrid command and store them in input_vector array ,So that it can be used as an argument to call the function stalagmitewe create another fuction file for the stalagmite function. here we take the ouput as -f since ga only minimises
- a given function.so if -f is considered it automatically gives us the maxima.
- We take no of iterations = num_spaces and we use the genetic algorithm function to caluclate the maxima of the function. we store the outputs into results which contain maximas and fopt which contain the value at which the maxima is obtained,Here the ga is initiated with random population.
- Specify the lower and upper bounds of x and y in this case,the ga is initiated with random population only and Follow the same procedure.specify the lower and upper bounds of x and y in this case,the ga is initiated with random population only.
FOR STUDY 3
-
Here in this case to set a initial population we need to use command called optimumoptions and get our value of 'populationsize' to 250.
now we store the results into xopt, yopt.now we need to plot the surface of x,y,-f to get the 3D plot.
We use the plot3 command to make a plot of xopt,yopt and -fopt to plot the optimum maximas.
Also let us plot the fopt functio so that we can visualise the optimum values at each iteration.
To get the time taken to complete cycle we used the tic toc commands to record the time.
-
Though the time of execution is more in this case we are sucessful to find out the correct maxima for the stalagmite function.
So by increasing the initrial population size of the ga we can increase the accuracy of the algorithm.
CONCLUSION
- We do Multiple iteration is to get the Global maxima in desired range. The main motive of doing so is to get the only 1 desired optimum output which is required for given problem.