Genetic Algorithm
1. AIM:
- To Optimize the given Stalagmite Function and find out Global Maxima of the function using ga (Genetic-Algorithm) in MATLAB.
2. OBJECTIVES OF THE PROJECT:
- Theory about ga (Genetic-Algorithm).
- Stopping criteria.
- The syntax for ga in MATLAB.
3. THEORY & GOVERNING EQUATIONS:
- Introduction to Genetic Algorithm.
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 to produce offspring of the next generation. A 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 machine learning.

The Notion of Natural Selection
The process of natural selection starts with the selection of the fittest individuals from a population. They produce offspring which inherit the characteristics of the parents and will be added to the next generation. If parents have better fitness, their offspring will be better than parents and have a better chance at surviving. This process keeps on iterating and in the end, a generation with the fittest individuals will be found.
This notion can be applied to a search problem. We consider a set of solutions for a problem and select the set of best ones out of them.
- Five phases are considered in a genetic algorithm.
- Initial population
- Fitness function
- Selection
- Crossover
- Mutation
- Initial Population
The process begins with a set of individuals which is called a Population. Each individual is a solution to the problem you want to solve.
An individual is characterized by a set of parameters (variables) known as Genes. Genes are joined into a string to form a Chromosome (solution).
In a genetic algorithm, the set of genes of an individual is represented using a string, in terms of an alphabet. Usually, binary values are used (a string of 1s and 0s). We say that we encode the genes in a chromosome.

Population, Chromosomes, and Genes
- Fitness Function
The fitness function determines how to fit an individual is (the ability of an individual to compete with other individuals). It gives a fitness score to each individual. The probability that an individual will be selected for reproduction is based on its fitness score.
- Selection
The idea of the selection phase is to select the fittest individuals and let them pass their genes to the next generation.
Two pairs of individuals (parents) are selected based on their fitness scores. Individuals with high fitness have more chances to be selected for reproduction.
- Crossover
Crossover is the most significant phase in a genetic algorithm. For each pair of parents to be mated, a crossover point is chosen at random from within the genes.
For example, consider the crossover point to be 3 as shown below.

Crossover point
Offspring are created by exchanging the genes of parents among themselves until the crossover point is reached.

Exchanging genes among parents
The new offspring are added to the population.

New offspring
- Mutation
In certain new offspring formed, some of their genes can be subjected to a mutation with a low random probability. This implies that some of the bits in the bit string can be flipped.

Mutation: Before and After
The mutation occurs to maintain diversity within the population and prevent premature convergence.
- Termination (Stopping Criteria)
The algorithm terminates if the population has converged (does not produce offspring which are significantly different from the previous generation). Then it is said that the genetic algorithm has provided a set of solutions to our problem.
The termination condition of a Genetic Algorithm is important in determining when a GA run will end. It has been observed that initially, the GA progresses very fast with better solutions coming in every few iterations, but this tends to saturate in the later stages where the improvements are very small. We usually want a termination condition such that our solution is close to the optimal, at the end of the run.
Usually, we keep one of the following termination conditions −
- When there has been no improvement in the population for X iterations.
- When we reach an absolute number of generations.
- When the objective function value has reached a certain pre-defined value.
For example, in a genetic algorithm, we keep a counter which keeps track of the generations for which there has been no improvement in the population. Initially, we set this counter to zero. Each time we don’t generate off-springs who are better than the individuals in the population, we increment the counter.
However, if the fitness of any of the off-springs is better, then we reset the counter to zero. The algorithm terminates when the counter reaches a predetermined value.
Like other parameters of a GA, the termination condition is also highly problem specific and the GA designer should try out various options to see what suits his particular problem the best.
Basic Terminology
Before beginning a discussion on Genetic Algorithms, it is essential to be familiar with some basic terminology that will be used throughout this tutorial.
- Population− It is a subset of all the possible (encoded) solutions to the given problem. The population for a GA is analogous to the population for human beings except that instead of human beings, we have Candidate Solutions representing human beings.
- Chromosomes− A chromosome is one such solution to the given problem.
- Gene− A gene is one element position of a chromosome.
- Allele− It is the value a gene takes for a particular chromosome.

