Changeset 11225
- Timestamp:
- 07/24/14 23:44:39 (10 years ago)
- Location:
- branches/HeuristicLab.BottomUpTreeDistance
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.BottomUpTreeDistance/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SimilarityCalculators/BottomUpSimilarityCalculator.cs
r11224 r11225 77 77 var reverseMap = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>(); // nodes of t2 => nodes of t1 78 78 79 foreach (var v in IterateBreadthOrdered(n1)) { 80 if (forwardMap.ContainsKey(v)) continue; 79 // visit nodes in order of decreasing height to ensure correct mapping 80 foreach (var v in n1.IterateNodesPrefix().OrderByDescending(x => compactedGraph[x].Weight)) { 81 if (forwardMap.ContainsKey(v)) 82 continue; 81 83 var kv = compactedGraph[v]; 82 84 ISymbolicExpressionTreeNode w = null; 83 foreach (var t in IterateBreadthOrdered(n2)) { 84 if (reverseMap.ContainsKey(t) || compactedGraph[t] != kv) continue; 85 foreach (var t in n2.IterateNodesPrefix()) { 86 if (reverseMap.ContainsKey(t) || compactedGraph[t] != kv) 87 continue; 85 88 w = t; 86 89 break; … … 96 99 var s = eV.Current; 97 100 var t = eW.Current; 98 if (reverseMap.ContainsKey(t)) 99 throw new Exception("Mapping already present"); 101 102 if (reverseMap.ContainsKey(t)) { 103 throw new Exception("A mapping to this node already exists."); 104 } 105 100 106 forwardMap[s] = t; 101 107 reverseMap[t] = s; … … 124 130 if (n.SubtreeCount == 0) { 125 131 var label = n.ToString(); 126 var z = new Vertex { Content = n, Label = label }; 127 labelsToVertices[z.Label] = z; 128 vertices.Add(z); 132 if (!labelsToVertices.ContainsKey(label)) { 133 var z = new Vertex { Content = n, Label = label }; 134 labelsToVertices[z.Label] = z; 135 vertices.Add(z); 136 } 137 nodesToVertices[n] = labelsToVertices[label]; 129 138 queue.Enqueue(n); 130 139 } else { … … 135 144 while (queue.Any()) { 136 145 var v = queue.Dequeue(); 137 string label; 138 if (v.SubtreeCount == 0) { 139 label = v.ToString(); 140 nodesToVertices[v] = labelsToVertices[label]; // 18 141 } else { 142 label = v.Symbol.Name; 146 147 if (v.SubtreeCount > 0) { 148 var label = v.Symbol.Name; 143 149 bool found = false; 144 150 var height = v.GetDepth(); … … 152 158 var w = vertices[i]; 153 159 var n = (ISymbolicExpressionTreeNode)w.Content; 154 if (n.SubtreeCount == 0) continue; // v is a function node so w will have to be a function node as well 155 if (height != (int)w.Weight || v.SubtreeCount != n.SubtreeCount || label != w.Label) 160 if (v.SubtreeCount != n.SubtreeCount || label != w.Label || height != (int)w.Weight) 156 161 continue; 157 162 158 163 // sort V and W when the symbol is commutative because we are dealing with unordered trees 159 var wSubtrees = sort ? w.OutArcs.Select(x => x.Target).OrderBy(x => x.Label)160 : w.OutArcs.Select(x => x.Target);164 var wSubtrees = n.Subtrees.Select(x => nodesToVertices[x]).ToList(); 165 if (sort) wSubtrees.Sort((a, b) => String.Compare(a.Label, b.Label, StringComparison.Ordinal)); 161 166 162 167 if (vSubtrees.SequenceEqual(wSubtrees)) { -
branches/HeuristicLab.BottomUpTreeDistance/HeuristicLab.Tests/BottomUpSimilarityCalculatorTest.cs
r11221 r11225 46 46 TestMatchedNodes("(+ (exp 2.1033) (/ -4.3072 (variable 2.4691 X7)))", "(/ 1 (+ (/ -4.3072 (variable 2.4691 X7)) (exp 2.1033)))", 6); 47 47 TestMatchedNodes("(+ (exp 2.1033) (/ -4.3072 (variable 2.4691 X7)))", "(/ 1 (+ (/ (variable 2.4691 X7) -4.3072) (exp 2.1033)))", 4); 48 49 var expr1 = "(* (- 1.2175e+1 (+ (/ (exp -1.4134e+1) (exp 9.2013)) (exp (log (exp (/ (exp (- (* -4.2461 (variable 2.2634 X5)) (- -9.6267e-1 3.3243))) (- (/ (/ (variable 1.0883 X1) (variable 6.9620e-1 X2)) (log 1.3011e+1)) (variable -4.3098e-1 X7)))))))) (log 1.3011e+1))"; 50 var expr2 = "(* (- 1.2175e+1 (+ (/ (/ (+ (variable 3.0140 X9) (variable 1.3430 X8)) -1.0864e+1) (exp 9.2013)) (exp (log (exp (/ (exp (- (* -4.2461 (variable 2.2634 X5)) (- -9.6267e-1 3.3243))) (- (/ (/ (variable 1.0883 X1) (variable 6.9620e-1 X2)) (log 1.3011e+1)) (variable -4.3098e-1 X7)))))))) (exp (variable 4.0899e-1 X7)))"; 51 52 TestMatchedNodes(expr1, expr2, 23); 53 48 54 } 49 55
Note: See TracChangeset
for help on using the changeset viewer.