Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3116_GAM_Interactions/HeuristicLab.Algorithms.DataAnalysis/3.4/GAM/Spline2dModel.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: 3.2 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
31
32  [Item("Spline model (2d)",
33    "Bi-variate spline model (wrapper for alglib.spline2sdmodel)")]
34  [StorableType("15E4446C-0C15-4B49-8661-51125071CF81")]
35  public sealed class Spline2dModel : RegressionModel {
36    // not storable! see persistence properties below
37    private alglib.spline2dinterpolant interpolant;
38
39    [Storable]
40    private string x1;
41    [Storable]
42    private string x2;
43    public override IEnumerable<string> VariablesUsedForPrediction => new[] { x1, x2 };
44
45    [StorableConstructor]
46    private Spline2dModel(StorableConstructorFlag deserializing) : base(deserializing) {
47      this.interpolant = new alglib.spline2dinterpolant();
48    }
49
50    private Spline2dModel(Spline2dModel orig, Cloner cloner) : base(orig, cloner) {
51      this.x1 = orig.x1;
52      this.x2 = orig.x2;
53      this.interpolant = (alglib.spline2dinterpolant)orig.interpolant.make_copy();
54    }
55    public Spline2dModel(alglib.spline2dinterpolant interpolant, string y, string x1, string x2)
56      : base(y, $"Spline model ({x1}, {x2})") {
57      this.interpolant = (alglib.spline2dinterpolant)interpolant.make_copy();
58      this.x1 = x1;
59      this.x2 = x2;
60    }
61
62
63    public override IDeepCloneable Clone(Cloner cloner) => new Spline2dModel(this, cloner);
64
65    public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
66      var solution = new RegressionSolution(this, (IRegressionProblemData)problemData.Clone());
67      solution.Name = $"Regression Spline ({x1},{x2})";
68
69      return solution;
70    }
71
72
73    public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
74      var x1 = dataset.GetDoubleValues(this.x1, rows);
75      var x2 = dataset.GetDoubleValues(this.x2, rows);
76
77      return x1.Zip(x2, (x1i, x2i) => alglib.spline2dcalc(interpolant, x1i, x2i));
78    }
79
80    #region persistence
81    [Storable]
82    public string SerializedInterpolant {
83      get {
84        alglib.spline2dserialize(interpolant, out var serialized);
85        return serialized;
86      }
87      set {
88        alglib.spline2dunserialize(value, out var obj);
89        interpolant = obj;
90      }
91    }
92    #endregion
93  }
94}
Note: See TracBrowser for help on using the repository browser.