Changeset 14426
- Timestamp:
- 11/28/16 17:51:37 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/TreeMatching/QueryMatch.cs
r14326 r14426 29 29 // by M. Götz, C. Koch and W. Martens in http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.182.5440 30 30 public class QueryMatch { 31 public ISymbolicExpressionTreeNodeEqualityComparer Comparer { get; private set; }31 public ISymbolicExpressionTreeNodeEqualityComparer EqualityComparer { get; private set; } 32 32 private readonly NodeInfo Inf = new NodeInfo { Index = int.MaxValue }; 33 33 private readonly NodeInfo Nil = new NodeInfo { Index = -1 }; 34 private readonly HashSet<string> commutativeSymbols = new HashSet<string> { "Addition", "Multiplication", "Average", "And", "Or", "Xor" }; 35 private readonly ISymbolicExpressionTreeNodeComparer nodeComparer = new SymbolicExpressionTreeNodeComparer(); 34 36 35 37 private QueryMatch() { } … … 41 43 public bool MatchParents { get; set; } 42 44 43 public QueryMatch(ISymbolicExpressionTreeNodeEqualityComparer comparer) {44 Comparer = comparer;45 public QueryMatch(ISymbolicExpressionTreeNodeEqualityComparer equalityComparer) { 46 EqualityComparer = equalityComparer; 45 47 } 46 48 … … 58 60 59 61 public bool Match(ISymbolicExpressionTreeNode data, ISymbolicExpressionTreeNode query) { 60 if (! Comparer.Equals(data, query))62 if (!EqualityComparer.Equals(data, query)) 61 63 return false; 62 64 … … 72 74 public IEnumerable<ISymbolicExpressionTree> GetMatchingTrees(IEnumerable<ISymbolicExpressionTree> data, ISymbolicExpressionTree query) { 73 75 var qRoot = query.Root.GetSubtree(0).GetSubtree(0); 74 var filtered = data.Where(x => x.Length >= query.Length && Comparer.Equals(x.Root.GetSubtree(0).GetSubtree(0), qRoot));76 var filtered = data.Where(x => x.Length >= query.Length && EqualityComparer.Equals(x.Root.GetSubtree(0).GetSubtree(0), qRoot)); 75 77 var qNodes = InitializePostOrder(query.Root.GetSubtree(0).GetSubtree(0)); 76 78 return from d in filtered let dNodes = InitializePostOrder(d.Root.GetSubtree(0).GetSubtree(0)) where Match(dNodes, qNodes) select d; 77 79 } 78 80 79 private bool AreMatching(NodeInfo a, NodeInfo b) {81 private bool AreMatching(NodeInfo d, NodeInfo q) { 80 82 // force the nodes to be on the same level 81 if (a.Level != b.Level) 82 return false; 83 84 bool equals = Comparer.Equals(a.Node, b.Node); 85 if (equals && MatchParents) { 86 var pa = a.Parent; 87 var pb = b.Parent; 88 if (pa == null && pb == null) 89 return true; 90 if (pa == null || pb == null) 91 return false; 92 return pa.Level == pb.Level && Comparer.Equals(pa.Node, pb.Node); 93 } 94 return equals; 83 if (d.Level != q.Level) 84 return false; 85 86 // compare nodes 87 bool equals = EqualityComparer.Equals(d.Node, q.Node); 88 89 if (!equals) 90 return false; 91 92 // compare node parents 93 if (MatchParents && !EqualityComparer.Equals(d.Node.Parent, q.Node.Parent)) 94 return false; 95 96 // compare children to make sure they are the same 97 if (d.Node.SubtreeCount > 0 && q.Node.SubtreeCount > 0) { 98 var dSubtrees = d.Node.Subtrees.ToList(); 99 100 if (commutativeSymbols.Contains(d.Node.Symbol.Name)) { 101 var qSubtrees = q.Node.Subtrees.Where(x => !(x is AnyNode || x is AnySubtree)).ToList(); 102 103 dSubtrees.Sort(nodeComparer); 104 qSubtrees.Sort(nodeComparer); 105 106 var nw = q.Node.SubtreeCount - qSubtrees.Count; // number of wildcards 107 if (nw == 0) 108 return dSubtrees.SequenceEqual(qSubtrees, EqualityComparer); 109 110 for (int i = 0, j = 0; i < dSubtrees.Count && j < qSubtrees.Count;) { 111 if (EqualityComparer.Equals(dSubtrees[i], qSubtrees[j])) { 112 ++i; 113 ++j; 114 } else { 115 if (nw == 0) 116 return false; 117 ++i; 118 --nw; 119 } 120 } 121 } else { 122 var qSubtrees = q.Node.Subtrees; 123 return dSubtrees.SequenceEqual(qSubtrees, EqualityComparer); 124 } 125 } 126 return true; 95 127 } 96 128 … … 167 199 if (n != nodes.Last()) n.Next = nodes[n.Index + 1]; 168 200 if (n != nodes.First()) n.Previous = nodes[n.Index - 1]; 169 NodeInfo parentInfo; 170 map.TryGetValue(n.Node.Parent, out parentInfo); 201 NodeInfo parentInfo = null; 202 if (n.Node.Parent != null) 203 map.TryGetValue(n.Node.Parent, out parentInfo); 171 204 n.Parent = parentInfo; 172 205 if (n.Parent == null) {
Note: See TracChangeset
for help on using the changeset viewer.