Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SimilarityCalculators/MaxCommonSubtreeSimilarityCalculator.cs @ 11458

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

#1772: HeuristicLab.Problems.DataAnalysis.Symbolic:

  • Merged trunk changes (SymbolicExpressionImporter)
  • Added Passthrough symbol (does not perform an operation, just transfers the input), adjusted interpreter and opcodes accordingly
  • Added diversity preserving crossover
  • Replaced IDistanceCalculator interface with ISymbolicDataAnalysisExpressionSimilarityCalculator and adjusted similarity calculators
  • Refactored tracing, moved functionality to the TraceCalculator (when this is complete the old code will be removed)
File size: 4.4 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 HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
26using HeuristicLab.Optimization.Operators;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
30  [StorableClass]
31  [Item("MaxCommonSubtreeSimilarityCalculator", "A similarity calculator based on the size of the maximum common subtree between two trees")]
32  public class MaxCommonSubtreeSimilarityCalculator : SingleObjectiveSolutionSimilarityCalculator, ISymbolicDataAnalysisExpressionSimilarityCalculator {
33    private readonly SymbolicExpressionTreeNodeSimilarityComparer comparer;
34    public bool MatchVariableNames {
35      get { return comparer.MatchVariableNames; }
36      set { comparer.MatchVariableNames = value; }
37    }
38
39    public bool MatchVariableWeights {
40      get { return comparer.MatchVariableWeights; }
41      set { comparer.MatchVariableWeights = value; }
42    }
43
44    public bool MatchConstantValues {
45      get { return comparer.MatchConstantValues; }
46      set { comparer.MatchConstantValues = value; }
47    }
48
49    [StorableConstructor]
50    protected MaxCommonSubtreeSimilarityCalculator(bool deserializing) : base(deserializing) { }
51
52    public override IDeepCloneable Clone(Cloner cloner) {
53      return new MaxCommonSubtreeSimilarityCalculator(this, cloner);
54    }
55
56    protected MaxCommonSubtreeSimilarityCalculator(MaxCommonSubtreeSimilarityCalculator original, Cloner cloner)
57      : base(original, cloner) {
58    }
59
60    public MaxCommonSubtreeSimilarityCalculator() {
61      comparer = new SymbolicExpressionTreeNodeSimilarityComparer {
62        MatchConstantValues = true,
63        MatchVariableNames = true,
64        MatchVariableWeights = true
65      };
66    }
67
68    public MaxCommonSubtreeSimilarityCalculator(bool matchVariableNames, bool matchVariableWeights, bool matchConstantValues) {
69      comparer = new SymbolicExpressionTreeNodeSimilarityComparer {
70        MatchConstantValues = matchConstantValues,
71        MatchVariableNames = matchVariableNames,
72        MatchVariableWeights = matchVariableWeights
73      };
74    }
75
76    public double CalculateSimilarity(ISymbolicExpressionTree t1, ISymbolicExpressionTree t2) {
77      return MaxCommonSubtreeSimilarity(t1, t2, comparer);
78    }
79
80    public override double CalculateSolutionSimilarity(IScope leftSolution, IScope rightSolution) {
81      var t1 = leftSolution.Variables[SolutionVariableName].Value as ISymbolicExpressionTree;
82      var t2 = rightSolution.Variables[SolutionVariableName].Value as ISymbolicExpressionTree;
83
84      if (t1 == null || t2 == null)
85        throw new ArgumentException("Cannot calculate similarity when one of the arguments is null.");
86
87      return MaxCommonSubtreeSimilarity(t1, t2, comparer);
88    }
89
90    public static double MaxCommonSubtreeSimilarity(ISymbolicExpressionTree a, ISymbolicExpressionTree b, SymbolicExpressionTreeNodeSimilarityComparer comparer) {
91      int max = 0;
92      var rootA = a.Root.GetSubtree(0).GetSubtree(0);
93      var rootB = b.Root.GetSubtree(0).GetSubtree(0);
94      foreach (var aa in rootA.IterateNodesBreadth()) {
95        int lenA = aa.GetLength();
96        if (lenA <= max) continue;
97        foreach (var bb in rootB.IterateNodesBreadth()) {
98          int lenB = bb.GetLength();
99          if (lenB <= max) continue;
100          int matches = SymbolicExpressionTreeMatching.Match(aa, bb, comparer);
101          if (matches == lenB && max < matches)
102            max = matches;
103        }
104      }
105      return 2.0 * max / (rootA.GetLength() + rootB.GetLength());
106    }
107  }
108}
Note: See TracBrowser for help on using the repository browser.