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