Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/TreeMatching/SymbolicExpressionTreeBottomUpSimilarityCalculator.cs @ 17091

Last change on this file since 17091 was 17091, checked in by abeham, 5 years ago

#2959: merged revisions 16278, 16279, 16283 to stable

File size: 10.1 KB
RevLine 
[11219]1#region License Information
2/* HeuristicLab
[15584]3 * Copyright (C) 2002-2018 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.Globalization;
[11219]25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.Optimization.Operators;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
[17091]32using NodeMap = System.Collections.Generic.Dictionary<HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ISymbolicExpressionTreeNode, HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ISymbolicExpressionTreeNode>;
33
[11221]34namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
[11219]35  [StorableClass]
[11910]36  [Item("SymbolicExpressionTreeBottomUpSimilarityCalculator", "A similarity calculator which uses the tree bottom-up distance as a similarity metric.")]
[12281]37  public class SymbolicExpressionTreeBottomUpSimilarityCalculator : SolutionSimilarityCalculator {
[11486]38    private readonly HashSet<string> commutativeSymbols = new HashSet<string> { "Addition", "Multiplication", "Average", "And", "Or", "Xor" };
[12280]39
[12281]40    public SymbolicExpressionTreeBottomUpSimilarityCalculator() { }
[12280]41    protected override bool IsCommutative { get { return true; } }
42
[17091]43    public bool MatchConstantValues { get; set; }
44    public bool MatchVariableWeights { get; set; }
45
[11918]46    [StorableConstructor]
[11947]47    protected SymbolicExpressionTreeBottomUpSimilarityCalculator(bool deserializing)
[11918]48      : base(deserializing) {
49    }
50
[11910]51    protected SymbolicExpressionTreeBottomUpSimilarityCalculator(SymbolicExpressionTreeBottomUpSimilarityCalculator original, Cloner cloner)
[11486]52      : base(original, cloner) {
[11239]53    }
54
[11219]55    public override IDeepCloneable Clone(Cloner cloner) {
[11910]56      return new SymbolicExpressionTreeBottomUpSimilarityCalculator(this, cloner);
[11219]57    }
58
[17091]59    #region static methods
60    private static ISymbolicExpressionTreeNode ActualRoot(ISymbolicExpressionTree tree) {
61      return tree.Root.GetSubtree(0).GetSubtree(0);
62    }
63
64    public static double CalculateSimilarity(ISymbolicExpressionTree t1, ISymbolicExpressionTree t2, bool strict = false) {
65      return CalculateSimilarity(ActualRoot(t1), ActualRoot(t2), strict);
66    }
67
68    public static double CalculateSimilarity(ISymbolicExpressionTreeNode n1, ISymbolicExpressionTreeNode n2, bool strict = false) {
69      var calculator = new SymbolicExpressionTreeBottomUpSimilarityCalculator { MatchConstantValues = strict, MatchVariableWeights = strict };
70      return CalculateSimilarity(n1, n2, strict);
71    }
72
73    public static Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode> ComputeBottomUpMapping(ISymbolicExpressionTree t1, ISymbolicExpressionTree t2, bool strict = false) {
74      return ComputeBottomUpMapping(ActualRoot(t1), ActualRoot(t2), strict);
75    }
76
77    public static Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode> ComputeBottomUpMapping(ISymbolicExpressionTreeNode n1, ISymbolicExpressionTreeNode n2, bool strict = false) {
78      var calculator = new SymbolicExpressionTreeBottomUpSimilarityCalculator { MatchConstantValues = strict, MatchVariableWeights = strict };
79      return calculator.ComputeBottomUpMapping(n1, n2);
80    }
81    #endregion
82
[11486]83    public double CalculateSimilarity(ISymbolicExpressionTree t1, ISymbolicExpressionTree t2) {
[17091]84      return CalculateSimilarity(t1, t2, out Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode> map);
85    }
86
87    public double CalculateSimilarity(ISymbolicExpressionTree t1, ISymbolicExpressionTree t2, out NodeMap map) {
88      if (t1 == t2) {
89        map = null;
[11486]90        return 1;
[17091]91      }
92      map = ComputeBottomUpMapping(t1, t2);
93      return 2.0 * map.Count / (t1.Length + t2.Length - 4); // -4 for skipping root and start symbols in the two trees
[11219]94    }
95
96    public override double CalculateSolutionSimilarity(IScope leftSolution, IScope rightSolution) {
[12281]97      if (leftSolution == rightSolution)
98        return 1.0;
99
[11219]100      var t1 = leftSolution.Variables[SolutionVariableName].Value as ISymbolicExpressionTree;
101      var t2 = rightSolution.Variables[SolutionVariableName].Value as ISymbolicExpressionTree;
102
103      if (t1 == null || t2 == null)
104        throw new ArgumentException("Cannot calculate similarity when one of the arguments is null.");
105
[11486]106      var similarity = CalculateSimilarity(t1, t2);
[11224]107      if (similarity > 1.0)
108        throw new Exception("Similarity value cannot be greater than 1");
109
110      return similarity;
[11219]111    }
112
[17091]113    public Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode> ComputeBottomUpMapping(ISymbolicExpressionTree t1, ISymbolicExpressionTree t2) {
114      return ComputeBottomUpMapping(t1.Root.GetSubtree(0).GetSubtree(0), t2.Root.GetSubtree(0).GetSubtree(0));
115    }
116
[11221]117    public Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode> ComputeBottomUpMapping(ISymbolicExpressionTreeNode n1, ISymbolicExpressionTreeNode n2) {
118      var compactedGraph = Compact(n1, n2);
[11219]119
[17091]120      IEnumerable<ISymbolicExpressionTreeNode> Subtrees(ISymbolicExpressionTreeNode node, bool commutative) {
121        var subtrees = node.IterateNodesPrefix();
122        return commutative ? subtrees.OrderBy(x => compactedGraph[x].Hash) : subtrees;
123      }
[11219]124
[17091]125      var nodes1 = n1.IterateNodesPostfix().OrderByDescending(x => x.GetLength()); // by descending length so that largest subtrees are mapped first
126      var nodes2 = (List<ISymbolicExpressionTreeNode>)n2.IterateNodesPostfix();
127
128      var forward = new NodeMap();
129      var reverse = new NodeMap();
130
131      foreach (ISymbolicExpressionTreeNode v in nodes1) {
132        if (forward.ContainsKey(v))
[11225]133          continue;
[17091]134
[11219]135        var kv = compactedGraph[v];
[17091]136        var commutative = v.SubtreeCount > 1 && commutativeSymbols.Contains(kv.Label);
137
138        foreach (ISymbolicExpressionTreeNode w in nodes2) {
139          if (w.GetLength() != kv.Length || w.GetDepth() != kv.Depth || reverse.ContainsKey(w) || compactedGraph[w] != kv)
[11225]140            continue;
[11219]141
[17091]142          // map one whole subtree to the other
143          foreach (var t in Subtrees(v, commutative).Zip(Subtrees(w, commutative), Tuple.Create)) {
144            forward[t.Item1] = t.Item2;
145            reverse[t.Item2] = t.Item1;
146          }
[11225]147
[17091]148          break;
[11219]149        }
150      }
151
[17091]152      return forward;
[11219]153    }
154
155    /// <summary>
156    /// Creates a compact representation of the two trees as a directed acyclic graph
157    /// </summary>
[11229]158    /// <param name="n1">The root of the first tree</param>
159    /// <param name="n2">The root of the second tree</param>
[11219]160    /// <returns>The compacted DAG representing the two trees</returns>
[11229]161    private Dictionary<ISymbolicExpressionTreeNode, GraphNode> Compact(ISymbolicExpressionTreeNode n1, ISymbolicExpressionTreeNode n2) {
162      var nodeMap = new Dictionary<ISymbolicExpressionTreeNode, GraphNode>(); // K
163      var labelMap = new Dictionary<string, GraphNode>(); // L
[11219]164
[11221]165      var nodes = n1.IterateNodesPostfix().Concat(n2.IterateNodesPostfix()); // the disjoint union F
[17091]166      var graph = new List<GraphNode>();
[11219]167
[17091]168      IEnumerable<GraphNode> Subtrees(GraphNode g, bool commutative) {
169        var subtrees = g.SymbolicExpressionTreeNode.Subtrees.Select(x => nodeMap[x]);
170        return commutative ? subtrees.OrderBy(x => x.Hash) : subtrees;
171      }
172
173      foreach (var node in nodes) {
174        var label = GetLabel(node);
175
176        if (node.SubtreeCount == 0) {
[11229]177          if (!labelMap.ContainsKey(label)) {
[17091]178            labelMap[label] = new GraphNode(node, label);
[11225]179          }
[17091]180          nodeMap[node] = labelMap[label];
[11219]181        } else {
[17091]182          var v = new GraphNode(node, label);
[11894]183          bool found = false;
[17091]184          var commutative = node.SubtreeCount > 1 && commutativeSymbols.Contains(label);
[11219]185
[17091]186          var vv = Subtrees(v, commutative);
[11219]187
[17091]188          foreach (var w in graph) {
189            if (v.Depth != w.Depth || v.SubtreeCount != w.SubtreeCount || v.Length != w.Length || v.Label != w.Label) {
[11219]190              continue;
[17091]191            }
[11219]192
[17091]193            var ww = Subtrees(w, commutative);
194            found = vv.SequenceEqual(ww);
[11219]195
[11894]196            if (found) {
[17091]197              nodeMap[node] = w;
[11219]198              break;
199            }
[11229]200          }
[11219]201          if (!found) {
[17091]202            nodeMap[node] = v;
203            graph.Add(v);
[11229]204          }
205        }
[11220]206      }
[11229]207      return nodeMap;
[11219]208    }
209
[17091]210    private string GetLabel(ISymbolicExpressionTreeNode node) {
[11486]211      if (node.SubtreeCount > 0)
212        return node.Symbol.Name;
213
[17091]214      if (node is ConstantTreeNode constant)
215        return MatchConstantValues ? constant.Value.ToString(CultureInfo.InvariantCulture) : constant.Symbol.Name;
[11964]216
[17091]217      if (node is VariableTreeNode variable)
218        return MatchVariableWeights ? variable.Weight + variable.VariableName : variable.VariableName;
[11486]219
220      return node.ToString();
221    }
222
[11229]223    private class GraphNode {
[17091]224      private GraphNode() { }
225
226      public GraphNode(ISymbolicExpressionTreeNode node, string label) {
227        SymbolicExpressionTreeNode = node;
228        Label = label;
229        Hash = GetHashCode();
230        Depth = node.GetDepth();
231        Length = node.GetLength();
232      }
233
234      public int Hash { get; }
235      public ISymbolicExpressionTreeNode SymbolicExpressionTreeNode { get; }
236      public string Label { get; }
237      public int Depth { get; }
[11894]238      public int SubtreeCount { get { return SymbolicExpressionTreeNode.SubtreeCount; } }
[17091]239      public int Length { get; }
[11219]240    }
241  }
242}
Note: See TracBrowser for help on using the repository browser.