GeneLib
Still under construction
Expression
Expression stores symbolic expressions as executable trees.
constructors
Expression( void ) NULL expression
Expression( NString &, opBag &) Expression("(plus 5 5)",OpsBag );
Expression( const Expression & ) Expression( OtherExpr );
Expression( istream &, opBag & ) Expression( infile , OpsBag );
operations
Difference expr3 = expr1.Difference(expr2)
returns the first occurence of a difference in the structure of the
expression tree. so if expr1 = ( plus ( time 4 4 ) 3 ) and
expr2 = ( plus ( divide 4 4 ) 3) , expr3= (divide 4 4)
Equivalent expr1.Equivalent( expr2 ) -> expr1 == expr2
returns true if both trees are structurally true.
( plus (times 4 4 ) 3 ) Equivalent ( plus 16 3 ) is False
Evalutate val = expr.Evaluate( args )
Evaluate expresses or runs the expression tree. This will return a value .
( plus ( times 4 4 ) 3) = 19. 2222323
ins & outs
print print( ostream & );
print takes either cout or any out going stream and dumps the tree into it.
printFormatted printFormatted( ostream & );
same as print but will 'pretty' print the output. ( plus ( divide 4 4 ) 3) will
become
(plus
( divide 4 4 )
3 )
)
this makes it easier to read when we are looking at large expressions. The
stream constructor will take either the formatted or straight file to build
a symbol tree.
NewExpr NewExpr( NString & );
Destroys the symbol tree, and creates a new one from the string given, using
the internally stored function set.
readFile readFile( file , opBag )
There are two forms of this method, one is given the file by name the other by
stream. In anycase the method needs a new opBag.
length
returns the number of nodes the symbol tree contains.
Gene is derived from Expression, it adds simple treemanipulation routines used to mutate
the symbol tree. Gene is also a friend of Marriage.
constructors
Gene constructors are the same as Expression constructors except for:
Gene( Expression & ) this is to upgrade an expression to a Gene.
Gene( Gene &) copy constructor.
Genetic methods
mutate
Mutate return a new Gene. The new gene has had some alteration
made to the symbol tree.
Gene *newGene = oldGene.mutate();
The new Gene inherits the old Gene's function set.
changeMutationChance, OneMutationAtATime,MoreThanOneMutationAtATime.
The mutation chance is the chance that some change will occure at some
node in the symbol tree of the copied tree during mutation. The default
value 0.01 at the top level. OneMutationAtATime indicates that once a
mutation has occured the mutator quits; this is the default action.
MoreThanOneMutationAtATime means that many mutations can occur,
potentially radically changing the original symbol tree.
Marriage is a strange class used to do crossovers between two Genes.
The kids are created by taking random splices from parents and swapping them.
one: two:
parents:
O T
/ \ <- splice here -> /\
O O T T
kids:
O T
/ \ /\
O T <- -> O T
constructor
Marriage( Gene * ,Gene *, numChilds)
The Marriage constructor takes pointers from both parents and the number of
children the marriage is expected to return. The number of children should be a
multiple of two since the crossover process creates two children automatically.
Mutation
Used to set flag whether to mutate children or not.
MakeChildren
Creates the children as specified in the constructor.
GetChildren GetChildren( Gene *** )
One must pass the address of an array of pointers to Genes to get the children
from the marriage.
print
Example:
Gene * papa,*mama;
Gene **kids[ NUM_KIDS ];
...
Marriage wedding( papa ,mama , NUM_KIDS );
wedding.MakeChildren();
wedding.GetChildren( & kids );
playWithKids( kids );
...
Function is a type that holds a user defined function.
opBag holds the collection of functions used by Expression.
constructor
opBag( opBag & ) There is only a copy constructor
messages
Exprs_sys_data is a structure that is passed as an argument to evaluate
an expression. This structure can be derived from to specialize the arguments according
to the functions contained in the Expression.