#region License Information
/* HeuristicLab
* Copyright (C) 2002-2017 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.Linq;
using System.Threading;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Problems.DataAnalysis;
namespace HeuristicLab.Algorithms.DataAnalysis {
[StorableClass]
[Item("GaussianProcessLeaf", "A leaf type that uses gaussian process models as leaf models.")]
public class GaussianProcessLeaf : LeafBase {
#region ParameterNames
public const string TriesParameterName = "Tries";
public const string RegressionParameterName = "Regression";
#endregion
#region ParameterProperties
public IFixedValueParameter TriesParameter {
get { return Parameters[TriesParameterName] as IFixedValueParameter; }
}
public IFixedValueParameter RegressionParameter {
get { return Parameters[RegressionParameterName] as IFixedValueParameter; }
}
#endregion
#region Properties
public int Tries {
get { return TriesParameter.Value.Value; }
}
public GaussianProcessRegression Regression {
get { return RegressionParameter.Value; }
}
#endregion
#region Constructors & Cloning
[StorableConstructor]
protected GaussianProcessLeaf(bool deserializing) : base(deserializing) { }
protected GaussianProcessLeaf(GaussianProcessLeaf original, Cloner cloner) : base(original, cloner) { }
public GaussianProcessLeaf() {
var gp = new GaussianProcessRegression();
gp.CovarianceFunctionParameter.Value = new CovarianceRationalQuadraticIso();
gp.MeanFunctionParameter.Value = new MeanLinear();
Parameters.Add(new FixedValueParameter(TriesParameterName, "Number of repetitions", new IntValue(10)));
Parameters.Add(new FixedValueParameter(RegressionParameterName, "The algorithm creating GPmodels", gp));
}
public override IDeepCloneable Clone(Cloner cloner) {
return new GaussianProcessLeaf(this, cloner);
}
#endregion
#region IModelType
public override bool ProvidesConfidence {
get { return true; }
}
public override IRegressionModel Build(IRegressionProblemData pd, IRandom random, CancellationToken cancellationToken, out int noParameters) {
if (pd.Dataset.Rows < MinLeafSize(pd)) throw new ArgumentException("The number of training instances is too small to create a gaussian process model");
Regression.Problem = new RegressionProblem {ProblemData = pd};
var cvscore = double.MaxValue;
GaussianProcessRegressionSolution sol = null;
for (var i = 0; i < Tries; i++) {
var res = RegressionTreeUtilities.RunSubAlgorithm(Regression, random.Next(), cancellationToken);
var t = res.Select(x => x.Value).OfType().FirstOrDefault();
var score = ((DoubleValue)res["Negative log pseudo-likelihood (LOO-CV)"].Value).Value;
if (score >= cvscore || t == null || double.IsNaN(t.TrainingRSquared)) continue;
cvscore = score;
sol = t;
}
Regression.Runs.Clear();
if (sol == null) throw new ArgumentException("Could not create Gaussian Process model");
noParameters = pd.Dataset.Rows + 1
+ Regression.CovarianceFunction.GetNumberOfParameters(pd.AllowedInputVariables.Count())
+ Regression.MeanFunction.GetNumberOfParameters(pd.AllowedInputVariables.Count());
return sol.Model;
}
public override int MinLeafSize(IRegressionProblemData pd) {
return 3;
}
#endregion
}
}