Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11918 was 11918, checked in by bburlacu, 9 years ago

#2215: Added missing storable constructor to SymbolicExpressionTreeBottomUpSimilarityCalculator.

File size: 9.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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;
24using System.Diagnostics;
25using System.Globalization;
26using System.Linq;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
30using HeuristicLab.Optimization.Operators;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
34  [StorableClass]
35  [Item("SymbolicExpressionTreeBottomUpSimilarityCalculator", "A similarity calculator which uses the tree bottom-up distance as a similarity metric.")]
36  public class SymbolicExpressionTreeBottomUpSimilarityCalculator : SingleObjectiveSolutionSimilarityCalculator {
37    private readonly HashSet<string> commutativeSymbols = new HashSet<string> { "Addition", "Multiplication", "Average", "And", "Or", "Xor" };
38    public bool MatchVariableWeights { get; set; }
39    public bool MatchConstantValues { get; set; }
40
41    public SymbolicExpressionTreeBottomUpSimilarityCalculator() { }
42
43    [StorableConstructor]
44    private SymbolicExpressionTreeBottomUpSimilarityCalculator(bool deserializing)
45      : base(deserializing) {
46    }
47
48    protected SymbolicExpressionTreeBottomUpSimilarityCalculator(SymbolicExpressionTreeBottomUpSimilarityCalculator original, Cloner cloner)
49      : base(original, cloner) {
50      MatchVariableWeights = original.MatchVariableWeights;
51      MatchConstantValues = original.MatchConstantValues;
52    }
53
54    public override IDeepCloneable Clone(Cloner cloner) {
55      return new SymbolicExpressionTreeBottomUpSimilarityCalculator(this, cloner);
56    }
57
58    public double CalculateSimilarity(ISymbolicExpressionTree t1, ISymbolicExpressionTree t2) {
59      if (t1 == t2)
60        return 1;
61
62      var map = ComputeBottomUpMapping(t1.Root, t2.Root);
63      return 2.0 * map.Count / (t1.Length + t2.Length);
64    }
65
66    public override double CalculateSolutionSimilarity(IScope leftSolution, IScope rightSolution) {
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
73      var similarity = CalculateSimilarity(t1, t2);
74      if (similarity > 1.0)
75        throw new Exception("Similarity value cannot be greater than 1");
76
77      return similarity;
78    }
79
80    public Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode> ComputeBottomUpMapping(ISymbolicExpressionTreeNode n1, ISymbolicExpressionTreeNode n2) {
81      var comparer = new SymbolicExpressionTreeNodeComparer(); // use a node comparer because it's faster than calling node.ToString() (strings are expensive) and comparing strings
82      var compactedGraph = Compact(n1, n2);
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
87      // visit nodes in order of decreasing height to ensure correct mapping
88      var nodes1 = n1.IterateNodesPrefix().ToList();
89      var nodes2 = n2.IterateNodesPrefix().ToList();
90      for (int i = 0; i < nodes1.Count; ++i) {
91        var v = nodes1[i];
92        if (forwardMap.ContainsKey(v))
93          continue;
94        var kv = compactedGraph[v];
95        ISymbolicExpressionTreeNode w = null;
96        for (int j = 0; j < nodes2.Count; ++j) {
97          var t = nodes2[j];
98          if (reverseMap.ContainsKey(t) || compactedGraph[t] != kv)
99            continue;
100          w = t;
101          break;
102        }
103        if (w == null) continue;
104
105        // at this point we know that v and w are isomorphic, however, the mapping cannot be done directly (as in the paper) because the trees are unordered (subtree order might differ)
106        // the solution is to sort subtrees by label using IterateBreadthOrdered (this will work because the subtrees are isomorphic!) and simultaneously iterate over the two subtrees
107        var vv = IterateBreadthOrdered(v, comparer).ToList();
108        var ww = IterateBreadthOrdered(w, comparer).ToList();
109        int len = Math.Min(vv.Count, ww.Count);
110        for (int j = 0; j < len; ++j) {
111          var s = vv[j];
112          var t = ww[j];
113          Debug.Assert(!reverseMap.ContainsKey(t));
114
115          forwardMap[s] = t;
116          reverseMap[t] = s;
117        }
118      }
119
120      return forwardMap;
121    }
122
123    /// <summary>
124    /// Creates a compact representation of the two trees as a directed acyclic graph
125    /// </summary>
126    /// <param name="n1">The root of the first tree</param>
127    /// <param name="n2">The root of the second tree</param>
128    /// <returns>The compacted DAG representing the two trees</returns>
129    private Dictionary<ISymbolicExpressionTreeNode, GraphNode> Compact(ISymbolicExpressionTreeNode n1, ISymbolicExpressionTreeNode n2) {
130      var nodeMap = new Dictionary<ISymbolicExpressionTreeNode, GraphNode>(); // K
131      var labelMap = new Dictionary<string, GraphNode>(); // L
132      var childrenCount = new Dictionary<ISymbolicExpressionTreeNode, int>(); // Children
133
134      var nodes = n1.IterateNodesPostfix().Concat(n2.IterateNodesPostfix()); // the disjoint union F
135      var list = new List<GraphNode>();
136      var queue = new Queue<ISymbolicExpressionTreeNode>();
137
138      foreach (var n in nodes) {
139        if (n.SubtreeCount == 0) {
140          var label = Label(n);
141          if (!labelMap.ContainsKey(label)) {
142            var z = new GraphNode { SymbolicExpressionTreeNode = n, Label = label };
143            labelMap[z.Label] = z;
144          }
145          nodeMap[n] = labelMap[label];
146          queue.Enqueue(n);
147        } else {
148          childrenCount[n] = n.SubtreeCount;
149        }
150      }
151      while (queue.Any()) {
152        var n = queue.Dequeue();
153        if (n.SubtreeCount > 0) {
154          bool found = false;
155          var label = n.Symbol.Name;
156          var depth = n.GetDepth();
157
158          bool sort = n.SubtreeCount > 1 && commutativeSymbols.Contains(label);
159          var nSubtrees = n.Subtrees.Select(x => nodeMap[x]).ToList();
160          if (sort) nSubtrees.Sort((a, b) => string.CompareOrdinal(a.Label, b.Label));
161
162          for (int i = list.Count - 1; i >= 0; --i) {
163            var w = list[i];
164            if (!(n.SubtreeCount == w.SubtreeCount && label == w.Label && depth == w.Depth))
165              continue;
166
167            // sort V and W when the symbol is commutative because we are dealing with unordered trees
168            var m = w.SymbolicExpressionTreeNode;
169            var mSubtrees = m.Subtrees.Select(x => nodeMap[x]).ToList();
170            if (sort) mSubtrees.Sort((a, b) => string.CompareOrdinal(a.Label, b.Label));
171
172            found = nSubtrees.SequenceEqual(mSubtrees);
173            if (found) {
174              nodeMap[n] = w;
175              break;
176            }
177          }
178
179          if (!found) {
180            var w = new GraphNode { SymbolicExpressionTreeNode = n, Label = label, Depth = depth };
181            list.Add(w);
182            nodeMap[n] = w;
183          }
184        }
185
186        if (n == n1 || n == n2)
187          continue;
188
189        var p = n.Parent;
190        if (p == null)
191          continue;
192
193        childrenCount[p]--;
194
195        if (childrenCount[p] == 0)
196          queue.Enqueue(p);
197      }
198
199      return nodeMap;
200    }
201
202    private IEnumerable<ISymbolicExpressionTreeNode> IterateBreadthOrdered(ISymbolicExpressionTreeNode node, ISymbolicExpressionTreeNodeComparer comparer) {
203      var list = new List<ISymbolicExpressionTreeNode> { node };
204      int i = 0;
205      while (i < list.Count) {
206        var n = list[i];
207        if (n.SubtreeCount > 0) {
208          var subtrees = commutativeSymbols.Contains(node.Symbol.Name) ? n.Subtrees.OrderBy(x => x, comparer) : n.Subtrees;
209          list.AddRange(subtrees);
210        }
211        i++;
212      }
213      return list;
214    }
215
216    private string Label(ISymbolicExpressionTreeNode node) {
217      if (node.SubtreeCount > 0)
218        return node.Symbol.Name;
219
220      var constant = node as ConstantTreeNode;
221      if (constant != null)
222        return MatchConstantValues ? constant.Value.ToString(CultureInfo.InvariantCulture) : constant.Symbol.Name;
223      var variable = node as VariableTreeNode;
224      if (variable != null) {
225        return MatchVariableWeights ? variable.Weight + variable.VariableName : variable.VariableName;
226      }
227
228      return node.ToString();
229    }
230
231    private class GraphNode {
232      public ISymbolicExpressionTreeNode SymbolicExpressionTreeNode;
233      public string Label;
234      public int Depth;
235      public int SubtreeCount { get { return SymbolicExpressionTreeNode.SubtreeCount; } }
236    }
237  }
238}
Note: See TracBrowser for help on using the repository browser.