Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3107_LearningALPS/HeuristicLab.Algorithms.DataAnalysis/3.4/GAM/Spline1dModel.cs @ 18242

Last change on this file since 18242 was 17839, checked in by mkommend, 4 years ago

#2898: Fixed base ctor call in Spline1dModel.

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