Code Page
Real Parse Trees

People

Dan Ashlock
Ron Nelson

Contents


Code links(top)


Parse tree language(top)

The parse trees that we are using are pointers to a node structure that can itself contain subtrees (additional pointers to other nodes). This is implemented with the following structure:
struct node {

  int type;       //integer type
  double val;     //value, mostly used for ephemeral constants
  node *args[3];  //possible arguments, at most 3


};
The type field contains an integer type code, the val field holds the value if the node is an ephemeral constant, and the args vector holds pointer to up to three arguments.

Subject to the limit that there must be at least on operation of each arity, operations can be turned on and off (see: Operation controls). The available node types are:

NameArityDescription
Neg1 Arithmetic negation.
Com1 Complement of input: (Com x) is 1-x.
Del1 Delay operator. When called this operator reports the contents of its value field, storing its current input in the value field. In effect it delays values for one evaluation. The routine InitDelay(t) can be used to set all delay value fields to zero before use.
Sin1 Sine of input.
Cos1 Cosine of input.
Atn1 Artangent of input.
Sqt1 Square root of input, save that the operation is transparent to negative numbers. So (Sqt 4) is 2 and (Sqr -4) is -2.
UA11 Unary ADF number 1.
UA21 Unary ADF number 2.
Add2 Addition.
Sub2 Subtraction.
Mul2 Multiplication.
Div2 Division, save that overflows are mapped to 0.
Max2 Maximum of arguments.
Min2 Minimum of arguments.
Eql2 Equality predicate, returns 1 if true, 0 if false.
Gtr2 Greater than predicate, returns 1 if true, 0 if false.
Les2 Less than predicate, returns 1 if true, 0 if false.
Geq2 Greater than or equal to predicate, returns 1 if true, 0 if false.
Leq2 Less than or equal to predicate, returns 1 if true, 0 if false.
Neq2 Inequality predicate, returns 1 if true, 0 if false.
BA12 Binary ADF 1.
BA22 Binary ADF 2.
Hil2 Hill-shaped function.
Trx2 X-direction trench-shaped function.
Try2 Y-direction trench-shaped function.
ITE3 If then else: if first argument is non-negative, return the second argument, otherwise return the third.

Command Reference(top)

Environment Controls (Up)

Defaults

The defualt number of variables is 1 and the default constant range is -1 to 1.

Operation Controls (Up)

TurnOn and TurnOff either enable or disable the use of a particular operation. The operation codes they currently accept are given below as named numric constants defined in rtree.h. It is the users responsibility to leave at least one operation of each possible arity active in the current version of the code.

NegOP : Unary minus DivOP : Division
ComOP : Complementation MaxOP : Maximum
DelOP : Delay line MinOP : Minimum
SinOP : Sine EqlOP : Equals?
CosOP : Cosine GtrOP : Greeater than?
AtnOP : Arctangent LesOP : Less Than?
SqtOP : Square root GeqOP : Greater/equal?
UA1OP : Unary ADF 1 LeqOP : Less/equal?
UA2OP : Unary ADF 2 NeqOP : Not equal?
AddOP : Addition BA1OP : Binary ADF 1
SubOP : Subtraction BA2OP : Binary ADF 2
MulOP : Multiplication HilOP : Hill
TrxOP : X-trench TryOP : Y-trench
ITEOP: If-then-else

Example of Usage

//Turn off all the trig operations
   TurnOff(SinOP);
   TurnOff(CosOP);
   TurnOff(AtnOP);

Defaults

All operations are turned on by default. Operations that require special code if they are not to cause crashed are delay (del) and the four ADF operations, UA1, UA2, BA1, and BA2.

Input and Output (Up)

At present the only I/O operations are overloading of the stream insertion operator << to perform output of the oarse trees in LISP-like notation.

Tree creation, deletion, and handling (Up)

Example of Usage

//Allocate a tree, print out its size, print out a random sub-node
//copy the tree, and then give pack the storage

node *t,*s;  //tree type is pointer to node

   t=RandTree(12);
   cout << t << endl << "has size:" << SizeTree(t) << "." << endl;
   cout << "One of the sub-trees is " << RandSubTree(t) << endl;
   s=CopyTree(t);
   cout << "A copy of t looks like " << s << endl;
   KillTree(s);
   KillTree(t);

Evaluation and reset routines (Up)

Example of Usage

//Create a parse tree with three variables and 
//one unary ADF and then evaluate it

node *t,*uADF1;            //parse tree and ADF

double v[3]={1.0,2.0,1.5}  //input variables, initialized for demo

   SetNumberOfVariables(3);  //three input variables

   TurnOff(DelOp);           //turn of the delay operation
   TurnOff(UA2Op);           //as well as the three
   TurnOff(BA1Op);           //types of ADFs
   TurnOff(BA2Op);           //we're not using in this demo

   t=RandTree(12);           //create a 12 node main tree
   TurnOff(UA1Op);           //turn off unary ADF to avoid calls in the ADF
   uADF1=RandTree(6);        //6 node ADF

   cout << t << endl << uADF1 << endl << " evaluate to "; 
   cout << EvalU1(t,uADF1,v) << endl;

   KillTree(t);
   KillTree(uADF1);

Genetic Opeartions (Up)