[8416] | 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 |
|
---|
[8463] | 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
[8323] | 24 | using System.Linq;
|
---|
[8416] | 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[8323] | 28 |
|
---|
[8416] | 29 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
| 30 | [StorableClass]
|
---|
| 31 | [Item(Name = "CovarianceProd",
|
---|
| 32 | Description = "Product covariance function for Gaussian processes.")]
|
---|
| 33 | public class CovarianceProd : Item, ICovarianceFunction {
|
---|
| 34 | [Storable]
|
---|
| 35 | private ItemList<ICovarianceFunction> factors;
|
---|
[8323] | 36 |
|
---|
[8416] | 37 | [Storable]
|
---|
| 38 | private int numberOfVariables;
|
---|
| 39 | public ItemList<ICovarianceFunction> Factors {
|
---|
| 40 | get { return factors; }
|
---|
[8323] | 41 | }
|
---|
| 42 |
|
---|
[8416] | 43 | [StorableConstructor]
|
---|
| 44 | protected CovarianceProd(bool deserializing)
|
---|
| 45 | : base(deserializing) {
|
---|
[8323] | 46 | }
|
---|
| 47 |
|
---|
[8416] | 48 | protected CovarianceProd(CovarianceProd original, Cloner cloner)
|
---|
| 49 | : base(original, cloner) {
|
---|
| 50 | this.factors = cloner.Clone(original.factors);
|
---|
| 51 | this.numberOfVariables = original.numberOfVariables;
|
---|
[8463] | 52 | AttachEventHandlers();
|
---|
[8323] | 53 | }
|
---|
| 54 |
|
---|
[8416] | 55 | public CovarianceProd()
|
---|
| 56 | : base() {
|
---|
| 57 | this.factors = new ItemList<ICovarianceFunction>();
|
---|
[8463] | 58 | AttachEventHandlers();
|
---|
[8416] | 59 | }
|
---|
| 60 |
|
---|
[8463] | 61 | private void AttachEventHandlers() {
|
---|
| 62 | this.factors.CollectionReset += (sender, args) => ClearCache();
|
---|
| 63 | this.factors.ItemsAdded += (sender, args) => ClearCache();
|
---|
| 64 | this.factors.ItemsRemoved += (sender, args) => ClearCache();
|
---|
| 65 | this.factors.ItemsReplaced += (sender, args) => ClearCache();
|
---|
| 66 | this.factors.ItemsMoved += (sender, args) => ClearCache();
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[8416] | 69 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 70 | return new CovarianceProd(this, cloner);
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | public int GetNumberOfParameters(int numberOfVariables) {
|
---|
| 74 | this.numberOfVariables = numberOfVariables;
|
---|
| 75 | return factors.Select(t => t.GetNumberOfParameters(numberOfVariables)).Sum();
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | public void SetParameter(double[] hyp) {
|
---|
| 79 | int offset = 0;
|
---|
| 80 | foreach (var t in factors) {
|
---|
| 81 | var numberOfParameters = t.GetNumberOfParameters(numberOfVariables);
|
---|
| 82 | t.SetParameter(hyp.Skip(offset).Take(numberOfParameters).ToArray());
|
---|
| 83 | offset += numberOfParameters;
|
---|
[8323] | 84 | }
|
---|
| 85 | }
|
---|
[8416] | 86 | public void SetData(double[,] x) {
|
---|
| 87 | SetData(x, x);
|
---|
| 88 | }
|
---|
[8323] | 89 |
|
---|
[8416] | 90 | public void SetData(double[,] x, double[,] xt) {
|
---|
| 91 | foreach (var t in factors) {
|
---|
| 92 | t.SetData(x, xt);
|
---|
[8323] | 93 | }
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | public double GetCovariance(int i, int j) {
|
---|
[8416] | 97 | return factors.Select(t => t.GetCovariance(i, j)).Aggregate((a, b) => a * b);
|
---|
[8323] | 98 | }
|
---|
| 99 |
|
---|
[8463] | 100 | private Dictionary<int, Tuple<int, int>> cachedParameterMap;
|
---|
[8439] | 101 | public double GetGradient(int i, int j, int k) {
|
---|
[8463] | 102 | if (cachedParameterMap == null) {
|
---|
| 103 | CalculateParameterMap();
|
---|
| 104 | }
|
---|
| 105 | int ti = cachedParameterMap[k].Item1;
|
---|
| 106 | k = cachedParameterMap[k].Item2;
|
---|
[8439] | 107 | double res = 1.0;
|
---|
| 108 | for (int ii = 0; ii < factors.Count; ii++) {
|
---|
| 109 | var f = factors[ii];
|
---|
[8463] | 110 | if (ii == ti) {
|
---|
| 111 | res *= f.GetGradient(i, j, k);
|
---|
[8439] | 112 | } else {
|
---|
| 113 | res *= f.GetCovariance(i, j);
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 | return res;
|
---|
| 117 | }
|
---|
[8463] | 118 |
|
---|
| 119 | private void ClearCache() {
|
---|
| 120 | cachedParameterMap = null;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | private void CalculateParameterMap() {
|
---|
| 124 | cachedParameterMap = new Dictionary<int, Tuple<int, int>>();
|
---|
| 125 | int k = 0;
|
---|
| 126 | for (int ti = 0; ti < factors.Count; ti++) {
|
---|
| 127 | for (int ti_k = 0; ti_k < factors[ti].GetNumberOfParameters(numberOfVariables); ti_k++) {
|
---|
| 128 | cachedParameterMap[k++] = Tuple.Create(ti, ti_k);
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
[8323] | 132 | }
|
---|
| 133 | }
|
---|