Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/07/13 14:53:43 (11 years ago)
Author:
bburlacu
Message:

#1772: Added new similarity measures and moved them to separate class SymbolicDataAnalysisExpressionTreeSimilarity.cs.

Location:
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj

    r9241 r9293  
    5050    <DebugType>pdbonly</DebugType>
    5151    <Optimize>true</Optimize>
    52     <OutputPath>..\..\..\..\Trunk\sources\bin\</OutputPath>
     52    <OutputPath>..\..\..\..\trunk\sources\bin\</OutputPath>
    5353    <DefineConstants>TRACE</DefineConstants>
    5454    <ErrorReport>prompt</ErrorReport>
     
    6666  </PropertyGroup>
    6767  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x64' ">
    68     <OutputPath>$(SolutionDir)\bin\</OutputPath>
     68    <OutputPath>..\..\..\..\trunk\sources\bin\</OutputPath>
    6969    <DefineConstants>TRACE</DefineConstants>
    7070    <Optimize>true</Optimize>
     
    8484  </PropertyGroup>
    8585  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
    86     <OutputPath>$(SolutionDir)\bin\</OutputPath>
     86    <OutputPath>..\..\..\..\trunk\sources\bin\</OutputPath>
    8787    <DefineConstants>TRACE</DefineConstants>
    8888    <Optimize>true</Optimize>
     
    200200    <Compile Include="Interfaces\ISymbolicDataAnalysisProblem.cs" />
    201201    <Compile Include="SymbolicDataAnalysisExpressionTreeMatching.cs" />
     202    <Compile Include="SymbolicDataAnalysisExpressionTreeSimilarity.cs" />
    202203    <Compile Include="SymbolicDataAnalysisModel.cs">
    203204      <SubType>Code</SubType>
  • branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeMatching.cs

    r9249 r9293  
    161161
    162162public static class SymbolicExpressionTreeMatching {
    163   public static bool IsSimilarTo(this ISymbolicExpressionTree t1, ISymbolicExpressionTree t2, SymbolicExpressionTreeNodeSimilarityComparer comparer) {
    164     return t1.Root.IsSimilarTo(t2.Root, comparer);
    165   }
    166   public static bool IsSimilarTo(this ISymbolicExpressionTreeNode t1, ISymbolicExpressionTreeNode t2, SymbolicExpressionTreeNodeSimilarityComparer comparer) {
    167     return Match(t1, t2, comparer) == Math.Min(t1.GetLength(), t2.GetLength());
    168   }
    169163  public static bool ContainsFragment(this ISymbolicExpressionTreeNode root, IFragment fragment, SymbolicExpressionTreeNodeSimilarityComparer comparer) {
    170164    return FindMatches(root, fragment.Root, comparer).Any();
     
    179173    // below, we use ">=" for Match(n, fragment, comp) >= fragmentLength because in case of relaxed conditions,
    180174    // we can have multiple matches of the same node
     175
    181176    return root.IterateNodesBreadth().Where(n => n.GetLength() >= fragmentLength && Match(n, fragment, comp) == fragmentLength);
    182177  }
     
    206201  }
    207202}
     203
     204public class MaximalCommonSubsequenceCalculator {
     205  private ISymbolicExpressionTreeNode[] x;
     206  private ISymbolicExpressionTreeNode[] y;
     207  private List<ISymbolicExpressionTreeNode> maxCommonSubseq;
     208  private double[,] matrix;
     209
     210  public SymbolicExpressionTreeNodeSimilarityComparer comparer;
     211
     212  public List<ISymbolicExpressionTreeNode> Calculate(ISymbolicExpressionTreeNode n1, ISymbolicExpressionTreeNode n2) {
     213    if (comparer == null) throw new Exception("Comparer cannot be null.");
     214
     215    x = n1.IterateNodesPrefix().ToArray();
     216    y = n2.IterateNodesPrefix().ToArray();
     217
     218    if (maxCommonSubseq == null) maxCommonSubseq = new List<ISymbolicExpressionTreeNode>();
     219    else maxCommonSubseq.Clear();
     220
     221    int n = x.Length;
     222    int m = y.Length;
     223
     224    matrix = new double[n + 1, m + 1];
     225
     226    for (int i = 0; i <= n; ++i) {
     227      for (int j = 0; j <= m; ++j) {
     228        if (i == 0 || j == 0) {
     229          matrix[i, j] = 0;
     230        } else if (comparer.Equals(x[i - 1], y[j - 1])) {
     231          matrix[i, j] = matrix[i - 1, j - 1] + 1;
     232        } else {
     233          matrix[i, j] = Math.Max(matrix[i - 1, j], matrix[i, j - 1]);
     234        }
     235      }
     236    }
     237    recon(n, m);
     238    return new List<ISymbolicExpressionTreeNode>(maxCommonSubseq);
     239  }
     240
     241  private void recon(int i, int j) {
     242    if (i == 0 || j == 0) return;
     243    if (comparer.Equals(x[i - 1], y[j - 1])) {
     244      recon(i - 1, j - 1);
     245      maxCommonSubseq.Add(x[i - 1]);
     246    } else if (matrix[i - 1, j] > matrix[i, j - 1]) {
     247      recon(i - 1, j);
     248    } else recon(i, j - 1);
     249  }
     250}
Note: See TracChangeset for help on using the changeset viewer.