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 |
|
---|
22 | using HEAL.Attic;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Problems.DataAnalysis;
|
---|
28 | using System;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
31 | [Item("Spline model (1d)",
|
---|
32 | "Univariate spline model (wrapper for alglib.spline1dmodel)")]
|
---|
33 | [StorableType("23D71839-E011-4DC5-B451-2D4C1177D743")]
|
---|
34 | public sealed class Spline1dModel : RegressionModel {
|
---|
35 | // not storable! see persistence properties below
|
---|
36 | private alglib.spline1d.spline1dinterpolant interpolant;
|
---|
37 |
|
---|
38 | [Storable(OldName = "variablesUsedForPrediction")]
|
---|
39 | private string[] StorableVariablesUsedForPrediction {
|
---|
40 | set {
|
---|
41 | if (value.Length > 1) throw new ArgumentException("A one-dimensional spline model supports only one input variable.");
|
---|
42 | inputVariable = value[0];
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | [Storable]
|
---|
47 | private string inputVariable;
|
---|
48 | public override IEnumerable<string> VariablesUsedForPrediction => new[] { inputVariable };
|
---|
49 |
|
---|
50 | [StorableConstructor]
|
---|
51 | private Spline1dModel(StorableConstructorFlag deserializing) : base(deserializing) {
|
---|
52 | this.interpolant = new alglib.spline1d.spline1dinterpolant();
|
---|
53 | }
|
---|
54 |
|
---|
55 | private Spline1dModel(Spline1dModel orig, Cloner cloner) : base(orig, cloner) {
|
---|
56 | this.inputVariable = orig.inputVariable;
|
---|
57 | if(orig.interpolant != null) this.interpolant = (alglib.spline1d.spline1dinterpolant)orig.interpolant.make_copy();
|
---|
58 | }
|
---|
59 | public Spline1dModel(alglib.spline1d.spline1dinterpolant interpolant, string targetVar, string inputVar)
|
---|
60 | : base(targetVar, $"Spline model ({inputVar})") {
|
---|
61 | this.interpolant = (alglib.spline1d.spline1dinterpolant)interpolant.make_copy();
|
---|
62 | this.inputVariable = inputVar;
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 | public override IDeepCloneable Clone(Cloner cloner) => new Spline1dModel(this, cloner);
|
---|
67 |
|
---|
68 | public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
|
---|
69 | var solution = new RegressionSolution(this, (IRegressionProblemData)problemData.Clone());
|
---|
70 | solution.Name = $"Regression Spline ({inputVariable})";
|
---|
71 |
|
---|
72 | return solution;
|
---|
73 | }
|
---|
74 |
|
---|
75 | public double GetEstimatedValue(double x) => alglib.spline1d.spline1dcalc(interpolant, x, null);
|
---|
76 |
|
---|
77 | public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
|
---|
78 | return dataset.GetDoubleValues(inputVariable, rows).Select(GetEstimatedValue);
|
---|
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 | }
|
---|