Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/TreeMatching/SymbolicExpressionTreeBottomUpSimilarityCalculator.cs @ 13791

Last change on this file since 13791 was 12103, checked in by mkommend, 10 years ago

#2326: Merged all branch changes in the trunk.

File size: 9.5 KB
RevLine 
[11219]1#region License Information
2/* HeuristicLab
[11978]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[11219]4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
[11486]24using System.Diagnostics;
25using System.Globalization;
[11219]26using System.Linq;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
30using HeuristicLab.Optimization.Operators;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
[11221]33namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
[11219]34  [StorableClass]
[11910]35  [Item("SymbolicExpressionTreeBottomUpSimilarityCalculator", "A similarity calculator which uses the tree bottom-up distance as a similarity metric.")]
[12103]36  public class SymbolicExpressionTreeBottomUpSimilarityCalculator : SolutionSimilarityCalculator {
[11486]37    private readonly HashSet<string> commutativeSymbols = new HashSet<string> { "Addition", "Multiplication", "Average", "And", "Or", "Xor" };
[12070]38
[12103]39    public SymbolicExpressionTreeBottomUpSimilarityCalculator() { }
[12070]40    protected override bool IsCommutative { get { return true; } }
41
[11918]42    [StorableConstructor]
[11921]43    protected SymbolicExpressionTreeBottomUpSimilarityCalculator(bool deserializing)
[11918]44      : base(deserializing) {
45    }
46
[11910]47    protected SymbolicExpressionTreeBottomUpSimilarityCalculator(SymbolicExpressionTreeBottomUpSimilarityCalculator original, Cloner cloner)
[11486]48      : base(original, cloner) {
[11239]49    }
50
[11219]51    public override IDeepCloneable Clone(Cloner cloner) {
[11910]52      return new SymbolicExpressionTreeBottomUpSimilarityCalculator(this, cloner);
[11219]53    }
54
[11486]55    public double CalculateSimilarity(ISymbolicExpressionTree t1, ISymbolicExpressionTree t2) {
56      if (t1 == t2)
57        return 1;
58
59      var map = ComputeBottomUpMapping(t1.Root, t2.Root);
60      return 2.0 * map.Count / (t1.Length + t2.Length);
[11219]61    }
62
63    public override double CalculateSolutionSimilarity(IScope leftSolution, IScope rightSolution) {
[12103]64      if (leftSolution == rightSolution)
65        return 1.0;
66
[11219]67      var t1 = leftSolution.Variables[SolutionVariableName].Value as ISymbolicExpressionTree;
68      var t2 = rightSolution.Variables[SolutionVariableName].Value as ISymbolicExpressionTree;
69
70      if (t1 == null || t2 == null)
71        throw new ArgumentException("Cannot calculate similarity when one of the arguments is null.");
72
[11486]73      var similarity = CalculateSimilarity(t1, t2);
[11224]74      if (similarity > 1.0)
75        throw new Exception("Similarity value cannot be greater than 1");
76
77      return similarity;
[11219]78    }
79
[11221]80    public Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode> ComputeBottomUpMapping(ISymbolicExpressionTreeNode n1, ISymbolicExpressionTreeNode n2) {
[11486]81      var comparer = new SymbolicExpressionTreeNodeComparer(); // use a node comparer because it's faster than calling node.ToString() (strings are expensive) and comparing strings
[11221]82      var compactedGraph = Compact(n1, n2);
[11219]83
84      var forwardMap = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>(); // nodes of t1 => nodes of t2
85      var reverseMap = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>(); // nodes of t2 => nodes of t1
86
[11225]87      // visit nodes in order of decreasing height to ensure correct mapping
[11978]88      var nodes1 = n1.IterateNodesPrefix().OrderByDescending(x => x.GetDepth()).ToList();
[11487]89      var nodes2 = n2.IterateNodesPrefix().ToList();
[11894]90      for (int i = 0; i < nodes1.Count; ++i) {
91        var v = nodes1[i];
[11225]92        if (forwardMap.ContainsKey(v))
93          continue;
[11219]94        var kv = compactedGraph[v];
95        ISymbolicExpressionTreeNode w = null;
[11894]96        for (int j = 0; j < nodes2.Count; ++j) {
97          var t = nodes2[j];
[11225]98          if (reverseMap.ContainsKey(t) || compactedGraph[t] != kv)
99            continue;
[11219]100          w = t;
101          break;
102        }
103        if (w == null) continue;
104
[11978]105        // at this point we know that v and w are isomorphic, however, the mapping cannot be done directly
106        // (as in the paper) because the trees are unordered (subtree order might differ). the solution is
107        // to sort subtrees from under commutative labels (this will work because the subtrees are isomorphic!)
108        // while iterating over the two subtrees
[11894]109        var vv = IterateBreadthOrdered(v, comparer).ToList();
110        var ww = IterateBreadthOrdered(w, comparer).ToList();
111        int len = Math.Min(vv.Count, ww.Count);
112        for (int j = 0; j < len; ++j) {
113          var s = vv[j];
114          var t = ww[j];
[11486]115          Debug.Assert(!reverseMap.ContainsKey(t));
[11225]116
[11219]117          forwardMap[s] = t;
118          reverseMap[t] = s;
119        }
120      }
121
122      return forwardMap;
123    }
124
125    /// <summary>
126    /// Creates a compact representation of the two trees as a directed acyclic graph
127    /// </summary>
[11229]128    /// <param name="n1">The root of the first tree</param>
129    /// <param name="n2">The root of the second tree</param>
[11219]130    /// <returns>The compacted DAG representing the two trees</returns>
[11229]131    private Dictionary<ISymbolicExpressionTreeNode, GraphNode> Compact(ISymbolicExpressionTreeNode n1, ISymbolicExpressionTreeNode n2) {
132      var nodeMap = new Dictionary<ISymbolicExpressionTreeNode, GraphNode>(); // K
133      var labelMap = new Dictionary<string, GraphNode>(); // L
[11219]134      var childrenCount = new Dictionary<ISymbolicExpressionTreeNode, int>(); // Children
135
[11221]136      var nodes = n1.IterateNodesPostfix().Concat(n2.IterateNodesPostfix()); // the disjoint union F
[11487]137      var list = new List<GraphNode>();
[11219]138      var queue = new Queue<ISymbolicExpressionTreeNode>();
139
140      foreach (var n in nodes) {
141        if (n.SubtreeCount == 0) {
[11978]142          var label = GetLabel(n);
[11229]143          if (!labelMap.ContainsKey(label)) {
144            var z = new GraphNode { SymbolicExpressionTreeNode = n, Label = label };
145            labelMap[z.Label] = z;
[11225]146          }
[11229]147          nodeMap[n] = labelMap[label];
[11219]148          queue.Enqueue(n);
149        } else {
150          childrenCount[n] = n.SubtreeCount;
151        }
152      }
153      while (queue.Any()) {
[11229]154        var n = queue.Dequeue();
155        if (n.SubtreeCount > 0) {
[11894]156          bool found = false;
[11229]157          var label = n.Symbol.Name;
158          var depth = n.GetDepth();
[11219]159
[11894]160          bool sort = n.SubtreeCount > 1 && commutativeSymbols.Contains(label);
161          var nSubtrees = n.Subtrees.Select(x => nodeMap[x]).ToList();
162          if (sort) nSubtrees.Sort((a, b) => string.CompareOrdinal(a.Label, b.Label));
[11219]163
[11229]164          for (int i = list.Count - 1; i >= 0; --i) {
165            var w = list[i];
[11894]166            if (!(n.SubtreeCount == w.SubtreeCount && label == w.Label && depth == w.Depth))
[11219]167              continue;
168
169            // sort V and W when the symbol is commutative because we are dealing with unordered trees
[11229]170            var m = w.SymbolicExpressionTreeNode;
[11894]171            var mSubtrees = m.Subtrees.Select(x => nodeMap[x]).ToList();
172            if (sort) mSubtrees.Sort((a, b) => string.CompareOrdinal(a.Label, b.Label));
[11219]173
[11894]174            found = nSubtrees.SequenceEqual(mSubtrees);
175            if (found) {
[11229]176              nodeMap[n] = w;
[11219]177              break;
178            }
[11229]179          }
[11219]180
181          if (!found) {
[11486]182            var w = new GraphNode { SymbolicExpressionTreeNode = n, Label = label, Depth = depth };
[11229]183            list.Add(w);
184            nodeMap[n] = w;
185          }
186        }
[11219]187
[11486]188        if (n == n1 || n == n2)
189          continue;
190
[11229]191        var p = n.Parent;
[11219]192        if (p == null)
193          continue;
194
195        childrenCount[p]--;
196
197        if (childrenCount[p] == 0)
198          queue.Enqueue(p);
[11220]199      }
[11219]200
[11229]201      return nodeMap;
[11219]202    }
203
[11486]204    private IEnumerable<ISymbolicExpressionTreeNode> IterateBreadthOrdered(ISymbolicExpressionTreeNode node, ISymbolicExpressionTreeNodeComparer comparer) {
[11219]205      var list = new List<ISymbolicExpressionTreeNode> { node };
206      int i = 0;
207      while (i < list.Count) {
208        var n = list[i];
209        if (n.SubtreeCount > 0) {
[11486]210          var subtrees = commutativeSymbols.Contains(node.Symbol.Name) ? n.Subtrees.OrderBy(x => x, comparer) : n.Subtrees;
[11219]211          list.AddRange(subtrees);
212        }
213        i++;
214      }
215      return list;
216    }
217
[11978]218    private static string GetLabel(ISymbolicExpressionTreeNode node) {
[11486]219      if (node.SubtreeCount > 0)
220        return node.Symbol.Name;
221
222      var constant = node as ConstantTreeNode;
223      if (constant != null)
[11950]224        return constant.Value.ToString(CultureInfo.InvariantCulture);
225
[11486]226      var variable = node as VariableTreeNode;
[11950]227      if (variable != null)
228        return variable.Weight + variable.VariableName;
[11486]229
230      return node.ToString();
231    }
232
[11229]233    private class GraphNode {
234      public ISymbolicExpressionTreeNode SymbolicExpressionTreeNode;
235      public string Label;
236      public int Depth;
[11894]237      public int SubtreeCount { get { return SymbolicExpressionTreeNode.SubtreeCount; } }
[11219]238    }
239  }
240}
Note: See TracBrowser for help on using the repository browser.