Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeSimilarityCalculator.cs @ 10302

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

#1772: - Added a ViewHost in the right side of the GenealogyGraphView which displays the encoding-specific content when a GenealogyGraphNode is clicked.

  • Migrated new SymbolicExpressionTreeChart (drawing the tree using the ReingoldTilfordLayoutEngine) to the new branch
  • Copied SymbolicDataAnalysisExpressionTreeMatching.cs and SymbolicDataAnalysisExpressionTreeSimilarityCalculator.cs to the new branch
File size: 6.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Data;
25using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
26using HeuristicLab.Operators;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
31  [StorableClass]
32  public class SymbolicDataAnalysisExpressionTreeSimilarityCalculator : SingleSuccessorOperator {
33    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
34    private const string CurrentSymbolicExpressionTreeParameterName = "CurrentSymbolicExpressionTree";
35    private const string SimilarityValuesParmeterName = "Similarity";
36    // comparer parameters
37    private const string MatchVariablesParameterName = "MatchVariableNames";
38    private const string MatchVariableWeightsParameterName = "MatchVariableWeights";
39    private const string MatchConstantValuesParameterName = "MatchConstantValues";
40
41    public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
42      get {
43        return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName];
44      }
45    }
46
47    public IValueParameter<ISymbolicExpressionTree> CurrentSymbolicExpressionTreeParameter {
48      get { return (IValueParameter<ISymbolicExpressionTree>)Parameters[CurrentSymbolicExpressionTreeParameterName]; }
49    }
50
51    public ILookupParameter<BoolValue> MatchVariableNamesParameter {
52      get { return (ILookupParameter<BoolValue>)Parameters[MatchVariablesParameterName]; }
53    }
54
55    public ILookupParameter<BoolValue> MatchVariableWeightsParameter {
56      get { return (ILookupParameter<BoolValue>)Parameters[MatchVariableWeightsParameterName]; }
57    }
58
59    public ILookupParameter<BoolValue> MatchConstantValuesParameter {
60      get { return (ILookupParameter<BoolValue>)Parameters[MatchConstantValuesParameterName]; }
61    }
62
63    public ILookupParameter<DoubleValue> SimilarityParameter {
64      get { return (ILookupParameter<DoubleValue>)Parameters[SimilarityValuesParmeterName]; }
65    }
66
67    public ISymbolicExpressionTree CurrentSymbolicExpressionTree {
68      get { return CurrentSymbolicExpressionTreeParameter.Value; }
69      set { CurrentSymbolicExpressionTreeParameter.Value = value; }
70    }
71
72    public SymbolicExpressionTreeNodeSimilarityComparer SimilarityComparer { get; set; }
73
74    public int MaximumTreeDepth { get; set; }
75
76    protected SymbolicDataAnalysisExpressionTreeSimilarityCalculator(
77      SymbolicDataAnalysisExpressionTreeSimilarityCalculator original, Cloner cloner)
78      : base(original, cloner) {
79    }
80
81    public override IDeepCloneable Clone(Cloner cloner) {
82      return new SymbolicDataAnalysisExpressionTreeSimilarityCalculator(this, cloner);
83    }
84
85    [StorableConstructor]
86    protected SymbolicDataAnalysisExpressionTreeSimilarityCalculator(bool deserializing)
87      : base(deserializing) {
88    }
89
90    public SymbolicDataAnalysisExpressionTreeSimilarityCalculator()
91      : base() {
92      Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName,
93        "The symbolic expression trees to analyze."));
94      Parameters.Add(new ValueParameter<ISymbolicExpressionTree>(CurrentSymbolicExpressionTreeParameterName, ""));
95      Parameters.Add(new LookupParameter<BoolValue>(MatchVariablesParameterName,
96        "Specify if the symbolic expression tree comparer should match variable names."));
97      Parameters.Add(new LookupParameter<BoolValue>(MatchVariableWeightsParameterName,
98        "Specify if the symbolic expression tree comparer should match variable weights."));
99      Parameters.Add(new LookupParameter<BoolValue>(MatchConstantValuesParameterName,
100        "Specify if the symbolic expression tree comparer should match constant values."));
101      Parameters.Add(new LookupParameter<DoubleValue>(SimilarityValuesParmeterName, ""));
102    }
103
104    public override IOperation Apply() {
105      var trees = SymbolicExpressionTreeParameter.ActualValue;
106
107      double similarity = 0.0;
108      var current = CurrentSymbolicExpressionTree;
109
110      bool found = false;
111      foreach (var tree in trees) {
112        if (tree == current) {
113          found = true;
114          continue;
115        }
116
117        if (found) {
118          similarity += MaxCommonSubtreeSimilarity(current, tree, SimilarityComparer);
119        }
120      }
121
122      lock (SimilarityParameter.ActualValue) {
123        SimilarityParameter.ActualValue.Value += similarity;
124      }
125      return base.Apply();
126    }
127
128    public static double CalculateSimilarity(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b,
129      SymbolicExpressionTreeNodeSimilarityComparer comp) {
130      return 2.0 * SymbolicExpressionTreeMatching.Match(a, b, comp) / (a.GetLength() + b.GetLength());
131    }
132
133    public static double MaxCommonSubtreeSimilarity(ISymbolicExpressionTree a, ISymbolicExpressionTree b,
134      SymbolicExpressionTreeNodeSimilarityComparer comparer) {
135      double max = 0;
136      var rootA = a.Root.GetSubtree(0).GetSubtree(0);
137      var rootB = b.Root.GetSubtree(0).GetSubtree(0);
138      foreach (var aa in rootA.IterateNodesBreadth()) {
139        int lenA = aa.GetLength();
140        if (lenA <= max) continue;
141        foreach (var bb in rootB.IterateNodesBreadth()) {
142          int lenB = bb.GetLength();
143          if (lenB <= max) continue;
144          int matches = SymbolicExpressionTreeMatching.Match(aa, bb, comparer);
145          if (max < matches) max = matches;
146        }
147      }
148      return 2.0 * max / (rootA.GetLength() + rootB.GetLength());
149    }
150
151    // returns true if both nodes are variables, or both are constants, or both are functions
152    private static bool SameType(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
153      if (a is VariableTreeNode) {
154        return b is VariableTreeNode;
155      }
156      if (a is ConstantTreeNode) {
157        return b is ConstantTreeNode;
158      }
159      return true;
160    }
161  }
162}
Note: See TracBrowser for help on using the repository browser.