Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceMask.cs @ 8803

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

#1902

  • fixed bug in cloning constructor of CovarianceMask
  • removed throwing Exceptions in CovariancePeriodic (the behaviour of the periodic covariance function now matches the GPML implementation again.
File size: 4.6 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Algorithms.DataAnalysis {
31  [StorableClass]
32  [Item(Name = "CovarianceMask",
33    Description = "Masking covariance function for dimension selection can be used to apply a covariance function only on certain input dimensions.")]
34  public sealed class CovarianceMask : ParameterizedNamedItem, ICovarianceFunction {
35    [Storable]
36    private int[] selectedDimensions;
37    [Storable]
38    private readonly ValueParameter<IntArray> selectedDimensionsParameter;
39    public IValueParameter<IntArray> SelectedDimensionsParameter {
40      get { return selectedDimensionsParameter; }
41    }
42
43    [Storable]
44    private ICovarianceFunction cov;
45    [Storable]
46    private readonly ValueParameter<ICovarianceFunction> covParameter;
47    public IValueParameter<ICovarianceFunction> CovarianceFunctionParameter {
48      get { return covParameter; }
49    }
50
51    [StorableConstructor]
52    private CovarianceMask(bool deserializing)
53      : base(deserializing) {
54    }
55
56    private CovarianceMask(CovarianceMask original, Cloner cloner)
57      : base(original, cloner) {
58      this.selectedDimensionsParameter = cloner.Clone(original.selectedDimensionsParameter);
59      if (original.selectedDimensions != null) {
60        this.selectedDimensions = (int[])original.selectedDimensions.Clone();
61      }
62
63      this.covParameter = cloner.Clone(original.covParameter);
64      this.cov = cloner.Clone(original.cov);
65      RegisterEvents();
66    }
67
68    public CovarianceMask()
69      : base() {
70      Name = ItemName;
71      Description = ItemDescription;
72
73      this.selectedDimensionsParameter = new ValueParameter<IntArray>("SelectedDimensions", "The dimensions on which the specified covariance function should be applied to.");
74      this.covParameter = new ValueParameter<ICovarianceFunction>("CovarianceFunction", "The covariance function that should be scaled.", new CovarianceSquaredExponentialIso());
75      cov = covParameter.Value;
76
77      Parameters.Add(selectedDimensionsParameter);
78      Parameters.Add(covParameter);
79
80      RegisterEvents();
81    }
82
83    public override IDeepCloneable Clone(Cloner cloner) {
84      return new CovarianceMask(this, cloner);
85    }
86
87    [StorableHook(HookType.AfterDeserialization)]
88    private void AfterDeserialization() {
89      RegisterEvents();
90    }
91
92    private void RegisterEvents() {
93      Util.AttachArrayChangeHandler<IntArray, int>(selectedDimensionsParameter, () => {
94        selectedDimensions = selectedDimensionsParameter.Value
95          .OrderBy(x => x)
96          .Distinct()
97          .ToArray();
98        if (selectedDimensions.Length == 0) selectedDimensions = null;
99      });
100      covParameter.ValueChanged += (sender, args) => { cov = covParameter.Value; };
101    }
102
103    public int GetNumberOfParameters(int numberOfVariables) {
104      if (selectedDimensions == null) return cov.GetNumberOfParameters(numberOfVariables);
105      else return cov.GetNumberOfParameters(selectedDimensions.Length);
106    }
107
108    public void SetParameter(double[] hyp) {
109      cov.SetParameter(hyp);
110    }
111
112    public double GetCovariance(double[,] x, int i, int j, IEnumerable<int> columnIndices) {
113      return cov.GetCovariance(x, i, j, selectedDimensions);
114    }
115
116    public IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices) {
117      return cov.GetGradient(x, i, j, selectedDimensions);
118    }
119
120    public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j, IEnumerable<int> columnIndices) {
121      return cov.GetCrossCovariance(x, xt, i, j, selectedDimensions);
122    }
123  }
124}
Note: See TracBrowser for help on using the repository browser.