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

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

#1902: added masking covariance function and made necessary changes to interface and utility class.

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      this.selectedDimensions = (int[])original.selectedDimensions.Clone();
60
61      this.covParameter = cloner.Clone(original.covParameter);
62      this.cov = cloner.Clone(original.cov);
63      RegisterEvents();
64    }
65
66    public CovarianceMask()
67      : base() {
68      Name = ItemName;
69      Description = ItemDescription;
70
71      this.selectedDimensionsParameter = new ValueParameter<IntArray>("SelectedDimensions", "The dimensions on which the specified covariance function should be applied to.");
72      this.covParameter = new ValueParameter<ICovarianceFunction>("CovarianceFunction", "The covariance function that should be scaled.", new CovarianceSquaredExponentialIso());
73      cov = covParameter.Value;
74
75      Parameters.Add(selectedDimensionsParameter);
76      Parameters.Add(covParameter);
77
78      RegisterEvents();
79    }
80
81    public override IDeepCloneable Clone(Cloner cloner) {
82      return new CovarianceMask(this, cloner);
83    }
84
85    [StorableHook(HookType.AfterDeserialization)]
86    private void AfterDeserialization() {
87      RegisterEvents();
88    }
89
90    private void RegisterEvents() {
91      Util.AttachArrayChangeHandler<IntArray, int>(selectedDimensionsParameter, () => {
92        selectedDimensions = selectedDimensionsParameter.Value
93          .OrderBy(x => x)
94          .Distinct()
95          .ToArray();
96        if (selectedDimensions.Length == 0) selectedDimensions = null;
97      });
98      covParameter.ValueChanged += (sender, args) => { cov = covParameter.Value; };
99    }
100
101    public int GetNumberOfParameters(int numberOfVariables) {
102      if (selectedDimensions == null) return cov.GetNumberOfParameters(numberOfVariables);
103      else return cov.GetNumberOfParameters(selectedDimensions.Length);
104    }
105
106    public void SetParameter(double[] hyp) {
107      cov.SetParameter(hyp);
108    }
109
110    public double GetCovariance(double[,] x, int i, int j, IEnumerable<int> columnIndices) {
111      return cov.GetCovariance(x, i, j, selectedDimensions);
112    }
113
114    public IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices) {
115      return cov.GetGradient(x, i, j, selectedDimensions);
116    }
117
118    public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j, IEnumerable<int> columnIndices) {
119      return cov.GetCrossCovariance(x, xt, i, j, selectedDimensions);
120    }
121  }
122}
Note: See TracBrowser for help on using the repository browser.