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