Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMaxCommonSequenceCalculator.cs @ 10833

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

#1772: Fixed another minor display bug concerning elite individuals. Fixed bug when saving fragments from mutation. Displayed quality as well in the SymbolicExpressionTreeTile.

  • Property svn:mergeinfo set to (toggle deleted branches)
    /trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMaxCommonSequenceCalculator.csmergedeligible
    /branches/Benchmarking/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMaxCommonSequenceCalculator.cs6917-7005
    /branches/CloningRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMaxCommonSequenceCalculator.cs4656-4721
    /branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMaxCommonSequenceCalculator.cs5471-5473
    /branches/DataAnalysis SolutionEnsembles/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMaxCommonSequenceCalculator.cs5815-6180
    /branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMaxCommonSequenceCalculator.cs4458-4459,​4462,​4464
    /branches/ExportSymbolicDataAnalysisSolutions/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMaxCommonSequenceCalculator.cs9511-9585
    /branches/GP.Grammar.Editor/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMaxCommonSequenceCalculator.cs6284-6795
    /branches/GP.Symbols (TimeLag, Diff, Integral)/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMaxCommonSequenceCalculator.cs5060
    /branches/HLScript/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMaxCommonSequenceCalculator.cs10331-10358
    /branches/HeuristicLab.DataAnalysis.Symbolic.LinearInterpreter/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMaxCommonSequenceCalculator.cs9271-9826
    /branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMaxCommonSequenceCalculator.cs7098-8789
    /branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMaxCommonSequenceCalculator.cs8388-8942
    /branches/LogResidualEvaluator/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMaxCommonSequenceCalculator.cs10202-10483
    /branches/NET40/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMaxCommonSequenceCalculator.cs5138-5162
    /branches/ParallelEngine/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMaxCommonSequenceCalculator.cs5175-5192
    /branches/ProblemInstancesRegressionAndClassification/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMaxCommonSequenceCalculator.cs7568-7810
    /branches/QAPAlgorithms/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMaxCommonSequenceCalculator.cs6350-6627
    /branches/Restructure trunk solution/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMaxCommonSequenceCalculator.cs6828
    /branches/SpectralKernelForGaussianProcesses/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMaxCommonSequenceCalculator.cs10204-10479
    /branches/SuccessProgressAnalysis/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMaxCommonSequenceCalculator.cs5370-5682
    /branches/Trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMaxCommonSequenceCalculator.cs6829-6865
    /branches/VNS/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMaxCommonSequenceCalculator.cs5594-5752
    /branches/histogram/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMaxCommonSequenceCalculator.cs5959-6341
    /stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMaxCommonSequenceCalculator.cs10032-10033
File size: 2.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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;
24
25namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Matching {
26  public class MaxCommonSequenceCalculator<T, TComp>
27    where T : class
28    where TComp : IEqualityComparer<T> {
29
30    private TComp comparer;
31    public TComp Comparer {
32      get { return comparer; }
33      set { comparer = value; }
34    }
35
36    private int[,] matrix;
37    private List<T> sequence;
38
39    public MaxCommonSequenceCalculator(TComp comparer) {
40      this.comparer = comparer;
41    }
42
43    /// <summary>
44    /// Calculate the maximal common subsequence between arrays a and b and return it.
45    /// THIS DOES NOT WORK FOR TREES
46    /// </summary>
47    /// <param name="a"></param>
48    /// <param name="b"></param>
49    /// <returns></returns>
50    public IEnumerable<T> Calculate(T[] a, T[] b) {
51      int m = a.Length;
52      int n = b.Length;
53
54      if (m == 0 || n == 0) return null;
55      sequence = new List<T>();
56      matrix = new int[m + 1, n + 1];
57
58      for (int i = 0; i <= m; ++i) {
59        for (int j = 0; j <= n; ++j) {
60          if (comparer.Equals(a[i - 1], b[j - 1])) {
61            matrix[i, j] = matrix[i - 1, j - 1] + 1;
62          } else {
63            matrix[i, j] = Math.Max(matrix[i - 1, j], matrix[i, j - 1]);
64          }
65        }
66      }
67      Recon(a, b, n, m); // build the common subsequence
68      return sequence;
69    }
70
71    private void Recon(T[] a, T[] b, int i, int j) {
72      while (i > 0 && j > 0) {
73        if (comparer.Equals(a[i - 1], b[j - 1])) {
74          Recon(a, b, i - 1, j - 1);
75          sequence.Add(a[i - 1]);
76        } else if (matrix[i - 1, j] > matrix[i, j - 1]) {
77          i = i - 1;
78          continue;
79        } else {
80          j = j - 1;
81          continue;
82        }
83        break;
84      }
85    }
86  }
87}
Note: See TracBrowser for help on using the repository browser.