Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2898: copied implementation from branch to trunk.

File size: 4.1 KB
RevLine 
[15775]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 * and the BEACON Center for the Study of Evolution in Action.
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21#endregion
22
23using System;
[17812]24using HEAL.Attic;
[15775]25using System.Collections.Generic;
26using System.Linq;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Problems.DataAnalysis;
31
32namespace HeuristicLab.Algorithms.DataAnalysis {
33  [Item("Spline model (1d)",
34    "Univariate spline model (wrapper for alglib.spline1dmodel)")]
[17812]35  [StorableType("23D71839-E011-4DC5-B451-2D4C1177D743")]
[15775]36  public sealed class Spline1dModel : RegressionModel {
37    // not storable! see persistence properties below
38    private alglib.spline1d.spline1dinterpolant interpolant;
39
40    [Storable]
[17812]41    private readonly string[] variablesUsedForPrediction;
[15775]42    public override IEnumerable<string> VariablesUsedForPrediction {
43      get {
44        return variablesUsedForPrediction;
45      }
46    }
47
48    [StorableConstructor]
[17812]49    private Spline1dModel(StorableConstructorFlag deserializing) : base(deserializing) {
[15775]50      this.interpolant = new alglib.spline1d.spline1dinterpolant();
51    }
52
53    private Spline1dModel(Spline1dModel orig, Cloner cloner) : base(orig, cloner) {
54      this.variablesUsedForPrediction = orig.VariablesUsedForPrediction.ToArray();
55      this.interpolant = (alglib.spline1d.spline1dinterpolant)orig.interpolant.make_copy();
56    }
57    public Spline1dModel(alglib.spline1d.spline1dinterpolant interpolant, string targetVar, string inputVar)
58      : base("Spline model (1d)", "Spline model (1d)") {
59      this.interpolant = (alglib.spline1d.spline1dinterpolant)interpolant.make_copy();
60      this.TargetVariable = targetVar;
61      this.variablesUsedForPrediction = new string[] { inputVar };
62    }
63
64
65    public override IDeepCloneable Clone(Cloner cloner) {
66      return new Spline1dModel(this, cloner);
67    }
68
69    public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
70      return new RegressionSolution(this, (IRegressionProblemData)problemData.Clone());
71    }
72
73    public double GetEstimatedValue(double x) {
74      return alglib.spline1d.spline1dcalc(interpolant, x);
75    }
76
77    public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
78      var x = dataset.GetDoubleValues(VariablesUsedForPrediction.First(), rows).ToArray();
79      foreach (var xi in x) {
80        yield return GetEstimatedValue(xi);
81      }
82    }
83
84    #region persistence
85    [Storable]
86    private double[] Interpolant_c {
87      get { return interpolant.c; }
88      set { interpolant.c = value; }
89    }
90    [Storable]
91    private double[] Interpolant_x {
92      get { return interpolant.x; }
93      set { interpolant.x = value; }
94    }
95    [Storable]
96    private int Interpolant_continuity {
97      get { return interpolant.continuity; }
98      set { interpolant.continuity = value; }
99    }
100    [Storable]
101    private int Interpolant_k {
102      get { return interpolant.k; }
103      set { interpolant.k = value; }
104    }
105    [Storable]
106    private int Interpolant_n {
107      get { return interpolant.n; }
108      set { interpolant.n = value; }
109    }
110    [Storable]
111    private bool Interpolant_periodic {
112      get { return interpolant.periodic; }
113      set { interpolant.periodic = value; }
114    }
115    #endregion
116  }
117}
Note: See TracBrowser for help on using the repository browser.