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 we want to use genetic algorithms
in order to discover a formula that applies
the &, |, and ^ logical operators to a combination
of Boolean events and in this case we have
five different true/false events and we want
to produce a calculated result that equals
this expected result so for example one possible
formula to discover here after going through
a number of generations is this formula written
in postfix notations and written in regular
notation it would be this formula and we have
those 3 truth tables that we are using so
for x & y than if eiher or both x and y are
0 than we would get 0 otherwise we would get
1 and if either or both x or y are 1 than
we would get 1 otherwise we would get 0 and
for x ^ y if x and y are the same than we
would get 0 and if they are different than
we would get 1 this is a prebuilt version
of the application so if i go ahead and run
it we went from generation 0 to generation
3 and we discovered this formula in postfix
notations or in regular notations it would
be written this way so going back to generation
0 we have in each generation a population
of 6 group of expressions so we have in each
group we have 4 different expressions so what
we're displaying is the expression and the
expected result and the calculated result
so we have a fitness of 3 for this group since
the expected result is equal to the calculated
result in 3 of the expressions and it is different
in this expression so i am displaying this
row or this group in table format here and
the groups in each generation are sorted by
fitness so this one has the highest fitness
and as we can see here the expected result
is different than the calculated result for
this expression and we go down here to this
table where all the expected result and calculated
result are the same for each expression next
i'll start by creating a new project and this
is gonna be running on Java 8 and here we
will have an expression class and it will
be in this package and we will also have 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 class
will evolve the population from one generation
to the next using selection crossover mutation
and elitism and lastly we will have a Driver
class with a main method which will be driving
this application now this is the training
data that i will be initially using now going
to the expression class an expression could
be a leaf meaning it is either 0 or 1 or it
could be made up of a left expression a right
expression and an operator and let me define
the operators that we will be using so 'and'
'or' and 'exclusive or'
and an expression will have an expected result
a calculated result and a String postfix expression
and a number of operands so for example here
we have five operands
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
we have here in order to get the operator
index and the crossover point 
and we will have 2 set methods for the expected
result and the calculated result and in case
we have a 0 or 1 expression we will set the
isLeaf flag to true and the postfixExpression
would be the String value of 0 or 1 and the
calculatedResult would be what's returned
from this method that i'll be defining shortly
and let's return 0 for now and in case we
don't have a leaf expression we will be using
this constructor that will be taking a right
expression and a left expression and a String
operator and a number of operands and we will
be using those to set the number of operands
the operator and the left expression the right
expression and for the postfix expression
it would be the left expression String value
space plus the right expression String value
plus space plus the operator and the calculated
result would be what's returned from calculateResult
and we need a toString method that will be
returning the String postfix expression and
finally we need 2 methods one that would return
the postfix formula let's return null for
now and the other will be returning the regular
formula that we discovered
now for calculateResult let's define this
return value and return it and two boolean
flags a leftFlag and a rightFlag and if the
expression is not a leaf expression we will
handle it here otherwise the return value
will be either 0 or 1 now going back here
if the left expression.calculateResult returns
1 than the leftFlaf will be set to true otherwise
it is false and if the rightExpression.calculateResult
returns 1 than the rightFlag will be set to
true and also otherwise it is false and if
the operator that is that we set over here
is equal to '&' and the leftFlag is true and
the rightFlag is true than the return value
would equal to 1 otherwise if the operator
is equal to '|' and the leftFlag is true or
the rightFlag is true than we also return
1 otherwise the return value in both cases
is 0 and also if the operator is equal to
'^' and the leftFlag is different than the
rightFlag than we return 1 also otherwise
we return 0 now for the getPostfixFormula
method this is the code that would return
that formula so we will use empty spaces in
order to pickup the different tokens in the
postfix expression and we will go through
those tokens and if we have '&' '|' or '^' than
we append that token plus a space to the stringBuffer
that we defined over here otherwise we append
instead of 0 or 1 we append 'e' and we append
0 1 or 2 3 or 4 in this case and we have 5
different events that we are using true false
events and in the case of a regular formula
of wanting to pickup the regular formula in
this method so this code would be doing that
so we do the same thing we pickup the token
based on the space in the postfix expression
and while we have more tokens also here we
define this Stack made up of Strings so if
token is equal to '&' or it's '|' or '^' than
we pop from the stack and put it in right
and do a stack.pop put it in left and than
push into the stack parentheses in between
there we have the left than the token space
and than the token and than space and right
and if we have 0 or 1 than we push onto the
stack 'e' and 0 1 etc. and we return stack.pop
so this should do it for this class
next the group class will have an ArrayList
of Expressions and a get method for this ArrayList
and 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 returns that fitness
and we will have a generateExpression method
that takes in a postfixStringExpression and
an ExpectedResult and a number of operands
and returns an instance of Expression so this
will be the return expression that we endup
returning and we define a Stack of Expressions
and we will use the space to pickup the different
tokens in that String expression and we'll
define a left Expression anda right Expression
and while we have tokens we'll check if that
token is '&' or '|' or '^' and in that case
the left Expression will be the valueof stack.pop
an the right Expression willbe the value of
stack.pop also and we will push onto the stack
a new Expression and we instantiatethat expression
with the left Expression the right Expression
the token and the number of operands otherwise
if we have a number than we push onto the
stack a new Expression with the valueof that
number and the return Expression willbe the
value of stack.pop and we will set the expected
result for that return expression and the
calculated result by calling returnExpression.calculateResult
so this should do it for this class next let's
go to the GeneticAlgorithm class and define
those constants the mutation rate we'll set
it to this value for the purpose of this tutorial
same thing for the crossover rate and the
tournament selection size and the population
size here it is 6 and the number of elite
groups for the purpose of this tutorial we'll
just not have any elite group 
so the Population will have an ArrayList of
Groups and the size of this ArrayList will
be the population size and we'll have a get
method that returns that ArrayList and we'll
have this java 8 method for sorting the groups
in the ArrayList of groups so sortByFitness
it does it this way and we need to randomly
generate operators given the number of operands
so we'll have this method that does that so
generateRandomOperators it takes in the number
of operands and since here we have 5 different
operands it means we gonna pickup 4 operators
randomly and we do it from here so we select
them from this ArrayList 
and we append them to this StringBuffer and
return a String containing those operators
with space in between and finally we'll have
3 different constructors so a default constructor
and one that takes in an instance of population
and returns a new instance and the third one
takes in a population size and a number of
operands and generate random expressions for
each group of expressions and adds the operators
to the operand foreach expression in the group
and this willbe the expected result coming
from here
and the number of operands so this should
do it for this class next let's go to the
GeneticAlgorithm class
and we'll have this crossoverPopulation method
that takes in the population and for convenience
number of operands and does a crossover for
all groups in the population and we'll have
another method that does mutation to the passed
in population and returns a mutated population
and we'll have this evolve method that evolves
the population by calling crossoverPopulation
and what's returned from that it calls mutatePopulation
on it and we will be using tournament selection
for crossover sowe'll have this selectTournamentPopulation
that takes in a population and for convenience
number of operands and return the selected
tournament population in this case it will
contain 2 2 groups and crossoverPopulation
will endup calling this crossoverGroup method
passing in two groups to do crossover on and
for convenience the number of operands and
mutatePopulation will endup calling this mutateGroup
method for each group in the population now
here we will define this crossoverPopulation
and return it but before doing that wewill
move the elite groups as is to the new population
to be returned the crossoverPopulation as
for the other groups in this passed in population
we will call selectTournamentPopulation and
get the fittest one from that population and
this would be group1 than call it again and
get the fittest one and this would be group2
and than we would do crossoverPopulation.getGroups()
and we will set the crossover of group1 and
group2 and we will repeat for all remaining
groups in this population meaning that are
not elite groups now for mutatePopulation
i'll define this mutatePopulation and return
it but before doing that we will move the
elite groups as is to the new population the
mutatePopulation as for the remaining groups
we will endup calling mutateGroup on each
one of those
now for selectTournamentPopulation 
we will randomly pickup groups in the passed
in population in this case 2 groups and put
them in this tournament population that we
define and sort them by fitness and return
that tournament population now for mutateGroup
we will be returning the passed in group after
mutating it and we will only perform mutation
if Math.random is smaller than the mutation
rate and here we will randomly pickup an operator
to be mutated and this will define it's position
and this will be the original operator and
i will make sure that the replacement operator
is different than the original operator so
now we have a new operator that we'll use
for replacement and we will go through all
expressions in this group and we will define
a StringBuffer and append the new operator
in the postfix expression in place of the
original operator and we will have new expressions
in place of the old ones accounting to the
new operator and this will be the mutated
group that we will endup returning now for
crossoverGroup let me define this group instance
and return it and this instance will contain
the crossover of group1 and group2 if it is
performed and let's pickup a random number
between 0 and 1 and for all expressions in
group1 we pickup one of the expressions and
we define this StringBuffer instance and we
will not perform crossover in case the crossover
rate is smaller or equal to the random number
that we picked up over here 
so we willappend the postfix expression as
is to this StringBuffer and we will add it
to the crossoverGroup and endup returning
this crossoverGroup which would be actually
group1 unchanged so if this is not the case
than we will perform crossover so here we
will pickup all the operands for this expression
and we need to add to it the operators and
we do it here and we add those operators from
group1 until this crossover point and from
that crossover point we add expression from
group2 or operators from group2 and we endup
returning the crossoverGroup and this should
do it for this class next let me go to the
Driver class and let me first add the methods
that do the printing so i have this printHeading
method that prints the
heading for each population and i have this
printPopulation method that prints the population
below the heading for a generation 
and this method that prints the groupof expressions
as a table and passit the group and going
back to main i'll first instantiate a new
Driver instance and pickup the number of operands
so we have 5 operands here and i picked the
[0][0] so we endup here which would be the
same for each one since we have to have the
same number of operands and i use the space
to separate them in order to be able to call
countTokens and count those numbers and i'll
pickupa new population instance pass it the
population size which is 6 and this number
of operands and i will instantiate a new GeneticAlgorithm
and start from generationNumber 0 and this
will print the heading the population and
the group of expressions as table for generation
0 and this code will go through the remaining
generations and print the heading the population
and the group of expressions for each generation
and i will first evolve the population 
and i will finally print the discovered formula
in regular notation and in postfix notation
and this should do it for this class next
let me go ahead and test run the application
so actually generation 0 did it so let's do
it again we went from generation 0 to generation
14 and we ended up discovering this formula
in postfix notation or written this way in
regular notation so for each one here the
expected result is equal to the calculated
result and this table represents this row
and it has 4 different expressions so we're
displaying the expression the expected result
and the calculated result finally let's try
with a different training data so we went
from generation 0 to generation 10 and we
discovered this formula or written this way
in postfix notation
