next up previous
Next: Generating an initial Up: Implementation in C Previous: Implementation in C

The basic tree structure

The following two structures make up the core of the genetic programming structures:

typedef struct _tt {
  node root;
  double fitness;
  int nodecount;
} treetype;

typedef treetype *population;

typedef struct _nt {
    struct _nt *left;
    struct _nt *right;
    struct _nt *parent;
    rtype *pvalue;
    rtype value;
    int func; 
} nodetype;

typedef nodetype *node;

The population type defines a pointer to a structure of values, which holds a pointer to the root of the ``program tree,'' the fitness of this program (evaluated after each generation), and the number of nodes in the program tree. The node type defines a set of pointers which make tranversal, positioning, and evaulation much easier.

The nodetype structure defines a binary (and possibly unary) tree, with pointers to the left and right children, and also to the parent node. For the root node, the *parent pointer is set to NULL. For all terminals, the *left and *right pointers are set to NULL.

The func variable indicates to the program what function is to performed on the children. The number refers to an index to a generalized function which is catered to the problem to be solved, and basically contains a switch statement to index the correct function.