Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/TreeMatching/SymbolicExpressionTreePhenotypicSimilarityCalculator.cs @ 12449

Last change on this file since 12449 was 12281, checked in by mkommend, 10 years ago

#2326: Merged r12103, 12108, r12110 and r12122 into stable.

File size: 4.2 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 System;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27using HeuristicLab.Optimization.Operators;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
31  [Item("SymbolicExpressionTreePhenotypicSimilarityCalculator", "An operator that calculates the similarity betweeon two trees based on the correlation of their outputs.")]
32  [StorableClass]
33  public class SymbolicExpressionTreePhenotypicSimilarityCalculator : SolutionSimilarityCalculator {
34    [Storable]
35    public IDataAnalysisProblemData ProblemData { get; set; }
36    [Storable]
37    public ISymbolicDataAnalysisExpressionTreeInterpreter Interpreter { get; set; }
38
39    protected override bool IsCommutative { get { return true; } }
40
41    [StorableConstructor]
42    protected SymbolicExpressionTreePhenotypicSimilarityCalculator(bool deserializing) : base(deserializing) { }
43
44    public SymbolicExpressionTreePhenotypicSimilarityCalculator(SymbolicExpressionTreePhenotypicSimilarityCalculator original, Cloner cloner)
45      : base(original, cloner) {
46      this.ProblemData = cloner.Clone(original.ProblemData);
47      this.Interpreter = cloner.Clone(original.Interpreter);
48    }
49
50    public override IDeepCloneable Clone(Cloner cloner) {
51      return new SymbolicExpressionTreePhenotypicSimilarityCalculator(this, cloner);
52    }
53
54    public SymbolicExpressionTreePhenotypicSimilarityCalculator() { }
55
56    public double CalculateSimilarity(ISymbolicExpressionTree t1, ISymbolicExpressionTree t2) {
57      if (Interpreter == null || ProblemData == null)
58        throw new InvalidOperationException("Cannot calculate phenotypic similarity when no interpreter or problem data were set.");
59
60      var v1 = Interpreter.GetSymbolicExpressionTreeValues(t1, ProblemData.Dataset, ProblemData.TrainingIndices);
61      var v2 = Interpreter.GetSymbolicExpressionTreeValues(t2, ProblemData.Dataset, ProblemData.TrainingIndices);
62
63      if (v1.Variance().IsAlmost(0) && v2.Variance().IsAlmost(0))
64        return 1.0;
65
66      OnlineCalculatorError error;
67      var r2 = OnlinePearsonsRSquaredCalculator.Calculate(v1, v2, out error);
68
69      if (r2 > 1.0)
70        r2 = 1.0;
71
72      return error == OnlineCalculatorError.None ? r2 : 0;
73    }
74
75    public override double CalculateSolutionSimilarity(IScope leftSolution, IScope rightSolution) {
76      if (leftSolution == rightSolution)
77        return 1.0;
78
79      if (!leftSolution.Variables.ContainsKey("EstimatedValues") || !rightSolution.Variables.ContainsKey("EstimatedValues"))
80        throw new ArgumentException("No estimated values are present in the subscopes.");
81
82      var leftValues = (DoubleArray)leftSolution.Variables["EstimatedValues"].Value;
83      var rightValues = (DoubleArray)rightSolution.Variables["EstimatedValues"].Value;
84
85      if (leftValues.Variance().IsAlmost(0) && rightValues.Variance().IsAlmost(0))
86        return 1.0;
87
88      OnlineCalculatorError error;
89      var r2 = OnlinePearsonsRSquaredCalculator.Calculate(leftValues, rightValues, out error);
90
91      if (r2 > 1.0)
92        r2 = 1.0; // sometimes due to fp errors it can happen that the r2 is over 1 (like 1.0000000009)
93
94      return error == OnlineCalculatorError.None ? r2 : 0;
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.