[8439] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[8439] | 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
|
---|
[8982] | 21 |
|
---|
| 22 | using System.Collections.Generic;
|
---|
[8439] | 23 | using System.Linq;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
[16565] | 26 | using HEAL.Attic;
|
---|
[8439] | 27 |
|
---|
| 28 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
[16565] | 29 | [StorableType("E8BB7879-43C7-4B9A-B9AC-C9CC482BC0D7")]
|
---|
[8439] | 30 | [Item(Name = "MeanSum", Description = "Sum of mean functions for Gaussian processes.")]
|
---|
[8612] | 31 | public sealed class MeanSum : Item, IMeanFunction {
|
---|
[8439] | 32 | [Storable]
|
---|
| 33 | private ItemList<IMeanFunction> terms;
|
---|
| 34 |
|
---|
| 35 | [Storable]
|
---|
| 36 | private int numberOfVariables;
|
---|
| 37 | public ItemList<IMeanFunction> Terms {
|
---|
| 38 | get { return terms; }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | [StorableConstructor]
|
---|
[16565] | 42 | private MeanSum(StorableConstructorFlag _) : base(_) { }
|
---|
[8612] | 43 | private MeanSum(MeanSum original, Cloner cloner)
|
---|
[8439] | 44 | : base(original, cloner) {
|
---|
| 45 | this.terms = cloner.Clone(original.terms);
|
---|
| 46 | this.numberOfVariables = original.numberOfVariables;
|
---|
| 47 | }
|
---|
| 48 | public MeanSum() {
|
---|
| 49 | this.terms = new ItemList<IMeanFunction>();
|
---|
| 50 | }
|
---|
| 51 |
|
---|
[8612] | 52 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 53 | return new MeanSum(this, cloner);
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | public int GetNumberOfParameters(int numberOfVariables) {
|
---|
| 57 | this.numberOfVariables = numberOfVariables;
|
---|
| 58 | return terms.Select(t => t.GetNumberOfParameters(numberOfVariables)).Sum();
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[8982] | 61 | public void SetParameter(double[] p) {
|
---|
[8439] | 62 | int offset = 0;
|
---|
| 63 | foreach (var t in terms) {
|
---|
| 64 | var numberOfParameters = t.GetNumberOfParameters(numberOfVariables);
|
---|
[8982] | 65 | t.SetParameter(p.Skip(offset).Take(numberOfParameters).ToArray());
|
---|
[8439] | 66 | offset += numberOfParameters;
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
| 69 |
|
---|
[13721] | 70 | public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, int[] columnIndices) {
|
---|
[8982] | 71 | var termMf = new List<ParameterizedMeanFunction>();
|
---|
| 72 | int totalNumberOfParameters = GetNumberOfParameters(numberOfVariables);
|
---|
| 73 | int[] termIndexMap = new int[totalNumberOfParameters]; // maps k-th parameter to the correct mean-term
|
---|
| 74 | int[] hyperParameterIndexMap = new int[totalNumberOfParameters]; // maps k-th parameter to the l-th parameter of the correct mean-term
|
---|
| 75 | int c = 0;
|
---|
| 76 | // get the parameterized mean function for each term
|
---|
| 77 | for (int termIndex = 0; termIndex < terms.Count; termIndex++) {
|
---|
| 78 | var numberOfParameters = terms[termIndex].GetNumberOfParameters(numberOfVariables);
|
---|
| 79 | termMf.Add(terms[termIndex].GetParameterizedMeanFunction(p.Take(numberOfParameters).ToArray(), columnIndices));
|
---|
| 80 | p = p.Skip(numberOfParameters).ToArray();
|
---|
| 81 |
|
---|
| 82 | for (int hyperParameterIndex = 0; hyperParameterIndex < numberOfParameters; hyperParameterIndex++) {
|
---|
| 83 | termIndexMap[c] = termIndex;
|
---|
| 84 | hyperParameterIndexMap[c] = hyperParameterIndex;
|
---|
| 85 | c++;
|
---|
| 86 | }
|
---|
[8439] | 87 | }
|
---|
| 88 |
|
---|
[8982] | 89 | var mf = new ParameterizedMeanFunction();
|
---|
| 90 | mf.Mean = (x, i) => termMf.Select(t => t.Mean(x, i)).Sum();
|
---|
| 91 | mf.Gradient = (x, i, k) => {
|
---|
| 92 | return termMf[termIndexMap[k]].Gradient(x, i, hyperParameterIndexMap[k]);
|
---|
| 93 | };
|
---|
| 94 | return mf;
|
---|
[8439] | 95 | }
|
---|
| 96 | }
|
---|
| 97 | }
|
---|