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
RevLine 
[11219]1#region License Information
[12891]2
[11219]3/* HeuristicLab
[14312]4 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[11219]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
[12891]22#endregion License Information
23
[11219]24using System;
25using System.Collections.Generic;
[11486]26using System.Diagnostics;
27using System.Globalization;
[11219]28using System.Linq;
29using HeuristicLab.Common;
30using HeuristicLab.Core;
31using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
32using HeuristicLab.Optimization.Operators;
33using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
34
[11221]35namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
[12891]36
[11219]37  [StorableClass]
[11910]38  [Item("SymbolicExpressionTreeBottomUpSimilarityCalculator", "A similarity calculator which uses the tree bottom-up distance as a similarity metric.")]
[12155]39  public class SymbolicExpressionTreeBottomUpSimilarityCalculator : SolutionSimilarityCalculator {
[11486]40    private readonly HashSet<string> commutativeSymbols = new HashSet<string> { "Addition", "Multiplication", "Average", "And", "Or", "Xor" };
[12155]41
[12891]42    public SymbolicExpressionTreeBottomUpSimilarityCalculator() {
43    }
44
[12155]45    protected override bool IsCommutative { get { return true; } }
[11219]46
[12287]47    public bool MatchVariableWeights { get; set; }
48    public bool MatchConstantValues { get; set; }
49
[11918]50    [StorableConstructor]
[11921]51    protected SymbolicExpressionTreeBottomUpSimilarityCalculator(bool deserializing)
[11918]52      : base(deserializing) {
53    }
54
[11910]55    protected SymbolicExpressionTreeBottomUpSimilarityCalculator(SymbolicExpressionTreeBottomUpSimilarityCalculator original, Cloner cloner)
[11486]56      : base(original, cloner) {
[11239]57    }
58
[11219]59    public override IDeepCloneable Clone(Cloner cloner) {
[11910]60      return new SymbolicExpressionTreeBottomUpSimilarityCalculator(this, cloner);
[11219]61    }
62
[11486]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);
[11219]69    }
70
71    public override double CalculateSolutionSimilarity(IScope leftSolution, IScope rightSolution) {
[12155]72      if (leftSolution == rightSolution)
73        return 1.0;
74
[11219]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
[11486]81      var similarity = CalculateSimilarity(t1, t2);
[11224]82      if (similarity > 1.0)
83        throw new Exception("Similarity value cannot be greater than 1");
84
85      return similarity;
[11219]86    }
87
[11221]88    public Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode> ComputeBottomUpMapping(ISymbolicExpressionTreeNode n1, ISymbolicExpressionTreeNode n2) {
[11486]89      var comparer = new SymbolicExpressionTreeNodeComparer(); // use a node comparer because it's faster than calling node.ToString() (strings are expensive) and comparing strings
[11221]90      var compactedGraph = Compact(n1, n2);
[11219]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
[11225]95      // visit nodes in order of decreasing height to ensure correct mapping
[12017]96      var nodes1 = n1.IterateNodesPrefix().OrderByDescending(x => x.GetDepth()).ToList();
[11487]97      var nodes2 = n2.IterateNodesPrefix().ToList();
[11894]98      for (int i = 0; i < nodes1.Count; ++i) {
99        var v = nodes1[i];
[11225]100        if (forwardMap.ContainsKey(v))
101          continue;
[11219]102        var kv = compactedGraph[v];
103        ISymbolicExpressionTreeNode w = null;
[11894]104        for (int j = 0; j < nodes2.Count; ++j) {
105          var t = nodes2[j];
[11225]106          if (reverseMap.ContainsKey(t) || compactedGraph[t] != kv)
107            continue;
[11219]108          w = t;
109          break;
110        }
111        if (w == null) continue;
112
[12017]113        // at this point we know that v and w are isomorphic, however, the mapping cannot be done directly
[12891]114        // (as in the paper) because the trees are unordered (subtree order might differ). the solution is
[12017]115        // to sort subtrees from under commutative labels (this will work because the subtrees are isomorphic!)
116        // while iterating over the two subtrees
[11894]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];
[11486]123          Debug.Assert(!reverseMap.ContainsKey(t));
[11225]124
[11219]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>
[11229]136    /// <param name="n1">The root of the first tree</param>
137    /// <param name="n2">The root of the second tree</param>
[11219]138    /// <returns>The compacted DAG representing the two trees</returns>
[11229]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
[11219]142      var childrenCount = new Dictionary<ISymbolicExpressionTreeNode, int>(); // Children
143
[11221]144      var nodes = n1.IterateNodesPostfix().Concat(n2.IterateNodesPostfix()); // the disjoint union F
[11487]145      var list = new List<GraphNode>();
[11219]146      var queue = new Queue<ISymbolicExpressionTreeNode>();
147
148      foreach (var n in nodes) {
149        if (n.SubtreeCount == 0) {
[12017]150          var label = GetLabel(n);
[11229]151          if (!labelMap.ContainsKey(label)) {
152            var z = new GraphNode { SymbolicExpressionTreeNode = n, Label = label };
153            labelMap[z.Label] = z;
[11225]154          }
[11229]155          nodeMap[n] = labelMap[label];
[11219]156          queue.Enqueue(n);
157        } else {
158          childrenCount[n] = n.SubtreeCount;
159        }
160      }
161      while (queue.Any()) {
[11229]162        var n = queue.Dequeue();
163        if (n.SubtreeCount > 0) {
[11894]164          bool found = false;
[11229]165          var label = n.Symbol.Name;
166          var depth = n.GetDepth();
[11219]167
[11894]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));
[11219]171
[11229]172          for (int i = list.Count - 1; i >= 0; --i) {
173            var w = list[i];
[11894]174            if (!(n.SubtreeCount == w.SubtreeCount && label == w.Label && depth == w.Depth))
[11219]175              continue;
176
177            // sort V and W when the symbol is commutative because we are dealing with unordered trees
[11229]178            var m = w.SymbolicExpressionTreeNode;
[11894]179            var mSubtrees = m.Subtrees.Select(x => nodeMap[x]).ToList();
180            if (sort) mSubtrees.Sort((a, b) => string.CompareOrdinal(a.Label, b.Label));
[11219]181
[11894]182            found = nSubtrees.SequenceEqual(mSubtrees);
183            if (found) {
[11229]184              nodeMap[n] = w;
[11219]185              break;
186            }
[11229]187          }
[11219]188
189          if (!found) {
[11486]190            var w = new GraphNode { SymbolicExpressionTreeNode = n, Label = label, Depth = depth };
[11229]191            list.Add(w);
192            nodeMap[n] = w;
193          }
194        }
[11219]195
[11486]196        if (n == n1 || n == n2)
197          continue;
198
[11229]199        var p = n.Parent;
[11219]200        if (p == null)
201          continue;
202
203        childrenCount[p]--;
204
205        if (childrenCount[p] == 0)
206          queue.Enqueue(p);
[11220]207      }
[11219]208
[11229]209      return nodeMap;
[11219]210    }
211
[11486]212    private IEnumerable<ISymbolicExpressionTreeNode> IterateBreadthOrdered(ISymbolicExpressionTreeNode node, ISymbolicExpressionTreeNodeComparer comparer) {
[11219]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) {
[11486]218          var subtrees = commutativeSymbols.Contains(node.Symbol.Name) ? n.Subtrees.OrderBy(x => x, comparer) : n.Subtrees;
[11219]219          list.AddRange(subtrees);
220        }
221        i++;
222      }
223      return list;
224    }
225
[12287]226    private string GetLabel(ISymbolicExpressionTreeNode node) {
[11486]227      if (node.SubtreeCount > 0)
228        return node.Symbol.Name;
229
230      var constant = node as ConstantTreeNode;
231      if (constant != null)
[12287]232        return MatchConstantValues ? constant.Value.ToString(CultureInfo.InvariantCulture) : node.Symbol.Name;
[11965]233
[11486]234      var variable = node as VariableTreeNode;
[11965]235      if (variable != null)
[12287]236        return MatchVariableWeights ? variable.Weight + variable.VariableName : variable.VariableName;
[11486]237
238      return node.ToString();
239    }
240
[11229]241    private class GraphNode {
242      public ISymbolicExpressionTreeNode SymbolicExpressionTreeNode;
243      public string Label;
244      public int Depth;
[11894]245      public int SubtreeCount { get { return SymbolicExpressionTreeNode.SubtreeCount; } }
[11219]246    }
247  }
[12891]248}
Note: See TracBrowser for help on using the repository browser.