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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
30 | [StorableClass]
|
---|
31 | [Item(Name = "CovarianceConst",
|
---|
32 | Description = "Constant covariance function for Gaussian processes.")]
|
---|
33 | public sealed class CovarianceConst : ParameterizedNamedItem, ICovarianceFunction {
|
---|
34 |
|
---|
35 | [Storable]
|
---|
36 | private double scale;
|
---|
37 | [Storable]
|
---|
38 | private readonly HyperParameter<DoubleValue> scaleParameter;
|
---|
39 | public IValueParameter<DoubleValue> ScaleParameter {
|
---|
40 | get { return scaleParameter; }
|
---|
41 | }
|
---|
42 |
|
---|
43 | [StorableConstructor]
|
---|
44 | private CovarianceConst(bool deserializing)
|
---|
45 | : base(deserializing) {
|
---|
46 | }
|
---|
47 |
|
---|
48 | private CovarianceConst(CovarianceConst original, Cloner cloner)
|
---|
49 | : base(original, cloner) {
|
---|
50 | this.scaleParameter = cloner.Clone(original.scaleParameter);
|
---|
51 | this.scale = original.scale;
|
---|
52 |
|
---|
53 | RegisterEvents();
|
---|
54 | }
|
---|
55 |
|
---|
56 | public CovarianceConst()
|
---|
57 | : base() {
|
---|
58 | Name = ItemName;
|
---|
59 | Description = ItemDescription;
|
---|
60 |
|
---|
61 | scaleParameter = new HyperParameter<DoubleValue>("Scale", "The scale of the constant covariance function.");
|
---|
62 | Parameters.Add(scaleParameter);
|
---|
63 | RegisterEvents();
|
---|
64 | }
|
---|
65 |
|
---|
66 | [StorableHook(HookType.AfterDeserialization)]
|
---|
67 | private void AfterDeserialization() {
|
---|
68 | RegisterEvents();
|
---|
69 | }
|
---|
70 |
|
---|
71 | // caching
|
---|
72 | private void RegisterEvents() {
|
---|
73 | Util.AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { scale = scaleParameter.Value.Value; });
|
---|
74 | }
|
---|
75 |
|
---|
76 |
|
---|
77 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
78 | return new CovarianceConst(this, cloner);
|
---|
79 | }
|
---|
80 |
|
---|
81 | public int GetNumberOfParameters(int numberOfVariables) {
|
---|
82 | return scaleParameter.Fixed ? 0 : 1;
|
---|
83 | }
|
---|
84 |
|
---|
85 | public void SetParameter(double[] hyp) {
|
---|
86 | if (!scaleParameter.Fixed && hyp.Length == 1) {
|
---|
87 | scaleParameter.SetValue(new DoubleValue(Math.Exp(2 * hyp[0])));
|
---|
88 | } else {
|
---|
89 | throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceConst", "hyp");
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | public double GetCovariance(double[,] x, int i, int j) {
|
---|
94 | return scale;
|
---|
95 | }
|
---|
96 |
|
---|
97 | public IEnumerable<double> GetGradient(double[,] x, int i, int j) {
|
---|
98 | yield return 2.0 * scale;
|
---|
99 | }
|
---|
100 |
|
---|
101 | public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {
|
---|
102 | return scale;
|
---|
103 | }
|
---|
104 | }
|
---|
105 | }
|
---|