Changeset 7497 for branches/HeuristicLab.Crossovers/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionDepthConstrainedCrossover.cs
- Timestamp:
- 02/21/12 11:08:58 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Crossovers/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionDepthConstrainedCrossover.cs
r7490 r7497 32 32 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 33 33 34 [Item("DepthConstrainedCrossover", "An operator which performs subtree swapping within a specific depth range.")] 34 [Item("DepthConstrainedCrossover", "An operator which performs subtree swapping within a specific depth range. The range parameter controls the crossover behavior:\n" + 35 "- HighLevel (upper 25% of the tree)\n" + 36 "- Standard (mid 50% of the tree)\n" + 37 "- LowLevel (lower 25% of the tree)")] 35 38 public sealed class SymbolicDataAnalysisExpressionDepthConstrainedCrossover<T> : 36 39 SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData { … … 60 63 DepthRangeParameter.ValidValues.Add(new StringValue(Enum.GetName(typeof(Ranges), Ranges.Standard)).AsReadOnly()); 61 64 DepthRangeParameter.ValidValues.Add(new StringValue(Enum.GetName(typeof(Ranges), Ranges.LowLevel)).AsReadOnly()); 65 Name = "DepthConstrainedCrossover"; 62 66 } 63 67 public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicDataAnalysisExpressionDepthConstrainedCrossover<T>(this, cloner); } … … 83 87 /// <returns></returns> 84 88 public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, int maxDepth, int maxLength, string mode) { 85 int depth = parent0.Root.GetDepth() - 1; // sub tract 1 because the tree levels are counted from 089 int depth = parent0.Root.GetDepth() - 1; // substract 1 because the tree levels are counted from 0 86 90 var depthRange = new IntRange(); 87 91 const int depthOffset = 2; // skip the first 2 levels (root + startNode) … … 118 122 119 123 var allowedBranches = (from s in GetNodesAtDepth(parent1, depthRange) 120 where 121 crossoverPoint0.IsMatchingPointType(s) && 122 s.GetDepth() + level <= maxDepth && 123 s.GetLength() + length <= maxLength 124 where s.GetDepth() + level <= maxDepth 125 where s.GetLength() + length <= maxLength 126 where crossoverPoint0.IsMatchingPointType(s) 124 127 select s).ToList(); 125 128 if (allowedBranches.Count == 0) return parent0; 126 129 var selectedBranch = allowedBranches.SelectRandom(random); 127 swap(crossoverPoint0, selectedBranch);130 Swap(crossoverPoint0, selectedBranch); 128 131 return parent0; 129 132 } … … 136 139 where depth <= depthRange.End 137 140 select node; 138 139 //var list = new List<Tuple<ISymbolicExpressionTreeNode, int>> { new Tuple<ISymbolicExpressionTreeNode, int>(root, 0) };140 //int offset = 0;141 //int level = 0;142 //while (level < range.End) {143 // ++level;144 // int count = list.Count;145 // for (int i = offset; i != count; ++i) {146 // if (list[i].Item1.Subtrees.Any())147 // list.AddRange(from s in list[i].Item1.Subtrees select new Tuple<ISymbolicExpressionTreeNode, int>(s, level));148 // }149 // offset = count;150 //}151 //// taking advantage of the fact that the list is already sorted by level152 //for (int i = list.Count - 1; i >= 0; --i) {153 // if (list[i].Item2 >= range.Start)154 // yield return list[i].Item1;155 // else break;156 //}157 }158 159 private static void swap(CutPoint crossoverPoint, ISymbolicExpressionTreeNode selectedBranch) {160 // perform the actual swap161 if (crossoverPoint.Child != null) {162 // manipulate the tree of parent0 in place163 // replace the branch in tree0 with the selected branch from tree1164 crossoverPoint.Parent.RemoveSubtree(crossoverPoint.ChildIndex);165 if (selectedBranch != null) {166 crossoverPoint.Parent.InsertSubtree(crossoverPoint.ChildIndex, selectedBranch);167 }168 } else {169 // child is null (additional child should be added under the parent)170 if (selectedBranch != null) {171 crossoverPoint.Parent.AddSubtree(selectedBranch);172 }173 }174 141 } 175 142 }
Note: See TracChangeset
for help on using the changeset viewer.