Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/GaussianProcessRegression.cs @ 14891

Last change on this file since 14891 was 14891, checked in by bwerth, 7 years ago

#2699 reworked kenel functions (beta is always a scaling factor now), added LU-Decomposition as a fall-back if Cholesky-decomposition fails

File size: 7.3 KB
Line 
1
2#region License Information
3/* HeuristicLab
4 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21#endregion
22
23using System;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.PluginInfrastructure;
31using HeuristicLab.Problems.DataAnalysis;
32
33namespace HeuristicLab.Algorithms.DataAnalysis {
34  /// <summary>
35  ///Gaussian process regression data analysis algorithm.
36  /// </summary>
37  [Item("Gaussian Process Regression", "Gaussian process regression data analysis algorithm.")]
38  [Creatable(CreatableAttribute.Categories.DataAnalysisRegression, Priority = 160)]
39  [StorableClass]
40  public sealed class GaussianProcessRegression : GaussianProcessBase, IStorableContent, IDataAnalysisAlgorithm<IRegressionProblem> {
41    public string Filename { get; set; }
42
43    public override Type ProblemType { get { return typeof(IRegressionProblem); } }
44    public new IRegressionProblem Problem
45    {
46      get { return (IRegressionProblem)base.Problem; }
47      set { base.Problem = value; }
48    }
49
50    private const string ModelParameterName = "Model";
51    private const string CreateSolutionParameterName = "CreateSolution";
52
53
54    #region parameter properties
55    public IConstrainedValueParameter<IGaussianProcessRegressionModelCreator> GaussianProcessModelCreatorParameter
56    {
57      get { return (IConstrainedValueParameter<IGaussianProcessRegressionModelCreator>)Parameters[ModelCreatorParameterName]; }
58    }
59    public IFixedValueParameter<GaussianProcessRegressionSolutionCreator> GaussianProcessSolutionCreatorParameter
60    {
61      get { return (IFixedValueParameter<GaussianProcessRegressionSolutionCreator>)Parameters[SolutionCreatorParameterName]; }
62    }
63    public IFixedValueParameter<BoolValue> CreateSolutionParameter
64    {
65      get { return (IFixedValueParameter<BoolValue>)Parameters[CreateSolutionParameterName]; }
66    }
67    #endregion
68    #region properties
69    public bool CreateSolution
70    {
71      get { return CreateSolutionParameter.Value.Value; }
72      set { CreateSolutionParameter.Value.Value = value; }
73    }
74    #endregion
75
76    [StorableConstructor]
77    private GaussianProcessRegression(bool deserializing) : base(deserializing) { }
78    private GaussianProcessRegression(GaussianProcessRegression original, Cloner cloner)
79      : base(original, cloner) {
80      RegisterEventHandlers();
81    }
82    public GaussianProcessRegression()
83      : base(new RegressionProblem()) {
84      this.name = ItemName;
85      this.description = ItemDescription;
86
87      var modelCreators = ApplicationManager.Manager.GetInstances<IGaussianProcessRegressionModelCreator>();
88      var defaultModelCreator = modelCreators.First(c => c is GaussianProcessRegressionModelCreator);
89
90      // GP regression and classification algorithms only differ in the model and solution creators,
91      // thus we use a common base class and use operator parameters to implement the specific versions.
92      // Different model creators can be implemented,
93      // but the solution creator is implemented in a generic fashion already and we don't allow derived solution creators
94      Parameters.Add(new ConstrainedValueParameter<IGaussianProcessRegressionModelCreator>(ModelCreatorParameterName, "The operator to create the Gaussian process model.",
95        new ItemSet<IGaussianProcessRegressionModelCreator>(modelCreators), defaultModelCreator));
96      // the solution creator cannot be changed
97      Parameters.Add(new FixedValueParameter<GaussianProcessRegressionSolutionCreator>(SolutionCreatorParameterName, "The solution creator for the algorithm",
98        new GaussianProcessRegressionSolutionCreator()));
99      Parameters[SolutionCreatorParameterName].Hidden = true;
100      // TODO: it would be better to deactivate the solution creator when this parameter is changed
101      Parameters.Add(new FixedValueParameter<BoolValue>(CreateSolutionParameterName, "Flag that indicates if a solution should be produced at the end of the run", new BoolValue(true)));
102      Parameters[CreateSolutionParameterName].Hidden = true;
103
104      ParameterizedModelCreators();
105      ParameterizeSolutionCreator(GaussianProcessSolutionCreatorParameter.Value);
106      RegisterEventHandlers();
107    }
108
109
110    [StorableHook(HookType.AfterDeserialization)]
111    private void AfterDeserialization() {
112      // BackwardsCompatibility3.3
113      #region Backwards compatible code, remove with 3.4
114      if (!Parameters.ContainsKey(CreateSolutionParameterName)) {
115        Parameters.Add(new FixedValueParameter<BoolValue>(CreateSolutionParameterName, "Flag that indicates if a solution should be produced at the end of the run", new BoolValue(true)));
116        Parameters[CreateSolutionParameterName].Hidden = true;
117      }
118      #endregion
119      RegisterEventHandlers();
120    }
121
122    public override IDeepCloneable Clone(Cloner cloner) {
123      return new GaussianProcessRegression(this, cloner);
124    }
125
126    #region events
127    private void RegisterEventHandlers() {
128      GaussianProcessModelCreatorParameter.ValueChanged += ModelCreatorParameter_ValueChanged;
129    }
130
131    private void ModelCreatorParameter_ValueChanged(object sender, EventArgs e) {
132      ParameterizedModelCreator(GaussianProcessModelCreatorParameter.Value);
133    }
134    #endregion
135
136    private void ParameterizedModelCreators() {
137      foreach (var creator in GaussianProcessModelCreatorParameter.ValidValues) {
138        ParameterizedModelCreator(creator);
139      }
140    }
141
142    private void ParameterizedModelCreator(IGaussianProcessRegressionModelCreator modelCreator) {
143      modelCreator.ProblemDataParameter.ActualName = Problem.ProblemDataParameter.Name;
144      modelCreator.MeanFunctionParameter.ActualName = MeanFunctionParameterName;
145      modelCreator.CovarianceFunctionParameter.ActualName = CovarianceFunctionParameterName;
146
147      // parameter names fixed by the algorithm
148      modelCreator.ModelParameter.ActualName = ModelParameterName;
149      modelCreator.HyperparameterParameter.ActualName = HyperparameterParameterName;
150      modelCreator.HyperparameterGradientsParameter.ActualName = HyperparameterGradientsParameterName;
151      modelCreator.NegativeLogLikelihoodParameter.ActualName = NegativeLogLikelihoodParameterName;
152    }
153
154    private void ParameterizeSolutionCreator(GaussianProcessRegressionSolutionCreator solutionCreator) {
155      solutionCreator.ModelParameter.ActualName = ModelParameterName;
156      solutionCreator.ProblemDataParameter.ActualName = Problem.ProblemDataParameter.Name;
157    }
158  }
159}
Note: See TracBrowser for help on using the repository browser.