Changeset 7497 for branches/HeuristicLab.Crossovers/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionDeterministicBestCrossover.cs
- Timestamp:
- 02/21/12 11:08:58 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Crossovers/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionDeterministicBestCrossover.cs
r7481 r7497 30 30 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 31 31 32 [Item("DeterministicBestCrossover", "An operator which performs subtree swapping in a deterministic way, by choosing the best subtree to be swapped in a certain position.")] 32 [Item("DeterministicBestCrossover", "An operator which performs subtree swapping by choosing the best subtree to be swapped in a certain position:\n" + 33 "- Take two parent individuals P0 and P1\n" + 34 "- Randomly choose a crossover point C from P0\n" + 35 "- Test all nodes from P1 to determine the one that produces the best child when inserted at place C in P0")] 33 36 public sealed class SymbolicDataAnalysisExpressionDeterministicBestCrossover<T> : SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData { 34 37 [StorableConstructor] … … 62 65 var crossoverPoints0 = new List<CutPoint>(); 63 66 parent0.Root.ForEachNodePostfix((n) => { 64 if (n. Subtrees.Any() && n!= parent0.Root)65 crossoverPoints0.Add Range(from s in n.Subtrees select new CutPoint(n, s));67 if (n.Parent != null && n.Parent != parent0.Root) 68 crossoverPoints0.Add(new CutPoint(n.Parent, n)); 66 69 }); 67 70 CutPoint crossoverPoint0 = crossoverPoints0.SelectRandom(random); … … 71 74 var allowedBranches = new List<ISymbolicExpressionTreeNode>(); 72 75 parent1.Root.ForEachNodePostfix((n) => { 73 if (n. Subtrees.Any() && n != parent1.Root)74 allowedBranches.AddRange(from s in n.Subtrees75 where crossoverPoint0.IsMatchingPointType(s) && s.GetDepth() + level <= maxDepth && s.GetLength() + length <= maxLength76 select s);76 if (n.Parent != null && n.Parent != parent1.Root) { 77 if (n.GetDepth() + level <= maxDepth && n.GetLength() + length <= maxLength && crossoverPoint0.IsMatchingPointType(n)) 78 allowedBranches.Add(n); 79 } 77 80 }); 78 81 … … 89 92 foreach (var node in allowedBranches) { 90 93 var parent = node.Parent; 91 swap(crossoverPoint0, node); // the swap will set the nodes parent to crossoverPoint0.Parent94 Swap(crossoverPoint0, node); // the swap will set the nodes parent to crossoverPoint0.Parent 92 95 double quality = evaluator.Evaluate(context, parent0, problemData, rows); 93 swap(crossoverPoint0, originalChild); // swap the child back (so that the next swap will not affect the currently swapped node from parent1)96 Swap(crossoverPoint0, originalChild); // swap the child back (so that the next swap will not affect the currently swapped node from parent1) 94 97 nodeQualities.Add(new Tuple<ISymbolicExpressionTreeNode, double>(node, quality)); 95 98 node.Parent = parent; // restore correct parent … … 108 111 109 112 // swap the node that would create the best offspring 110 swap(crossoverPoint0, selectedBranch); 111 113 Swap(crossoverPoint0, selectedBranch); 112 114 return parent0; 113 }114 115 private static void swap(CutPoint crossoverPoint, ISymbolicExpressionTreeNode selectedBranch) {116 if (crossoverPoint.Child != null) {117 // manipulate the tree of parent0 in place118 // replace the branch in tree0 with the selected branch from tree1119 crossoverPoint.Parent.RemoveSubtree(crossoverPoint.ChildIndex);120 if (selectedBranch != null) {121 crossoverPoint.Parent.InsertSubtree(crossoverPoint.ChildIndex, selectedBranch);122 }123 } else {124 // child is null (additional child should be added under the parent)125 if (selectedBranch != null) {126 crossoverPoint.Parent.AddSubtree(selectedBranch);127 }128 }129 115 } 130 116 }
Note: See TracChangeset
for help on using the changeset viewer.