source: branches/2847_M5Regression/HeuristicLab.Algorithms.DataAnalysis/3.4/M5Regression/LeafTypes/GaussianProcessLeaf.cs @ 16847

Last change on this file since 16847 was 16847, checked in by gkronber, 2 months ago

#2847: made some minor changes while reviewing

File size: 4.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2017 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Linq;
24using System.Threading;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Parameters;
29using HeuristicLab.Problems.DataAnalysis;
30using HEAL.Attic;
31
32namespace HeuristicLab.Algorithms.DataAnalysis {
33  [StorableType("852B9F7D-9C2B-4574-BB71-EE70106EA809")]
34  [Item("GaussianProcessLeaf", "A leaf type that uses Gaussian process models as leaf models.")]
35  public class GaussianProcessLeaf : LeafBase {
36    #region ParameterNames
37    public const string TriesParameterName = "Tries";
38    public const string RegressionParameterName = "Regression";
39    #endregion
40
41    #region ParameterProperties
42    public IFixedValueParameter<IntValue> TriesParameter {
43      get { return (IFixedValueParameter<IntValue>)Parameters[TriesParameterName]; }
44    }
45    public IValueParameter<GaussianProcessRegression> RegressionParameter {
46      get { return (IValueParameter<GaussianProcessRegression>)Parameters[RegressionParameterName]; }
47    }
48    #endregion
49
50    #region Properties
51    public int Tries {
52      get { return TriesParameter.Value.Value; }
53      set { TriesParameter.Value.Value = value; }
54    }
55    public GaussianProcessRegression Regression {
56      get { return RegressionParameter.Value; }
57      set { RegressionParameter.Value = value; }
58    }
59    #endregion
60
61    #region Constructors & Cloning
62    [StorableConstructor]
63    protected GaussianProcessLeaf(StorableConstructorFlag _) : base(_) { }
64    protected GaussianProcessLeaf(GaussianProcessLeaf original, Cloner cloner) : base(original, cloner) { }
65    public GaussianProcessLeaf() {
66      var gp = new GaussianProcessRegression();
67      gp.CovarianceFunctionParameter.Value = new CovarianceRationalQuadraticIso();
68      gp.MeanFunctionParameter.Value = new MeanLinear();
69
70      Parameters.Add(new FixedValueParameter<IntValue>(TriesParameterName, "Number of restarts (default = 10)", new IntValue(10)));
71      Parameters.Add(new ValueParameter<GaussianProcessRegression>(RegressionParameterName, "The algorithm creating Gaussian process models", gp));
72    }
73    public override IDeepCloneable Clone(Cloner cloner) {
74      return new GaussianProcessLeaf(this, cloner);
75    }
76    #endregion
77
78    #region IModelType
79    public override bool ProvidesConfidence {
80      get { return true; }
81    }
82
83    public override IRegressionModel Build(IRegressionProblemData pd, IRandom random, CancellationToken cancellationToken, out int numberOfParameters) {
84      if (pd.Dataset.Rows < MinLeafSize(pd)) throw new ArgumentException("The number of training instances is too small to create a Gaussian process model");
85      Regression.Problem = new RegressionProblem { ProblemData = pd };
86      var cvscore = double.MaxValue;
87      GaussianProcessRegressionSolution sol = null;
88
89      for (var i = 0; i < Tries; i++) {
90        var res = RegressionTreeUtilities.RunSubAlgorithm(Regression, random.Next(), cancellationToken);
91        var t = res.Select(x => x.Value).OfType<GaussianProcessRegressionSolution>().FirstOrDefault();
92        var score = ((DoubleValue)res["Negative log pseudo-likelihood (LOO-CV)"].Value).Value;
93        if (score >= cvscore || t == null || double.IsNaN(t.TrainingRSquared)) continue;
94        cvscore = score;
95        sol = t;
96      }
97      Regression.Runs.Clear();
98      if (sol == null) throw new ArgumentException("Could not create Gaussian process model");
99
100      numberOfParameters = pd.Dataset.Rows + 1
101                           + Regression.CovarianceFunction.GetNumberOfParameters(pd.AllowedInputVariables.Count())
102                           + Regression.MeanFunction.GetNumberOfParameters(pd.AllowedInputVariables.Count());
103      return sol.Model;
104    }
105
106    public override int MinLeafSize(IRegressionProblemData pd) {
107      return 3;
108    }
109    #endregion
110  }
111}
Note: See TracBrowser for help on using the repository browser.