1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2014 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 HeuristicLab.Common;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Data;
|
---|
25 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
26 | using HeuristicLab.Optimization.Operators;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
30 | [Item("SymbolicExpressionTreePhenotypicSimilarityCalculator", "An operator that calculates the similarity betweeon two trees based on the correlation of their outputs.")]
|
---|
31 | [StorableClass]
|
---|
32 | public class SymbolicExpressionTreePhenotypicSimilarityCalculator : SingleObjectiveSolutionSimilarityCalculator {
|
---|
33 | public IDataAnalysisProblemData ProblemData { get; set; }
|
---|
34 | public ISymbolicDataAnalysisExpressionTreeInterpreter Interpreter { get; set; }
|
---|
35 |
|
---|
36 | [StorableConstructor]
|
---|
37 | protected SymbolicExpressionTreePhenotypicSimilarityCalculator(bool deserializing) : base(deserializing) { }
|
---|
38 |
|
---|
39 | public SymbolicExpressionTreePhenotypicSimilarityCalculator(SymbolicExpressionTreePhenotypicSimilarityCalculator original, Cloner cloner)
|
---|
40 | : base(original, cloner) {
|
---|
41 | this.ProblemData = original.ProblemData;
|
---|
42 | this.Interpreter = original.Interpreter;
|
---|
43 | }
|
---|
44 |
|
---|
45 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
46 | return new SymbolicExpressionTreePhenotypicSimilarityCalculator(this, cloner);
|
---|
47 | }
|
---|
48 |
|
---|
49 | public SymbolicExpressionTreePhenotypicSimilarityCalculator() { }
|
---|
50 |
|
---|
51 | public double CalculateSimilarity(ISymbolicExpressionTree t1, ISymbolicExpressionTree t2) {
|
---|
52 | var v1 = Interpreter.GetSymbolicExpressionTreeValues(t1, ProblemData.Dataset, ProblemData.TrainingIndices);
|
---|
53 | var v2 = Interpreter.GetSymbolicExpressionTreeValues(t2, ProblemData.Dataset, ProblemData.TrainingIndices);
|
---|
54 |
|
---|
55 | OnlineCalculatorError error;
|
---|
56 | var r2 = OnlinePearsonsRSquaredCalculator.Calculate(v1, v2, out error);
|
---|
57 |
|
---|
58 | if (r2 > 1.0)
|
---|
59 | r2 = 1.0;
|
---|
60 |
|
---|
61 | return error == OnlineCalculatorError.None ? r2 : 0;
|
---|
62 | }
|
---|
63 |
|
---|
64 | public override double CalculateSolutionSimilarity(IScope leftSolution, IScope rightSolution) {
|
---|
65 | var leftValues = (DoubleArray)leftSolution.Variables["EstimatedValues"].Value;
|
---|
66 | var rightValues = (DoubleArray)rightSolution.Variables["EstimatedValues"].Value;
|
---|
67 | OnlineCalculatorError error;
|
---|
68 | var r2 = OnlinePearsonsRSquaredCalculator.Calculate(leftValues, rightValues, out error);
|
---|
69 |
|
---|
70 | if (r2 > 1.0)
|
---|
71 | r2 = 1.0; // sometimes due to fp errors it can happen that the r2 is over 1 (like 1.0000000009)
|
---|
72 |
|
---|
73 | return error == OnlineCalculatorError.None ? r2 : 0;
|
---|
74 | }
|
---|
75 | }
|
---|
76 | }
|
---|