예제 #1
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);
        }
예제 #2
0
        public Individual mutate()
        {
            /*
             * randomly choose a node and swap it with randomly generated tree with max depth of 3
             * (not a full tree)
             */
            int        randNum;
            Individual mutatedIndividual = new Individual(this);
            const int  MIN            = 2;
            Node       mutateNode     = null;
            Node       mutateNodeSwap = null;

            randNum    = rnd.Next(this.getStrategyRoot().countNodes() - MIN + 1) + MIN;
            mutateNode = mutatedIndividual.getStrategyRoot().getNode(mutatedIndividual.getStrategyRoot(), randNum - 1);

            if (mutateNode.GetType() == typeof(Terminal))
            {
                // if the selected node is a terminal,
                // set it to another random terminal
                ((Terminal)mutateNode).setRandTerminal();
            }
            else
            {
                // if the selected node is a function,
                // generate a random tree (not full) of max depth of 3 and swap the nodes

                // need to research other sizes of random generated mutation tree
                mutateNodeSwap = Node.generateFullTree(3, board, this);
                mutatedIndividual.getStrategyRoot().swapNodes(mutateNode, mutateNodeSwap);
            }
            return(mutatedIndividual);
        }
예제 #3
0
 // Generate a random tree
 public bool growRandomTree(int maxDepth)
 {
     if (root != null)
     {
         return(false);
     }
     root = Node.generateFullTree(maxDepth, board, individual);
     root.setIsRoot(true);
     return(true);
 }