Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanFunctions/MeanConst.cs @ 16565

Last change on this file since 16565 was 16565, checked in by gkronber, 5 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

File size: 3.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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 HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Parameters;
27using HEAL.Attic;
28
29namespace HeuristicLab.Algorithms.DataAnalysis {
30  [StorableType("6E29FC23-D11B-4F32-9101-DB2BF5B2F29E")]
31  [Item(Name = "MeanConst", Description = "Constant mean function for Gaussian processes.")]
32  public sealed class MeanConst : ParameterizedNamedItem, IMeanFunction {
33    public IValueParameter<DoubleValue> ValueParameter {
34      get { return (IValueParameter<DoubleValue>)Parameters["Value"]; }
35    }
36
37    [StorableConstructor]
38    private MeanConst(StorableConstructorFlag _) : base(_) { }
39    private MeanConst(MeanConst original, Cloner cloner)
40      : base(original, cloner) {
41    }
42    public MeanConst()
43      : base() {
44      this.name = ItemName;
45      this.description = ItemDescription;
46
47      Parameters.Add(new OptionalValueParameter<DoubleValue>("Value", "The constant value for the constant mean function."));
48    }
49
50    public override IDeepCloneable Clone(Cloner cloner) {
51      return new MeanConst(this, cloner);
52    }
53
54    public int GetNumberOfParameters(int numberOfVariables) {
55      return ValueParameter.Value != null ? 0 : 1;
56    }
57
58    public void SetParameter(double[] p) {
59      double c;
60      GetParameters(p, out c);
61      ValueParameter.Value = new DoubleValue(c);
62    }
63
64    private void GetParameters(double[] p, out double c) {
65      if (ValueParameter.Value == null) {
66        c = p[0];
67      } else {
68        if (p.Length > 0)
69          throw new ArgumentException(
70            "The length of the parameter vector does not match the number of free parameters for the constant mean function.",
71            "p");
72        c = ValueParameter.Value.Value;
73      }
74    }
75
76    public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, int[] columnIndices) {
77      double c;
78      GetParameters(p, out c);
79      var mf = new ParameterizedMeanFunction();
80      mf.Mean = (x, i) => c;
81      mf.Gradient = (x, i, k) => {
82        if (k > 0) throw new ArgumentException();
83        return 1.0;
84      };
85      return mf;
86    }
87  }
88}
Note: See TracBrowser for help on using the repository browser.