Beispiel #1
0
 public void trim(int maxDepth, Individual individual)
 {
     /*
      * traverse all nodes and check depths
      * if reached maximum depth-1, check if the children of the current node (if there are any)
      * are functions, and if so, replace them with random terminals, otherwise, do nothing
      */
     for (int i = 0; i < this.getNumChildren(); i++)
     {
         if (maxDepth == 1)
         {
             // one level below is the maximal depth,
             // if the child is a function,
             // swap with a terminal, else, do nothing
             if (children[i] is Function)
             {
                 this.children[i] = new Terminal(board, individual.getRandomTerminal(), individual);
             }
         }
         else if (this.children[i] != null)
         {
             // if haven't reached the maximal depth, and the child is a function,
             // recursively call each child with the trim method
             this.children[i].trim(maxDepth - 1, individual);
         }
     }
 }
Beispiel #2
0
        public static Node generateFullTree(int maxDepth, Board board, Individual individual)
        {
            /*
             * generate a full grown tree
             */
            Node root;

            //If we are not at the max depth, choose a function
            if (maxDepth > 1)
            {
                // create new function
                root = new Function(false, board, individual.getRandomFunction(), individual);
                // create the proper amount of children (according to the function that was just created)
                root.children = new Node[root.getNumChildren()];
            }
            //Otherwise, choose a terminal
            else
            {
                // create new terminal
                root = new Terminal(board, individual.getRandomTerminal(), individual);
            }
            root.height = maxDepth;
            //Recursively assign child nodes
            for (int i = 0; i < root.getNumChildren(); i++)
            {
                root.children[i] = Node.generateFullTree(maxDepth - 1, board, individual);
            }
            // return the created root
            return(root);
        }
Beispiel #3
0
 public void setRandTerminal()
 {
     /*
      * set the terminal randomly by randomly selecting index from the terminalList
      * and assigning the terminalIdentity with it
      * ******************
      * if the terminal that was selected is RandVal, also assign a random value
      * randomly select whether the terminal refers to friend or enemy when evaluating
      * ******************
      * if the terminal that was selected is IsRandIndex, also assign a random index value
      * this terminal will return if the inspected index equals to the generated index value (1/0)
      * ******************
      * all terminals besides ..Rand.. returns amount of friendly/enemy pieces at various scenarios
      */
     terminalIdentity = individual.getRandomTerminal();
     if (terminalIdentity == null)
     {
         terminalIdentity = allowedTerminalList[rnd.Next(0, allowedTerminalList.Length)];
     }
     if (terminalIdentity.Equals("RandVal", StringComparison.OrdinalIgnoreCase))
     {
         value = rnd.Next(0, MAX_RAND_VALUE);
     }
     else
     {
         friendOrEnemy = rnd.Next(1, 2);
     }
 }
Beispiel #4
0
        public static Node generateTree(int maxDepth, Board board, Individual individual)
        {
            /*
             * works very similarly to generateFullTree
             * instead of ALWAYS creating a function if the maximum depth hasn't reached
             * randomly select either terminal or a function, causing some branches to stop developing
             */
            Node root;

            //If we are not at the max depth, choose a function
            if (maxDepth > 1)
            {
                Random rand = new Random();
                if (rnd.NextDouble() >= 0.5)
                {
                    root          = new Function(false, board, individual.getRandomFunction(), individual);
                    root.children = new Node[root.getNumChildren()];
                }
                else
                {
                    root = new Terminal(board, individual.getRandomTerminal(), individual);
                }
            }
            //Otherwise, choose a terminal
            else
            {
                root = new Terminal(board, individual.getRandomTerminal(), individual);
            }
            root.height = maxDepth;
            //Recursively assign child nodes
            for (int i = 0; i < root.getNumChildren(); i++)
            {
                root.children[i] = Node.generateTree(maxDepth - 1, board, individual);
            }
            return(root);
        }