#region License Information
/* HeuristicLab
* Copyright (C) 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 System;
using System.Collections.Generic;
using HEAL.Attic;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
using HeuristicLab.Parameters;
namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
[Item("Pearson R² & Average Similarity Evaluator", "Calculates the Pearson R² and the average similarity of a symbolic regression solution candidate.")]
[StorableType("FE514989-E619-48B8-AC8E-9A2202708F65")]
public class PearsonRSquaredAverageSimilarityEvaluator : SymbolicRegressionMultiObjectiveEvaluator {
private const string StrictSimilarityParameterName = "StrictSimilarity";
private const string AverageSimilarityParameterName = "AverageSimilarity";
private readonly object locker = new object();
private readonly SymbolicDataAnalysisExpressionTreeAverageSimilarityCalculator SimilarityCalculator = new SymbolicDataAnalysisExpressionTreeAverageSimilarityCalculator();
public IFixedValueParameter StrictSimilarityParameter {
get { return (IFixedValueParameter)Parameters[StrictSimilarityParameterName]; }
}
public ILookupParameter AverageSimilarityParameter {
get { return (ILookupParameter)Parameters[AverageSimilarityParameterName]; }
}
public bool StrictSimilarity {
get { return StrictSimilarityParameter.Value.Value; }
}
[StorableConstructor]
protected PearsonRSquaredAverageSimilarityEvaluator(StorableConstructorFlag _) : base(_) { }
protected PearsonRSquaredAverageSimilarityEvaluator(PearsonRSquaredAverageSimilarityEvaluator original, Cloner cloner)
: base(original, cloner) {
}
public override IDeepCloneable Clone(Cloner cloner) {
return new PearsonRSquaredAverageSimilarityEvaluator(this, cloner);
}
public PearsonRSquaredAverageSimilarityEvaluator() : base() {
Parameters.Add(new FixedValueParameter(StrictSimilarityParameterName, "Use strict similarity calculation.", new BoolValue(false)));
Parameters.Add(new LookupParameter(AverageSimilarityParameterName));
}
public override IEnumerable Maximization { get { return new bool[2] { true, false }; } } // maximize R² and minimize average similarity
public override IOperation InstrumentedApply() {
IEnumerable rows = GenerateRowsToEvaluate();
var tree = SymbolicExpressionTreeParameter.ActualValue;
var problemData = ProblemDataParameter.ActualValue;
var interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue;
var estimationLimits = EstimationLimitsParameter.ActualValue;
var applyLinearScaling = ApplyLinearScalingParameter.ActualValue.Value;
if (UseParameterOptimization) {
SymbolicRegressionParameterOptimizationEvaluator.OptimizeParameters(interpreter, tree, problemData, rows, applyLinearScaling, ParameterOptimizationIterations, updateVariableWeights: ParameterOptimizationUpdateVariableWeights, lowerEstimationLimit: estimationLimits.Lower, upperEstimationLimit: estimationLimits.Upper);
}
double r2 = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(
tree, problemData, rows, interpreter, applyLinearScaling,
estimationLimits.Lower, estimationLimits.Upper);
if (DecimalPlaces >= 0)
r2 = Math.Round(r2, DecimalPlaces);
lock (locker) {
if (AverageSimilarityParameter.ActualValue == null) {
var context = new ExecutionContext(null, SimilarityCalculator, ExecutionContext.Scope.Parent);
SimilarityCalculator.StrictSimilarity = StrictSimilarity;
SimilarityCalculator.Execute(context, CancellationToken);
}
}
var avgSimilarity = AverageSimilarityParameter.ActualValue.Value;
QualitiesParameter.ActualValue = new DoubleArray(new[] { r2, avgSimilarity });
return base.InstrumentedApply();
}
public override double[] Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable rows) {
SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
AverageSimilarityParameter.ExecutionContext = context;
EstimationLimitsParameter.ExecutionContext = context;
ApplyLinearScalingParameter.ExecutionContext = context;
var estimationLimits = EstimationLimitsParameter.ActualValue;
var applyLinearScaling = ApplyLinearScalingParameter.ActualValue.Value;
double r2 = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(
tree, problemData, rows,
SymbolicDataAnalysisTreeInterpreterParameter.ActualValue,
applyLinearScaling,
estimationLimits.Lower, estimationLimits.Upper);
lock (locker) {
if (AverageSimilarityParameter.ActualValue == null) {
var ctx = new ExecutionContext(null, SimilarityCalculator, context.Scope.Parent);
SimilarityCalculator.StrictSimilarity = StrictSimilarity;
SimilarityCalculator.Execute(context, CancellationToken);
}
}
var avgSimilarity = AverageSimilarityParameter.ActualValue.Value;
SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
EstimationLimitsParameter.ExecutionContext = null;
ApplyLinearScalingParameter.ExecutionContext = null;
return new[] { r2, avgSimilarity };
}
}
}