[8401] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[8401] | 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;
|
---|
[8484] | 23 | using System.Collections.Generic;
|
---|
[8323] | 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
[8612] | 27 | using HeuristicLab.Data;
|
---|
[8982] | 28 | using HeuristicLab.Parameters;
|
---|
[16565] | 29 | using HEAL.Attic;
|
---|
[8323] | 30 |
|
---|
[8371] | 31 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
[16565] | 32 | [StorableType("88CB6895-6CD8-4F81-8E3D-905EEBB7E77A")]
|
---|
[8615] | 33 | [Item(Name = "CovarianceSquaredExponentialArd", Description = "Squared exponential covariance function with automatic relevance determination for Gaussian processes.")]
|
---|
| 34 | public sealed class CovarianceSquaredExponentialArd : ParameterizedNamedItem, ICovarianceFunction {
|
---|
[8982] | 35 | public IValueParameter<DoubleValue> ScaleParameter {
|
---|
| 36 | get { return (IValueParameter<DoubleValue>)Parameters["Scale"]; }
|
---|
| 37 | }
|
---|
[8473] | 38 |
|
---|
[8982] | 39 | public IValueParameter<DoubleArray> InverseLengthParameter {
|
---|
| 40 | get { return (IValueParameter<DoubleArray>)Parameters["InverseLength"]; }
|
---|
| 41 | }
|
---|
[10489] | 42 | private bool HasFixedInverseLengthParameter {
|
---|
| 43 | get { return InverseLengthParameter.Value != null; }
|
---|
| 44 | }
|
---|
| 45 | private bool HasFixedScaleParameter {
|
---|
| 46 | get { return ScaleParameter.Value != null; }
|
---|
| 47 | }
|
---|
[8323] | 48 |
|
---|
| 49 | [StorableConstructor]
|
---|
[16565] | 50 | private CovarianceSquaredExponentialArd(StorableConstructorFlag _) : base(_) { }
|
---|
[8615] | 51 | private CovarianceSquaredExponentialArd(CovarianceSquaredExponentialArd original, Cloner cloner)
|
---|
[8323] | 52 | : base(original, cloner) {
|
---|
| 53 | }
|
---|
[8615] | 54 | public CovarianceSquaredExponentialArd()
|
---|
[8323] | 55 | : base() {
|
---|
[8612] | 56 | Name = ItemName;
|
---|
| 57 | Description = ItemDescription;
|
---|
| 58 |
|
---|
[8982] | 59 | Parameters.Add(new OptionalValueParameter<DoubleValue>("Scale", "The scale parameter of the squared exponential covariance function with ARD."));
|
---|
| 60 | Parameters.Add(new OptionalValueParameter<DoubleArray>("InverseLength", "The inverse length parameter for automatic relevance determination."));
|
---|
[8323] | 61 | }
|
---|
| 62 |
|
---|
| 63 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[8615] | 64 | return new CovarianceSquaredExponentialArd(this, cloner);
|
---|
[8323] | 65 | }
|
---|
| 66 |
|
---|
[8612] | 67 | public int GetNumberOfParameters(int numberOfVariables) {
|
---|
| 68 | return
|
---|
[10489] | 69 | (HasFixedScaleParameter ? 0 : 1) +
|
---|
| 70 | (HasFixedInverseLengthParameter ? 0 : numberOfVariables);
|
---|
[8612] | 71 | }
|
---|
| 72 |
|
---|
[8982] | 73 | public void SetParameter(double[] p) {
|
---|
| 74 | double scale;
|
---|
| 75 | double[] inverseLength;
|
---|
| 76 | GetParameterValues(p, out scale, out inverseLength);
|
---|
| 77 | ScaleParameter.Value = new DoubleValue(scale);
|
---|
| 78 | InverseLengthParameter.Value = new DoubleArray(inverseLength);
|
---|
| 79 | }
|
---|
[8612] | 80 |
|
---|
[8982] | 81 | private void GetParameterValues(double[] p, out double scale, out double[] inverseLength) {
|
---|
| 82 | int c = 0;
|
---|
| 83 | // gather parameter values
|
---|
[10489] | 84 | if (HasFixedInverseLengthParameter) {
|
---|
[9108] | 85 | inverseLength = InverseLengthParameter.Value.ToArray();
|
---|
| 86 | } else {
|
---|
| 87 | int length = p.Length;
|
---|
[10493] | 88 | if (!HasFixedScaleParameter) length--;
|
---|
[9108] | 89 | inverseLength = p.Select(e => 1.0 / Math.Exp(e)).Take(length).ToArray();
|
---|
| 90 | c += inverseLength.Length;
|
---|
| 91 | }
|
---|
[10489] | 92 | if (HasFixedScaleParameter) {
|
---|
[8982] | 93 | scale = ScaleParameter.Value.Value;
|
---|
| 94 | } else {
|
---|
| 95 | scale = Math.Exp(2 * p[c]);
|
---|
| 96 | c++;
|
---|
[8612] | 97 | }
|
---|
[8982] | 98 | if (p.Length != c) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceSquaredExponentialArd", "p");
|
---|
[8416] | 99 | }
|
---|
| 100 |
|
---|
[13721] | 101 | public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) {
|
---|
[8982] | 102 | double scale;
|
---|
| 103 | double[] inverseLength;
|
---|
| 104 | GetParameterValues(p, out scale, out inverseLength);
|
---|
[10489] | 105 | var fixedInverseLength = HasFixedInverseLengthParameter;
|
---|
| 106 | var fixedScale = HasFixedScaleParameter;
|
---|
[8982] | 107 | // create functions
|
---|
| 108 | var cov = new ParameterizedCovarianceFunction();
|
---|
| 109 | cov.Covariance = (x, i, j) => {
|
---|
| 110 | double d = i == j
|
---|
| 111 | ? 0.0
|
---|
| 112 | : Util.SqrDist(x, i, j, inverseLength, columnIndices);
|
---|
| 113 | return scale * Math.Exp(-d / 2.0);
|
---|
| 114 | };
|
---|
| 115 | cov.CrossCovariance = (x, xt, i, j) => {
|
---|
| 116 | double d = Util.SqrDist(x, i, xt, j, inverseLength, columnIndices);
|
---|
| 117 | return scale * Math.Exp(-d / 2.0);
|
---|
| 118 | };
|
---|
[10489] | 119 | cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, inverseLength, fixedInverseLength, fixedScale);
|
---|
[8982] | 120 | return cov;
|
---|
[8323] | 121 | }
|
---|
| 122 |
|
---|
[9108] | 123 | // order of returned gradients must match the order in GetParameterValues!
|
---|
[13784] | 124 | private static IList<double> GetGradient(double[,] x, int i, int j, int[] columnIndices, double scale, double[] inverseLength,
|
---|
[10489] | 125 | bool fixedInverseLength, bool fixedScale) {
|
---|
[8484] | 126 | double d = i == j
|
---|
| 127 | ? 0.0
|
---|
[8678] | 128 | : Util.SqrDist(x, i, j, inverseLength, columnIndices);
|
---|
[9108] | 129 |
|
---|
[8933] | 130 | int k = 0;
|
---|
[13784] | 131 | var g = new List<double>((!fixedInverseLength ? columnIndices.Length : 0) + (!fixedScale ? 1 : 0));
|
---|
[10489] | 132 | if (!fixedInverseLength) {
|
---|
[13721] | 133 | for (int c = 0; c < columnIndices.Length; c++) {
|
---|
| 134 | var columnIndex = columnIndices[c];
|
---|
[10489] | 135 | double sqrDist = Util.SqrDist(x[i, columnIndex] * inverseLength[k], x[j, columnIndex] * inverseLength[k]);
|
---|
[13784] | 136 | g.Add(scale * Math.Exp(-d / 2.0) * sqrDist);
|
---|
[10489] | 137 | k++;
|
---|
| 138 | }
|
---|
[8323] | 139 | }
|
---|
[13784] | 140 | if (!fixedScale) g.Add(2.0 * scale * Math.Exp(-d / 2.0));
|
---|
| 141 | return g;
|
---|
[8323] | 142 | }
|
---|
| 143 | }
|
---|
| 144 | }
|
---|