- Timestamp:
- 03/31/11 19:20:29 (14 years ago)
- Location:
- trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Crossovers/SubtreeCrossover.cs
r5809 r5916 88 88 double internalCrossoverPointProbability, int maxTreeLength, int maxTreeDepth) { 89 89 // select a random crossover point in the first parent 90 ISymbolicExpressionTreeNodecrossoverPoint0;91 int replacedSubtreeIndex;92 SelectCrossoverPoint(random, parent0, internalCrossoverPointProbability, maxTreeLength, maxTreeDepth, out crossoverPoint0, out replacedSubtreeIndex); 93 90 CutPoint crossoverPoint0; 91 SelectCrossoverPoint(random, parent0, internalCrossoverPointProbability, maxTreeLength, maxTreeDepth, out crossoverPoint0); 92 93 int childLength = crossoverPoint0.Child != null ? crossoverPoint0.Child.GetLength() : 0; 94 94 // calculate the max length and depth that the inserted branch can have 95 int maxInsertedBranchLength = maxTreeLength - (parent0.Length - crossoverPoint0.GetSubtree(replacedSubtreeIndex).GetLength()); 96 int maxInsertedBranchDepth = maxTreeDepth - GetBranchLevel(parent0.Root, crossoverPoint0); 95 int maxInsertedBranchLength = maxTreeLength - (parent0.Length - childLength); 96 int maxInsertedBranchDepth = maxTreeDepth - GetBranchLevel(parent0.Root, crossoverPoint0.Parent); 97 97 98 98 99 List<ISymbolicExpressionTreeNode> allowedBranches = new List<ISymbolicExpressionTreeNode>(); … … 100 101 if (n.GetLength() <= maxInsertedBranchLength && 101 102 n.GetDepth() <= maxInsertedBranchDepth && 102 IsMatchingPointType(crossoverPoint0, replacedSubtreeIndex,n))103 IsMatchingPointType(crossoverPoint0, n)) 103 104 allowedBranches.Add(n); 104 105 }); 106 // empty branch 107 if (IsMatchingPointType(crossoverPoint0, null)) allowedBranches.Add(null); 105 108 106 109 if (allowedBranches.Count == 0) { … … 109 112 var selectedBranch = SelectRandomBranch(random, allowedBranches, internalCrossoverPointProbability); 110 113 111 // manipulate the tree of parent0 in place 112 // replace the branch in tree0 with the selected branch from tree1 113 crossoverPoint0.RemoveSubtree(replacedSubtreeIndex); 114 crossoverPoint0.InsertSubtree(replacedSubtreeIndex, selectedBranch); 114 if (crossoverPoint0.Child != null) { 115 // manipulate the tree of parent0 in place 116 // replace the branch in tree0 with the selected branch from tree1 117 crossoverPoint0.Parent.RemoveSubtree(crossoverPoint0.ChildIndex); 118 if (selectedBranch != null) { 119 crossoverPoint0.Parent.InsertSubtree(crossoverPoint0.ChildIndex, selectedBranch); 120 } 121 } else { 122 // child is null (additional child should be added under the parent) 123 if (selectedBranch != null) { 124 crossoverPoint0.Parent.AddSubtree(selectedBranch); 125 } 126 } 115 127 return parent0; 116 128 } 117 129 } 118 130 119 private static bool IsMatchingPointType(ISymbolicExpressionTreeNode parent, int replacedSubtreeIndex, ISymbolicExpressionTreeNode branch) { 120 // check syntax constraints of direct parent - child relation 121 if (!parent.Grammar.ContainsSymbol(branch.Symbol) || 122 !parent.Grammar.IsAllowedChildSymbol(parent.Symbol, branch.Symbol, replacedSubtreeIndex)) return false; 123 124 bool result = true; 125 // check point type for the whole branch 126 branch.ForEachNodePostfix((n) => { 127 result = 128 result && 129 parent.Grammar.ContainsSymbol(n.Symbol) && 130 n.SubtreesCount >= parent.Grammar.GetMinimumSubtreeCount(n.Symbol) && 131 n.SubtreesCount <= parent.Grammar.GetMaximumSubtreeCount(n.Symbol); 132 }); 133 return result; 134 } 135 136 private static void SelectCrossoverPoint(IRandom random, ISymbolicExpressionTree parent0, double internalNodeProbability, int maxBranchLength, int maxBranchDepth, out ISymbolicExpressionTreeNode crossoverPoint, out int subtreeIndex) { 131 private static bool IsMatchingPointType(CutPoint cutPoint, ISymbolicExpressionTreeNode newChild) { 132 var parent = cutPoint.Parent; 133 if (newChild == null) { 134 // make sure that one subtree can be removed and that only the last subtree is removed 135 return parent.Grammar.GetMinimumSubtreeCount(parent.Symbol) < parent.SubtreesCount && 136 cutPoint.ChildIndex == parent.SubtreesCount - 1; 137 } else { 138 // check syntax constraints of direct parent - child relation 139 if (!parent.Grammar.ContainsSymbol(newChild.Symbol) || 140 !parent.Grammar.IsAllowedChildSymbol(parent.Symbol, newChild.Symbol, cutPoint.ChildIndex)) return false; 141 142 bool result = true; 143 // check point type for the whole branch 144 newChild.ForEachNodePostfix((n) => { 145 result = 146 result && 147 parent.Grammar.ContainsSymbol(n.Symbol) && 148 n.SubtreesCount >= parent.Grammar.GetMinimumSubtreeCount(n.Symbol) && 149 n.SubtreesCount <= parent.Grammar.GetMaximumSubtreeCount(n.Symbol); 150 }); 151 return result; 152 } 153 } 154 155 private static void SelectCrossoverPoint(IRandom random, ISymbolicExpressionTree parent0, double internalNodeProbability, int maxBranchLength, int maxBranchDepth, out CutPoint crossoverPoint) { 137 156 if (internalNodeProbability < 0.0 || internalNodeProbability > 1.0) throw new ArgumentException("internalNodeProbability"); 138 157 List<CutPoint> internalCrossoverPoints = new List<CutPoint>(); … … 149 168 } 150 169 } 170 // add one additional extension point if the number of sub trees for the symbol is not full 171 if (n.SubtreesCount < n.Grammar.GetMaximumSubtreeCount(n.Symbol)) { 172 // empty extension point 173 internalCrossoverPoints.Add(new CutPoint(n, n.SubtreesCount)); 174 } 151 175 } 152 176 }); … … 156 180 if (internalCrossoverPoints.Count > 0) { 157 181 // select internal crossover point or leaf 158 var selectedCrossoverPoint = internalCrossoverPoints[random.Next(internalCrossoverPoints.Count)]; 159 crossoverPoint = selectedCrossoverPoint.Parent; 160 subtreeIndex = selectedCrossoverPoint.ChildIndex; 182 crossoverPoint = internalCrossoverPoints[random.Next(internalCrossoverPoints.Count)]; 161 183 } else { 162 184 // otherwise select external node 163 var selectedCrossoverPoint = leafCrossoverPoints[random.Next(leafCrossoverPoints.Count)]; 164 crossoverPoint = selectedCrossoverPoint.Parent; 165 subtreeIndex = selectedCrossoverPoint.ChildIndex; 185 crossoverPoint = leafCrossoverPoints[random.Next(leafCrossoverPoints.Count)]; 166 186 } 167 187 } else if (leafCrossoverPoints.Count > 0) { 168 188 // select from leaf crossover point if possible 169 var selectedCrossoverPoint = leafCrossoverPoints[random.Next(leafCrossoverPoints.Count)]; 170 crossoverPoint = selectedCrossoverPoint.Parent; 171 subtreeIndex = selectedCrossoverPoint.ChildIndex; 189 crossoverPoint = leafCrossoverPoints[random.Next(leafCrossoverPoints.Count)]; 172 190 } else { 173 191 // otherwise select internal crossover point 174 var selectedCrossoverPoint = internalCrossoverPoints[random.Next(internalCrossoverPoints.Count)]; 175 crossoverPoint = selectedCrossoverPoint.Parent; 176 subtreeIndex = selectedCrossoverPoint.ChildIndex; 192 crossoverPoint = internalCrossoverPoints[random.Next(internalCrossoverPoints.Count)]; 177 193 } 178 194 } … … 185 201 // select internal node if possible 186 202 allowedInternalBranches = (from branch in branches 187 where branch .Subtrees.Any()203 where branch != null && branch.Subtrees.Any() 188 204 select branch).ToList(); 189 205 if (allowedInternalBranches.Count > 0) { … … 192 208 // no internal nodes allowed => select leaf nodes 193 209 allowedLeafBranches = (from branch in branches 194 where !branch.Subtrees.Any()210 where branch == null || !branch.Subtrees.Any() 195 211 select branch).ToList(); 196 212 return allowedLeafBranches.SelectRandom(random); … … 199 215 // select leaf node if possible 200 216 allowedLeafBranches = (from branch in branches 201 where !branch.Subtrees.Any()217 where branch == null || !branch.Subtrees.Any() 202 218 select branch).ToList(); 203 219 if (allowedLeafBranches.Count > 0) { … … 205 221 } else { 206 222 allowedInternalBranches = (from branch in branches 207 where branch .Subtrees.Any()223 where branch != null && branch.Subtrees.Any() 208 224 select branch).ToList(); 209 225 return allowedInternalBranches.SelectRandom(random); -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/CutPoint.cs
r5809 r5916 34 34 this.childIndex = parent.IndexOfSubtree(child); 35 35 } 36 public CutPoint(ISymbolicExpressionTreeNode parent, int childIndex) { 37 this.Parent = parent; 38 this.childIndex = childIndex; 39 this.Child = null; 40 } 36 41 } 37 42 }
Note: See TracChangeset
for help on using the changeset viewer.