Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeSimilarityCalculator.cs @ 12009

Last change on this file since 12009 was 12009, checked in by ascheibe, 9 years ago

#2212 updated copyright year

File size: 5.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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    private readonly ISymbolicExpressionTreeDistanceCalculator distanceCalculator;
42
43    public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
44      get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
45    }
46    public IValueParameter<ISymbolicExpressionTree> CurrentSymbolicExpressionTreeParameter {
47      get { return (IValueParameter<ISymbolicExpressionTree>)Parameters[CurrentSymbolicExpressionTreeParameterName]; }
48    }
49
50    public ILookupParameter<DoubleValue> SimilarityParameter {
51      get { return (ILookupParameter<DoubleValue>)Parameters[SimilarityValuesParmeterName]; }
52    }
53    public ISymbolicExpressionTree CurrentSymbolicExpressionTree {
54      get { return CurrentSymbolicExpressionTreeParameter.Value; }
55      set { CurrentSymbolicExpressionTreeParameter.Value = value; }
56    }
57
58    public int MaximumTreeDepth { get; set; }
59
60    protected SymbolicDataAnalysisExpressionTreeSimilarityCalculator(
61      SymbolicDataAnalysisExpressionTreeSimilarityCalculator original, Cloner cloner)
62      : base(original, cloner) {
63    }
64
65    public override IDeepCloneable Clone(Cloner cloner) {
66      return new SymbolicDataAnalysisExpressionTreeSimilarityCalculator(this, cloner);
67    }
68
69    [StorableConstructor]
70    protected SymbolicDataAnalysisExpressionTreeSimilarityCalculator(bool deserializing)
71      : base(deserializing) {
72    }
73
74    private SymbolicDataAnalysisExpressionTreeSimilarityCalculator() {
75    }
76
77    public SymbolicDataAnalysisExpressionTreeSimilarityCalculator(ISymbolicExpressionTreeDistanceCalculator distanceCalculator)
78      : base() {
79      this.distanceCalculator = distanceCalculator;
80
81      Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees to analyze."));
82      Parameters.Add(new ValueParameter<ISymbolicExpressionTree>(CurrentSymbolicExpressionTreeParameterName, ""));
83      Parameters.Add(new LookupParameter<BoolValue>(MatchVariablesParameterName, "Specify if the symbolic expression tree comparer should match variable names."));
84      Parameters.Add(new LookupParameter<BoolValue>(MatchVariableWeightsParameterName, "Specify if the symbolic expression tree comparer should match variable weghts."));
85      Parameters.Add(new LookupParameter<BoolValue>(MatchConstantValuesParameterName, "Specify if the symbolic expression tree comparer should match constant values."));
86      Parameters.Add(new LookupParameter<DoubleValue>(SimilarityValuesParmeterName, ""));
87    }
88
89    public override IOperation Apply() {
90      var trees = SymbolicExpressionTreeParameter.ActualValue;
91
92      double similarity = 0.0;
93      var current = CurrentSymbolicExpressionTree;
94
95      bool found = false;
96      foreach (var tree in trees) {
97        if (tree == current) {
98          found = true;
99          continue;
100        }
101
102        if (found) {
103          var distance = distanceCalculator.CalculateDistance(current, tree);
104          similarity += 1 - distance;
105        }
106      }
107
108      lock (SimilarityParameter.ActualValue) {
109        SimilarityParameter.ActualValue.Value += similarity;
110      }
111      return base.Apply();
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.