Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17867 was 17867, checked in by gkronber, 19 months ago

#2898: simplified code in Spline1dModel

File size: 3.8 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 => variablesUsedForPrediction;
40
41    [StorableConstructor]
42    private Spline1dModel(StorableConstructorFlag deserializing) : base(deserializing) {
43      this.interpolant = new alglib.spline1d.spline1dinterpolant();
44    }
45
46    private Spline1dModel(Spline1dModel orig, Cloner cloner) : base(orig, cloner) {
47      this.variablesUsedForPrediction = orig.VariablesUsedForPrediction.ToArray();
48      this.interpolant = (alglib.spline1d.spline1dinterpolant)orig.interpolant.make_copy();
49    }
50    public Spline1dModel(alglib.spline1d.spline1dinterpolant interpolant, string targetVar, string inputVar)
51      : base(targetVar, "Spline model (1d)") {
52      this.interpolant = (alglib.spline1d.spline1dinterpolant)interpolant.make_copy();
53      this.variablesUsedForPrediction = new string[] { inputVar };
54    }
55
56
57    public override IDeepCloneable Clone(Cloner cloner) => new Spline1dModel(this, cloner);
58
59    public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
60      return new RegressionSolution(this, (IRegressionProblemData)problemData.Clone());
61    }
62
63    public double GetEstimatedValue(double x) => alglib.spline1d.spline1dcalc(interpolant, x);
64
65    public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
66      return dataset.GetDoubleValues(VariablesUsedForPrediction.First(), rows).Select(GetEstimatedValue);
67    }
68
69    #region persistence
70    [Storable]
71    private double[] Interpolant_c {
72      get { return interpolant.c; }
73      set { interpolant.c = value; }
74    }
75    [Storable]
76    private double[] Interpolant_x {
77      get { return interpolant.x; }
78      set { interpolant.x = value; }
79    }
80    [Storable]
81    private int Interpolant_continuity {
82      get { return interpolant.continuity; }
83      set { interpolant.continuity = value; }
84    }
85    [Storable]
86    private int Interpolant_k {
87      get { return interpolant.k; }
88      set { interpolant.k = value; }
89    }
90    [Storable]
91    private int Interpolant_n {
92      get { return interpolant.n; }
93      set { interpolant.n = value; }
94    }
95    [Storable]
96    private bool Interpolant_periodic {
97      get { return interpolant.periodic; }
98      set { interpolant.periodic = value; }
99    }
100    #endregion
101  }
102}
Note: See TracBrowser for help on using the repository browser.