Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.BottomUpTreeDistance/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/TreeDistance/IsomorphicTreeDistanceCalculator.cs @ 11211

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

#2215: Added implementation of the BottomUpTreeDistanceCalculator in branches/HeuristicLab.BottomUpTreeDistance.

File size: 3.0 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 HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26
27namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
28  [StorableClass]
29  [Item("Isomorphic tree distance calculator", "An operator that computes the distance between two symbolic expression trees based on the size of their largest common subtree.")]
30  public class IsomorphicTreeDistanceCalculator : Item, ISymbolicExpressionTreeDistanceCalculator {
31    private readonly SymbolicExpressionTreeNodeSimilarityComparer comparer;
32
33    public IsomorphicTreeDistanceCalculator() {
34      comparer = new SymbolicExpressionTreeNodeSimilarityComparer {
35        MatchConstantValues = true,
36        MatchVariableNames = true,
37        MatchVariableWeights = true
38      };
39    }
40
41    protected IsomorphicTreeDistanceCalculator(IsomorphicTreeDistanceCalculator original, Cloner cloner)
42      : base(original, cloner) {
43      this.comparer = original.comparer;
44    }
45
46    public override IDeepCloneable Clone(Cloner cloner) {
47      return new IsomorphicTreeDistanceCalculator(this, cloner);
48    }
49
50    public double CalculateDistance(ISymbolicExpressionTree t1, ISymbolicExpressionTree t2) {
51      return 1 - MaxCommonSubtreeSimilarity(t1, t2, comparer); // we return the complement of the similarity to resemble the notion of a "distance"
52    }
53
54    public static double MaxCommonSubtreeSimilarity(ISymbolicExpressionTree a, ISymbolicExpressionTree b, SymbolicExpressionTreeNodeSimilarityComparer comparer) {
55      int max = 0;
56      var rootA = a.Root.GetSubtree(0).GetSubtree(0);
57      var rootB = b.Root.GetSubtree(0).GetSubtree(0);
58      foreach (var aa in rootA.IterateNodesBreadth()) {
59        int lenA = aa.GetLength();
60        if (lenA <= max) continue;
61        foreach (var bb in rootB.IterateNodesBreadth()) {
62          int lenB = bb.GetLength();
63          if (lenB <= max) continue;
64          int matches = SymbolicExpressionTreeMatching.Match(aa, bb, comparer);
65          if (max < matches) max = matches;
66        }
67      }
68      return 2.0 * max / (rootA.GetLength() + rootB.GetLength());
69    }
70  }
71}
Note: See TracBrowser for help on using the repository browser.