Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/GaussianProcessRegression.cs @ 8471

Last change on this file since 8471 was 8421, checked in by gkronber, 12 years ago

#1902 fixed default mean and covariance function for GP

File size: 11.0 KB
Line 
1
2#region License Information
3/* HeuristicLab
4 * Copyright (C) 2002-2012 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.Collections.Generic;
25using System.Linq;
26using HeuristicLab.Algorithms.GradientDescent;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Data;
30using HeuristicLab.Operators;
31using HeuristicLab.Optimization;
32using HeuristicLab.Parameters;
33using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
34using HeuristicLab.PluginInfrastructure;
35using HeuristicLab.Problems.DataAnalysis;
36
37namespace HeuristicLab.Algorithms.DataAnalysis {
38  /// <summary>
39  ///Gaussian process regression data analysis algorithm.
40  /// </summary>
41  [Item("Gaussian Process Regression", "Gaussian process regression data analysis algorithm.")]
42  [Creatable("Data Analysis")]
43  [StorableClass]
44  public sealed class GaussianProcessRegression : EngineAlgorithm, IStorableContent {
45    public string Filename { get; set; }
46
47    public override Type ProblemType { get { return typeof(IRegressionProblem); } }
48    public new IRegressionProblem Problem {
49      get { return (IRegressionProblem)base.Problem; }
50      set { base.Problem = value; }
51    }
52
53    private const string MeanFunctionParameterName = "MeanFunction";
54    private const string CovarianceFunctionParameterName = "CovarianceFunction";
55    private const string MinimizationIterationsParameterName = "Iterations";
56    private const string ApproximateGradientsParameterName = "ApproximateGradients";
57    private const string SeedParameterName = "Seed";
58    private const string SetSeedRandomlyParameterName = "SetSeedRandomly";
59
60    #region parameter properties
61    public IConstrainedValueParameter<IMeanFunction> MeanFunctionParameter {
62      get { return (IConstrainedValueParameter<IMeanFunction>)Parameters[MeanFunctionParameterName]; }
63    }
64    public IConstrainedValueParameter<ICovarianceFunction> CovarianceFunctionParameter {
65      get { return (IConstrainedValueParameter<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    #endregion
77    #region properties
78    public IMeanFunction MeanFunction {
79      set { MeanFunctionParameter.Value = value; }
80      get { return MeanFunctionParameter.Value; }
81    }
82    public ICovarianceFunction CovarianceFunction {
83      set { CovarianceFunctionParameter.Value = value; }
84      get { return CovarianceFunctionParameter.Value; }
85    }
86    public int MinimizationIterations {
87      set { MinimizationIterationsParameter.Value.Value = value; }
88      get { return MinimizationIterationsParameter.Value.Value; }
89    }
90    public int Seed { get { return SeedParameter.Value.Value; } set { SeedParameter.Value.Value = value; } }
91    public bool SetSeedRandomly { get { return SetSeedRandomlyParameter.Value.Value; } set { SetSeedRandomlyParameter.Value.Value = value; } }
92    #endregion
93
94    [StorableConstructor]
95    private GaussianProcessRegression(bool deserializing) : base(deserializing) { }
96    private GaussianProcessRegression(GaussianProcessRegression original, Cloner cloner)
97      : base(original, cloner) {
98    }
99    public GaussianProcessRegression()
100      : base() {
101      this.name = ItemName;
102      this.description = ItemDescription;
103
104      Problem = new RegressionProblem();
105
106      List<IMeanFunction> meanFunctions = ApplicationManager.Manager.GetInstances<IMeanFunction>().ToList();
107      List<ICovarianceFunction> covFunctions = ApplicationManager.Manager.GetInstances<ICovarianceFunction>().ToList();
108
109      Parameters.Add(new ConstrainedValueParameter<IMeanFunction>(MeanFunctionParameterName, "The mean function to use.",
110        new ItemSet<IMeanFunction>(meanFunctions), meanFunctions.OfType<MeanConst>().First()));
111      Parameters.Add(new ConstrainedValueParameter<ICovarianceFunction>(CovarianceFunctionParameterName, "The covariance function to use.",
112        new ItemSet<ICovarianceFunction>(covFunctions), covFunctions.OfType<CovarianceSEiso>().First()));
113      Parameters.Add(new ValueParameter<IntValue>(MinimizationIterationsParameterName, "The number of iterations for likelihood optimization with LM-BFGS.", new IntValue(20)));
114      Parameters.Add(new ValueParameter<IntValue>(SeedParameterName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
115      Parameters.Add(new ValueParameter<BoolValue>(SetSeedRandomlyParameterName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
116
117      Parameters.Add(new ValueParameter<BoolValue>(ApproximateGradientsParameterName, "Indicates that gradients should not be approximated (necessary for LM-BFGS).", new BoolValue(false)));
118      Parameters[ApproximateGradientsParameterName].Hidden = true; // should not be changed
119
120      var randomCreator = new HeuristicLab.Random.RandomCreator();
121      var gpInitializer = new GaussianProcessHyperparameterInitializer();
122      var bfgsInitializer = new LbfgsInitializer();
123      var makeStep = new LbfgsMakeStep();
124      var branch = new ConditionalBranch();
125      var modelCreator = new GaussianProcessRegressionModelCreator();
126      var updateResults = new LbfgsUpdateResults();
127      var analyzer = new LbfgsAnalyzer();
128      var finalModelCreator = new GaussianProcessRegressionModelCreator();
129      var finalAnalyzer = new LbfgsAnalyzer();
130      var solutionCreator = new GaussianProcessRegressionSolutionCreator();
131
132      OperatorGraph.InitialOperator = randomCreator;
133      randomCreator.SeedParameter.ActualName = SeedParameterName;
134      randomCreator.SeedParameter.Value = null;
135      randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameterName;
136      randomCreator.SetSeedRandomlyParameter.Value = null;
137      randomCreator.Successor = gpInitializer;
138
139      gpInitializer.CovarianceFunctionParameter.ActualName = CovarianceFunctionParameterName;
140      gpInitializer.MeanFunctionParameter.ActualName = MeanFunctionParameterName;
141      gpInitializer.ProblemDataParameter.ActualName = Problem.ProblemDataParameter.Name;
142      gpInitializer.HyperparameterParameter.ActualName = modelCreator.HyperparameterParameter.Name;
143      gpInitializer.RandomParameter.ActualName = randomCreator.RandomParameter.Name;
144      gpInitializer.Successor = bfgsInitializer;
145
146      bfgsInitializer.IterationsParameter.ActualName = MinimizationIterationsParameterName;
147      bfgsInitializer.PointParameter.ActualName = modelCreator.HyperparameterParameter.Name;
148      bfgsInitializer.ApproximateGradientsParameter.ActualName = ApproximateGradientsParameterName;
149      bfgsInitializer.Successor = makeStep;
150
151      makeStep.StateParameter.ActualName = bfgsInitializer.StateParameter.Name;
152      makeStep.PointParameter.ActualName = modelCreator.HyperparameterParameter.Name;
153      makeStep.Successor = branch;
154
155      branch.ConditionParameter.ActualName = makeStep.TerminationCriterionParameter.Name;
156      branch.FalseBranch = modelCreator;
157      branch.TrueBranch = finalModelCreator;
158
159      modelCreator.ProblemDataParameter.ActualName = Problem.ProblemDataParameter.Name;
160      modelCreator.MeanFunctionParameter.ActualName = MeanFunctionParameterName;
161      modelCreator.CovarianceFunctionParameter.ActualName = CovarianceFunctionParameterName;
162      modelCreator.Successor = updateResults;
163
164      updateResults.StateParameter.ActualName = bfgsInitializer.StateParameter.Name;
165      updateResults.QualityParameter.ActualName = modelCreator.NegativeLogLikelihoodParameter.Name;
166      updateResults.QualityGradientsParameter.ActualName = modelCreator.HyperparameterGradientsParameter.Name;
167      updateResults.ApproximateGradientsParameter.ActualName = ApproximateGradientsParameterName;
168      updateResults.Successor = analyzer;
169
170      analyzer.QualityParameter.ActualName = modelCreator.NegativeLogLikelihoodParameter.Name;
171      analyzer.PointParameter.ActualName = modelCreator.HyperparameterParameter.Name;
172      analyzer.QualityGradientsParameter.ActualName = modelCreator.HyperparameterGradientsParameter.Name;
173      analyzer.StateParameter.ActualName = bfgsInitializer.StateParameter.Name;
174      analyzer.PointsTableParameter.ActualName = "Hyperparameter table";
175      analyzer.QualityGradientsTableParameter.ActualName = "Gradients table";
176      analyzer.QualitiesTableParameter.ActualName = "Negative log likelihood table";
177      analyzer.Successor = makeStep;
178
179      finalModelCreator.ProblemDataParameter.ActualName = Problem.ProblemDataParameter.Name;
180      finalModelCreator.MeanFunctionParameter.ActualName = MeanFunctionParameterName;
181      finalModelCreator.CovarianceFunctionParameter.ActualName = CovarianceFunctionParameterName;
182      finalModelCreator.HyperparameterParameter.ActualName = bfgsInitializer.PointParameter.ActualName;
183      finalModelCreator.Successor = finalAnalyzer;
184
185      finalAnalyzer.QualityParameter.ActualName = modelCreator.NegativeLogLikelihoodParameter.Name;
186      finalAnalyzer.PointParameter.ActualName = modelCreator.HyperparameterParameter.Name;
187      finalAnalyzer.QualityGradientsParameter.ActualName = modelCreator.HyperparameterGradientsParameter.Name;
188      finalAnalyzer.PointsTableParameter.ActualName = analyzer.PointsTableParameter.ActualName;
189      finalAnalyzer.QualityGradientsTableParameter.ActualName = analyzer.QualityGradientsTableParameter.ActualName;
190      finalAnalyzer.QualitiesTableParameter.ActualName = analyzer.QualitiesTableParameter.ActualName;
191      finalAnalyzer.Successor = solutionCreator;
192
193      solutionCreator.ModelParameter.ActualName = finalModelCreator.ModelParameter.Name;
194      solutionCreator.ProblemDataParameter.ActualName = Problem.ProblemDataParameter.Name;
195    }
196
197    [StorableHook(HookType.AfterDeserialization)]
198    private void AfterDeserialization() { }
199
200    public override IDeepCloneable Clone(Cloner cloner) {
201      return new GaussianProcessRegression(this, cloner);
202    }
203  }
204}
Note: See TracBrowser for help on using the repository browser.