Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/RadialBasisFunctions/KernelFunctions/KernelBase.cs @ 14386

Last change on this file since 14386 was 14386, checked in by bwerth, 7 years ago

#2699 moved RadialBasisFunctions from Problems.SurrogateProblem to Algorithms.DataAnalysis

File size: 5.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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
22using System;
23using System.Collections;
24using System.Collections.Generic;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Algorithms.DataAnalysis {
32  [StorableClass]
33  public abstract class KernelBase<T> : ParameterizedNamedItem, IKernelFunction<T> {
34
35    #region Parameternames
36    private const string DistanceParameterName = "Distance";
37    protected const string BetaParameterName = "Beta";
38    #endregion
39    #region Parameterproperties
40    public ValueParameter<IDistance<T>> DistanceParameter
41    {
42      get { return Parameters[DistanceParameterName] as ValueParameter<IDistance<T>>; }
43    }
44
45    public IFixedValueParameter<DoubleValue> BetaParameter
46    {
47      get { return Parameters[BetaParameterName] as FixedValueParameter<DoubleValue>; }
48    }
49
50    #endregion
51    #region Properties
52    public IDistance<T> Distance
53    {
54      get { return DistanceParameter.Value; }
55    }
56
57    public double Beta
58    {
59      get { return BetaParameter.Value.Value; }
60    }
61
62    #endregion
63
64    #region HLConstructors & Boilerplate
65    [StorableConstructor]
66    protected KernelBase(bool deserializing) : base(deserializing) { }
67    [StorableHook(HookType.AfterDeserialization)]
68    private void AfterDeserialization() { }
69
70    protected KernelBase(KernelBase<T> original, Cloner cloner)
71      : base(original, cloner) { }
72
73    protected KernelBase() {
74      Parameters.Add(new ValueParameter<IDistance<T>>(DistanceParameterName, "The distance function used for kernel calculation"));
75      DistanceParameter.Value = new EuclidianDistance() as IDistance<T>;
76    }
77    #endregion
78
79    public double Get(T a, T b) {
80      return Get(Distance.Get(a, b));
81    }
82
83    protected abstract double Get(double norm);
84
85    public int GetNumberOfParameters(int numberOfVariables) {
86      return 1;
87    }
88
89    public void SetParameter(double[] p) {
90      if (p != null && p.Length == 1) BetaParameter.Value.Value = p[0];
91    }
92
93    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) {
94      if (p == null || p.Length != 1) throw new ArgumentException("Illegal parametrization");
95      var myClone = (KernelBase<T>)Clone(new Cloner());
96      myClone.BetaParameter.Value.Value = p[0];
97      var cov = new ParameterizedCovarianceFunction {
98        Covariance = (x, i, j) => myClone.Get(GetNorm(x, x, i, j, columnIndices)),
99        CrossCovariance = (x, xt, i, j) => myClone.Get(GetNorm(x, xt, i, j, columnIndices)),
100        CovarianceGradient = (x, i, j) => new List<double> { myClone.GetGradient(GetNorm(x, x, i, j, columnIndices)) }
101      };
102      return cov;
103    }
104
105    protected abstract double GetGradient(double norm);
106
107    protected double GetNorm(double[,] x, double[,] xt, int i, int j, int[] columnIndices) {
108      var dist = Distance as IDistance<IEnumerable<double>>;
109      if (dist == null) throw new ArgumentException("The Distance needs to apply to double-Vectors");
110      var r1 = new IndexedEnumerable(x, i, columnIndices);
111      var r2 = new IndexedEnumerable(xt, j, columnIndices);
112      return dist.Get(r1, r2);
113    }
114    internal class IndexedEnumerable : IEnumerable<double> {
115      private readonly double[,] data;
116      private readonly int row;
117      private readonly int[] columnIndices;
118
119      public IndexedEnumerable(double[,] data, int row, int[] columnIndices) {
120        this.data = data;
121        this.row = row;
122        this.columnIndices = columnIndices;
123      }
124
125      public IEnumerator<double> GetEnumerator() {
126        return new IndexedEnumerator(data, row, columnIndices);
127      }
128
129      IEnumerator IEnumerable.GetEnumerator() {
130        return new IndexedEnumerator(data, row, columnIndices);
131      }
132    }
133    internal class IndexedEnumerator : IEnumerator<double> {
134      private readonly IEnumerator<int> column;
135      private readonly double[,] data;
136      private readonly int row;
137
138      public IndexedEnumerator(double[,] data, int row, int[] columnIndices) {
139        this.data = data;
140        this.row = row;
141        column = ((IEnumerable<int>)columnIndices).GetEnumerator();
142      }
143
144      public double Current
145      {
146        get { return data[row, column.Current]; }
147      }
148
149      object IEnumerator.Current
150      {
151        get
152        {
153          return data[row, column.Current];
154        }
155      }
156
157      public void Dispose() { }
158
159      public bool MoveNext() {
160        return column.MoveNext();
161      }
162
163      public void Reset() {
164        column.Reset();
165      }
166    }
167  }
168}
Note: See TracBrowser for help on using the repository browser.