source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/GaussianProcessBase.cs @ 13118

Last change on this file since 13118 was 13118, checked in by gkronber, 6 years ago

#2497: added hidden parameter to turn on/off scaling of input variables in Gaussian process models

File size: 11.3 KB
Line 
1
2#region License Information
3/* HeuristicLab
4 * Copyright (C) 2002-2015 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.Algorithms.GradientDescent;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Operators;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.PluginInfrastructure;
34using HeuristicLab.Problems.DataAnalysis;
35
36namespace HeuristicLab.Algorithms.DataAnalysis {
37  /// <summary>
38  /// Base class for Gaussian process data analysis algorithms (regression and classification).
39  /// </summary>
40  [StorableClass]
41  public abstract class GaussianProcessBase : EngineAlgorithm {
42    protected const string MeanFunctionParameterName = "MeanFunction";
43    protected const string CovarianceFunctionParameterName = "CovarianceFunction";
44    protected const string MinimizationIterationsParameterName = "Iterations";
45    protected const string ApproximateGradientsParameterName = "ApproximateGradients";
46    protected const string SeedParameterName = "Seed";
47    protected const string SetSeedRandomlyParameterName = "SetSeedRandomly";
48    protected const string ModelCreatorParameterName = "GaussianProcessModelCreator";
49    protected const string NegativeLogLikelihoodParameterName = "NegativeLogLikelihood";
50    protected const string HyperparameterParameterName = "Hyperparameter";
51    protected const string HyperparameterGradientsParameterName = "HyperparameterGradients";
52    protected const string SolutionCreatorParameterName = "GaussianProcessSolutionCreator";
53    protected const string ScaleInputValuesParameterName = "ScaleInputValues";
54
55    public new IDataAnalysisProblem Problem {
56      get { return (IDataAnalysisProblem)base.Problem; }
57      set { base.Problem = value; }
58    }
59
60    #region parameter properties
61    public IValueParameter<IMeanFunction> MeanFunctionParameter {
62      get { return (IValueParameter<IMeanFunction>)Parameters[MeanFunctionParameterName]; }
63    }
64    public IValueParameter<ICovarianceFunction> CovarianceFunctionParameter {
65      get { return (IValueParameter<ICovarianceFunction>)Parameters[CovarianceFunctionParameterName]; }
66    }
67    public IValueParameter<IntValue> MinimizationIterationsParameter {
68      get { return (IValueParameter<IntValue>)Parameters[MinimizationIterationsParameterName]; }
69    }
70    public IValueParameter<IntValue> SeedParameter {
71      get { return (IValueParameter<IntValue>)Parameters[SeedParameterName]; }
72    }
73    public IValueParameter<BoolValue> SetSeedRandomlyParameter {
74      get { return (IValueParameter<BoolValue>)Parameters[SetSeedRandomlyParameterName]; }
75    }
76    public IFixedValueParameter<BoolValue> ScaleInputValuesParameter {
77      get { return (IFixedValueParameter<BoolValue>)Parameters[ScaleInputValuesParameterName]; }
78    }
79    #endregion
80    #region properties
81    public IMeanFunction MeanFunction {
82      set { MeanFunctionParameter.Value = value; }
83      get { return MeanFunctionParameter.Value; }
84    }
85    public ICovarianceFunction CovarianceFunction {
86      set { CovarianceFunctionParameter.Value = value; }
87      get { return CovarianceFunctionParameter.Value; }
88    }
89    public int MinimizationIterations {
90      set { MinimizationIterationsParameter.Value.Value = value; }
91      get { return MinimizationIterationsParameter.Value.Value; }
92    }
93    public int Seed { get { return SeedParameter.Value.Value; } set { SeedParameter.Value.Value = value; } }
94    public bool SetSeedRandomly { get { return SetSeedRandomlyParameter.Value.Value; } set { SetSeedRandomlyParameter.Value.Value = value; } }
95
96    public bool ScaleInputValues {
97      get { return ScaleInputValuesParameter.Value.Value; }
98      set { ScaleInputValuesParameter.Value.Value = value; }
99    }
100    #endregion
101
102    [StorableConstructor]
103    protected GaussianProcessBase(bool deserializing) : base(deserializing) { }
104    protected GaussianProcessBase(GaussianProcessBase original, Cloner cloner)
105      : base(original, cloner) {
106    }
107    protected GaussianProcessBase(IDataAnalysisProblem problem)
108      : base() {
109      Problem = problem;
110      Parameters.Add(new ValueParameter<IMeanFunction>(MeanFunctionParameterName, "The mean function to use.", new MeanConst()));
111      Parameters.Add(new ValueParameter<ICovarianceFunction>(CovarianceFunctionParameterName, "The covariance function to use.", new CovarianceSquaredExponentialIso()));
112      Parameters.Add(new ValueParameter<IntValue>(MinimizationIterationsParameterName, "The number of iterations for likelihood optimization with LM-BFGS.", new IntValue(20)));
113      Parameters.Add(new ValueParameter<IntValue>(SeedParameterName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
114      Parameters.Add(new ValueParameter<BoolValue>(SetSeedRandomlyParameterName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
115
116      Parameters.Add(new ValueParameter<BoolValue>(ApproximateGradientsParameterName, "Indicates that gradients should not be approximated (necessary for LM-BFGS).", new BoolValue(false)));
117      Parameters[ApproximateGradientsParameterName].Hidden = true; // should not be changed
118
119      Parameters.Add(new FixedValueParameter<BoolValue>(ScaleInputValuesParameterName,
120        "Determines if the input variable values are scaled to the range [0..1] for training.", new BoolValue(true)));
121      Parameters[ScaleInputValuesParameterName].Hidden = true;
122
123      // necessary for BFGS
124      Parameters.Add(new ValueParameter<BoolValue>("Maximization", new BoolValue(false)));
125      Parameters["Maximization"].Hidden = true;
126
127      var randomCreator = new HeuristicLab.Random.RandomCreator();
128      var gpInitializer = new GaussianProcessHyperparameterInitializer();
129      var bfgsInitializer = new LbfgsInitializer();
130      var makeStep = new LbfgsMakeStep();
131      var branch = new ConditionalBranch();
132      var modelCreator = new Placeholder();
133      var updateResults = new LbfgsUpdateResults();
134      var analyzer = new LbfgsAnalyzer();
135      var finalModelCreator = new Placeholder();
136      var finalAnalyzer = new LbfgsAnalyzer();
137      var solutionCreator = new Placeholder();
138
139      OperatorGraph.InitialOperator = randomCreator;
140      randomCreator.SeedParameter.ActualName = SeedParameterName;
141      randomCreator.SeedParameter.Value = null;
142      randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameterName;
143      randomCreator.SetSeedRandomlyParameter.Value = null;
144      randomCreator.Successor = gpInitializer;
145
146      gpInitializer.CovarianceFunctionParameter.ActualName = CovarianceFunctionParameterName;
147      gpInitializer.MeanFunctionParameter.ActualName = MeanFunctionParameterName;
148      gpInitializer.ProblemDataParameter.ActualName = Problem.ProblemDataParameter.Name;
149      gpInitializer.HyperparameterParameter.ActualName = HyperparameterParameterName;
150      gpInitializer.RandomParameter.ActualName = randomCreator.RandomParameter.Name;
151      gpInitializer.Successor = bfgsInitializer;
152
153      bfgsInitializer.IterationsParameter.ActualName = MinimizationIterationsParameterName;
154      bfgsInitializer.PointParameter.ActualName = HyperparameterParameterName;
155      bfgsInitializer.ApproximateGradientsParameter.ActualName = ApproximateGradientsParameterName;
156      bfgsInitializer.Successor = makeStep;
157
158      makeStep.StateParameter.ActualName = bfgsInitializer.StateParameter.Name;
159      makeStep.PointParameter.ActualName = HyperparameterParameterName;
160      makeStep.Successor = branch;
161
162      branch.ConditionParameter.ActualName = makeStep.TerminationCriterionParameter.Name;
163      branch.FalseBranch = modelCreator;
164      branch.TrueBranch = finalModelCreator;
165
166      modelCreator.OperatorParameter.ActualName = ModelCreatorParameterName;
167      modelCreator.Successor = updateResults;
168
169      updateResults.StateParameter.ActualName = bfgsInitializer.StateParameter.Name;
170      updateResults.QualityParameter.ActualName = NegativeLogLikelihoodParameterName;
171      updateResults.QualityGradientsParameter.ActualName = HyperparameterGradientsParameterName;
172      updateResults.ApproximateGradientsParameter.ActualName = ApproximateGradientsParameterName;
173      updateResults.Successor = analyzer;
174
175      analyzer.QualityParameter.ActualName = NegativeLogLikelihoodParameterName;
176      analyzer.PointParameter.ActualName = HyperparameterParameterName;
177      analyzer.QualityGradientsParameter.ActualName = HyperparameterGradientsParameterName;
178      analyzer.StateParameter.ActualName = bfgsInitializer.StateParameter.Name;
179      analyzer.PointsTableParameter.ActualName = "Hyperparameter table";
180      analyzer.QualityGradientsTableParameter.ActualName = "Gradients table";
181      analyzer.QualitiesTableParameter.ActualName = "Negative log likelihood table";
182      analyzer.Successor = makeStep;
183
184      finalModelCreator.OperatorParameter.ActualName = ModelCreatorParameterName;
185      finalModelCreator.Successor = finalAnalyzer;
186
187      finalAnalyzer.QualityParameter.ActualName = NegativeLogLikelihoodParameterName;
188      finalAnalyzer.PointParameter.ActualName = HyperparameterParameterName;
189      finalAnalyzer.QualityGradientsParameter.ActualName = HyperparameterGradientsParameterName;
190      finalAnalyzer.PointsTableParameter.ActualName = analyzer.PointsTableParameter.ActualName;
191      finalAnalyzer.QualityGradientsTableParameter.ActualName = analyzer.QualityGradientsTableParameter.ActualName;
192      finalAnalyzer.QualitiesTableParameter.ActualName = analyzer.QualitiesTableParameter.ActualName;
193      finalAnalyzer.Successor = solutionCreator;
194
195      solutionCreator.OperatorParameter.ActualName = SolutionCreatorParameterName;
196    }
197
198    [StorableHook(HookType.AfterDeserialization)]
199    private void AfterDeserialization() {
200      // BackwardsCompatibility3.4
201      #region Backwards compatible code, remove with 3.5
202      if (!Parameters.ContainsKey("Maximization")) {
203        Parameters.Add(new ValueParameter<BoolValue>("Maximization", new BoolValue(false)));
204        Parameters["Maximization"].Hidden = true;
205      }
206
207      if (!Parameters.ContainsKey(ScaleInputValuesParameterName)) {
208        Parameters.Add(new FixedValueParameter<BoolValue>(ScaleInputValuesParameterName,
209          "Determines if the input variable values are scaled to the range [0..1] for training.", new BoolValue(true)));
210        Parameters[ScaleInputValuesParameterName].Hidden = true;
211      }
212      #endregion
213    }
214  }
215}
Note: See TracBrowser for help on using the repository browser.