Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/07/15 12:11:09 (10 years ago)
Author:
bburlacu
Message:

#1772: Merged trunk changes.

Location:
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic

  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/TreeMatching/SymbolicExpressionTreePhenotypicSimilarityCalculator.cs

    r12017 r12155  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    2020#endregion
    2121
     22using System;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Core;
     
    2829
    2930namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    30   [Item("PhenotypicSimilarityCalculator", "An operator that calculates the similarity betweeon two trees based on the correlation of their outputs.")]
     31  [Item("SymbolicExpressionTreePhenotypicSimilarityCalculator", "An operator that calculates the similarity betweeon two trees based on the correlation of their outputs.")]
    3132  [StorableClass]
    32   public class PhenotypicSimilarityCalculator : SingleObjectiveSolutionSimilarityCalculator, ISymbolicDataAnalysisExpressionSimilarityCalculator {
     33  public class SymbolicExpressionTreePhenotypicSimilarityCalculator : SolutionSimilarityCalculator {
     34    [Storable]
    3335    public IDataAnalysisProblemData ProblemData { get; set; }
     36    [Storable]
    3437    public ISymbolicDataAnalysisExpressionTreeInterpreter Interpreter { get; set; }
    3538
     39    protected override bool IsCommutative { get { return true; } }
     40
    3641    [StorableConstructor]
    37     protected PhenotypicSimilarityCalculator(bool deserializing) : base(deserializing) { }
     42    protected SymbolicExpressionTreePhenotypicSimilarityCalculator(bool deserializing) : base(deserializing) { }
    3843
    39     public PhenotypicSimilarityCalculator(PhenotypicSimilarityCalculator original, Cloner cloner)
     44    public SymbolicExpressionTreePhenotypicSimilarityCalculator(SymbolicExpressionTreePhenotypicSimilarityCalculator original, Cloner cloner)
    4045      : base(original, cloner) {
    41       this.ProblemData = original.ProblemData;
    42       this.Interpreter = original.Interpreter;
     46      this.ProblemData = cloner.Clone(original.ProblemData);
     47      this.Interpreter = cloner.Clone(original.Interpreter);
    4348    }
    4449
    4550    public override IDeepCloneable Clone(Cloner cloner) {
    46       return new PhenotypicSimilarityCalculator(this, cloner);
     51      return new SymbolicExpressionTreePhenotypicSimilarityCalculator(this, cloner);
    4752    }
    4853
    49     public PhenotypicSimilarityCalculator() { }
     54    public SymbolicExpressionTreePhenotypicSimilarityCalculator() { }
    5055
    5156    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
    5260      var v1 = Interpreter.GetSymbolicExpressionTreeValues(t1, ProblemData.Dataset, ProblemData.TrainingIndices);
    5361      var v2 = Interpreter.GetSymbolicExpressionTreeValues(t2, ProblemData.Dataset, ProblemData.TrainingIndices);
     62
     63      if (v1.Variance().IsAlmost(0) && v2.Variance().IsAlmost(0))
     64        return 1.0;
    5465
    5566      OnlineCalculatorError error;
     
    6374
    6475    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
    6582      var leftValues = (DoubleArray)leftSolution.Variables["EstimatedValues"].Value;
    6683      var rightValues = (DoubleArray)rightSolution.Variables["EstimatedValues"].Value;
     84
     85      if (leftValues.Variance().IsAlmost(0) && rightValues.Variance().IsAlmost(0))
     86        return 1.0;
     87
    6788      OnlineCalculatorError error;
    6889      var r2 = OnlinePearsonsRSquaredCalculator.Calculate(leftValues, rightValues, out error);
Note: See TracChangeset for help on using the changeset viewer.