Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanConst.cs @ 8612

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

#1902 implemented all mean and covariance functions with parameters as ParameterizedNamedItems

File size: 3.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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 System;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Algorithms.DataAnalysis {
30  [StorableClass]
31  [Item(Name = "MeanConst", Description = "Constant mean function for Gaussian processes.")]
32  public sealed class MeanConst : ParameterizedNamedItem, IMeanFunction {
33    [Storable]
34    private double c;
35    [Storable]
36    private readonly HyperParameter<DoubleValue> valueParameter;
37    public IValueParameter<DoubleValue> ValueParameter { get { return valueParameter; } }
38
39    [StorableConstructor]
40    private MeanConst(bool deserializing) : base(deserializing) { }
41    private MeanConst(MeanConst original, Cloner cloner)
42      : base(original, cloner) {
43      this.c = original.c;
44      this.valueParameter = cloner.Clone(original.valueParameter);
45      RegisterEvents();
46    }
47    public MeanConst()
48      : base() {
49      this.name = ItemName;
50      this.description = ItemDescription;
51
52      this.valueParameter = new HyperParameter<DoubleValue>("Value", "The constant value for the constant mean function.");
53      Parameters.Add(valueParameter);
54      RegisterEvents();
55    }
56
57    public override IDeepCloneable Clone(Cloner cloner) {
58      return new MeanConst(this, cloner);
59    }
60
61    [StorableHook(HookType.AfterDeserialization)]
62    private void AfterDeserialization() {
63      RegisterEvents();
64    }
65
66    private void RegisterEvents() {
67      Util.AttachValueChangeHandler<DoubleValue, double>(valueParameter, () => { c = valueParameter.Value.Value; });
68    }
69
70    public int GetNumberOfParameters(int numberOfVariables) {
71      return valueParameter.Fixed ? 0 : 1;
72    }
73
74    public void SetParameter(double[] hyp) {
75      if (!valueParameter.Fixed) {
76        valueParameter.SetValue(new DoubleValue(hyp[0]));
77      } else if (hyp.Length > 0)
78        throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for the constant mean function.", "hyp");
79    }
80
81    public double[] GetMean(double[,] x) {
82      return Enumerable.Repeat(c, x.GetLength(0)).ToArray();
83    }
84
85    public double[] GetGradients(int k, double[,] x) {
86      if (k > 0) throw new ArgumentException();
87      return Enumerable.Repeat(1.0, x.GetLength(0)).ToArray();
88    }
89  }
90}
Note: See TracBrowser for help on using the repository browser.