Project

Global Maxima of an optimized Stalagmite Function

About 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.

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.

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.

Optimization is the process of making something better. In any process, we have a set of inputs and a set of outputs as shown in the following figure.

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 which gives the optimal solution. The aim of optimization is to find that point or set of points in the search space.

Some difficult problems like the Travelling Salesperson Problem (TSP), have real-world applications like path finding and VLSI Design. Now imagine that you are using your GPS Navigation system, and it takes a few minutes (or even a few hours) to compute the “optimal” path from the source to destination. Delay in such real-world applications is not acceptable and therefore a “good-enough” solution, which is delivered “fast” is what is required.

How it Works:

As we now know they\'re based on the process of natural selection, this means they take the fundamental properties of natural selection and apply them to whatever problem it is we\'re trying to solve.

The basic process for a genetic algorithm is:

  1. Initialization - Create an initial population. This population is usually randomly generated and can be any desired size, from only a few individuals to thousands.
  2. Evaluation - Each member of the population is then evaluated and we calculate a \'fitness\' for that individual. The fitness value is calculated by how well it fits with our desired requirements. These requirements could be simple, \'faster algorithms are better\', or more complex, \'stronger materials are better but they shouldn\'t be too heavy\'.
  3. Selection - We want to be constantly improving our populations overall fitness. Selection helps us to do this by discarding the bad designs and only keeping the best individuals in the population. There are a few different selection methods but the basic idea is the same, make it more likely that fitter individuals will be selected for our next generation.
  4. Crossover - During crossover we create new individuals by combining aspects of our selected individuals. We can think of this as mimicking how sex works in nature. The hope is that by combining certain traits from two or more individuals we will create an even \'fitter\' offspring which will inherit the best traits from each of it\'s parents.
  5. Mutation - We need to add a little bit randomness into our populations\' genetics otherwise every combination of solutions we can create would be in our initial population. Mutation typically works by making very small changes at random to an individuals genome.
  6. And repeat! - Now we have our next generation we can start again from step two until we reach a termination condition.

Main Program For Global Maxima of an optimized stalagmite function:

%Program to calculate the global maxima of a stalagmite function
clear all
close all
clc

%Defining our search space
x=linspace(0,0.8,200);
y=linspace(0,0.8,200);

%Increasing GA iterations 
options=optimoptions(\'ga\');
options=optimoptions(options,\'PopulationSize\',200);

%creating a 2D 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
  
%Plotting
surfc(xx,yy,f)
title(\'Stalagmite function\')
xlabel(\'x values\')
ylabel(\'y values\')
zlabel(\'fitness function values\')
shading interp

%Performing genetic algorithm for optimization
[inputs,fval]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options)

%Results
fprintf(\'The best function value (maximum value) found was: %g\\n\', fval);
fprintf(\'Final x value was: %g\\n\', inputs(1));
fprintf(\'Final y value was: %g\\n\', inputs(2));

Its Stalagmite Function:

%Stalagmite function for calculating the global maxima

function[maxima]=stalagmite(input_vector)
  
     x=input_vector(1)
     y=input_vector(2)
     
     
     f1=(sin((5.1*pi*x+0.5)))^6;
     f2=(sin((5.1*pi*y+0.5)))^6;
     
     f3=exp(-4*log(2)*(x-0.0667)^2)/0.64;
     f4=exp(-4*log(2)*(y-0.0667)^2)/0.64;
     
     maxima=-(f1*f2*f3*f4);
end
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.