Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Algorithms.DataAnalysis/3.4/GAM/Spline1dModel.cs @ 17815

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

#2898: fix header

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;
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("Spline model (1d)", "Spline model (1d)") {
56      this.interpolant = (alglib.spline1d.spline1dinterpolant)interpolant.make_copy();
57      this.TargetVariable = targetVar;
58      this.variablesUsedForPrediction = new string[] { inputVar };
59    }
60
61
62    public override IDeepCloneable Clone(Cloner cloner) {
63      return new Spline1dModel(this, cloner);
64    }
65
66    public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
67      return new RegressionSolution(this, (IRegressionProblemData)problemData.Clone());
68    }
69
70    public double GetEstimatedValue(double x) {
71      return alglib.spline1d.spline1dcalc(interpolant, x);
72    }
73
74    public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
75      var x = dataset.GetDoubleValues(VariablesUsedForPrediction.First(), rows).ToArray();
76      foreach (var xi in x) {
77        yield return GetEstimatedValue(xi);
78      }
79    }
80
81    #region persistence
82    [Storable]
83    private double[] Interpolant_c {
84      get { return interpolant.c; }
85      set { interpolant.c = value; }
86    }
87    [Storable]
88    private double[] Interpolant_x {
89      get { return interpolant.x; }
90      set { interpolant.x = value; }
91    }
92    [Storable]
93    private int Interpolant_continuity {
94      get { return interpolant.continuity; }
95      set { interpolant.continuity = value; }
96    }
97    [Storable]
98    private int Interpolant_k {
99      get { return interpolant.k; }
100      set { interpolant.k = value; }
101    }
102    [Storable]
103    private int Interpolant_n {
104      get { return interpolant.n; }
105      set { interpolant.n = value; }
106    }
107    [Storable]
108    private bool Interpolant_periodic {
109      get { return interpolant.periodic; }
110      set { interpolant.periodic = value; }
111    }
112    #endregion
113  }
114}
Note: See TracBrowser for help on using the repository browser.