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