[15775] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17815] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
[15775] | 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 |
|
---|
[17812] | 22 | using HEAL.Attic;
|
---|
[15775] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Problems.DataAnalysis;
|
---|
[17889] | 28 | using System;
|
---|
[15775] | 29 |
|
---|
| 30 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
| 31 | [Item("Spline model (1d)",
|
---|
| 32 | "Univariate spline model (wrapper for alglib.spline1dmodel)")]
|
---|
[17812] | 33 | [StorableType("23D71839-E011-4DC5-B451-2D4C1177D743")]
|
---|
[15775] | 34 | public sealed class Spline1dModel : RegressionModel {
|
---|
| 35 | // not storable! see persistence properties below
|
---|
| 36 | private alglib.spline1d.spline1dinterpolant interpolant;
|
---|
| 37 |
|
---|
[17889] | 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 |
|
---|
[15775] | 46 | [Storable]
|
---|
[17889] | 47 | private string inputVariable;
|
---|
| 48 | public override IEnumerable<string> VariablesUsedForPrediction => new[] { inputVariable };
|
---|
[15775] | 49 |
|
---|
| 50 | [StorableConstructor]
|
---|
[17812] | 51 | private Spline1dModel(StorableConstructorFlag deserializing) : base(deserializing) {
|
---|
[15775] | 52 | this.interpolant = new alglib.spline1d.spline1dinterpolant();
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | private Spline1dModel(Spline1dModel orig, Cloner cloner) : base(orig, cloner) {
|
---|
[17889] | 56 | this.inputVariable = orig.inputVariable;
|
---|
[15775] | 57 | this.interpolant = (alglib.spline1d.spline1dinterpolant)orig.interpolant.make_copy();
|
---|
| 58 | }
|
---|
| 59 | public Spline1dModel(alglib.spline1d.spline1dinterpolant interpolant, string targetVar, string inputVar)
|
---|
[17889] | 60 | : base(targetVar, $"Spline model ({inputVar})") {
|
---|
[17867] | 61 | this.interpolant = (alglib.spline1d.spline1dinterpolant)interpolant.make_copy();
|
---|
[17889] | 62 | this.inputVariable = inputVar;
|
---|
[15775] | 63 | }
|
---|
| 64 |
|
---|
| 65 |
|
---|
[17867] | 66 | public override IDeepCloneable Clone(Cloner cloner) => new Spline1dModel(this, cloner);
|
---|
[15775] | 67 |
|
---|
| 68 | public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
|
---|
[17889] | 69 | var solution = new RegressionSolution(this, (IRegressionProblemData)problemData.Clone());
|
---|
| 70 | solution.Name = $"Regression Spline ({inputVariable})";
|
---|
| 71 |
|
---|
| 72 | return solution;
|
---|
[15775] | 73 | }
|
---|
| 74 |
|
---|
[17867] | 75 | public double GetEstimatedValue(double x) => alglib.spline1d.spline1dcalc(interpolant, x);
|
---|
[15775] | 76 |
|
---|
| 77 | public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
|
---|
[17889] | 78 | return dataset.GetDoubleValues(inputVariable, rows).Select(GetEstimatedValue);
|
---|
[15775] | 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 | }
|
---|