this tutorial will start with an explanation
of the application functionality and 2nd i
will run a prebuilt version of this application
and explain the output and 3rd i will step
by step write and explain the code using an
eclipse ide now we want to use genetic algorithm
in order to discover a formula that applies
the addition subtraction multiplication division
and modulo arithmetic operators to a collection
of tuples and produce a calculated result
that equals this expected result and in this
example one possible formula to discover after
going through a number of generations is this
formula in postfix notations and it would
be written this way in regular notations this
is the prebuilt application so if i go ahead
and run it we went from generation 0 to generation
5 and we ended up discovering this formula
in postfix notations and it would be written
this way in regular notations and in each
generation we have a population of 6 groups
of expressions and each group of expressions
has 3 expressions and for this group we have
a fitness of 2 since the calculated result
is equal to the expected result for those
2 expressions and it's different here and
we have the groups sorted by fitness and this
represents this it's a table representation
of this group of expressions so we have those
2 expressions have the expected result is
equal to the calculated result and here we
have all the expected results are equal to
the calculated results next i went ahead and
created a new project and it has a Driver
class with a main method and this class will
be driving the application and an Expression
class and a Group class representing a group
of expressions and a Population class representing
a population of groups of expressions and
a GeneticAlgorithm class and this is where
we will have the logic for selection crossover
mutation and elitism now in the Driver class
let's define the training data as a two dimensional
array String array so we have the operands
and the expected result and we here have 3
expressions and we will define the operators
that can be used in this application here
in the Expression class so we can use addition
subtraction multiplication division and modulo
operators and the operands in the expressions
can have one or more digits so let me define
the numbOfDigits and we will be able to pick
that up using the StringTokenizer and than
nextToken and the length so we end up picking
up this for now so we end up with 1 but it
is possible that it would be 2 digits or more
and an expression could be a number meaning
it would be a leaf expression so i will have
this boolean isLeaf or it could be made up
of a left expression a right expression and
an operator and an expression or a class an
instance of the Expression class will have
an expected result a calculated result and
a String postfix expression and a number of
operands and that would be in this case six
operands since we have 6 here and here and
here and we will have get methods for the
expected result the calculated result and
the postfix expression and we will use the
number of operands and the number of digits
so number of digits from here and number of
operands in order to find out what the operator
index is and what the crossover point is and
i will have set methods for the expected result
and the calculated result and in case we have
a leaf expression meaning a number expression
we will have this constructor that takes in
that number as a String and we will set the
isLeaf flag to true and the postfix expression
would be that leaf expression and we will
have a calculateResult method that would calculate
the result and set the calculatedResult here
so let's define that method here 
so for now it's returning 0 and i'll be adding
the logic later on and in case it is a non-leaf
expression we will be using this constructor
that takes in a right expression a left expression
and an operator as a String and for convenience
a number of operands and we set those here
and for the postfix expression that would
be the left expression String value with a
space plus the right expression String value
with a space and than the operator and same
as here the calculatedResult would be what's
returned from the calculateResult method and
the toString method would return the String
value of the postfix expression and i need
two methods that would return the postfix
formula and the regular formula that would
be discovered so we will have those 2 methods
getPostfixFormula and getRegularFormula so
in both those two methods we will use space
to pickup the tokens in the posfix expression
and i will use a while loop to go through
the tokens and if that token is an arithmetic
operator than i will append that token that
operator plus space to the StringBuffer that
we defined over here otherwise we will append
'x' and an increasing value of the 'i' counter
plus space so it would be x1 x2 x3 etc. and
we endup returning the String value of that
StringBuffer and for the regular formula we
do the same thing we go through the different
tokens with a while loop and if that token
is an arithmetic operator than we do stack.pop
and put it in this right and another stack.pop
put it in left and than we push onto the stack
parentheses opening and closing and than the
String value of left and than the token and
space and the String value of right otherwise
we will push onto the stack x1 x2 x3 etc.
and what would be returned as a regular formula
would be the value of stack.pop and finally
let's go ahead and put the logic in the calculateResult
method so if we have a leaf than the Double
that is returned so the returnValue would
be the postfix expression so that number and
we return that number other wise if we have
a leaf than the value of leftCalculatedResult
would be leftExpression.calculateResult and
the value for rightCalculatedResult would
be rightExpression.calculateResult and here
we do an if else statements going through
and if we have addition as an arithmetic operator
than we perform that addition and we put that
value in returnValue and same thing for subtraction
multiplication division and modulo operator
so this should do it for this class next the
Group class will have an ArrayList of Expressions
and a get method that returns that ArrayList
and a Group will have a fitness and a getFitness
method that goes through all the Expressions
in the ArrayList and for each Expression where
the expected result is equal to the calculated
result it adds 1 to the fitness and it ends
up returning that fitness and we will have
a generateExpression method that takes a String
expression and an expected result as a String
and a number of operands for convenience and
returns an Expression instance so we will
use the space to pickup all the tokens in
the passed in String Expression and here we
are going through those tokens and if the
token is an arithmetic operator than we handle
it here otherwise if it is a number we simply
push onto the stack so we defined Stack here
of Expression so we push onto the stack a
new Expression passing in the token the number
as a String so if it is an arithmetic operator
than we do stack.pop and that would be the
left expression and we do another stack.pop
and that would be the right expression and
than we push onto the stack a new expression
passing in the left expression the right expression
the token and for convenience the number of
operands and here we define the return expression
so that return expression would be what's
returned from stack.pop and we set the exprected
result and the calculated result for that
return expression and the calculated result
is set by calling the calculateResult method
on that expression returnExpression and we
endup returning that returnExpression so this
should do it for this class next let's go
to the GeneticAlgorithm class and define some
constants so the mutation rate the crossover
rate and we'll set them to 0.5 for the purpose
of this tutorial and the tournament selection
size will be 2 2 groups and the population
size so we would have 6 groups in a population
and the number of elite groups will be 1 now
going to the population class we'll have an
ArrayList of groups and the size of that ArrayList
will be the population size and we'll have
a get method that returns those groups and
a sortByFitness method that sort the groups
in this ArrayList by fitness it is using JAVA
8 and we will need to randomly generate operators
given the number of operands so we will have
this method generateRandomOperators so the
number of operators will be the number of
operands minus 1 and we generate this number
of operators so we use Math.random and Expression.OPERATORS.size
so we're using this ArrayList to pickup randomly
operators from here and finally we will have
3 different constructors a default constructor
and one that takes in another instance of
Population and use that instance to construct
and return a new instance of Population and
a 3rd one that takes in the population size
and the number of operands and goes through
each one of the training data adding the operators
to those operands so this is what we are doing
here so we do group.generateExpression which
would return an Expression instance and we
add it to this group that we instantiate here
and we endup adding that group to this groups
ArrayList
and this would be the operands space and than
the operator and this is the expected result
from here 
and for convenience the number of operands
so this should do it for this class next let's
go to the GeneticAlgorithm class and since
this would be very similar to the previous
tutorial i am going to go very fast on this
class so we'll have 
a crossoverGroup method and a mutateGroup
method so this one performs crossover on a
Group so we're passing group1 and group2 and
for convenience the number of operands and
it performs crossover on those 2 groups and
return a crossoverGroup and for mutateGroup
we are passing it a group and for convenience
a number or operands and it does mutation
on that group and returns it as a mutated
group and we will be using tournament selection
so this is the method that does that we are
passing it the full population and for convenience
the number of operands and it instantiates
a tournament population and returns that randomly
selected tournament population after sorting
it by fitness and in this case we'll have
2 groups in this population since the tournament
selection size is 2 now those 2 methods the
crossoverGroup and the mutateGroup methods
will endup being called by crossoverPopulation
and by mutatePopulation and the crossoverPopulation
makjes calls to the selectTournamentPopulation
two times in order to pickup group1 and group2
that are used when we call crossoverGroup
and finally we will have this public evolve
method that takes in a population and for
convenience a number of operands and ends
up calling the crossoverPopulation and what's
returned from that it calls mutatePopulation
on it and ends up returning that population
so this should do it for this class next
let's go to the Driver class and let's define
a lower calculated result bound and an upper
calculated result bound and we will have this
method that prints the heading for each generation
and three formating methods so formatStringExpression
and formatExpectedResult and one for formating
the calculated result and another print method
so printPopulation and this will print the
population below the heading per generation
and we will have a print goup of expressions
as a table 
and now going to main
i will instantiate a new Driver and pickup
number of operands and instantiate a new population
so this would be generation 0 and i will print
that generation and print the group of expressions
as a table so the fittest group in that generation
we print it as a table and next let me instantiate
a GeneticAlgorithm and i'll go through that
population checking the fitness of the fittest
group in that generation and if not all the
expected results are equal to the training
results than we keep doing the while loop
and here we will evolve the population and
than print that population and print the group
of expressions as a table of the fittest group
in that generation and when all the expected
results are equal to the calculated results
in the fittest group in a generation than
we exit that while loop and find out what
the formula that we discovered in postfix
notation and as a regular notation formula
so this should do it next let's go ahead and
test run this application so we just did two
generations and we discovered this formula
in postfix notation and written this way in
regular notation so here all the expected
results and calculated results are equal
and we have all the groups sorted by fitness
in each generation so this group has the highest
fitness and let's go ahead and try it with
a different training data so here we will
have 4 different expressions we went to generation
35 and from generation 0 we have fitness of
4 so all the expected results are equal to
the calculated results and we discovered this
formula in postfix notation written this way
in regular notation and let's also try one
with here we have 1 digit for each one of
the operands so let's have 3 digits for each
one of the operands and see if it can handle
that
so here we go so we discovered this formula
written this way in regular notation and all
the expected results are equal to the calculated
results here we went to generation 9 from
generation 0
