1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2019 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 HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HEAL.Attic;
|
---|
30 | using HeuristicLab.Problems.DataAnalysis;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
33 | [StorableType("0C20B1D2-9A58-4E77-9300-F5D76650DC19")]
|
---|
34 | [Item(Name = "GaussianProcessRegressionModelCreator",
|
---|
35 | Description = "Creates a Gaussian process model for regression given the data, the hyperparameters, a mean function, and a covariance function.")]
|
---|
36 | public sealed class GaussianProcessRegressionModelCreator : GaussianProcessModelCreator, IGaussianProcessRegressionModelCreator {
|
---|
37 | private const string ProblemDataParameterName = "ProblemData";
|
---|
38 |
|
---|
39 | #region Parameter Properties
|
---|
40 | public ILookupParameter<IRegressionProblemData> ProblemDataParameter {
|
---|
41 | get { return (ILookupParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName]; }
|
---|
42 | }
|
---|
43 | #endregion
|
---|
44 |
|
---|
45 | #region Properties
|
---|
46 | private IRegressionProblemData ProblemData {
|
---|
47 | get { return ProblemDataParameter.ActualValue; }
|
---|
48 | }
|
---|
49 | #endregion
|
---|
50 | [StorableConstructor]
|
---|
51 | private GaussianProcessRegressionModelCreator(StorableConstructorFlag _) : base(_) { }
|
---|
52 | private GaussianProcessRegressionModelCreator(GaussianProcessRegressionModelCreator original, Cloner cloner) : base(original, cloner) { }
|
---|
53 | public GaussianProcessRegressionModelCreator()
|
---|
54 | : base() {
|
---|
55 | Parameters.Add(new LookupParameter<IRegressionProblemData>(ProblemDataParameterName, "The regression problem data for the Gaussian process model."));
|
---|
56 | }
|
---|
57 |
|
---|
58 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
59 | return new GaussianProcessRegressionModelCreator(this, cloner);
|
---|
60 | }
|
---|
61 |
|
---|
62 | public override IOperation Apply() {
|
---|
63 | try {
|
---|
64 | var model = Create(ProblemData, Hyperparameter.ToArray(), MeanFunction, CovarianceFunction, ScaleInputValues);
|
---|
65 | ModelParameter.ActualValue = model;
|
---|
66 | NegativeLogLikelihoodParameter.ActualValue = new DoubleValue(model.NegativeLogLikelihood);
|
---|
67 | NegativeLogPseudoLikelihoodParameter.ActualValue = new DoubleValue(model.LooCvNegativeLogPseudoLikelihood);
|
---|
68 | HyperparameterGradientsParameter.ActualValue = new RealVector(model.HyperparameterGradients);
|
---|
69 | return base.Apply();
|
---|
70 | }
|
---|
71 | catch (ArgumentException) { }
|
---|
72 | catch (alglib.alglibexception) { }
|
---|
73 | NegativeLogLikelihoodParameter.ActualValue = new DoubleValue(1E300);
|
---|
74 | HyperparameterGradientsParameter.ActualValue = new RealVector(Hyperparameter.Count());
|
---|
75 | return base.Apply();
|
---|
76 | }
|
---|
77 |
|
---|
78 | public static IGaussianProcessModel Create(IRegressionProblemData problemData, double[] hyperparameter, IMeanFunction meanFunction, ICovarianceFunction covarianceFunction, bool scaleInputs = true) {
|
---|
79 | return new GaussianProcessModel(problemData.Dataset, problemData.TargetVariable, problemData.AllowedInputVariables, problemData.TrainingIndices, hyperparameter, meanFunction, covarianceFunction, scaleInputs);
|
---|
80 | }
|
---|
81 | }
|
---|
82 | }
|
---|