Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/GaussianProcessRegression.cs

Last change on this file was 17246, checked in by gkronber, 5 years ago

#2925: merged r17037:17242 from trunk to branch

File size: 7.2 KB
Line 
1
2#region License Information
3/* HeuristicLab
4 * Copyright (C) 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 HEAL.Attic;
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  [StorableType("3A1DF0A2-66D1-47BB-8BE4-23003BD34271")]
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      get { return (IRegressionProblem)base.Problem; }
46      set { base.Problem = value; }
47    }
48
49    private const string ModelParameterName = "Model";
50    private const string CreateSolutionParameterName = "CreateSolution";
51
52
53    #region parameter properties
54    public IConstrainedValueParameter<IGaussianProcessRegressionModelCreator> GaussianProcessModelCreatorParameter {
55      get { return (IConstrainedValueParameter<IGaussianProcessRegressionModelCreator>)Parameters[ModelCreatorParameterName]; }
56    }
57    public IFixedValueParameter<GaussianProcessRegressionSolutionCreator> GaussianProcessSolutionCreatorParameter {
58      get { return (IFixedValueParameter<GaussianProcessRegressionSolutionCreator>)Parameters[SolutionCreatorParameterName]; }
59    }
60    public IFixedValueParameter<BoolValue> CreateSolutionParameter {
61      get { return (IFixedValueParameter<BoolValue>)Parameters[CreateSolutionParameterName]; }
62    }
63    #endregion
64    #region properties
65    public bool CreateSolution {
66      get { return CreateSolutionParameter.Value.Value; }
67      set { CreateSolutionParameter.Value.Value = value; }
68    }
69    #endregion
70
71    [StorableConstructor]
72    private GaussianProcessRegression(StorableConstructorFlag _) : base(_) { }
73    private GaussianProcessRegression(GaussianProcessRegression original, Cloner cloner)
74      : base(original, cloner) {
75      RegisterEventHandlers();
76    }
77    public GaussianProcessRegression()
78      : base(new RegressionProblem()) {
79      this.name = ItemName;
80      this.description = ItemDescription;
81
82      var modelCreators = ApplicationManager.Manager.GetInstances<IGaussianProcessRegressionModelCreator>();
83      var defaultModelCreator = modelCreators.First(c => c is GaussianProcessRegressionModelCreator);
84
85      // GP regression and classification algorithms only differ in the model and solution creators,
86      // thus we use a common base class and use operator parameters to implement the specific versions.
87      // Different model creators can be implemented,
88      // but the solution creator is implemented in a generic fashion already and we don't allow derived solution creators
89      Parameters.Add(new ConstrainedValueParameter<IGaussianProcessRegressionModelCreator>(ModelCreatorParameterName, "The operator to create the Gaussian process model.",
90        new ItemSet<IGaussianProcessRegressionModelCreator>(modelCreators), defaultModelCreator));
91      // the solution creator cannot be changed
92      Parameters.Add(new FixedValueParameter<GaussianProcessRegressionSolutionCreator>(SolutionCreatorParameterName, "The solution creator for the algorithm",
93        new GaussianProcessRegressionSolutionCreator()));
94      Parameters[SolutionCreatorParameterName].Hidden = true;
95      // TODO: it would be better to deactivate the solution creator when this parameter is changed
96      Parameters.Add(new FixedValueParameter<BoolValue>(CreateSolutionParameterName, "Flag that indicates if a solution should be produced at the end of the run", new BoolValue(true)));
97      Parameters[CreateSolutionParameterName].Hidden = true;
98
99      ParameterizedModelCreators();
100      ParameterizeSolutionCreator(GaussianProcessSolutionCreatorParameter.Value);
101      RegisterEventHandlers();
102    }
103
104
105    [StorableHook(HookType.AfterDeserialization)]
106    private void AfterDeserialization() {
107      // BackwardsCompatibility3.3
108      #region Backwards compatible code, remove with 3.4
109      if (!Parameters.ContainsKey(CreateSolutionParameterName)) {
110        Parameters.Add(new FixedValueParameter<BoolValue>(CreateSolutionParameterName, "Flag that indicates if a solution should be produced at the end of the run", new BoolValue(true)));
111        Parameters[CreateSolutionParameterName].Hidden = true;
112      }
113      #endregion
114      RegisterEventHandlers();
115    }
116
117    public override IDeepCloneable Clone(Cloner cloner) {
118      return new GaussianProcessRegression(this, cloner);
119    }
120
121    #region events
122    private void RegisterEventHandlers() {
123      GaussianProcessModelCreatorParameter.ValueChanged += ModelCreatorParameter_ValueChanged;
124    }
125
126    private void ModelCreatorParameter_ValueChanged(object sender, EventArgs e) {
127      ParameterizedModelCreator(GaussianProcessModelCreatorParameter.Value);
128    }
129    #endregion
130
131    private void ParameterizedModelCreators() {
132      foreach (var creator in GaussianProcessModelCreatorParameter.ValidValues) {
133        ParameterizedModelCreator(creator);
134      }
135    }
136
137    private void ParameterizedModelCreator(IGaussianProcessRegressionModelCreator modelCreator) {
138      modelCreator.ProblemDataParameter.ActualName = Problem.ProblemDataParameter.Name;
139      modelCreator.MeanFunctionParameter.ActualName = MeanFunctionParameterName;
140      modelCreator.CovarianceFunctionParameter.ActualName = CovarianceFunctionParameterName;
141
142      // parameter names fixed by the algorithm
143      modelCreator.ModelParameter.ActualName = ModelParameterName;
144      modelCreator.HyperparameterParameter.ActualName = HyperparameterParameterName;
145      modelCreator.HyperparameterGradientsParameter.ActualName = HyperparameterGradientsParameterName;
146      modelCreator.NegativeLogLikelihoodParameter.ActualName = NegativeLogLikelihoodParameterName;
147    }
148
149    private void ParameterizeSolutionCreator(GaussianProcessRegressionSolutionCreator solutionCreator) {
150      solutionCreator.ModelParameter.ActualName = ModelParameterName;
151      solutionCreator.ProblemDataParameter.ActualName = Problem.ProblemDataParameter.Name;
152    }
153  }
154}
Note: See TracBrowser for help on using the repository browser.