source: branches/HeuristicLab.BottomUpTreeDistance/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SimilarityCalculators/BottomUpSimilarityCalculator.cs @ 11229

Last change on this file since 11229 was 11229, checked in by bburlacu, 7 years ago

#2215: Refactored and simplified DirectedGraph and related components API, simplified the BottomUpSimilarityCalculator by not using a directed graph and vertices but a simpler object so that the similarity calculator is self-contained.

File size: 8.2 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.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Optimization.Operators;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
32  [StorableClass]
33  [Item("BottomUpSimilarityCalculator", "A similarity calculator which uses the tree bottom-up distance as a similarity metric.")]
34  public class BottomUpSimilarityCalculator : SingleObjectiveSolutionSimilarityCalculator {
35    private readonly HashSet<string> commutativeSymbols = new HashSet<string> { "Addition", "Multiplication", "Average", "And", "Or", "Xor" };
36
37    public BottomUpSimilarityCalculator() { }
38
39    public override IDeepCloneable Clone(Cloner cloner) {
40      return new BottomUpSimilarityCalculator(this, cloner);
41    }
42
43    protected BottomUpSimilarityCalculator(BottomUpSimilarityCalculator original, Cloner cloner)
44      : base(original, cloner) {
45    }
46
47    public override double CalculateSolutionSimilarity(IScope leftSolution, IScope rightSolution) {
48      var t1 = leftSolution.Variables[SolutionVariableName].Value as ISymbolicExpressionTree;
49      var t2 = rightSolution.Variables[SolutionVariableName].Value as ISymbolicExpressionTree;
50
51      if (t1 == null || t2 == null)
52        throw new ArgumentException("Cannot calculate similarity when one of the arguments is null.");
53
54      var similarity = CalculateSolutionSimilarity(t1, t2);
55      if (similarity > 1.0)
56        throw new Exception("Similarity value cannot be greater than 1");
57
58      return similarity;
59    }
60
61    public double CalculateSolutionSimilarity(ISymbolicExpressionTree t1, ISymbolicExpressionTree t2) {
62      if (t1 == t2)
63        return 1;
64
65      var map = ComputeBottomUpMapping(t1.Root, t2.Root);
66      return 2.0 * map.Count / (t1.Length + t2.Length);
67    }
68
69    public Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode> ComputeBottomUpMapping(ISymbolicExpressionTreeNode n1, ISymbolicExpressionTreeNode n2) {
70      var compactedGraph = Compact(n1, n2);
71
72      var forwardMap = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>(); // nodes of t1 => nodes of t2
73      var reverseMap = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>(); // nodes of t2 => nodes of t1
74
75      // visit nodes in order of decreasing height to ensure correct mapping
76      foreach (var v in n1.IterateNodesPrefix().OrderByDescending(x => compactedGraph[x].Depth)) {
77        if (forwardMap.ContainsKey(v))
78          continue;
79        var kv = compactedGraph[v];
80        ISymbolicExpressionTreeNode w = null;
81        foreach (var t in n2.IterateNodesPrefix()) {
82          if (reverseMap.ContainsKey(t) || compactedGraph[t] != kv)
83            continue;
84          w = t;
85          break;
86        }
87        if (w == null) continue;
88
89        // 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)
90        // 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
91        var eV = IterateBreadthOrdered(v).GetEnumerator();
92        var eW = IterateBreadthOrdered(w).GetEnumerator();
93
94        while (eV.MoveNext() && eW.MoveNext()) {
95          var s = eV.Current;
96          var t = eW.Current;
97
98          if (reverseMap.ContainsKey(t)) {
99            throw new Exception("A mapping to this node already exists.");
100          }
101
102          forwardMap[s] = t;
103          reverseMap[t] = s;
104        }
105      }
106
107      return forwardMap;
108    }
109
110    /// <summary>
111    /// Creates a compact representation of the two trees as a directed acyclic graph
112    /// </summary>
113    /// <param name="n1">The root of the first tree</param>
114    /// <param name="n2">The root of the second tree</param>
115    /// <returns>The compacted DAG representing the two trees</returns>
116    private Dictionary<ISymbolicExpressionTreeNode, GraphNode> Compact(ISymbolicExpressionTreeNode n1, ISymbolicExpressionTreeNode n2) {
117      var nodeMap = new Dictionary<ISymbolicExpressionTreeNode, GraphNode>(); // K
118      var labelMap = new Dictionary<string, GraphNode>(); // L
119      var childrenCount = new Dictionary<ISymbolicExpressionTreeNode, int>(); // Children
120
121      var nodes = n1.IterateNodesPostfix().Concat(n2.IterateNodesPostfix()); // the disjoint union F
122      var list = new List<GraphNode>();
123      var queue = new Queue<ISymbolicExpressionTreeNode>();
124
125      foreach (var n in nodes) {
126        if (n.SubtreeCount == 0) {
127          var label = n.ToString();
128          if (!labelMap.ContainsKey(label)) {
129            var z = new GraphNode { SymbolicExpressionTreeNode = n, Label = label };
130            labelMap[z.Label] = z;
131            list.Add(z);
132          }
133          nodeMap[n] = labelMap[label];
134          queue.Enqueue(n);
135        } else {
136          childrenCount[n] = n.SubtreeCount;
137        }
138      }
139      while (queue.Any()) {
140        var n = queue.Dequeue();
141
142        if (n.SubtreeCount > 0) {
143          var label = n.Symbol.Name;
144          bool found = false;
145          var depth = n.GetDepth();
146
147          bool sort = commutativeSymbols.Contains(label);
148          var nNodes = n.Subtrees.Select(x => nodeMap[x]).ToList();
149          if (sort) nNodes.Sort((a, b) => String.Compare(a.Label, b.Label, StringComparison.Ordinal));
150
151          for (int i = list.Count - 1; i >= 0; --i) {
152            var w = list[i];
153
154            if (!(n.SubtreeCount == w.ChildrenCount && label == w.Label && depth == w.Depth))
155              continue;
156
157            // sort V and W when the symbol is commutative because we are dealing with unordered trees
158            var m = w.SymbolicExpressionTreeNode;
159            var mNodes = m.Subtrees.Select(x => nodeMap[x]).ToList();
160            if (sort) mNodes.Sort((a, b) => String.Compare(a.Label, b.Label, StringComparison.Ordinal));
161
162            if (nNodes.SequenceEqual(mNodes)) {
163              nodeMap[n] = w;
164              found = true;
165              break;
166            }
167          }
168
169          if (!found) {
170            var w = new GraphNode { SymbolicExpressionTreeNode = n, Label = label, Depth = depth, ChildrenCount = n.SubtreeCount };
171            list.Add(w);
172            nodeMap[n] = w;
173          }
174        }
175
176        var p = n.Parent;
177        if (p == null)
178          continue;
179
180        childrenCount[p]--;
181
182        if (childrenCount[p] == 0)
183          queue.Enqueue(p);
184      }
185
186      return nodeMap;
187    }
188
189    private IEnumerable<ISymbolicExpressionTreeNode> IterateBreadthOrdered(ISymbolicExpressionTreeNode node) {
190      var list = new List<ISymbolicExpressionTreeNode> { node };
191      int i = 0;
192      while (i < list.Count) {
193        var n = list[i];
194        if (n.SubtreeCount > 0) {
195          var subtrees = commutativeSymbols.Contains(node.Symbol.Name) ? n.Subtrees.OrderBy(x => x.ToString(), StringComparer.Ordinal) : n.Subtrees;
196          list.AddRange(subtrees);
197        }
198        i++;
199      }
200      return list;
201    }
202
203    private class GraphNode {
204      public ISymbolicExpressionTreeNode SymbolicExpressionTreeNode;
205      public string Label;
206      public int Depth;
207      public int ChildrenCount;
208    }
209  }
210}
Note: See TracBrowser for help on using the repository browser.