/// <summary> /// Population Initialization method following the "PTC1" method described in "Sean Luke. Two fast tree-creation algorithms for genetic programming. IEEE Transactions in Evolutionary Computation, 4(3), 2000b." /// </summary> /// <param name="parent_node">The node for which the child nodes are generated in this method</param> /// <param name="p">expected probability</param> /// <param name="allowableDepth">The maximum tree depth</param> private void PTC1(TGPNode parent_node, double p, int allowableDepth) { int child_count = parent_node.Arity; for (int i = 0; i != child_count; i++) { TGPPrimitive data = null; if (allowableDepth == 0) { data = FindRandomTerminal(); } else if (DistributionModel.GetUniform() <= p) { data = mOperatorSet.FindRandomOperator(); } else { data = FindRandomTerminal(); } TGPNode child = parent_node.CreateChild(data); if (!data.IsTerminal) { PTC1(child, p, allowableDepth - 1); } } }
/// <summary> /// Population Initialization Method described in "Kumar Chellapilla. Evolving computer programs without subtree crossover. IEEE Transactions on Evolutionary Computation, 1(3):209–216, September 1997." /// </summary> /// <param name="parent_node"></param> /// <param name="s"></param> /// <param name="?"></param> private void RandomBranch(TGPNode parent_node, int s) { int child_count = parent_node.Arity; for (int i = 0; i != child_count; i++) { TGPOperator non_terminal = FindRandomOperatorWithArityLessThan(s); if (non_terminal == null) { TGPNode child = parent_node.CreateChild(FindRandomTerminal()); } else { TGPNode child = parent_node.CreateChild(non_terminal); int b_n = non_terminal.Arity; int s_pi = (int)System.Math.Floor((double)s / b_n); RandomBranch(child, s_pi); } } }
/// <summary> /// Method that creates a subtree of maximum depth /// </summary> /// <param name="pRoot">The root node of the subtree</param> /// <param name="allowableDepth">The maximum depth</param> /// <param name="method">The method used to build the subtree</param> public void CreateWithDepth(TGPNode pRoot, int allowableDepth, string method) { int child_count = pRoot.Arity; for (int i = 0; i != child_count; ++i) { TGPPrimitive primitive = FindRandomPrimitive(allowableDepth, method); TGPNode child = pRoot.CreateChild(primitive); if (!primitive.IsTerminal) { CreateWithDepth(child, allowableDepth - 1, method); } } }