- Timestamp:
- 11/22/11 13:29:11 (13 years ago)
- Location:
- branches/gp-crossover/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4
- Files:
-
- 4 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 } -
branches/gp-crossover/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/CutPoint.cs
r5916 r7035 22 22 23 23 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { 24 internalclass CutPoint {24 public class CutPoint { 25 25 public ISymbolicExpressionTreeNode Parent { get; set; } 26 26 public ISymbolicExpressionTreeNode Child { get; set; } … … 39 39 this.Child = null; 40 40 } 41 42 public bool IsMatchingPointType(ISymbolicExpressionTreeNode newChild) { 43 var parent = this.Parent; 44 if (newChild == null) { 45 // make sure that one subtree can be removed and that only the last subtree is removed 46 return parent.Grammar.GetMinimumSubtreeCount(parent.Symbol) < parent.SubtreeCount && 47 this.ChildIndex == parent.SubtreeCount - 1; 48 } else { 49 // check syntax constraints of direct parent - child relation 50 if (!parent.Grammar.ContainsSymbol(newChild.Symbol) || 51 !parent.Grammar.IsAllowedChildSymbol(parent.Symbol, newChild.Symbol, this.ChildIndex)) 52 return false; 53 54 bool result = true; 55 // check point type for the whole branch 56 newChild.ForEachNodePostfix((n) => { 57 result = 58 result && 59 parent.Grammar.ContainsSymbol(n.Symbol) && 60 n.SubtreeCount >= parent.Grammar.GetMinimumSubtreeCount(n.Symbol) && 61 n.SubtreeCount <= parent.Grammar.GetMaximumSubtreeCount(n.Symbol); 62 }); 63 return result; 64 } 65 } 41 66 } 42 67 } -
branches/gp-crossover/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Interfaces/ISymbolicExpressionTreeNode.cs
r6803 r7035 32 32 int GetDepth(); 33 33 int GetLength(); 34 int GetBranchLevel(ISymbolicExpressionTreeNode child); 34 35 35 36 IEnumerable<ISymbolicExpressionTreeNode> IterateNodesPostfix(); -
branches/gp-crossover/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeNode.cs
r6803 r7035 125 125 } 126 126 127 public int GetBranchLevel(ISymbolicExpressionTreeNode child) { 128 return GetBranchLevel(this, child); 129 } 130 131 private static int GetBranchLevel(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode point) { 132 if (root == point) 133 return 0; 134 foreach (var subtree in root.Subtrees) { 135 int branchLevel = GetBranchLevel(subtree, point); 136 if (branchLevel < int.MaxValue) 137 return 1 + branchLevel; 138 } 139 return int.MaxValue; 140 } 141 127 142 public virtual void ResetLocalParameters(IRandom random) { } 128 143 public virtual void ShakeLocalParameters(IRandom random, double shakingFactor) { }
Note: See TracChangeset
for help on using the changeset viewer.