[11219] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15583] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
[11486] | 24 | using System.Globalization;
|
---|
[11219] | 25 | using System.Linq;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 29 | using HeuristicLab.Optimization.Operators;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 |
|
---|
[16283] | 32 | using NodeMap = System.Collections.Generic.Dictionary<HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ISymbolicExpressionTreeNode, HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ISymbolicExpressionTreeNode>;
|
---|
| 33 |
|
---|
[11221] | 34 | namespace 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.")]
|
---|
[12103] | 37 | public class SymbolicExpressionTreeBottomUpSimilarityCalculator : SolutionSimilarityCalculator {
|
---|
[11486] | 38 | private readonly HashSet<string> commutativeSymbols = new HashSet<string> { "Addition", "Multiplication", "Average", "And", "Or", "Xor" };
|
---|
[12070] | 39 |
|
---|
[12103] | 40 | public SymbolicExpressionTreeBottomUpSimilarityCalculator() { }
|
---|
[12070] | 41 | protected override bool IsCommutative { get { return true; } }
|
---|
| 42 |
|
---|
[16278] | 43 | public bool MatchConstantValues { get; set; }
|
---|
| 44 | public bool MatchVariableWeights { get; set; }
|
---|
| 45 |
|
---|
[11918] | 46 | [StorableConstructor]
|
---|
[11921] | 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 |
|
---|
[16283] | 59 | #region static methods
|
---|
| 60 | private static ISymbolicExpressionTreeNode ActualRoot(ISymbolicExpressionTree tree) {
|
---|
| 61 | return tree.Root.GetSubtree(0).GetSubtree(0);
|
---|
| 62 | }
|
---|
[11486] | 63 |
|
---|
[16283] | 64 | public static double CalculateSimilarity(ISymbolicExpressionTree t1, ISymbolicExpressionTree t2, bool strict = false) {
|
---|
| 65 | return CalculateSimilarity(ActualRoot(t1), ActualRoot(t2), strict);
|
---|
[11219] | 66 | }
|
---|
| 67 |
|
---|
[16283] | 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 |
|
---|
| 83 | public double CalculateSimilarity(ISymbolicExpressionTree t1, ISymbolicExpressionTree t2) {
|
---|
| 84 | return CalculateSimilarity(t1, t2, out Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode> map);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | public double CalculateSimilarity(ISymbolicExpressionTree t1, ISymbolicExpressionTree t2, out NodeMap map) {
|
---|
[16278] | 88 | if (t1 == t2) {
|
---|
| 89 | map = null;
|
---|
| 90 | return 1;
|
---|
| 91 | }
|
---|
[16283] | 92 | map = ComputeBottomUpMapping(t1, t2);
|
---|
[16278] | 93 | return 2.0 * map.Count / (t1.Length + t2.Length - 4); // -4 for skipping root and start symbols in the two trees
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[11219] | 96 | public override double CalculateSolutionSimilarity(IScope leftSolution, IScope rightSolution) {
|
---|
[12103] | 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)
|
---|
[14354] | 108 | throw new Exception("Similarity value cannot be greater than 1");
|
---|
[11224] | 109 |
|
---|
| 110 | return similarity;
|
---|
[11219] | 111 | }
|
---|
| 112 |
|
---|
[16283] | 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 |
|
---|
[16283] | 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 |
|
---|
[16283] | 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();
|
---|
[16278] | 127 |
|
---|
[16283] | 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;
|
---|
[16278] | 134 |
|
---|
[11219] | 135 | var kv = compactedGraph[v];
|
---|
[16283] | 136 | var commutative = v.SubtreeCount > 1 && commutativeSymbols.Contains(kv.Label);
|
---|
[16278] | 137 |
|
---|
[16283] | 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;
|
---|
[16278] | 141 |
|
---|
[16283] | 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;
|
---|
[16278] | 146 | }
|
---|
[11219] | 147 |
|
---|
[16283] | 148 | break;
|
---|
[16278] | 149 | }
|
---|
[11219] | 150 | }
|
---|
| 151 |
|
---|
[16283] | 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
|
---|
[16278] | 166 | var graph = new List<GraphNode>();
|
---|
[11219] | 167 |
|
---|
[16283] | 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 |
|
---|
[16278] | 173 | foreach (var node in nodes) {
|
---|
| 174 | var label = GetLabel(node);
|
---|
| 175 |
|
---|
| 176 | if (node.SubtreeCount == 0) {
|
---|
[11229] | 177 | if (!labelMap.ContainsKey(label)) {
|
---|
[16283] | 178 | labelMap[label] = new GraphNode(node, label);
|
---|
[11225] | 179 | }
|
---|
[16278] | 180 | nodeMap[node] = labelMap[label];
|
---|
[11219] | 181 | } else {
|
---|
[16278] | 182 | var v = new GraphNode(node, label);
|
---|
[11894] | 183 | bool found = false;
|
---|
[16278] | 184 | var commutative = node.SubtreeCount > 1 && commutativeSymbols.Contains(label);
|
---|
[11219] | 185 |
|
---|
[16283] | 186 | var vv = Subtrees(v, commutative);
|
---|
[11219] | 187 |
|
---|
[16283] | 188 | foreach (var w in graph) {
|
---|
[16278] | 189 | if (v.Depth != w.Depth || v.SubtreeCount != w.SubtreeCount || v.Length != w.Length || v.Label != w.Label) {
|
---|
[11219] | 190 | continue;
|
---|
[16278] | 191 | }
|
---|
[11219] | 192 |
|
---|
[16283] | 193 | var ww = Subtrees(w, commutative);
|
---|
[16278] | 194 | found = vv.SequenceEqual(ww);
|
---|
[11219] | 195 |
|
---|
[11894] | 196 | if (found) {
|
---|
[16278] | 197 | nodeMap[node] = w;
|
---|
[11219] | 198 | break;
|
---|
| 199 | }
|
---|
[11229] | 200 | }
|
---|
[11219] | 201 | if (!found) {
|
---|
[16278] | 202 | nodeMap[node] = v;
|
---|
| 203 | graph.Add(v);
|
---|
[11229] | 204 | }
|
---|
| 205 | }
|
---|
[11220] | 206 | }
|
---|
[11229] | 207 | return nodeMap;
|
---|
[11219] | 208 | }
|
---|
| 209 |
|
---|
[16278] | 210 | private string GetLabel(ISymbolicExpressionTreeNode node) {
|
---|
[11486] | 211 | if (node.SubtreeCount > 0)
|
---|
| 212 | return node.Symbol.Name;
|
---|
| 213 |
|
---|
[16278] | 214 | if (node is ConstantTreeNode constant)
|
---|
| 215 | return MatchConstantValues ? constant.Value.ToString(CultureInfo.InvariantCulture) : constant.Symbol.Name;
|
---|
[11950] | 216 |
|
---|
[16278] | 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 {
|
---|
[16278] | 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; } }
|
---|
[16278] | 239 | public int Length { get; }
|
---|
[11219] | 240 | }
|
---|
| 241 | }
|
---|
| 242 | }
|
---|