- Genotype− Genotype is the population in the computation space. In the computation space, the solutions are represented in a way that can be easily understood and manipulated using a computing system.
- Phenotype− Phenotype is the population in the actual real-world solution space in which solutions are represented in a way they are represented in real-world situations.
- Decoding and Encoding− For simple problems, the phenotype and genotype spaces are the same. However, in most cases, the phenotype and genotype spaces are different. Decoding is a process of transforming a solution from the genotype to the phenotype space, while encoding is a process of transforming from the phenotype to the genotype space. Decoding should be fast as it is carried out repeatedly in a GA during the fitness value calculation.
For example, consider the 0/1 Knapsack Problem. The Phenotype space consists of solutions that just contain the item numbers of the items to be picked.
However, in the genotype space, it can be represented as a binary string of length n (where n is the number of items). A 0 at position x represents that xth item is picked while a 1 represents the reverse. This is a case where genotype and phenotype spaces are different.

- Fitness Function− A fitness function simply defined as a function that takes the solution as input and produces the suitability of the solution as the output. In some cases, the fitness function and the objective function may be the same, while in others it might be different based on the problem.
- Genetic Operators− These alter the genetic composition of the offspring. These include crossover, mutation, selection, etc.
Basic Structure
The basic structure of a GA is as follows −
We start with an initial population (which may be generated at random or seeded by other heuristics), select parents from this population for mating. Apply crossover and mutation operators on the parents to generate new off-springs. And finally, these off-springs replace the existing individuals in the population, and the process repeats. In this way, genetic algorithms try to mimic human evolution to some extent.

