Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMatching.cs @ 11385

Last change on this file since 11385 was 11385, checked in by bburlacu, 10 years ago

#1772: Merged changes to SymbolicExpressionTreeMatching.cs from trunk.

  • Property svn:mergeinfo set to (toggle deleted branches)
    /trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMatching.csmergedeligible
    /branches/Benchmarking/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMatching.cs6917-7005
    /branches/CloningRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMatching.cs4656-4721
    /branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMatching.cs5471-5473
    /branches/DataAnalysis SolutionEnsembles/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMatching.cs5815-6180
    /branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMatching.cs4458-4459,​4462,​4464
    /branches/ExportSymbolicDataAnalysisSolutions/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMatching.cs9511-9585
    /branches/GP.Grammar.Editor/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMatching.cs6284-6795
    /branches/GP.Symbols (TimeLag, Diff, Integral)/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMatching.cs5060
    /branches/HLScript/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMatching.cs10331-10358
    /branches/HeuristicLab.DataAnalysis.Symbolic.LinearInterpreter/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMatching.cs9271-9826
    /branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMatching.cs7098-8789
    /branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMatching.cs8388-8942
    /branches/LogResidualEvaluator/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMatching.cs10202-10483
    /branches/NET40/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMatching.cs5138-5162
    /branches/ParallelEngine/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMatching.cs5175-5192
    /branches/ProblemInstancesRegressionAndClassification/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMatching.cs7568-7810
    /branches/QAPAlgorithms/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMatching.cs6350-6627
    /branches/Restructure trunk solution/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMatching.cs6828
    /branches/SpectralKernelForGaussianProcesses/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMatching.cs10204-10479
    /branches/SuccessProgressAnalysis/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMatching.cs5370-5682
    /branches/Trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMatching.cs6829-6865
    /branches/VNS/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMatching.cs5594-5752
    /branches/histogram/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMatching.cs5959-6341
    /stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMatching.cs10032-10033
File size: 5.1 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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
22#endregion
23
24using System;
25using System.Collections.Generic;
26using System.Linq;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28//using HeuristicLab.EvolutionTracking;
29
30namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
31  public static class SymbolicExpressionTreeMatching {
32    public static bool ContainsSubtree(this ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode subtree, ISymbolicExpressionTreeNodeSimilarityComparer comparer) {
33      return FindMatches(root, subtree, comparer).Any();
34    }
35    public static IEnumerable<ISymbolicExpressionTreeNode> FindMatches(ISymbolicExpressionTree tree, ISymbolicExpressionTreeNode subtree, ISymbolicExpressionTreeNodeSimilarityComparer comparer) {
36      return FindMatches(tree.Root, subtree, comparer);
37    }
38
39    public static IEnumerable<ISymbolicExpressionTreeNode> FindMatches(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode subtree, ISymbolicExpressionTreeNodeSimilarityComparer comp) {
40      var fragmentLength = subtree.GetLength();
41      return root.IterateNodesBreadth().Where(n => n.GetLength() >= fragmentLength && Match(n, subtree, comp) >= fragmentLength);
42    }
43
44    ///<summary>
45    /// Finds the longest common subsequence in quadratic time and linear space
46    /// Variant of:
47    /// D. S. Hirschberg. A linear space algorithm for or computing maximal common subsequences. 1975.
48    /// http://dl.acm.org/citation.cfm?id=360861
49    /// </summary>
50    /// <returns>Number of pairs that were matched</returns>
51    public static int Match(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b, ISymbolicExpressionTreeNodeSimilarityComparer comp) {
52      if (!comp.Equals(a, b)) return 0;
53      int m = a.SubtreeCount;
54      int n = b.SubtreeCount;
55      if (m == 0 || n == 0) return 1;
56      var matrix = new int[m + 1, n + 1];
57      for (int i = 1; i <= m; ++i) {
58        var ai = a.GetSubtree(i - 1);
59        for (int j = 1; j <= n; ++j) {
60          var bj = b.GetSubtree(j - 1);
61          int match = Match(ai, bj, comp);
62          matrix[i, j] = Math.Max(Math.Max(matrix[i, j - 1], matrix[i - 1, j]), matrix[i - 1, j - 1] + match);
63        }
64      }
65      return matrix[m, n] + 1;
66    }
67
68    /// <summary>
69    /// Calculates the difference between two symbolic expression trees.
70    /// </summary>
71    /// <param name="tree">The first symbolic expression tree</param>
72    /// <param name="other">The second symbolic expression tree</param>
73    /// <returns>Returns the root of the subtree (from T1) by which T1 differs from T2, or null if no difference is found.</returns>
74    public static ISymbolicExpressionTreeNode Difference(this ISymbolicExpressionTree tree, ISymbolicExpressionTree other) {
75      return Difference(tree.Root, other.Root);
76    }
77
78    public static ISymbolicExpressionTreeNode Difference(this ISymbolicExpressionTreeNode node, ISymbolicExpressionTreeNode other) {
79      var these = node.IterateNodesPrefix().ToList();
80      var others = other.IterateNodesPrefix().ToList();
81
82      var minCount = Math.Min(these.Count, others.Count);
83      var list = new List<ISymbolicExpressionTreeNode>();
84
85      for (int i = 0; i < minCount; ++i) {
86        if (these[i].ToString() != others[i].ToString())
87          list.Add(these[i]);
88      }
89
90      return list.Count > 0 ? LowestCommonAncestor(node, list) : null;
91    }
92
93    private static ISymbolicExpressionTreeNode LowestCommonAncestor(ISymbolicExpressionTreeNode root, List<ISymbolicExpressionTreeNode> nodes) {
94      if (nodes.Count == 0)
95        throw new ArgumentException("The nodes list should contain at least one element.");
96
97      if (nodes.Count == 1)
98        return nodes[0];
99
100      int lowestLevel = nodes.Min(x => root.GetBranchLevel(x));
101
102      // bring the nodes in the nodes to the same level (relative to the root)
103      for (int i = 0; i < nodes.Count; ++i) {
104        var node = nodes[i];
105        var level = root.GetBranchLevel(node);
106        for (int j = lowestLevel; j < level; ++j)
107          node = node.Parent;
108        nodes[i] = node;
109      }
110
111      // while not all the elements in the nodes are equal, go one level up
112      while (nodes.Any(x => x != nodes[0])) {
113        for (int i = 0; i < nodes.Count; ++i)
114          nodes[i] = nodes[i].Parent;
115      }
116
117      return nodes[0];
118    }
119  }
120}
Note: See TracBrowser for help on using the repository browser.