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