- Syntax for GA
f = ga(func, nvars, A, b, [ ], [ ], lb, ub, nonlcon, Intcon, options)
[x, fval] = ga(func, nvars, A, b, Aeq, Beq, lb, ub, nonlcon, Intcon, options)
x = optimum solution
func = fitness function.
nvars = number of variables,
A, b = Linear inequality constraints
Aeq, Beq = Linear equality constraints
lb = Lower bound(limit)
ub = Upper bound(limit)
nonlcon = Use for nonlinear constraints
Intcon = to get solution in integer
options = To set population size minimize with non-default options
BODY OF THE CONTENT:
- MATLAB Programming language is used.
- Stalagmite Function
`f(x,y)=f_(1,x) f_(2,x) f_(1,y) f_(2,y)`
`f_(1,x)=[sin(5.1πx+0.5) ]^6`
`f_(1,y)=[sin(5.1πy+0.5) ]^6`
`f_(2,x)=exp[-4 ln(2) (x-0.0667)^2/0.64]`
`f_(2,x)=exp[-4 ln(2) (x-0.0667)^2/0.64]`
- The above equations are used in the stalagmite function file to use in the main program.
- File Name : stalagmite.m
-
function[fxy]=stalagmite(a) x=a(1,1); y=a(1,2); f1x=(sin(5.1*pi*x+0.5))^6; f1y=(sin(5.1*pi*y+0.5))^6; f2x=exp(-4*log(2)*((x-0.0667)^2)/0.64); f2y=exp(-4*log(2)*((y-0.0667)^2)/0.64); fxy=(-1)*f1x*f2x*f1y*f2y; end
- Since ga (Genetic-Algorithm) is used to optimize the stalagmite function, ga finds the minimum value of the function.
- Therefore, while taking the output from the function we have to multiply it with (-1) so the plot will be reversed and we can get the maximum value by using the inbuild ga function.
- Main Program/Code
- File Name : Genetic_Algorithm.m
- The Program/Code is explained in sections below to better understand the working of the program.
- Step-Wise Explanation is provided in the code by using next to its comments.
1. Plotting the Stalagmite Surface
- Firstly, creating the mesh from the two vectors will be acting as a search space to find the maxima of the function.
- Creating the mesh of these two vectors using the mesh command
- Now calculating the values of fitness function at each point of the mesh grid formed.
- Now creating its surface through surfc command which will also provide the contour of the generated surface.
- While using surfc applying the -ve sign to the function will generate the surface upside down which will be quite good to get the maxima of the function, as it will provide a positive value for the maxima using the -ve sign.
-
clear all close all clc %Defining our search place. x=linspace(0,0.6,150); %Defining our search place in X-Direction. y=linspace(0,0.6,150); %Defining our search place in Y-Direction. %Creating a two-dimensional mesh grid. [xx yy]=meshgrid(x,y); %Evaluating the stalagmite function for i=1:length(xx) %Using 'for' loop to collect data for every X-Value. for j=1:length(yy) %Using 'for' loop to collect data for every Y-Value. inputvector(1)=xx(i,j); %Collecting X-Value for Stalagmite Function. inputvector(2)=yy(i,j); %Collecting Y-Value for Stalagmite Function. f(i,j)=stalagmite(inputvector); %Collecting Outputs of Stalagmite Function. end %Ending 'for' function. end %Ending 'for' function. %Plotting Stalagmite Function figure(1) %Figure for Plotting Stalagmite function in 3-D. surfc(xx,yy,-f) %Generating a Surface for Plotting Stalagmite function in 3-D. xlabel('x-axis') %Labelling the X-Axis. ylabel('y-axis') %Labelling the Y-Axis. zlabel('z-axis') %Labelling the Z-Axis. shading interp %Removing grid-lines from surface. - Its outputs/Results.
- The Plot generated is shown below
2. Case(1)-Statistical Behaviour Without Limits/Boundation
- In the first case, the behavior of the algorithm is studied by not defining the limits. The GA does not run within a particular search space, since we have not defined bounds
- In this evaluating the optimum value of fitness function at each iteration and calculating it using the above-mentioned syntax in the intro part (which gives the inputs i.e., Values of x and y at which the stalagmite function is minimum)
- Then plotting it into the surf command and also providing the optimum values of the function that how it is varying
- Also using the tic and toc command to evaluate the time used for optimizing the function.
-
%Statistical Behaviour with Unbounded Region. tic %Start Stopwatch Timer. for i=1:50 %Starting For-Loop to do 50 Iterations. [inputs1, fopt_ubr(i)]=ga(@stalagmite,2); %Using inbuild ga(Genetic-Algorithm) to Find Optimum value of function stalagmite. x_opt_ubr(i)=inputs1(1,1); %Collecting X-Value of Optimum value of function stalagmite for every iteration. y_opt_ubr(i)=inputs1(1,2); %Collecting Y-Value of Optimum value of function stalagmite for every iteration. end %Ending 'for' function. study_time_1=toc; %Stop Stopwatch Timer and collect time data. figure(2) %Figure for Plotting Stalagmite function in 3-D with Unbounded Region. txt1=sprintf('Study Time %1.4f sec',study_time_1); %Text to display time taken to evaluate Optimization. sgtitle(txt1) %Display time taken to evaluate Optimization. %Creating 2*1 plots in Single Figure. subplot(2,1,1) %Defining 1st Plot from 2*1 Plots in single figure. surfc(xx,yy,-f) %Generating a surface for Plotting Stalagmite function in 3-D with Unbounded Region. xlabel('x-axis') %Labelling the X-Axis. ylabel('y-axis') %Labelling the Y-Axis. zlabel('z-axis') %Labelling the Z-Axis. grid on %Display Axis Grid Lines. shading interp %Removing grid-lines from the surface. hold on %Command to add multiple Plots in a single Plot. plot3(x_opt_ubr,y_opt_ubr,-fopt_ubr,'-o','markerfacecolor','r'); %Plotting the Optimized points on the Surface/3-D Space. subplot(2,1,2) %Defining 2nd Plot from 2*1 Plots in single figure. plot(-fopt_ubr); %Plotting Optimum Value vs no. of Iterations. xlabel('Iterations') %Labelling the X-Axis. ylabel('Function Maximum Value') %Labelling the Y-Axis. - Its outputs/Results.
- The Plot generated is shown below

