Changeset 7496
- Timestamp:
- 02/21/12 11:03:08 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Crossovers/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionSemanticSimilarityCrossover.cs
r7488 r7496 30 30 31 31 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 32 [Item("SemanticSimilarityCrossover", "An operator which performs subtree swapping based on the semantic similarity between subtrees.")] 32 [Item("SemanticSimilarityCrossover", "An operator which performs subtree swapping based on the notion semantic similarity between subtrees\n" + 33 "(criteria: mean of the absolute differences between the estimated output values of the two subtrees, falling into a user-defined range)\n" + 34 "- Take two parent individuals P0 and P1\n" + 35 "- Randomly choose a node N from the P0\n" + 36 "- Find the first node M that satisfies the semantic similarity criteria\n" + 37 "- Swap N for M and return P0")] 33 38 public sealed class SymbolicDataAnalysisExpressionSemanticSimilarityCrossover<T> : SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData { 34 39 private const string SemanticSimilarityRangeParameterName = "SemanticSimilarityRange"; … … 52 57 : base() { 53 58 Parameters.Add(new ValueLookupParameter<DoubleRange>(SemanticSimilarityRangeParameterName, "Semantic similarity interval.", new DoubleRange(0.0001, 10))); 59 Name = "SemanticSimilarityCrossover"; 54 60 } 55 61 public override IDeepCloneable Clone(Cloner cloner) { … … 72 78 var crossoverPoints0 = new List<CutPoint>(); 73 79 parent0.Root.ForEachNodePostfix((n) => { 74 if (n.Subtrees.Any() && n != parent0.Root) 75 foreach (var child in n.Subtrees) 76 crossoverPoints0.Add(new CutPoint(n, child)); 80 if (n.Parent != null && n.Parent != parent0.Root) 81 crossoverPoints0.Add(new CutPoint(n.Parent, n)); 77 82 }); 78 83 var crossoverPoint0 = crossoverPoints0.SelectRandom(random); … … 82 87 var allowedBranches = new List<ISymbolicExpressionTreeNode>(); 83 88 parent1.Root.ForEachNodePostfix((n) => { 84 if (n.Subtrees.Any() && n != parent1.Root) 85 allowedBranches.AddRange(n.Subtrees.Where(s => crossoverPoint0.IsMatchingPointType(s) && s.GetDepth() + level <= maxDepth && s.GetLength() + length <= maxLength)); 89 if (n.Parent != null && n.Parent != parent1.Root) { 90 if (n.GetDepth() + level <= maxDepth && n.GetLength() + length <= maxLength && crossoverPoint0.IsMatchingPointType(n)) 91 allowedBranches.Add(n); 92 } 86 93 }); 87 94 … … 117 124 // perform the actual swap 118 125 if (selectedBranch != null) 119 swap(crossoverPoint0, selectedBranch); 120 126 Swap(crossoverPoint0, selectedBranch); 121 127 return parent0; 122 }123 124 private static void swap(CutPoint crossoverPoint, ISymbolicExpressionTreeNode selectedBranch) {125 // perform the actual swap126 if (crossoverPoint.Child != null) {127 // manipulate the tree of parent0 in place128 // replace the branch in tree0 with the selected branch from tree1129 crossoverPoint.Parent.RemoveSubtree(crossoverPoint.ChildIndex);130 if (selectedBranch != null) {131 crossoverPoint.Parent.InsertSubtree(crossoverPoint.ChildIndex, selectedBranch);132 }133 } else {134 // child is null (additional child should be added under the parent)135 if (selectedBranch != null) {136 crossoverPoint.Parent.AddSubtree(selectedBranch);137 }138 }139 128 } 140 129 }
Note: See TracChangeset
for help on using the changeset viewer.