Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.Orienteering/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceScale.cs @ 11868

Last change on this file since 11868 was 11185, checked in by pfleck, 10 years ago

#2208 merged trunk and updated version info

File size: 4.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Algorithms.DataAnalysis {
32  [StorableClass]
33  [Item(Name = "CovarianceScale",
34    Description = "Scale covariance function for Gaussian processes.")]
35  public sealed class CovarianceScale : ParameterizedNamedItem, ICovarianceFunction {
36    public IValueParameter<DoubleValue> ScaleParameter {
37      get { return (IValueParameter<DoubleValue>)Parameters["Scale"]; }
38    }
39    private bool HasFixedScaleParameter {
40      get { return ScaleParameter.Value != null; }
41    }
42
43    public IValueParameter<ICovarianceFunction> CovarianceFunctionParameter {
44      get { return (IValueParameter<ICovarianceFunction>)Parameters["CovarianceFunction"]; }
45    }
46
47    [StorableConstructor]
48    private CovarianceScale(bool deserializing)
49      : base(deserializing) {
50    }
51
52    private CovarianceScale(CovarianceScale original, Cloner cloner)
53      : base(original, cloner) {
54    }
55
56    public CovarianceScale()
57      : base() {
58      Name = ItemName;
59      Description = ItemDescription;
60
61      Parameters.Add(new OptionalValueParameter<DoubleValue>("Scale", "The scale parameter."));
62      Parameters.Add(new ValueParameter<ICovarianceFunction>("CovarianceFunction", "The covariance function that should be scaled.", new CovarianceSquaredExponentialIso()));
63    }
64
65    public override IDeepCloneable Clone(Cloner cloner) {
66      return new CovarianceScale(this, cloner);
67    }
68
69    public int GetNumberOfParameters(int numberOfVariables) {
70      return (HasFixedScaleParameter ? 0 : 1) + CovarianceFunctionParameter.Value.GetNumberOfParameters(numberOfVariables);
71    }
72
73    public void SetParameter(double[] p) {
74      double scale;
75      GetParameterValues(p, out scale);
76      ScaleParameter.Value = new DoubleValue(scale);
77      CovarianceFunctionParameter.Value.SetParameter(p.Skip(1).ToArray());
78    }
79
80    private void GetParameterValues(double[] p, out double scale) {
81      // gather parameter values
82      if (HasFixedScaleParameter) {
83        scale = ScaleParameter.Value.Value;
84      } else {
85        scale = Math.Exp(2 * p[0]);
86      }
87    }
88
89    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int> columnIndices) {
90      double scale;
91      GetParameterValues(p, out scale);
92      var fixedScale = HasFixedScaleParameter;
93      var subCov = CovarianceFunctionParameter.Value.GetParameterizedCovarianceFunction(p.Skip(1).ToArray(), columnIndices);
94      // create functions
95      var cov = new ParameterizedCovarianceFunction();
96      cov.Covariance = (x, i, j) => scale * subCov.Covariance(x, i, j);
97      cov.CrossCovariance = (x, xt, i, j) => scale * subCov.CrossCovariance(x, xt, i, j);
98      cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, subCov, fixedScale);
99      return cov;
100    }
101
102    private static IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices, double scale, ParameterizedCovarianceFunction cov,
103      bool fixedScale) {
104      if (!fixedScale) {
105        yield return 2 * scale * cov.Covariance(x, i, j);
106      }
107      foreach (var g in cov.CovarianceGradient(x, i, j))
108        yield return scale * g;
109    }
110  }
111}
Note: See TracBrowser for help on using the repository browser.