- From the above output, it is clear that since the inputs are boundless GA algorithm has mapped local maxima points outside our stalagmite function.
- Also, we cannot infer a particular value of global maxima from the above graph, and it is hence pointless.
- The study time is very little as the ga algorithm is made only to run 50 iterations.
- The results are not that accurate and giving a lot of variation in optimizing the function.
3. Case(2)-Statistical Behaviour Limits/Boundation
- Here this case is similar to Case(1) only change here is that we have provided limits for the search area to find the optimum value for our function using the inbuild ga function.
- But here when giving upper and lower limits/bounds we have to give a very good guess or know the function properly since we have potted the function from 0 to 0.6 for both X-axis & Y-axis.
- We have bounded the search space for ga from 0 to 0.6 for both X-axis & Y-axis.
- Then plotting it into the surf command and also providing the optimum values of the function that how it is varying
- Also using the tic and toc command to evaluate the time used for optimizing the function.
-
%Statistical Behaviour with Bounded Region. tic %Start Stopwatch Timer. for i=1:50 %Starting For-Loop to do 50 Iterations. [inputs1, fopt_br(i)]=ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6]); %Using inbuild ga(Genetic-Algorithm) to Find Optimum value of function stalagmite. x_opt_br(i)=inputs1(1,1); %Collecting X-Value of Optimum value of function stalagmite for every iteration. y_opt_br(i)=inputs1(1,2); %Collecting Y-Value of Optimum value of function stalagmite for every iteration. end %Ending 'for' function. study_time_2=toc; %Stop Stopwatch Timer and collect time data. figure(3) %Figure for Plotting Stalagmite function in 3-D with Bounded Region. txt2=sprintf('Study Time %1.4f sec',study_time_2); %Text to display time taken to evaluate Optimization. sgtitle(txt2) %Display time taken to evaluate Optimization. %Creating 2*1 plots in Single Figure. subplot(2,1,1) %Defining 1st Plot from 2*1 Plots in single figure. surfc(xx,yy,-f) %Generating a surface for Plotting Stalagmite function in 3-D with Bounded Region. xlabel('x-axis') %Labelling the X-Axis. ylabel('y-axis') %Labelling the Y-Axis. zlabel('z-axis') %Labelling the Z-Axis. grid on %Display Axis Grid Lines. shading interp %Removing grid-lines from the surface. hold on %Command to add multiple Plots in a single Plot. plot3(x_opt_br,y_opt_br,-fopt_br,'-o','markerfacecolor','r'); %Plotting the Optimized points on the Surface/3-D Space. subplot(2,1,2) %Defining 2nd Plot from 2*1 Plots in single figure. plot(-fopt_br); %Plotting Optimum Value vs no. of Iterations. xlabel('Iterations') %Labelling the X-Axis. ylabel('Function Maximum Value') %Labelling the Y-Axis. - Its outputs/Results.
- The Plot generated is shown below.

- From the above output, it is clear that since the inputs are bounded GA algorithm has mapped local maxima points inside our stalagmite function.
- The above graph is clearer and more optimized than compared to Case(1).
- The study time is very little as the ga algorithm is made only to run 50 iterations.
- The results are much accurate and giving less variation in optimizing the function than the Case(1).
- Here we get 4 values for optimum point.
4. Case(3)-Statistical Behaviour Limits/Boundation with Increase in Population Size.
- Here this case is similar to Case(2) only change here is that we have provided increased Population size to find the optimum value for our function using the inbuild ga function.
- In this case, Population Size is increased to converge the optimum values to Global Maxima.
- We have used optimoptions command/function to increase the population size.
- Here Population Size is 300.
- Then plotting it into the surf command and also providing the optimum values of the function that how it is varying
- Also using the tic and toc command to evaluate the time used for optimizing the function.
- The cons for this case are that it takes more time to get the results as compared with both cases Case(1) & Case(2).
-
%Statistical Behaviour with Bounded Region with Increase in Size of Population. options=optimoptions('ga','populationsize',300); %Increasing the Size of Population by using optimoptions inbuild function. tic %Start Stopwatch Timer. for i=1:50 %Starting For-Loop to do 50 Iterations. [inputs1, fopt_p(i)]=ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6],[],[],options); %Using inbuild ga(Genetic-Algorithm) to Find Optimum value of function stalagmite. x_opt_p(i)=inputs1(1,1); %Collecting X-Value of Optimum value of function stalagmite for every iteration. y_opt_p(i)=inputs1(1,2); %Collecting Y-Value of Optimum value of function stalagmite for every iteration. end %Ending 'for' function. study_time_3=toc; %Stop Stopwatch Timer and collect time data. figure(4) %Figure for Plotting Stalagmite function in 3-D with Bounded Region with Increase in Size of Population. txt3=sprintf('Study Time %1.4f sec',study_time_3); %Text to display time taken to evaluate Optimization. sgtitle(txt3) %Display time taken to evaluate Optimization. %Creating 2*1 plots in Single Figure. subplot(2,1,1) %Defining 1st Plot from 2*1 Plots in single figure. surfc(xx,yy,-f) %Generating a surface for Plotting Stalagmite function in 3-D with Bounded Region with Increase in Size of Population. xlabel('x-axis') %Labelling the X-Axis. ylabel('y-axis') %Labelling the Y-Axis. zlabel('z-axis') %Labelling the Z-Axis. grid on %Display Axis Grid Lines. shading interp %Removing grid-lines from the surface. hold on %Command to add multiple Plots in a single Plot. plot3(x_opt_p,y_opt_p,-fopt_p,'-o','markerfacecolor','r'); %Plotting the Optimized points on the Surface/3-D Space. subplot(2,1,2) %Defining 2nd Plot from 2*1 Plots in single figure. plot(-fopt_p); %Plotting Optimum Value vs no. of Iterations. xlabel('Iterations') %Labelling the X-Axis. ylabel('Function Maximum Value') %Labelling the Y-Axis. Optimum_Solution=sprintf('Maximum value is %1.4f at %1.4f,%1.4f',-fopt_p(1),x_opt_p(1),y_opt_p(1)); %Text to display Optimized Solution/Value. title(Optimum_Solution) %Display Optimized Solution/Value. - Its outputs/Results.
- The Plot generated is shown below.

