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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Algorithms.DataAnalysis.KernelRidgeRegression {
|
---|
31 | [StorableClass]
|
---|
32 | public abstract class KernelBase : ParameterizedNamedItem, IKernel {
|
---|
33 |
|
---|
34 | #region Parameternames
|
---|
35 | private const string DistanceParameterName = "Distance";
|
---|
36 | #endregion
|
---|
37 | #region Parameterproperties
|
---|
38 | public ValueParameter<IDistance> DistanceParameter {
|
---|
39 | get { return Parameters[DistanceParameterName] as ValueParameter<IDistance>; }
|
---|
40 | }
|
---|
41 |
|
---|
42 | [Storable]
|
---|
43 | public double? Beta { get; set; }
|
---|
44 | #endregion
|
---|
45 | #region Properties
|
---|
46 | public IDistance Distance {
|
---|
47 | get { return DistanceParameter.Value; }
|
---|
48 | set { DistanceParameter.Value = value; }
|
---|
49 | }
|
---|
50 |
|
---|
51 | #endregion
|
---|
52 |
|
---|
53 | [StorableConstructor]
|
---|
54 | protected KernelBase(bool deserializing) : base(deserializing) { }
|
---|
55 | [StorableHook(HookType.AfterDeserialization)]
|
---|
56 | private void AfterDeserialization() { }
|
---|
57 |
|
---|
58 | protected KernelBase(KernelBase original, Cloner cloner)
|
---|
59 | : base(original, cloner) {
|
---|
60 | Beta = original.Beta;
|
---|
61 | }
|
---|
62 |
|
---|
63 | protected KernelBase() {
|
---|
64 | Parameters.Add(new ValueParameter<IDistance>(DistanceParameterName, "The distance function used for kernel calculation"));
|
---|
65 | DistanceParameter.Value = new EuclideanDistance();
|
---|
66 | }
|
---|
67 |
|
---|
68 | public double Get(object a, object b) {
|
---|
69 | return Get(Distance.Get(a, b));
|
---|
70 | }
|
---|
71 |
|
---|
72 | protected abstract double Get(double norm);
|
---|
73 |
|
---|
74 | public int GetNumberOfParameters(int numberOfVariables) {
|
---|
75 | return Beta.HasValue ? 0 : 1;
|
---|
76 | }
|
---|
77 |
|
---|
78 | public void SetParameter(double[] p) {
|
---|
79 | if (p != null && p.Length == 1) Beta = new double?(p[0]);
|
---|
80 | }
|
---|
81 |
|
---|
82 | public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) {
|
---|
83 | if (p.Length != GetNumberOfParameters(columnIndices.Length)) throw new ArgumentException("Illegal parametrization");
|
---|
84 | var myClone = (KernelBase)Clone(new Cloner());
|
---|
85 | myClone.SetParameter(p);
|
---|
86 | var cov = new ParameterizedCovarianceFunction {
|
---|
87 | Covariance = (x, i, j) => myClone.Get(GetNorm(x, x, i, j, columnIndices)),
|
---|
88 | CrossCovariance = (x, xt, i, j) => myClone.Get(GetNorm(x, xt, i, j, columnIndices)),
|
---|
89 | CovarianceGradient = (x, i, j) => new List<double> { myClone.GetGradient(GetNorm(x, x, i, j, columnIndices)) }
|
---|
90 | };
|
---|
91 | return cov;
|
---|
92 | }
|
---|
93 |
|
---|
94 | protected abstract double GetGradient(double norm);
|
---|
95 |
|
---|
96 | protected double GetNorm(double[,] x, double[,] xt, int i, int j, int[] columnIndices) {
|
---|
97 | var dist = Distance as IDistance<IEnumerable<double>>;
|
---|
98 | if (dist == null) throw new ArgumentException("The distance needs to apply to double vectors");
|
---|
99 | var r1 = columnIndices.Select(c => x[i, c]);
|
---|
100 | var r2 = columnIndices.Select(c => xt[j, c]);
|
---|
101 | return dist.Get(r1, r2);
|
---|
102 | }
|
---|
103 | }
|
---|
104 | } |
---|