#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 HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Operators;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HEAL.Attic;
using HeuristicLab.Problems.DataAnalysis;
namespace HeuristicLab.Algorithms.DataAnalysis {
[StorableType("9255DA02-C545-4952-AB36-5A588EE41407")]
[Item(Name = "GaussianProcessRegressionSolutionCreator",
Description = "Creates a Gaussian process solution from a trained model.")]
public sealed class GaussianProcessRegressionSolutionCreator : SingleSuccessorOperator {
private const string ProblemDataParameterName = "ProblemData";
private const string ModelParameterName = "GaussianProcessRegressionModel";
private const string SolutionParameterName = "Solution";
private const string ResultsParameterName = "Results";
private const string TrainingRSquaredResultName = "Training R²";
private const string TestRSquaredResultName = "Test R²";
private const string CreateSolutionParameterName = "CreateSolution";
private const string NegLogPseudoLikelihood = "Negative log pseudo-likelihood (LOO-CV)";
#region Parameter Properties
public ILookupParameter ProblemDataParameter {
get { return (ILookupParameter)Parameters[ProblemDataParameterName]; }
}
public ILookupParameter SolutionParameter {
get { return (ILookupParameter)Parameters[SolutionParameterName]; }
}
public ILookupParameter ModelParameter {
get { return (ILookupParameter)Parameters[ModelParameterName]; }
}
public ILookupParameter ResultsParameter {
get { return (ILookupParameter)Parameters[ResultsParameterName]; }
}
public ILookupParameter CreateSolutionParameter {
get { return (ILookupParameter)Parameters[CreateSolutionParameterName]; }
}
#endregion
[StorableConstructor]
private GaussianProcessRegressionSolutionCreator(StorableConstructorFlag _) : base(_) { }
private GaussianProcessRegressionSolutionCreator(GaussianProcessRegressionSolutionCreator original, Cloner cloner) : base(original, cloner) { }
public GaussianProcessRegressionSolutionCreator()
: base() {
// in
Parameters.Add(new LookupParameter(ProblemDataParameterName, "The regression problem data for the Gaussian process solution."));
Parameters.Add(new LookupParameter(ModelParameterName, "The Gaussian process regression model to use for the solution."));
Parameters.Add(new LookupParameter(CreateSolutionParameterName, "Flag that indicates if a solution should be produced at the end of the run"));
// in & out
Parameters.Add(new LookupParameter(ResultsParameterName, "The result collection of the algorithm."));
// out
Parameters.Add(new LookupParameter(SolutionParameterName, "The produced Gaussian process solution."));
}
[StorableHook(HookType.AfterDeserialization)]
private void AfterDeserialization() {
// BackwardsCompatibility3.3
#region Backwards compatible code, remove with 3.4
if (!Parameters.ContainsKey(CreateSolutionParameterName)) {
Parameters.Add(new LookupParameter(CreateSolutionParameterName, "Flag that indicates if a solution should be produced at the end of the run"));
}
#endregion
}
public override IDeepCloneable Clone(Cloner cloner) {
return new GaussianProcessRegressionSolutionCreator(this, cloner);
}
public override IOperation Apply() {
if (ModelParameter.ActualValue != null && CreateSolutionParameter.ActualValue.Value == true) {
var m = (IGaussianProcessModel)ModelParameter.ActualValue.Clone();
m.FixParameters();
var data = (IRegressionProblemData)ProblemDataParameter.ActualValue.Clone();
var s = new GaussianProcessRegressionSolution(m, data);
SolutionParameter.ActualValue = s;
var results = ResultsParameter.ActualValue;
if (!results.ContainsKey(SolutionParameterName)) {
results.Add(new Result(SolutionParameterName, "The Gaussian process regression solution", s));
results.Add(new Result(TrainingRSquaredResultName,
"The Pearson's R² of the Gaussian process solution on the training partition.",
new DoubleValue(s.TrainingRSquared)));
results.Add(new Result(TestRSquaredResultName,
"The Pearson's R² of the Gaussian process solution on the test partition.",
new DoubleValue(s.TestRSquared)));
results.Add(new Result(NegLogPseudoLikelihood,
"The negative log pseudo-likelihood (from leave-one-out-cross-validation).",
new DoubleValue(m.LooCvNegativeLogPseudoLikelihood)));
} else {
results[SolutionParameterName].Value = s;
results[TrainingRSquaredResultName].Value = new DoubleValue(s.TrainingRSquared);
results[TestRSquaredResultName].Value = new DoubleValue(s.TestRSquared);
results[NegLogPseudoLikelihood].Value = new DoubleValue(m.LooCvNegativeLogPseudoLikelihood);
}
}
return base.Apply();
}
}
}