Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14872 was 14872, checked in by gkronber, 7 years ago

#2699: made a number of changes mainly to RBF regression model

File size: 5.4 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 : ParameterizedNamedItem, ICovarianceFunction {
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> DistanceParameter {
41      get { return Parameters[DistanceParameterName] as ValueParameter<IDistance>; }
42    }
43
44    public IFixedValueParameter<DoubleValue> BetaParameter {
45      get { return Parameters[BetaParameterName] as FixedValueParameter<DoubleValue>; }
46    }
47
48    #endregion
49    #region Properties
50    public IDistance Distance {
51      get { return DistanceParameter.Value; }
52    }
53
54    public double Beta {
55      get { return BetaParameter.Value.Value; }
56    }
57
58    #endregion
59
60    [StorableConstructor]
61    protected KernelBase(bool deserializing) : base(deserializing) { }
62    [StorableHook(HookType.AfterDeserialization)]
63    private void AfterDeserialization() { }
64
65    protected KernelBase(KernelBase original, Cloner cloner)
66      : base(original, cloner) { }
67
68    protected KernelBase() {
69      Parameters.Add(new ValueParameter<IDistance>(DistanceParameterName, "The distance function used for kernel calculation"));
70      DistanceParameter.Value = new EuclideanDistance();
71    }
72
73    public double Get(object a, object b) {
74      return Get(Distance.Get(a, b));
75    }
76
77    protected abstract double Get(double norm);
78
79    public int GetNumberOfParameters(int numberOfVariables) {
80      return 1;
81    }
82
83    public void SetParameter(double[] p) {
84      if (p != null && p.Length == 1) BetaParameter.Value.Value = p[0];
85    }
86
87    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) {
88      if (p == null || p.Length != 1) throw new ArgumentException("Illegal parametrization");
89      var myClone = (KernelBase)Clone(new Cloner());
90      myClone.BetaParameter.Value.Value = p[0];
91      var cov = new ParameterizedCovarianceFunction {
92        Covariance = (x, i, j) => myClone.Get(GetNorm(x, x, i, j, columnIndices)),
93        CrossCovariance = (x, xt, i, j) => myClone.Get(GetNorm(x, xt, i, j, columnIndices)),
94        CovarianceGradient = (x, i, j) => new List<double> { myClone.GetGradient(GetNorm(x, x, i, j, columnIndices)) }
95      };
96      return cov;
97    }
98
99    protected abstract double GetGradient(double norm);
100
101    protected double GetNorm(double[,] x, double[,] xt, int i, int j, int[] columnIndices) {
102      var dist = Distance as IDistance<IEnumerable<double>>;
103      if (dist == null) throw new ArgumentException("The distance needs to apply to double vectors");
104      var r1 = new IndexedEnumerable(x, i, columnIndices);
105      var r2 = new IndexedEnumerable(xt, j, columnIndices);
106      return dist.Get(r1, r2);
107    }
108    internal class IndexedEnumerable : IEnumerable<double> {
109      private readonly double[,] data;
110      private readonly int row;
111      private readonly int[] columnIndices;
112
113      public IndexedEnumerable(double[,] data, int row, int[] columnIndices) {
114        this.data = data;
115        this.row = row;
116        this.columnIndices = columnIndices;
117      }
118
119      public IEnumerator<double> GetEnumerator() {
120        return new IndexedEnumerator(data, row, columnIndices);
121      }
122
123      IEnumerator IEnumerable.GetEnumerator() {
124        return new IndexedEnumerator(data, row, columnIndices);
125      }
126    }
127    internal class IndexedEnumerator : IEnumerator<double> {
128      private readonly IEnumerator<int> column;
129      private readonly double[,] data;
130      private readonly int row;
131
132      public IndexedEnumerator(double[,] data, int row, int[] columnIndices) {
133        this.data = data;
134        this.row = row;
135        column = ((IEnumerable<int>)columnIndices).GetEnumerator();
136      }
137
138      public double Current {
139        get { return data[row, column.Current]; }
140      }
141
142      object IEnumerator.Current {
143        get {
144          return data[row, column.Current];
145        }
146      }
147
148      public void Dispose() { }
149
150      public bool MoveNext() {
151        return column.MoveNext();
152      }
153
154      public void Reset() {
155        column.Reset();
156      }
157    }
158  }
159}
Note: See TracBrowser for help on using the repository browser.