Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3116_GAM_Interactions/HeuristicLab.Algorithms.DataAnalysis/3.4/GAM/Spline1dModel.cs @ 17933

Last change on this file since 17933 was 17933, checked in by gkronber, 3 years ago

#3116: added GAM base-learners for 2d cubic splines and 3d functions (using alglib RBF model)

File size: 4.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 HEAL.Attic;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Problems.DataAnalysis;
28using System;
29
30namespace HeuristicLab.Algorithms.DataAnalysis {
31
32  [Item("Spline model (1d)",
33    "Univariate spline model (wrapper for alglib.spline1dmodel)")]
34  [StorableType("23D71839-E011-4DC5-B451-2D4C1177D743")]
35  public sealed class Spline1dModel : RegressionModel {
36    // not storable! see persistence properties below
37    private alglib.spline1d.spline1dinterpolant interpolant;
38
39    [Storable(OldName = "variablesUsedForPrediction")]
40    private string[] StorableVariablesUsedForPrediction {
41      set {
42        if (value.Length > 1) throw new ArgumentException("A one-dimensional spline model supports only one input variable.");
43        x1 = value[0];
44      }
45    }
46
47    [Storable]
48    private string x1;
49    public override IEnumerable<string> VariablesUsedForPrediction => new[] { x1 };
50
51    [StorableConstructor]
52    private Spline1dModel(StorableConstructorFlag deserializing) : base(deserializing) {
53      this.interpolant = new alglib.spline1d.spline1dinterpolant();
54    }
55
56    private Spline1dModel(Spline1dModel orig, Cloner cloner) : base(orig, cloner) {
57      this.x1 = orig.x1;
58      this.interpolant = (alglib.spline1d.spline1dinterpolant)orig.interpolant.make_copy();
59    }
60    public Spline1dModel(alglib.spline1d.spline1dinterpolant interpolant, string targetVar, string inputVar)
61      : base(targetVar, $"Spline model ({inputVar})") {
62      this.interpolant = (alglib.spline1d.spline1dinterpolant)interpolant.make_copy();
63      this.x1 = inputVar;
64    }
65
66
67    public override IDeepCloneable Clone(Cloner cloner) => new Spline1dModel(this, cloner);
68
69    public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
70      var solution = new RegressionSolution(this, (IRegressionProblemData)problemData.Clone());
71      solution.Name = $"Regression Spline ({x1})";
72
73      return solution;
74    }
75
76    public double GetEstimatedValue(double x) => alglib.spline1d.spline1dcalc(interpolant, x, null);
77
78    public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
79      return dataset.GetDoubleValues(x1, rows).Select(GetEstimatedValue);
80    }
81
82    #region persistence
83    [Storable]
84    private double[] Interpolant_c {
85      get { return interpolant.c; }
86      set { interpolant.c = value; }
87    }
88    [Storable]
89    private double[] Interpolant_x {
90      get { return interpolant.x; }
91      set { interpolant.x = value; }
92    }
93    [Storable]
94    private int Interpolant_continuity {
95      get { return interpolant.continuity; }
96      set { interpolant.continuity = value; }
97    }
98    [Storable]
99    private int Interpolant_k {
100      get { return interpolant.k; }
101      set { interpolant.k = value; }
102    }
103    [Storable]
104    private int Interpolant_n {
105      get { return interpolant.n; }
106      set { interpolant.n = value; }
107    }
108    [Storable]
109    private bool Interpolant_periodic {
110      get { return interpolant.periodic; }
111      set { interpolant.periodic = value; }
112    }
113    #endregion
114  }
115}
Note: See TracBrowser for help on using the repository browser.