[9515] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11594] | 3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[9515] | 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 = "CovariancePolynomial",
|
---|
| 34 | Description = "Polynomial covariance function for Gaussian processes.")]
|
---|
| 35 | public sealed class CovariancePolynomial : ParameterizedNamedItem, ICovarianceFunction {
|
---|
[9516] | 36 | public IValueParameter<DoubleValue> ConstParameter {
|
---|
| 37 | get { return (IValueParameter<DoubleValue>)Parameters["Const"]; }
|
---|
[9515] | 38 | }
|
---|
| 39 |
|
---|
| 40 | public IValueParameter<DoubleValue> ScaleParameter {
|
---|
| 41 | get { return (IValueParameter<DoubleValue>)Parameters["Scale"]; }
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | public IValueParameter<IntValue> DegreeParameter {
|
---|
| 45 | get { return (IValueParameter<IntValue>)Parameters["Degree"]; }
|
---|
| 46 | }
|
---|
[11594] | 47 | private bool HasFixedConstParameter {
|
---|
| 48 | get { return ConstParameter.Value != null; }
|
---|
| 49 | }
|
---|
| 50 | private bool HasFixedScaleParameter {
|
---|
| 51 | get { return ScaleParameter.Value != null; }
|
---|
| 52 | }
|
---|
[9515] | 53 |
|
---|
| 54 | [StorableConstructor]
|
---|
| 55 | private CovariancePolynomial(bool deserializing)
|
---|
| 56 | : base(deserializing) {
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | private CovariancePolynomial(CovariancePolynomial original, Cloner cloner)
|
---|
| 60 | : base(original, cloner) {
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | public CovariancePolynomial()
|
---|
| 64 | : base() {
|
---|
| 65 | Name = ItemName;
|
---|
| 66 | Description = ItemDescription;
|
---|
| 67 |
|
---|
[9516] | 68 | Parameters.Add(new OptionalValueParameter<DoubleValue>("Const", "Additive constant in the polymomial."));
|
---|
[9535] | 69 | Parameters.Add(new OptionalValueParameter<DoubleValue>("Scale", "The scale parameter of the polynomial covariance function."));
|
---|
[9515] | 70 | Parameters.Add(new ValueParameter<IntValue>("Degree", "The degree of the polynomial (only non-zero positive values allowed).", new IntValue(2)));
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 74 | return new CovariancePolynomial(this, cloner);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | public int GetNumberOfParameters(int numberOfVariables) {
|
---|
| 78 | return
|
---|
[11594] | 79 | (HasFixedConstParameter ? 0 : 1) +
|
---|
| 80 | (HasFixedScaleParameter ? 0 : 1);
|
---|
[9515] | 81 | }
|
---|
| 82 |
|
---|
| 83 | public void SetParameter(double[] p) {
|
---|
[9516] | 84 | double @const, scale;
|
---|
| 85 | GetParameterValues(p, out @const, out scale);
|
---|
| 86 | ConstParameter.Value = new DoubleValue(@const);
|
---|
[9515] | 87 | ScaleParameter.Value = new DoubleValue(scale);
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[9516] | 90 | private void GetParameterValues(double[] p, out double @const, out double scale) {
|
---|
[9515] | 91 | // gather parameter values
|
---|
| 92 | int n = 0;
|
---|
[11594] | 93 | if (HasFixedConstParameter) {
|
---|
[9516] | 94 | @const = ConstParameter.Value.Value;
|
---|
[9515] | 95 | } else {
|
---|
[9516] | 96 | @const = Math.Exp(p[n]);
|
---|
[9515] | 97 | n++;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[11594] | 100 | if (HasFixedScaleParameter) {
|
---|
[9515] | 101 | scale = ScaleParameter.Value.Value;
|
---|
| 102 | } else {
|
---|
| 103 | scale = Math.Exp(2 * p[n]);
|
---|
| 104 | n++;
|
---|
| 105 | }
|
---|
| 106 | if (p.Length != n) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovariancePolynomial", "p");
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int> columnIndices) {
|
---|
[9516] | 110 | double @const, scale;
|
---|
| 111 | int degree = DegreeParameter.Value.Value;
|
---|
| 112 | if (degree <= 0) throw new ArgumentException("The degree parameter for CovariancePolynomial must be greater than zero.");
|
---|
| 113 | GetParameterValues(p, out @const, out scale);
|
---|
[11594] | 114 | var fixedConst = HasFixedConstParameter;
|
---|
| 115 | var fixedScale = HasFixedScaleParameter;
|
---|
[9515] | 116 | // create functions
|
---|
| 117 | var cov = new ParameterizedCovarianceFunction();
|
---|
[9516] | 118 | cov.Covariance = (x, i, j) => scale * Math.Pow(@const + Util.ScalarProd(x, i, j, 1.0, columnIndices), degree);
|
---|
| 119 | cov.CrossCovariance = (x, xt, i, j) => scale * Math.Pow(@const + Util.ScalarProd(x, i, xt, j, 1.0, columnIndices), degree);
|
---|
[11594] | 120 | cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, @const, scale, degree, columnIndices, fixedConst, fixedScale);
|
---|
[9515] | 121 | return cov;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
[11594] | 124 | private static IEnumerable<double> GetGradient(double[,] x, int i, int j, double c, double scale, int degree, IEnumerable<int> columnIndices,
|
---|
| 125 | bool fixedConst, bool fixedScale) {
|
---|
[9515] | 126 | double s = Util.ScalarProd(x, i, j, 1.0, columnIndices);
|
---|
[11594] | 127 | if (!fixedConst) yield return c * degree * scale * Math.Pow(c + s, degree - 1);
|
---|
| 128 | if (!fixedScale) yield return 2 * scale * Math.Pow(c + s, degree);
|
---|
[9515] | 129 | }
|
---|
| 130 | }
|
---|
| 131 | }
|
---|