/*************************************************** Interface code for real parse tree routines. Predicting no change energy savings project. -Code by Dan Ashlock -Project Director Ron Nelson ***************************************************/ /* The basic data structure is the node. It is meant to be a node (terminal or operation) in a parse tree. The node contains the identity of the operation or terminal, a field to hold the current value of the node (if any), and pointers to other nodes. We adopt the convention that a nill pointer, if evaluated, returns a 0 but prints as a nil. TYPE ENCODING The type is a two byte integer where the upper byte holds the number of arguments the opeartion takes and the lower is an index number. As follows: Type Code Identity 0 Ephemeral constant 1 x1 (first terminal) 2 x2 (second terminal) ... ... 256 Negation (Unary Minus) 257 Complement (1-X) 258 Delay 259 Sin 260 Cos 261 Tan 262 Sqrt 512 + 513 - 514 * 515 / 516 max 517 min 518 = (equality) 519 > 520 < 521 >= 522 <= 523 <> 768 ITE (if first argument positive return second otherwise return third). */ #ifndef RTREE_H #define RTREE_H //Name constants for operation control #define NegOP 256 #define ComOP 257 #define DelOP 258 #define SinOP 259 #define CosOP 260 #define AtnOP 261 #define SqtOP 262 #define UA1OP 263 #define UA2OP 264 #define AddOP 512 #define SubOP 513 #define MulOP 514 #define DivOP 515 #define MaxOP 516 #define MinOP 517 #define EqlOP 518 #define GtrOP 519 #define LesOP 520 #define GeqOP 521 #define LeqOP 522 #define NeqOP 523 #define BA1OP 524 #define BA2OP 525 #define BelOP 526 #define TrxOP 527 #define TryOP 528 #define ITEOP 768 struct node { int type; //integer type double val; //value, mostly used for ephemeral constants node *args[3]; //possible arguments, at most 3 }; //Controls for terminals void SetNumberOfVariables(int n); void SetConstantRange(double min,double max); //Controls for Operations void TurnOn(int OpCode); void TurnOff(int OpCode); //Overload << to output parse trees extern ostream& operator <<(ostream&,node *); //Tree creation, delition, and handling node *RandTree(int n); //generate a random tree of size n void KillTree(node *t); //deallocate a tree node *CopyTree(node *t); //copy a tree int SizeTree(node *t); //compute the size of a tree node *SubTree(node *t,int &n); //find the nth subnode - destroys n node *RandSubTree(node*t); //find a random subnode //Evaluation routines void InitDelay(node *t); //zero out the delay operators double Eval(node *t,double *vars); //evaluate *t on vars[0..nvars-1] double EvalU1(node *t,node *u, //evaluate with one unary ADF double *vars); double EvalU2(node *t,node *u1, //evaluate with two unary ADFs node *u2,double *vars); double EvalB1(node *t,node *b, //evaluate with one unary ADF double *vars); double EvalB2(node *t,node *b1, //evaluate with two binary ADFs node *b2,double *vars); double EvalUB(node *t,node *u, //evaluate with one unary, node *b,double *vars); //one binary ADF double EvalUB2(node *t,node *u1, //evaluate with two unary, node *u2,node* b1, //two binary ADF node *b2,double *vars); //Genetic Operators void mutateST(node *t); //subtree mutation void mutateND(node *t); //node mutation void cross(node *t,node *s); //crossover void chop(node *t,int k); //chop down to size at most k #endif