#region License Information /* HeuristicLab * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; using HeuristicLab.Optimization.Operators; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Problems.DataAnalysis.Symbolic { [Item("SymbolicExpressionTreePhenotypicSimilarityCalculator", "An operator that calculates the similarity betweeon two trees based on the correlation of their outputs.")] [StorableClass] public class SymbolicExpressionTreePhenotypicSimilarityCalculator : SingleObjectiveSolutionSimilarityCalculator { [Storable] public IDataAnalysisProblemData ProblemData { get; set; } [Storable] public ISymbolicDataAnalysisExpressionTreeInterpreter Interpreter { get; set; } [StorableConstructor] protected SymbolicExpressionTreePhenotypicSimilarityCalculator(bool deserializing) : base(deserializing) { } public SymbolicExpressionTreePhenotypicSimilarityCalculator(SymbolicExpressionTreePhenotypicSimilarityCalculator original, Cloner cloner) : base(original, cloner) { this.ProblemData = cloner.Clone(original.ProblemData); this.Interpreter = cloner.Clone(original.Interpreter); } public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicExpressionTreePhenotypicSimilarityCalculator(this, cloner); } public SymbolicExpressionTreePhenotypicSimilarityCalculator() { } public double CalculateSimilarity(ISymbolicExpressionTree t1, ISymbolicExpressionTree t2) { var v1 = Interpreter.GetSymbolicExpressionTreeValues(t1, ProblemData.Dataset, ProblemData.TrainingIndices); var v2 = Interpreter.GetSymbolicExpressionTreeValues(t2, ProblemData.Dataset, ProblemData.TrainingIndices); OnlineCalculatorError error; var r2 = OnlinePearsonsRSquaredCalculator.Calculate(v1, v2, out error); if (r2 > 1.0) r2 = 1.0; return error == OnlineCalculatorError.None ? r2 : 0; } public override double CalculateSolutionSimilarity(IScope leftSolution, IScope rightSolution) { if (leftSolution == rightSolution) return 1.0; var leftValues = (DoubleArray)leftSolution.Variables["EstimatedValues"].Value; var rightValues = (DoubleArray)rightSolution.Variables["EstimatedValues"].Value; if (leftValues.Variance().IsAlmost(0) && rightValues.Variance().IsAlmost(0)) return 1.0; OnlineCalculatorError error; var r2 = OnlinePearsonsRSquaredCalculator.Calculate(leftValues, rightValues, out error); if (r2 > 1.0) r2 = 1.0; // sometimes due to fp errors it can happen that the r2 is over 1 (like 1.0000000009) return error == OnlineCalculatorError.None ? r2 : 0; } } }