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 |
|
---|
22 | using System;
|
---|
23 | using System.Linq;
|
---|
24 | using System.Threading;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 | using HeuristicLab.Problems.DataAnalysis;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
33 | [StorableClass]
|
---|
34 | [Item("GaussianProcessLeaf", "A leaf type that uses gaussian process models as leaf models.")]
|
---|
35 | public class GaussianProcessLeaf : ParameterizedNamedItem, ILeafType<IGaussianProcessModel> {
|
---|
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 Parameters[TriesParameterName] as IFixedValueParameter<IntValue>; }
|
---|
44 | }
|
---|
45 | public IFixedValueParameter<GaussianProcessRegression> RegressionParameter {
|
---|
46 | get { return Parameters[RegressionParameterName] as IFixedValueParameter<GaussianProcessRegression>; }
|
---|
47 | }
|
---|
48 | #endregion
|
---|
49 |
|
---|
50 | #region Properties
|
---|
51 | public int Tries {
|
---|
52 | get { return TriesParameter.Value.Value; }
|
---|
53 | }
|
---|
54 | public GaussianProcessRegression Regression {
|
---|
55 | get { return RegressionParameter.Value; }
|
---|
56 | }
|
---|
57 | #endregion
|
---|
58 |
|
---|
59 | #region Constructors & Cloning
|
---|
60 | [StorableConstructor]
|
---|
61 | private GaussianProcessLeaf(bool deserializing) : base(deserializing) { }
|
---|
62 | private GaussianProcessLeaf(GaussianProcessLeaf original, Cloner cloner) : base(original, cloner) { }
|
---|
63 | public GaussianProcessLeaf() {
|
---|
64 | var gp = new GaussianProcessRegression();
|
---|
65 | gp.CovarianceFunctionParameter.Value = new CovarianceRationalQuadraticIso();
|
---|
66 | gp.MeanFunctionParameter.Value = new MeanLinear();
|
---|
67 |
|
---|
68 | Parameters.Add(new FixedValueParameter<IntValue>(TriesParameterName, "Number of repetitions", new IntValue(10)));
|
---|
69 | Parameters.Add(new FixedValueParameter<GaussianProcessRegression>(RegressionParameterName, "The algorithm creating GPmodels", gp));
|
---|
70 | }
|
---|
71 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
72 | return new GaussianProcessLeaf(this, cloner);
|
---|
73 | }
|
---|
74 | #endregion
|
---|
75 |
|
---|
76 | #region IModelType
|
---|
77 | public IGaussianProcessModel BuildModel(IRegressionProblemData pd, IRandom random, CancellationToken cancellation, out int noParameters) {
|
---|
78 | if (pd.Dataset.Rows < MinLeafSize(pd)) throw new ArgumentException("The number of training instances is too small to create a linear model");
|
---|
79 | Regression.Problem = new RegressionProblem {ProblemData = pd};
|
---|
80 | var cvscore = double.MaxValue;
|
---|
81 | GaussianProcessRegressionSolution sol = null;
|
---|
82 |
|
---|
83 | for (var i = 0; i < Tries; i++) {
|
---|
84 | var res = M5StaticUtilities.RunSubAlgorithm(Regression, random.Next(), cancellation);
|
---|
85 | var t = res.Select(x => x.Value).OfType<GaussianProcessRegressionSolution>().FirstOrDefault();
|
---|
86 | var score = ((DoubleValue) res["Negative log pseudo-likelihood (LOO-CV)"].Value).Value;
|
---|
87 | if (score >= cvscore || t == null || double.IsNaN(t.TrainingRSquared)) continue;
|
---|
88 | cvscore = score;
|
---|
89 | sol = t;
|
---|
90 | }
|
---|
91 |
|
---|
92 | if (sol == null) throw new ArgumentException("Could not create Gaussian Process model");
|
---|
93 |
|
---|
94 | noParameters = pd.Dataset.Rows + 1
|
---|
95 | + Regression.CovarianceFunction.GetNumberOfParameters(pd.AllowedInputVariables.Count())
|
---|
96 | + Regression.MeanFunction.GetNumberOfParameters(pd.AllowedInputVariables.Count());
|
---|
97 | return sol.Model;
|
---|
98 | }
|
---|
99 |
|
---|
100 | public int MinLeafSize(IRegressionProblemData pd) {
|
---|
101 | return 3;
|
---|
102 | }
|
---|
103 | #endregion
|
---|
104 | }
|
---|
105 | } |
---|