- From the above output, it is clear that since the inputs are bounded and increased the population size GA algorithm has mapped the local maxima point of the stalagmite function.
- The above graph is clearer and more optimized than compared to Case(2).
- The results are accurate and giving very little variation in optimizing the function than the Case(2).
- Here we get the Global Optimum Point.
5. Stopping Criteria & Best Fit.
- The Theory about stopping criteria is explained earlier in this project.
- By using gaplotstopping in the optimoptions we get the plot of percentage/levels of stopping criteria satisfied.
- And using gaplotbestf in the optimoptions we get the plot that plots the best score value and mean score versus generation.
-
%Stopping Criteria options=optimoptions('ga','PlotFcn',{@gaplotstopping,@gaplotbestf},'populationsize',300); %Increasing the Size of Population by using optimoptions inbuild function. tic %Start Stopwatch Timer. [inputs1, fopt_s]=ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6],[],[],options); %Using inbuild ga(Genetic-Algorithm) to Find Optimum value of function stalagmite. x_opt_s=inputs1(1,1); %Collecting X-Value of Optimum value of function stalagmite for every iteration. y_opt_s=inputs1(1,2); %Collecting Y-Value of Optimum value of function stalagmite for every iteration. study_time_4=toc; %Stop Stopwatch Timer and collect time data. figure(5) %Figure for Plotting Stopping Criteria and Best Fit. txt4=sprintf('Study Time %1.4f sec',study_time_4); %Text to display time taken to evaluate. sgtitle(txt4) %Display time taken to evaluate. - Its outputs/Results.
- The Plot generated is shown below.

