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); }
public void setRandFunction() { functionlIdentity = individual.getRandomFunction(); if (functionlIdentity == null) { functionlIdentity = allowedFunctionList[rnd.Next(0, allowedFunctionList.Length)]; } }
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); }