[8620] | 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 System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.Parameters;
|
---|
| 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 |
|
---|
| 31 | namespace 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 | [Storable]
|
---|
| 37 | private double sf2;
|
---|
| 38 | [Storable]
|
---|
| 39 | private readonly HyperParameter<DoubleValue> scaleParameter;
|
---|
| 40 | public IValueParameter<DoubleValue> ScaleParameter {
|
---|
| 41 | get { return scaleParameter; }
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | [Storable]
|
---|
| 45 | private ICovarianceFunction cov;
|
---|
| 46 | [Storable]
|
---|
| 47 | private readonly ValueParameter<ICovarianceFunction> covParameter;
|
---|
| 48 | public IValueParameter<ICovarianceFunction> CovarianceFunctionParameter {
|
---|
| 49 | get { return covParameter; }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | [StorableConstructor]
|
---|
| 53 | private CovarianceScale(bool deserializing)
|
---|
| 54 | : base(deserializing) {
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | private CovarianceScale(CovarianceScale original, Cloner cloner)
|
---|
| 58 | : base(original, cloner) {
|
---|
| 59 | this.scaleParameter = cloner.Clone(original.scaleParameter);
|
---|
| 60 | this.sf2 = original.sf2;
|
---|
| 61 |
|
---|
| 62 | this.covParameter = cloner.Clone(original.covParameter);
|
---|
| 63 | this.cov = cloner.Clone(original.cov);
|
---|
| 64 | RegisterEvents();
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | public CovarianceScale()
|
---|
| 68 | : base() {
|
---|
| 69 | Name = ItemName;
|
---|
| 70 | Description = ItemDescription;
|
---|
| 71 |
|
---|
| 72 | this.scaleParameter = new HyperParameter<DoubleValue>("Scale", "The scale parameter.");
|
---|
| 73 | this.covParameter = new ValueParameter<ICovarianceFunction>("CovarianceFunction", "The covariance function that should be scaled.", new CovarianceSquaredExponentialIso());
|
---|
| 74 |
|
---|
| 75 | Parameters.Add(this.scaleParameter);
|
---|
| 76 | Parameters.Add(covParameter);
|
---|
| 77 |
|
---|
| 78 | RegisterEvents();
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 82 | return new CovarianceScale(this, cloner);
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 86 | private void AfterDeserialization() {
|
---|
| 87 | RegisterEvents();
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | private void RegisterEvents() {
|
---|
| 91 | Util.AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { sf2 = scaleParameter.Value.Value; });
|
---|
| 92 | covParameter.ValueChanged += (sender, args) => { cov = covParameter.Value; };
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | public int GetNumberOfParameters(int numberOfVariables) {
|
---|
| 96 | return (scaleParameter.Fixed ? 0 : 1) + cov.GetNumberOfParameters(numberOfVariables);
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | public void SetParameter(double[] hyp) {
|
---|
| 100 | int i = 0;
|
---|
| 101 | if (!scaleParameter.Fixed) {
|
---|
| 102 | scaleParameter.SetValue(new DoubleValue(Math.Exp(2 * hyp[i])));
|
---|
| 103 | i++;
|
---|
| 104 | }
|
---|
| 105 | cov.SetParameter(hyp.Skip(i).ToArray());
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | public double GetCovariance(double[,] x, int i, int j) {
|
---|
| 109 | return sf2 * cov.GetCovariance(x, i, j);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | public IEnumerable<double> GetGradient(double[,] x, int i, int j) {
|
---|
| 113 | yield return 2 * sf2 * cov.GetCovariance(x, i, j);
|
---|
| 114 | foreach (var g in cov.GetGradient(x, i, j))
|
---|
| 115 | yield return sf2 * g;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {
|
---|
| 119 | return sf2 * cov.GetCrossCovariance(x, xt, i, j);
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 | }
|
---|