- Now the Complete program and its results/outputs are below.
-
% Try to understand the concept of the genetic algorithm by referring to various sources before you attend this challenge. Look out for information regarding the stalagmite function and ways to optimize it. % Write a code in MATLAB to optimize the stalagmite function and find the global maxima of the function. % 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 F maximum vs no. of iterations. Title and axes labels are a must, legends could be shown if necessary. clear all close all clc %Defining our search place. x=linspace(0,0.6,150); %Defining our search place in X-Direction. y=linspace(0,0.6,150); %Defining our search place in Y-Direction. %Creating a two-dimensional mesh grid. [xx yy]=meshgrid(x,y); %Evaluating the stalagmite function for i=1:length(xx) %Using 'for' loop to collect data for every X-Value. for j=1:length(yy) %Using 'for' loop to collect data for every Y-Value. inputvector(1)=xx(i,j); %Collecting X-Value for Stalagmite Function. inputvector(2)=yy(i,j); %Collecting Y-Value for Stalagmite Function. f(i,j)=stalagmite(inputvector); %Collecting Outputs of Stalagmite Function. end %Ending 'for' function. end %Ending 'for' function. %Plotting Stalagmite Function figure(1) %Figure for Plotting Stalagmite function in 3-D. surfc(xx,yy,-f) %Generating a Surface for Plotting Stalagmite function in 3-D. xlabel('x-axis') %Labelling the X-Axis. ylabel('y-axis') %Labelling the Y-Axis. zlabel('z-axis') %Labelling the Z-Axis. shading interp %Removing grid-lines from surface. %Statistical Behaviour with Unbounded Region. tic %Start Stopwatch Timer. for i=1:50 %Starting For-Loop to do 50 Iterations. [inputs1, fopt_ubr(i)]=ga(@stalagmite,2); %Using inbuild ga(Genetic-Algorithm) to Find Optimum value of function stalagmite. x_opt_ubr(i)=inputs1(1,1); %Collecting X-Value of Optimum value of function stalagmite for every iteration. y_opt_ubr(i)=inputs1(1,2); %Collecting Y-Value of Optimum value of function stalagmite for every iteration. end %Ending 'for' function. study_time_1=toc; %Stop Stopwatch Timer and collect time data. figure(2) %Figure for Plotting Stalagmite function in 3-D with Unbounded Region. txt1=sprintf('Study Time %1.4f sec',study_time_1); %Text to display time taken to evaluate Optimization. sgtitle(txt1) %Display time taken to evaluate Optimization. %Creating 2*1 plots in Single Figure. subplot(2,1,1) %Defining 1st Plot from 2*1 Plots in single figure. surfc(xx,yy,-f) %Generating a surface for Plotting Stalagmite function in 3-D with Unbounded Region. xlabel('x-axis') %Labelling the X-Axis. ylabel('y-axis') %Labelling the Y-Axis. zlabel('z-axis') %Labelling the Z-Axis. grid on %Display Axis Grid Lines. shading interp %Removing grid-lines from the surface. hold on %Command to add multiple Plots in a single Plot. plot3(x_opt_ubr,y_opt_ubr,-fopt_ubr,'-o','markerfacecolor','r'); %Plotting the Optimized points on the Surface/3-D Space. subplot(2,1,2) %Defining 2nd Plot from 2*1 Plots in single figure. plot(-fopt_ubr); %Plotting Optimum Value vs no. of Iterations. xlabel('Iterations') %Labelling the X-Axis. ylabel('Function Maximum Value') %Labelling the Y-Axis. %Statistical Behaviour with Bounded Region. tic %Start Stopwatch Timer. for i=1:50 %Starting For-Loop to do 50 Iterations. [inputs1, fopt_br(i)]=ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6]); %Using inbuild ga(Genetic-Algorithm) to Find Optimum value of function stalagmite. x_opt_br(i)=inputs1(1,1); %Collecting X-Value of Optimum value of function stalagmite for every iteration. y_opt_br(i)=inputs1(1,2); %Collecting Y-Value of Optimum value of function stalagmite for every iteration. end %Ending 'for' function. study_time_2=toc; %Stop Stopwatch Timer and collect time data. figure(3) %Figure for Plotting Stalagmite function in 3-D with Bounded Region. txt2=sprintf('Study Time %1.4f sec',study_time_2); %Text to display time taken to evaluate Optimization. sgtitle(txt2) %Display time taken to evaluate Optimization. %Creating 2*1 plots in Single Figure. subplot(2,1,1) %Defining 1st Plot from 2*1 Plots in single figure. surfc(xx,yy,-f) %Generating a surface for Plotting Stalagmite function in 3-D with Bounded Region. xlabel('x-axis') %Labelling the X-Axis. ylabel('y-axis') %Labelling the Y-Axis. zlabel('z-axis') %Labelling the Z-Axis. grid on %Display Axis Grid Lines. shading interp %Removing grid-lines from the surface. hold on %Command to add multiple Plots in a single Plot. plot3(x_opt_br,y_opt_br,-fopt_br,'-o','markerfacecolor','r'); %Plotting the Optimized points on the Surface/3-D Space. subplot(2,1,2) %Defining 2nd Plot from 2*1 Plots in single figure. plot(-fopt_br); %Plotting Optimum Value vs no. of Iterations. xlabel('Iterations') %Labelling the X-Axis. ylabel('Function Maximum Value') %Labelling the Y-Axis. %Statistical Behaviour with Bounded Region with Increase in Size of Population. options=optimoptions('ga','populationsize',300); %Increasing the Size of Population by using optimoptions inbuild function. tic %Start Stopwatch Timer. for i=1:50 %Starting For-Loop to do 50 Iterations. [inputs1, fopt_p(i)]=ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6],[],[],options); %Using inbuild ga(Genetic-Algorithm) to Find Optimum value of function stalagmite. x_opt_p(i)=inputs1(1,1); %Collecting X-Value of Optimum value of function stalagmite for every iteration. y_opt_p(i)=inputs1(1,2); %Collecting Y-Value of Optimum value of function stalagmite for every iteration. end %Ending 'for' function. study_time_3=toc; %Stop Stopwatch Timer and collect time data. figure(4) %Figure for Plotting Stalagmite function in 3-D with Bounded Region with Increase in Size of Population. txt3=sprintf('Study Time %1.4f sec',study_time_3); %Text to display time taken to evaluate Optimization. sgtitle(txt3) %Display time taken to evaluate Optimization. %Creating 2*1 plots in Single Figure. subplot(2,1,1) %Defining 1st Plot from 2*1 Plots in single figure. surfc(xx,yy,-f) %Generating a surface for Plotting Stalagmite function in 3-D with Bounded Region with Increase in Size of Population. xlabel('x-axis') %Labelling the X-Axis. ylabel('y-axis') %Labelling the Y-Axis. zlabel('z-axis') %Labelling the Z-Axis. grid on %Display Axis Grid Lines. shading interp %Removing grid-lines from the surface. hold on %Command to add multiple Plots in a single Plot. plot3(x_opt_p,y_opt_p,-fopt_p,'-o','markerfacecolor','r'); %Plotting the Optimized points on the Surface/3-D Space. subplot(2,1,2) %Defining 2nd Plot from 2*1 Plots in single figure. plot(-fopt_p); %Plotting Optimum Value vs no. of Iterations. xlabel('Iterations') %Labelling the X-Axis. ylabel('Function Maximum Value') %Labelling the Y-Axis. Optimum_Solution=sprintf('Maximum value is %1.4f at %1.4f,%1.4f',-fopt_p(1),x_opt_p(1),y_opt_p(1)); %Text to display Optimized Solution/Value. title(Optimum_Solution) %Display Optimized Solution/Value. %Stopping Criteria options=optimoptions('ga','PlotFcn',{@gaplotstopping,@gaplotbestf},'populationsize',300); %Increasing the Size of Population by using optimoptions inbuild function. tic %Start Stopwatch Timer. [inputs1, fopt_s]=ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6],[],[],options); %Using inbuild ga(Genetic-Algorithm) to Find Optimum value of function stalagmite. x_opt_s=inputs1(1,1); %Collecting X-Value of Optimum value of function stalagmite for every iteration. y_opt_s=inputs1(1,2); %Collecting Y-Value of Optimum value of function stalagmite for every iteration. study_time_4=toc; %Stop Stopwatch Timer and collect time data. figure(5) %Figure for Plotting Stopping Criteria and Best Fit. txt4=sprintf('Study Time %1.4f sec',study_time_4); %Text to display time taken to evaluate. sgtitle(txt4) %Display time taken to evaluate. clc %Clearing the Command Window study_time_1 %Display time taken to evaluate Optimization for Plotting Stalagmite function in 3-D with Unbounded Region. study_time_2 %Display time taken to evaluate Optimization for Plotting Stalagmite function in 3-D with Bounded Region. study_time_3 %Display time taken to evaluate Optimization for Plotting Stalagmite function in 3-D with Bounded Region with Increase in Size of Population. Optimum_Solution %Display Optimized Solution/Value. - Results/Outputs.
- I have first written the alternate program to calculate the Optimize Value using the ga function, only just by using ‘FOR’ and ‘IF’ loops, it always gives the output with 99% accuracy.
- Alternate Program/Code
- File Name Optimization_max_min.m
- Firstly, creating the mesh from the two vectors will be acting as a search space to find the maxima of the function.
- Creating the mesh of these two vectors using the mesh command
- Now calculating the values of fitness function at each point of the mesh grid formed.
- ‘FOR’ loop is used to run the 1000 iteration of the ga function. The GA does not run within a particular search space, since we have not defined bounds
- In this evaluating the optimum value of fitness function at each iteration and calculating it using the above-mentioned syntax in the intro part (which gives the inputs i.e. Values of x and y at which the stalagmite function is minimum)
- Multiplying it (-1) to convert it to the maximum value.
- ‘IF’ loop is used to update and collect the Maximum Values and their Input Values.
- Now creating its surface through surfc command which will also provide the contour of the generated surface.
- While using surfc applying the -ve sign to the function will generate the surface upside down which will be quite good to get the maxima of the function, as it will provide a positive value for the maxima using the -ve sign.
- After plotting the surface, the Optimized Point is also plot on the surface within the same plot.
- Also using the tic and toc command to evaluate the time used for optimizing the function.
- Step-Wise Explanation is provided in the code by using next to its comments.
-
% Try to understand the concept of the genetic algorithm by referring to various sources before you attend this challenge. Look out for information regarding the stalagmite function and ways to optimize it. % Write a code in MATLAB to optimize the stalagmite function and find the global maxima of the function. % 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 F maximum vs no. of iterations. Title and axes labels are a must, legends could be shown if necessary. clear all close all clc %Defining our search place. x=linspace(0,0.6,150); %Defining our search place in X-Direction. y=linspace(0,0.6,150); %Defining our search place in Y-Direction. %Creating a two-dimensional mesh grid. [xx yy]=meshgrid(x,y); %Evaluating the stalagmite function for i=1:length(xx) %Using 'for' loop to collect data for every X-Value. for j=1:length(yy) %Using 'for' loop to collect data for every Y-Value. inputvector(1)=xx(i,j); %Collecting X-Value for Stalagmite Function. inputvector(2)=yy(i,j); %Collecting Y-Value for Stalagmite Function. f(i,j)=stalagmite(inputvector); %Collecting Outputs of Stalagmite Function. end %Ending 'for' function. end %Ending 'for' function. fval_O=0; %Initial Maximum Value inputs_O=[0,0]; %Initial Outputs tic %Start Stopwatch Timer. for i=1:1000; %Starting For-Loop to do 1000 Iterations. [inputs, fval(i)]=ga(@stalagmite,2); %Using inbuild ga(Genetic-Algorithm) to Find Optimum value of function stalagmite. z=-fval; %Collecting the Optimized Maximum Value if z(i)>fval_O; %Providing condition to check compare Maximum Values. fval_O=z(i); %Updating Maximum Value. inputs_O=inputs; %Updating Inputs for Maximum Value. else fval_O=fval_O; %Updating Maximum Value. inputs_O=inputs_O; %Updating Inputs for Maximum Value. end %Ending 'if' function. end %Ending 'for' function. studytime=toc %Stop Stopwatch Timer and collect time data. %Plotting Stalagmite Function figure(1) %Figure for Plotting Stalagmite function in 3-D. surfc(xx,yy,-f) %Generating a Surface for Plotting Stalagmite function in 3-D. xlabel('x-axis') %Labelling the X-Axis. ylabel('y-axis') %Labelling the Y-Axis. zlabel('z-axis') %Labelling the Z-Axis. grid on %Display Axis Grid Lines. shading interp %Removing grid-lines from surface. hold on %Command to add multiple Plots in a single Plot. plot3(inputs_O(1),inputs_O(2),fval_O,'-o','markerfacecolor','r'); %Plotting the Optimized points on the Surface/3-D Space. txt=sprintf('Study Time %1.4f sec',studytime); %Text to display time taken to evaluate Optimization. title(txt); %Display time taken to evaluate Optimization. %Displaying the Solution. clc %Clearing the Command Window studytime %Display time taken to evaluate Optimization for Plotting Stalagmite function. inputs_O %Display Inputs for Maximum Value. fval_O %Display Maximum Value. - Results/Outputs.
- CONCLUSION (For Alternate Program) :
- The alternate program to calculate the Optimize Value using the ga function is made just by using ‘FOR’ and ‘IF’ loops.
- It takes a very much time as compare with other cases from earlier programs.
- It doesn’t provide the exact optimum value since ga is useful for optimizing the function but still, the result is not accurate as the optimum maximum value for the function lies at (-1).
- It always gives the output with more than 99% accuracy.
5. CONCLUSION:
- For Stalagmite function, Plotting the Stalagmite Surface
- Since ga (Genetic-Algorithm) is used to optimize the stalagmite function, ga finds the minimum value of the function.
- Therefore, while taking the output from the function we have to multiply it with (-1) so the plot will be reversed and we can get the maximum value by using the inbuild ga function.
- Case(1)-Statistical Behaviour Without Limits/Boundation
- From the above output, it is clear that since the inputs are boundless GA algorithm has mapped local maxima points outside our stalagmite function.
- Also, we cannot infer a particular value of global maxima from the above graph, and it is hence pointless.
- The study time is very little as the ga algorithm is made only to run 50 iterations.
- The results are not that accurate and giving a lot of variation in optimizing the function.
- Case(2)-Statistical Behaviour Limits/Boundation
- From the above output, it is clear that since the inputs are bounded GA algorithm has mapped local maxima points inside our stalagmite function.
- The above graph is clearer and more optimized than compared to Case(1).
- The study time is very little as the ga algorithm is made only to run 50 iterations.
- The results are much accurate and giving less variation in optimizing the function than the Case(1).
- Here we get 4 values for optimum point.
- Case(3)-Statistical Behaviour Limits/Boundation with Increase in Population Size.
- From the above output, it is clear that since the inputs are bounded and increased the population size GA algorithm has mapped the local maxima point of the stalagmite function.
- The above graph is clearer and more optimized than compared to Case(2).
- The results are accurate and giving very little variation in optimizing the function than the Case(2).
- Here we get the Global Optimum Point.
6. REFERENCES:









