public TGPNode CreateChild(TGPPrimitive data) { TGPNode node = new TGPNode(data, this); mChildNodes.Add(node); return(node); }
public void Swap(TGPNode point2) { TGPNode parent1 = this.mParent; TGPNode parent2 = point2.mParent; if (parent1 == null || parent2 == null) { TGPPrimitive content1 = this.mData; TGPPrimitive content2 = point2.mData; this.mData = content2; point2.mData = content1; List <TGPNode> children1 = this.mChildNodes.ToList(); List <TGPNode> children2 = point2.mChildNodes.ToList(); this.RemoveAllChildren(); point2.RemoveAllChildren(); for (int i = 0; i < children1.Count; ++i) { point2.mChildNodes.Add(children1[i]); children1[i].Parent = point2; } for (int i = 0; i < children2.Count; ++i) { this.mChildNodes.Add(children2[i]); children2[i].Parent = this; } } else { int child_index1 = parent1.IndexOfChild(this); int child_index2 = parent2.IndexOfChild(point2); parent1.ReplaceChildAt(child_index1, point2); parent2.ReplaceChildAt(child_index2, this); } }
/// <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> /// Method that creates a GP tree with a maximum tree depth /// </summary> /// <param name="allowableDepth">The maximum tree depth</param> /// <param name="method">The name of the method used to create the GP tree</param> /// <param name="tag">The additional information used to create the GP tree if any</param> public void CreateWithDepth(int allowableDepth, string method, object tag = null) { // Population Initialization method following the "RandomBranch" method described in "Kumar Chellapilla. Evolving computer programs without subtree crossover. IEEE Transactions on Evolutionary Computation, 1(3):209–216, September 1997." if (method == INITIALIZATION_METHOD_RANDOMBRANCH) { int s = allowableDepth; //tree size TGPOperator non_terminal = FindRandomOperatorWithArityLessThan(s); if (non_terminal == null) { mRootNode = new TGPNode(FindRandomTerminal()); } else { mRootNode = new TGPNode(non_terminal); int b_n = non_terminal.Arity; s = (int)System.Math.Floor((double)s / b_n); RandomBranch(mRootNode, s); } CalcLength(); CalcDepth(); } // 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." else if (method == INITIALIZATION_METHOD_PTC1) { int expectedTreeSize = Convert.ToInt32(tag); int b_n_sum = 0; for (int i = 0; i < mOperatorSet.OperatorCount; ++i) { b_n_sum += mOperatorSet.FindOperatorByIndex(i).Arity; } double p = (1 - 1.0 / expectedTreeSize) / ((double)b_n_sum / mOperatorSet.OperatorCount); TGPPrimitive data = null; if (DistributionModel.GetUniform() <= p) { data = mOperatorSet.FindRandomOperator(); } else { data = FindRandomTerminal(); } mRootNode = new TGPNode(data); PTC1(mRootNode, p, allowableDepth - 1); CalcLength(); CalcDepth(); } else // handle full and grow method { mRootNode = new TGPNode(FindRandomPrimitive(allowableDepth, method)); CreateWithDepth(mRootNode, allowableDepth - 1, method); CalcLength(); CalcDepth(); } }
public void AddPrimitive(TGPPrimitive primitive) { mPrimitiveIdentifiers[primitive.Symbol] = mPrimitives.Count; mPrimitives.Add(primitive.Symbol); if (IsVariable(primitive)) { mVariableIdentifiers[primitive.Symbol] = mVariables.Count; mVariables.Add(primitive.Symbol); } }
public bool IsVariable(TGPPrimitive primitive) { if (primitive.IsTerminal) { TGPTerminal terminal = primitive as TGPTerminal; if (!terminal.IsConstant) { return(true); } } return(false); }
/// <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); } } }
public TGPNode(TGPPrimitive data, TGPNode parent = null) { mData = data; mParent = parent; }