Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeSimilarity.cs @ 9293

Last change on this file since 9293 was 9293, checked in by bburlacu, 12 years ago

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

File size: 7.6 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
22
23using System;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.Operators;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
34  [StorableClass]
35  public class SymbolicDataAnalysisExpressionTreeSimilarityCalculator : SingleSuccessorOperator {
36    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
37    private const string CurrentSymbolicExpressionTreeParameterName = "CurrentSymbolicExpressionTree";
38    private const string SimilarityValuesParmeterName = "Similarity";
39    // comparer parameters
40    private const string MatchVariablesParameterName = "MatchVariableNames";
41    private const string MatchVariableWeightsParameterName = "MatchVariableWeights";
42    private const string MatchConstantValuesParameterName = "MatchConstantValues";
43
44
45    public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
46      get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
47    }
48    public IValueParameter<ISymbolicExpressionTree> CurrentSymbolicExpressionTreeParameter {
49      get { return (IValueParameter<ISymbolicExpressionTree>)Parameters[CurrentSymbolicExpressionTreeParameterName]; }
50    }
51    public ILookupParameter<BoolValue> MatchVariableNamesParameter {
52      get { return (ILookupParameter<BoolValue>)Parameters[MatchVariablesParameterName]; }
53    }
54    public ILookupParameter<BoolValue> MatchVariableWeightsParameter {
55      get { return (ILookupParameter<BoolValue>)Parameters[MatchVariableWeightsParameterName]; }
56    }
57    public ILookupParameter<BoolValue> MatchConstantValuesParameter {
58      get { return (ILookupParameter<BoolValue>)Parameters[MatchConstantValuesParameterName]; }
59    }
60    public ILookupParameter<DoubleValue> SimilarityParameter {
61      get { return (ILookupParameter<DoubleValue>)Parameters[SimilarityValuesParmeterName]; }
62    }
63
64    public ISymbolicExpressionTree CurrentSymbolicExpressionTree {
65      get { return CurrentSymbolicExpressionTreeParameter.Value; }
66      set { CurrentSymbolicExpressionTreeParameter.Value = value; }
67    }
68
69    public SymbolicExpressionTreeNodeSimilarityComparer SimilarityComparer { get; set; }
70
71    protected SymbolicDataAnalysisExpressionTreeSimilarityCalculator(SymbolicDataAnalysisExpressionTreeSimilarityCalculator original, Cloner cloner) : base(original, cloner) { }
72    public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicDataAnalysisExpressionTreeSimilarityCalculator(this, cloner); }
73    [StorableConstructor]
74    protected SymbolicDataAnalysisExpressionTreeSimilarityCalculator(bool deserializing) : base(deserializing) { }
75
76    public SymbolicDataAnalysisExpressionTreeSimilarityCalculator()
77      : base() {
78      Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees to analyze."));
79      Parameters.Add(new ValueParameter<ISymbolicExpressionTree>(CurrentSymbolicExpressionTreeParameterName, ""));
80      Parameters.Add(new LookupParameter<BoolValue>(MatchVariablesParameterName, "Specify if the symbolic expression tree comparer should match variable names."));
81      Parameters.Add(new LookupParameter<BoolValue>(MatchVariableWeightsParameterName, "Specify if the symbolic expression tree comparer should match variable weights."));
82      Parameters.Add(new LookupParameter<BoolValue>(MatchConstantValuesParameterName, "Specify if the symbolic expression tree comparer should match constant values."));
83      Parameters.Add(new LookupParameter<DoubleValue>(SimilarityValuesParmeterName, ""));
84    }
85
86    public override IOperation Apply() {
87      var trees = SymbolicExpressionTreeParameter.ActualValue;
88
89      bool found = false;
90      double similarity = 0.0;
91      var current = CurrentSymbolicExpressionTree;
92
93      foreach (var tree in trees) {
94        if (tree == current) { found = true; }
95        if (!found) continue;
96
97        similarity += SymbolicDataAnalysisExpressionTreeSimilarity.MaxCommonSubtreeSimilarity(current, tree, SimilarityComparer);
98      }
99      lock (SimilarityParameter.ActualValue) {
100        SimilarityParameter.ActualValue.Value += similarity;
101      }
102      return base.Apply();
103    }
104  }
105
106
107
108  public static class SymbolicDataAnalysisExpressionTreeSimilarity {
109    /// <summary>
110    /// Returns a similarity value based on the size of the maximum common subtree according to the given equality comparison.
111    /// </summary>
112    /// <param name="a"></param>
113    /// <param name="b"></param>
114    /// <param name="comparer"></param>
115    /// <returns>Similarity degree between the two trees, scaled between [0,1], where 1 = similar, 0 = non-similar</returns>
116    public static double MaxCommonSubtreeSimilarity(ISymbolicExpressionTree a, ISymbolicExpressionTree b, SymbolicExpressionTreeNodeSimilarityComparer comparer) {
117      double max = 0;
118      foreach (var aa in a.Root.GetSubtree(0).GetSubtree(0).IterateNodesBreadth()) {
119        int lenA = aa.GetLength();
120        if (lenA <= max) continue;
121        foreach (var bb in b.Root.GetSubtree(0).GetSubtree(0).IterateNodesBreadth()) {
122          int lenB = bb.GetLength();
123          if (lenB <= max) continue;
124          int matches = SymbolicExpressionTreeMatching.Match(aa, bb, comparer);
125          if (max < matches) max = matches;
126        }
127      }
128      return max / Math.Max(a.Length, b.Length);
129    }
130
131    private static double CalculateSimilarity(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b, SymbolicExpressionTreeNodeSimilarityComparer comp) {
132      return (double)SymbolicExpressionTreeMatching.Match(a, b, comp) / Math.Max(a.GetLength(), b.GetLength());
133    }
134
135    public static double CalculateCompoundSimilarity(ISymbolicExpressionTree a, ISymbolicExpressionTree b, SymbolicExpressionTreeNodeSimilarityComparer comparer) {
136      var nA = a.Root.GetSubtree(0).GetSubtree(0);
137      var nB = b.Root.GetSubtree(0).GetSubtree(0);
138
139      var itemsA = nA.IterateNodesBreadth().Where(n => n.SubtreeCount > 0).Select(n => new MatchItem { Node = n, Matched = false }).ToArray();
140      var itemsB = nB.IterateNodesBreadth().Where(n => n.SubtreeCount > 0).Select(n => new MatchItem { Node = n, Matched = false }).ToArray();
141
142
143      double similaritySum = 0;
144
145      foreach (var ia in itemsA) {
146        foreach (var ib in itemsB) {
147          similaritySum += CalculateSimilarity(ia.Node, ib.Node, comparer);
148        }
149      }
150      return similaritySum / (itemsA.Length * itemsB.Length);
151    }
152  }
153
154  class MatchItem {
155    public ISymbolicExpressionTreeNode Node;
156    public bool Matched;
157  }
158}
Note: See TracBrowser for help on using the repository browser.