Changeset 7495
 Timestamp:
 02/21/12 11:01:02 (12 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

branches/HeuristicLab.Crossovers/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionContextAwareCrossover.cs
r7481 r7495 30 30 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 31 31 32 [Item("ContextAwareCrossover", "An operator which deterministically choses the best insertion point for a randomly selected node.")] 32 [Item("ContextAwareCrossover", "An operator which deterministically choses the best insertion point for a randomly selected node:\n" + 33 " Take two parent individuals P0 and P1\n" + 34 " Randomly choose a node N from P1\n" + 35 " Test all crossover points from P0 to determine the best location for N to be inserted")] 33 36 public sealed class SymbolicDataAnalysisExpressionContextAwareCrossover<T> : SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData { 34 37 [StorableConstructor] … … 63 66 var possibleChildren = new List<ISymbolicExpressionTreeNode>(); 64 67 parent1.Root.ForEachNodePostfix((n) => { 65 if (n. Subtrees.Any() && n != parent0.Root)66 possibleChildren.Add Range(n.Subtrees);68 if (n.Parent != null && n.Parent != parent1.Root) 69 possibleChildren.Add(n); 67 70 }); 68 71 var selectedChild = possibleChildren.SelectRandom(random); … … 71 74 72 75 parent0.Root.ForEachNodePostfix((n) => { 73 if (n.Subtrees.Any() && n != parent0.Root) 74 crossoverPoints.AddRange(from s in n.Subtrees 75 let crossoverPoint = new CutPoint(n, s) 76 let totalDepth = parent0.Root.GetBranchLevel(s) + selectedChild.GetDepth() 77 let totalLength = parent0.Root.GetLength()  s.GetLength() + selectedChild.GetLength() 78 where crossoverPoint.IsMatchingPointType(selectedChild) && totalDepth <= maxDepth && totalLength <= maxLength 79 select crossoverPoint); 76 if (n.Parent != null && n.Parent != parent0.Root) { 77 var totalDepth = parent0.Root.GetBranchLevel(n) + selectedChild.GetDepth(); 78 var totalLength = parent0.Root.GetLength()  n.GetLength() + selectedChild.GetLength(); 79 if (totalDepth <= maxDepth && totalLength <= maxLength) { 80 var crossoverPoint = new CutPoint(n.Parent, n); 81 if (crossoverPoint.IsMatchingPointType(selectedChild)) 82 crossoverPoints.Add(crossoverPoint); 83 } 84 } 80 85 }); 81 86 … … 86 91 var parent = selectedChild.Parent; 87 92 // perform a swap and check the quality of the solution 88 swap(crossoverPoint, selectedChild);93 Swap(crossoverPoint, selectedChild); 89 94 double quality = evaluator.Evaluate(context, parent0, problemData, rows); 90 95 qualities.Add(new Tuple<CutPoint, double>(crossoverPoint, quality)); … … 92 97 selectedChild.Parent = parent; 93 98 // swap the replaced subtree back into the tree so that the structure is preserved 94 swap(crossoverPoint, crossoverPoint.Child);99 Swap(crossoverPoint, crossoverPoint.Child); 95 100 } 96 101 … … 99 104 // swap the node that would create the best offspring 100 105 // this last swap makes a total of (2 * crossoverPoints.Count() + 1) swap operations. 101 swap(crossoverPoint0, selectedChild);106 Swap(crossoverPoint0, selectedChild); 102 107 } 103 108 104 109 return parent0; 105 110 } 106 107 private static void swap(CutPoint crossoverPoint, ISymbolicExpressionTreeNode selectedBranch) {108 if (crossoverPoint.Child != null) {109 // manipulate the tree of parent0 in place110 // replace the branch in tree0 with the selected branch from tree1111 crossoverPoint.Parent.RemoveSubtree(crossoverPoint.ChildIndex);112 if (selectedBranch != null) {113 crossoverPoint.Parent.InsertSubtree(crossoverPoint.ChildIndex, selectedBranch);114 }115 } else {116 // child is null (additional child should be added under the parent)117 if (selectedBranch != null) {118 crossoverPoint.Parent.AddSubtree(selectedBranch);119 }120 }121 }122 111 } 123 112 }
Note: See TracChangeset
for help on using the changeset viewer.