[12029] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16565] | 3 | * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[12029] | 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 |
|
---|
[12068] | 22 | using System;
|
---|
[12029] | 23 | using HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 27 | using HeuristicLab.Optimization.Operators;
|
---|
[16565] | 28 | using HEAL.Attic;
|
---|
[12029] | 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.")]
|
---|
[16565] | 32 | [StorableType("163CE915-506E-472B-B77F-156ECC9D9C05")]
|
---|
[12086] | 33 | public class SymbolicExpressionTreePhenotypicSimilarityCalculator : SolutionSimilarityCalculator {
|
---|
[12064] | 34 | [Storable]
|
---|
[12029] | 35 | public IDataAnalysisProblemData ProblemData { get; set; }
|
---|
[12064] | 36 | [Storable]
|
---|
[12029] | 37 | public ISymbolicDataAnalysisExpressionTreeInterpreter Interpreter { get; set; }
|
---|
| 38 |
|
---|
[12086] | 39 | protected override bool IsCommutative { get { return true; } }
|
---|
| 40 |
|
---|
[12029] | 41 | [StorableConstructor]
|
---|
[16565] | 42 | protected SymbolicExpressionTreePhenotypicSimilarityCalculator(StorableConstructorFlag _) : base(_) { }
|
---|
[12029] | 43 |
|
---|
| 44 | public SymbolicExpressionTreePhenotypicSimilarityCalculator(SymbolicExpressionTreePhenotypicSimilarityCalculator original, Cloner cloner)
|
---|
| 45 | : base(original, cloner) {
|
---|
[12064] | 46 | this.ProblemData = cloner.Clone(original.ProblemData);
|
---|
| 47 | this.Interpreter = cloner.Clone(original.Interpreter);
|
---|
[12029] | 48 | }
|
---|
| 49 |
|
---|
| 50 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 51 | return new SymbolicExpressionTreePhenotypicSimilarityCalculator(this, cloner);
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[12086] | 54 | public SymbolicExpressionTreePhenotypicSimilarityCalculator() { }
|
---|
[12029] | 55 |
|
---|
| 56 | public double CalculateSimilarity(ISymbolicExpressionTree t1, ISymbolicExpressionTree t2) {
|
---|
[12068] | 57 | if (Interpreter == null || ProblemData == null)
|
---|
| 58 | throw new InvalidOperationException("Cannot calculate phenotypic similarity when no interpreter or problem data were set.");
|
---|
| 59 |
|
---|
[12029] | 60 | var v1 = Interpreter.GetSymbolicExpressionTreeValues(t1, ProblemData.Dataset, ProblemData.TrainingIndices);
|
---|
| 61 | var v2 = Interpreter.GetSymbolicExpressionTreeValues(t2, ProblemData.Dataset, ProblemData.TrainingIndices);
|
---|
| 62 |
|
---|
[12068] | 63 | if (v1.Variance().IsAlmost(0) && v2.Variance().IsAlmost(0))
|
---|
| 64 | return 1.0;
|
---|
| 65 |
|
---|
[12029] | 66 | OnlineCalculatorError error;
|
---|
[12641] | 67 | var r = OnlinePearsonsRCalculator.Calculate(v1, v2, out error);
|
---|
[12029] | 68 |
|
---|
[12839] | 69 | var r2 = error == OnlineCalculatorError.None ? r * r : 0;
|
---|
[12029] | 70 |
|
---|
[12839] | 71 | if (r2 > 1.0)
|
---|
| 72 | r2 = 1.0;
|
---|
| 73 |
|
---|
| 74 | return r2;
|
---|
[12029] | 75 | }
|
---|
| 76 |
|
---|
| 77 | public override double CalculateSolutionSimilarity(IScope leftSolution, IScope rightSolution) {
|
---|
[12049] | 78 | if (leftSolution == rightSolution)
|
---|
| 79 | return 1.0;
|
---|
| 80 |
|
---|
[12068] | 81 | if (!leftSolution.Variables.ContainsKey("EstimatedValues") || !rightSolution.Variables.ContainsKey("EstimatedValues"))
|
---|
| 82 | throw new ArgumentException("No estimated values are present in the subscopes.");
|
---|
| 83 |
|
---|
[12029] | 84 | var leftValues = (DoubleArray)leftSolution.Variables["EstimatedValues"].Value;
|
---|
| 85 | var rightValues = (DoubleArray)rightSolution.Variables["EstimatedValues"].Value;
|
---|
[12055] | 86 |
|
---|
| 87 | if (leftValues.Variance().IsAlmost(0) && rightValues.Variance().IsAlmost(0))
|
---|
| 88 | return 1.0;
|
---|
| 89 |
|
---|
[12029] | 90 | OnlineCalculatorError error;
|
---|
[12641] | 91 | var r = OnlinePearsonsRCalculator.Calculate(leftValues, rightValues, out error);
|
---|
[12029] | 92 |
|
---|
[12839] | 93 | var r2 = error == OnlineCalculatorError.None ? r * r : 0;
|
---|
[14354] | 94 |
|
---|
| 95 | if (r2 > 1.0)
|
---|
| 96 | r2 = 1.0;
|
---|
| 97 |
|
---|
[12839] | 98 | return r2;
|
---|
[12029] | 99 | }
|
---|
| 100 | }
|
---|
| 101 | }
|
---|