Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2965_CancelablePersistence/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/GaussianProcessRegressionSolutionCreator.cs @ 16752

Last change on this file since 16752 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 6.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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 HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Operators;
26using HeuristicLab.Optimization;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Problems.DataAnalysis;
30
31namespace HeuristicLab.Algorithms.DataAnalysis {
32  [StorableClass]
33  [Item(Name = "GaussianProcessRegressionSolutionCreator",
34    Description = "Creates a Gaussian process solution from a trained model.")]
35  public sealed class GaussianProcessRegressionSolutionCreator : SingleSuccessorOperator {
36    private const string ProblemDataParameterName = "ProblemData";
37    private const string ModelParameterName = "GaussianProcessRegressionModel";
38    private const string SolutionParameterName = "Solution";
39    private const string ResultsParameterName = "Results";
40    private const string TrainingRSquaredResultName = "Training R²";
41    private const string TestRSquaredResultName = "Test R²";
42    private const string CreateSolutionParameterName = "CreateSolution";
43    private const string NegLogPseudoLikelihood = "Negative log pseudo-likelihood (LOO-CV)";
44
45    #region Parameter Properties
46    public ILookupParameter<IRegressionProblemData> ProblemDataParameter {
47      get { return (ILookupParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName]; }
48    }
49    public ILookupParameter<IGaussianProcessSolution> SolutionParameter {
50      get { return (ILookupParameter<IGaussianProcessSolution>)Parameters[SolutionParameterName]; }
51    }
52    public ILookupParameter<IGaussianProcessModel> ModelParameter {
53      get { return (ILookupParameter<IGaussianProcessModel>)Parameters[ModelParameterName]; }
54    }
55    public ILookupParameter<ResultCollection> ResultsParameter {
56      get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
57    }
58    public ILookupParameter<BoolValue> CreateSolutionParameter {
59      get { return (ILookupParameter<BoolValue>)Parameters[CreateSolutionParameterName]; }
60    }
61    #endregion
62
63    [StorableConstructor]
64    private GaussianProcessRegressionSolutionCreator(bool deserializing) : base(deserializing) { }
65    private GaussianProcessRegressionSolutionCreator(GaussianProcessRegressionSolutionCreator original, Cloner cloner) : base(original, cloner) { }
66    public GaussianProcessRegressionSolutionCreator()
67      : base() {
68      // in
69      Parameters.Add(new LookupParameter<IRegressionProblemData>(ProblemDataParameterName, "The regression problem data for the Gaussian process solution."));
70      Parameters.Add(new LookupParameter<IGaussianProcessModel>(ModelParameterName, "The Gaussian process regression model to use for the solution."));
71      Parameters.Add(new LookupParameter<BoolValue>(CreateSolutionParameterName, "Flag that indicates if a solution should be produced at the end of the run"));
72
73      // in & out
74      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The result collection of the algorithm."));
75      // out
76      Parameters.Add(new LookupParameter<IGaussianProcessSolution>(SolutionParameterName, "The produced Gaussian process solution."));
77    }
78
79    [StorableHook(HookType.AfterDeserialization)]
80    private void AfterDeserialization() {
81      // BackwardsCompatibility3.3
82      #region Backwards compatible code, remove with 3.4
83      if (!Parameters.ContainsKey(CreateSolutionParameterName)) {
84        Parameters.Add(new LookupParameter<BoolValue>(CreateSolutionParameterName, "Flag that indicates if a solution should be produced at the end of the run"));
85      }
86      #endregion
87    }
88
89    public override IDeepCloneable Clone(Cloner cloner) {
90      return new GaussianProcessRegressionSolutionCreator(this, cloner);
91    }
92
93    public override IOperation Apply() {
94      if (ModelParameter.ActualValue != null && CreateSolutionParameter.ActualValue.Value == true) {
95        var m = (IGaussianProcessModel)ModelParameter.ActualValue.Clone();
96        m.FixParameters();
97        var data = (IRegressionProblemData)ProblemDataParameter.ActualValue.Clone();
98        var s = new GaussianProcessRegressionSolution(m, data);
99
100
101        SolutionParameter.ActualValue = s;
102        var results = ResultsParameter.ActualValue;
103        if (!results.ContainsKey(SolutionParameterName)) {
104          results.Add(new Result(SolutionParameterName, "The Gaussian process regression solution", s));
105          results.Add(new Result(TrainingRSquaredResultName,
106                                 "The Pearson's R² of the Gaussian process solution on the training partition.",
107                                 new DoubleValue(s.TrainingRSquared)));
108          results.Add(new Result(TestRSquaredResultName,
109                                 "The Pearson's R² of the Gaussian process solution on the test partition.",
110                                 new DoubleValue(s.TestRSquared)));
111          results.Add(new Result(NegLogPseudoLikelihood,
112                                 "The negative log pseudo-likelihood (from leave-one-out-cross-validation).",
113                                 new DoubleValue(m.LooCvNegativeLogPseudoLikelihood)));
114        } else {
115          results[SolutionParameterName].Value = s;
116          results[TrainingRSquaredResultName].Value = new DoubleValue(s.TrainingRSquared);
117          results[TestRSquaredResultName].Value = new DoubleValue(s.TestRSquared);
118          results[NegLogPseudoLikelihood].Value = new DoubleValue(m.LooCvNegativeLogPseudoLikelihood);
119        }
120      }
121      return base.Apply();
122    }
123  }
124}
Note: See TracBrowser for help on using the repository browser.