Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceOverhaul/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanFunctions/MeanModel.cs

Last change on this file was 14711, checked in by gkronber, 8 years ago

#2520

  • renamed StorableClass -> StorableType
  • changed persistence to use GUIDs instead of type names
File size: 4.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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
21using System;
22using System.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Problems.DataAnalysis;
29
30namespace HeuristicLab.Algorithms.DataAnalysis {
31  [StorableType("FD14EE57-5A90-4E3B-B383-E5CB42CEFFE4")]
32  [Item(Name = "MeanModel", Description = "A mean function for Gaussian processes that uses a regression solution created with a different algorithm to calculate the mean.")]
33  // essentially an adaptor which maps from IMeanFunction to IRegressionSolution
34  public sealed class MeanModel : ParameterizedNamedItem, IMeanFunction {
35    public IValueParameter<IRegressionSolution> RegressionSolutionParameter {
36      get { return (IValueParameter<IRegressionSolution>)Parameters["RegressionSolution"]; }
37    }
38    public IRegressionSolution RegressionSolution {
39      get { return RegressionSolutionParameter.Value; }
40      set { RegressionSolutionParameter.Value = value; }
41    }
42
43    [StorableConstructor]
44    private MeanModel(bool deserializing) : base(deserializing) { }
45    private MeanModel(MeanModel original, Cloner cloner)
46      : base(original, cloner) {
47    }
48
49    public MeanModel()
50      : base("MeanModel", "A mean function for Gaussian processes that uses a regression solution created with a different algorithm to calculate the mean.") {
51      Parameters.Add(new ValueParameter<IRegressionSolution>("RegressionSolution", "The solution containing the model that should be used for the mean prediction."));
52    }
53
54    public MeanModel(IRegressionSolution solution)
55      : this() {
56      // here we cannot check if the model is actually compatible (uses only input variables that are available)
57      // we only assume that the list of allowed inputs in the regression solution is the same as the list of allowed
58      // inputs in the Gaussian process.
59      // later we might get an error or bad behaviour when the mean function is evaluated
60      RegressionSolution = solution;
61    }
62
63    public override IDeepCloneable Clone(Cloner cloner) {
64      return new MeanModel(this, cloner);
65    }
66
67    public int GetNumberOfParameters(int numberOfVariables) {
68      return 0; // no support for hyperparameters for regression models yet
69    }
70
71    public void SetParameter(double[] p) {
72      if (p.Length > 0) throw new ArgumentException("No parameters allowed for model-based mean function.", "p");
73    }
74
75    public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, IEnumerable<int> columnIndices) {
76      if (p.Length > 0) throw new ArgumentException("No parameters allowed for model-based mean function.", "p");
77      var solution = RegressionSolution;
78      var variableNames = solution.ProblemData.AllowedInputVariables.ToArray();
79      if (variableNames.Length != columnIndices.Count())
80        throw new ArgumentException("The number of input variables does not match in MeanModel");
81      var variableValues = variableNames.Select(_ => new List<double>() { 0.0 }).ToArray(); // or of zeros
82      // uses modifyable dataset to pass values to the model
83      var ds = new ModifiableDataset(variableNames, variableValues);
84      var mf = new ParameterizedMeanFunction();
85      var model = solution.Model; // effort for parameter access only once
86      mf.Mean = (x, i) => {
87        ds.ReplaceRow(0, Util.GetRow(x, i, columnIndices).OfType<object>());
88        return model.GetEstimatedValues(ds, 0.ToEnumerable()).Single(); // evaluate the model on the specified row only
89      };
90      mf.Gradient = (x, i, k) => {
91        if (k > 0)
92          throw new ArgumentException();
93        return 0.0;
94      };
95      return mf;
96    }
97  }
98}
Note: See TracBrowser for help on using the repository browser.