Changeset 7494
- Timestamp:
- 02/21/12 10:57:40 (13 years ago)
- Location:
- branches/HeuristicLab.Crossovers/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Crossovers/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionCrossover.cs
r7481 r7494 149 149 return rows.Where(i => i < testPartitionStart || testPartitionEnd <= i); 150 150 } 151 152 protected static void Swap(CutPoint crossoverPoint, ISymbolicExpressionTreeNode selectedBranch) { 153 if (crossoverPoint.Child != null) { 154 // manipulate the tree of parent0 in place 155 // replace the branch in tree0 with the selected branch from tree1 156 crossoverPoint.Parent.RemoveSubtree(crossoverPoint.ChildIndex); 157 if (selectedBranch != null) { 158 crossoverPoint.Parent.InsertSubtree(crossoverPoint.ChildIndex, selectedBranch); 159 } 160 } else { 161 // child is null (additional child should be added under the parent) 162 if (selectedBranch != null) { 163 crossoverPoint.Parent.AddSubtree(selectedBranch); 164 } 165 } 166 } 151 167 } 152 168 } -
branches/HeuristicLab.Crossovers/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover.cs
r7481 r7494 30 30 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 31 31 32 [Item("ProbabilisticFunctionalCrossover", "An operator which performs subtree swapping based on the behavioral similarity between subtrees.")] 32 [Item("ProbabilisticFunctionalCrossover", "An operator which performs subtree swapping based on the behavioral similarity between subtrees:\n" + 33 "- Take two parent individuals P0 and P1\n" + 34 "- Randomly choose a node N from P0\n" + 35 "- For each matching node M from P1, calculate the behavioral distance:\n" + 36 "\t\tD(N,M) = 0.5 * ( abs(max(N) - max(M)) + abs(min(N) - min(M)) )\n" + 37 "- Make a probabilistic weighted choice of node M from P1, based on the inversed and normalized behavioral distance")] 33 38 public sealed class SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover<T> : SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData { 34 39 [StorableConstructor] … … 38 43 public SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover() 39 44 : base() { 45 Name = "ProbabilisticFunctionalCrossover"; 40 46 } 41 47 public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover<T>(this, cloner); } … … 53 59 /// Randomly choose a node i from the first parent, then for each matching node j from the second parent, calculate the behavioral distance based on the range: 54 60 /// d(i,j) = 0.5 * ( abs(max(i) - max(j)) + abs(min(i) - min(j)) ). 55 /// Next, assign probabilities for the selection of the second cross point based on the inversed and normalized behavioral distance and 56 /// choose the second crosspoint via a random weighted selection procedure. 61 /// Next, assign probabilities for the selection of a node j based on the inversed and normalized behavioral distance, then make a random weighted choice. 57 62 /// </summary> 58 63 public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, … … 60 65 var crossoverPoints0 = new List<CutPoint>(); 61 66 parent0.Root.ForEachNodePostfix((n) => { 62 if (n.Subtrees.Any() && n != parent0.Root) 63 crossoverPoints0.AddRange(from s in n.Subtrees 64 select new CutPoint(n, s)); 67 // the if clauses prevent the root or the startnode from being selected, although the startnode can be the parent of the node being swapped. 68 if (n.Parent != null && n.Parent != parent0.Root) { 69 crossoverPoints0.Add(new CutPoint(n.Parent, n)); 70 } 65 71 }); 66 72 var crossoverPoint0 = crossoverPoints0.SelectRandom(random); … … 70 76 var allowedBranches = new List<ISymbolicExpressionTreeNode>(); 71 77 parent1.Root.ForEachNodePostfix((n) => { 72 if (n. Subtrees.Any() && n != parent1.Root)73 allowedBranches.AddRange(from s in n.Subtrees74 where crossoverPoint0.IsMatchingPointType(s) && s.GetDepth() + level <= maxDepth && s.GetLength() + length <= maxLength75 select s);78 if (n.Parent != null && n.Parent != parent1.Root) { 79 if (n.GetDepth() + level <= maxDepth && n.GetLength() + length <= maxLength && crossoverPoint0.IsMatchingPointType(n)) 80 allowedBranches.Add(n); 81 } 76 82 }); 77 83 … … 107 113 108 114 // remove branches with an infinite or NaN behavioral distance 109 int count = weights.Count, idx = 0; 110 while (idx < count) { 111 if (Double.IsNaN(weights[idx]) || Double.IsInfinity(weights[idx])) { 112 weights.RemoveAt(idx); 113 allowedBranches.RemoveAt(idx); 114 --count; 115 } else { 116 ++idx; 115 for (int i = weights.Count - 1; i >= 0; --i) { 116 if (Double.IsNaN(weights[i]) || Double.IsInfinity(weights[i])) { 117 weights.RemoveAt(i); 118 allowedBranches.RemoveAt(i); 117 119 } 118 120 } 119 120 121 // check if there are any allowed branches left 121 122 if (allowedBranches.Count == 0) … … 139 140 selectedBranch = allowedBranches.SelectRandom(weights, random); 140 141 } 141 swap(crossoverPoint0, selectedBranch);142 Swap(crossoverPoint0, selectedBranch); 142 143 return parent0; 143 }144 145 private static void swap(CutPoint crossoverPoint, ISymbolicExpressionTreeNode selectedBranch) {146 if (crossoverPoint.Child != null) {147 // manipulate the tree of parent0 in place148 // replace the branch in tree0 with the selected branch from tree1149 crossoverPoint.Parent.RemoveSubtree(crossoverPoint.ChildIndex);150 if (selectedBranch != null) {151 crossoverPoint.Parent.InsertSubtree(crossoverPoint.ChildIndex, selectedBranch);152 }153 } else {154 // child is null (additional child should be added under the parent)155 if (selectedBranch != null) {156 crossoverPoint.Parent.AddSubtree(selectedBranch);157 }158 }159 144 } 160 145 }
Note: See TracChangeset
for help on using the changeset viewer.