Changeset 7035 for branches/gp-crossover/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Crossovers/SubtreeCrossover.cs
- Timestamp:
- 11/22/11 13:29:11 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/gp-crossover/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Crossovers/SubtreeCrossover.cs
r6803 r7035 94 94 // calculate the max length and depth that the inserted branch can have 95 95 int maxInsertedBranchLength = maxTreeLength - (parent0.Length - childLength); 96 int maxInsertedBranchDepth = maxTreeDepth - GetBranchLevel(parent0.Root,crossoverPoint0.Parent);96 int maxInsertedBranchDepth = maxTreeDepth - parent0.Root.GetBranchLevel(crossoverPoint0.Parent); 97 97 98 98 List<ISymbolicExpressionTreeNode> allowedBranches = new List<ISymbolicExpressionTreeNode>(); 99 99 parent1.Root.ForEachNodePostfix((n) => { 100 100 if (n.GetLength() <= maxInsertedBranchLength && 101 n.GetDepth() <= maxInsertedBranchDepth && 102 IsMatchingPointType(crossoverPoint0, n)) 101 n.GetDepth() <= maxInsertedBranchDepth && crossoverPoint0.IsMatchingPointType(n)) 103 102 allowedBranches.Add(n); 104 103 }); 105 104 // empty branch 106 if ( IsMatchingPointType(crossoverPoint0,null)) allowedBranches.Add(null);105 if (crossoverPoint0.IsMatchingPointType(null)) allowedBranches.Add(null); 107 106 108 107 if (allowedBranches.Count == 0) { … … 125 124 } 126 125 return parent0; 127 }128 }129 130 private static bool IsMatchingPointType(CutPoint cutPoint, ISymbolicExpressionTreeNode newChild) {131 var parent = cutPoint.Parent;132 if (newChild == null) {133 // make sure that one subtree can be removed and that only the last subtree is removed134 return parent.Grammar.GetMinimumSubtreeCount(parent.Symbol) < parent.SubtreeCount &&135 cutPoint.ChildIndex == parent.SubtreeCount - 1;136 } else {137 // check syntax constraints of direct parent - child relation138 if (!parent.Grammar.ContainsSymbol(newChild.Symbol) ||139 !parent.Grammar.IsAllowedChildSymbol(parent.Symbol, newChild.Symbol, cutPoint.ChildIndex)) return false;140 141 bool result = true;142 // check point type for the whole branch143 newChild.ForEachNodePostfix((n) => {144 result =145 result &&146 parent.Grammar.ContainsSymbol(n.Symbol) &&147 n.SubtreeCount >= parent.Grammar.GetMinimumSubtreeCount(n.Symbol) &&148 n.SubtreeCount <= parent.Grammar.GetMaximumSubtreeCount(n.Symbol);149 });150 return result;151 126 } 152 127 } … … 226 201 } 227 202 } 228 229 private static int GetBranchLevel(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode point) {230 if (root == point) return 0;231 foreach (var subtree in root.Subtrees) {232 int branchLevel = GetBranchLevel(subtree, point);233 if (branchLevel < int.MaxValue) return 1 + branchLevel;234 }235 return int.MaxValue;236 }237 203 } 238 204 }
Note: See TracChangeset
for help on using the changeset viewer.