Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/TreeMatching/SymbolicExpressionTreeBottomUpSimilarityCalculator.cs @ 15301

Last change on this file since 15301 was 14312, checked in by bburlacu, 8 years ago

#1772: Merge trunk changes. Delete unnecessary files (sliding window).

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