1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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 System;
|
---|
23 | using System.Collections.Concurrent;
|
---|
24 | using System.Collections.Generic;
|
---|
25 | using System.Linq;
|
---|
26 | using System.Threading;
|
---|
27 | using System.Threading.Tasks;
|
---|
28 | using HeuristicLab.Common;
|
---|
29 | using HeuristicLab.Core;
|
---|
30 | using HeuristicLab.Data;
|
---|
31 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
32 | using HeuristicLab.Optimization;
|
---|
33 | using HeuristicLab.Parameters;
|
---|
34 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
35 | using HeuristicLab.Problems.DataAnalysis;
|
---|
36 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
37 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
|
---|
38 |
|
---|
39 | namespace HeuristicLab.Algorithms.DataAnalysis.Experimental {
|
---|
40 | [Item("Splines", "")]
|
---|
41 | [Creatable(CreatableAttribute.Categories.DataAnalysisRegression, Priority = 102)]
|
---|
42 | [StorableClass]
|
---|
43 | public sealed class Splines : FixedDataAnalysisAlgorithm<IRegressionProblem> {
|
---|
44 | [StorableConstructor]
|
---|
45 | private Splines(bool deserializing) : base(deserializing) { }
|
---|
46 | [StorableHook(HookType.AfterDeserialization)]
|
---|
47 | private void AfterDeserialization() {
|
---|
48 | }
|
---|
49 |
|
---|
50 | private Splines(Splines original, Cloner cloner)
|
---|
51 | : base(original, cloner) {
|
---|
52 | }
|
---|
53 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
54 | return new Splines(this, cloner);
|
---|
55 | }
|
---|
56 |
|
---|
57 | public Splines()
|
---|
58 | : base() {
|
---|
59 | var validTypes = new ItemSet<StringValue>(
|
---|
60 | new[] {
|
---|
61 | "Monotone", "Akima", "Catmull-Rom", "Cubic", "Linear"
|
---|
62 | }.Select(s => new StringValue(s)));
|
---|
63 |
|
---|
64 | Parameters.Add(new ConstrainedValueParameter<StringValue>("Type", "The type of spline (as supported by alglib)", validTypes, validTypes.First()));
|
---|
65 | Problem = new RegressionProblem();
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 | protected override void Run(CancellationToken cancellationToken) {
|
---|
70 | double[,] inputMatrix = Problem.ProblemData.Dataset.ToArray(Problem.ProblemData.AllowedInputVariables.Concat(new string[] { Problem.ProblemData.TargetVariable }), Problem.ProblemData.TrainingIndices);
|
---|
71 | if (inputMatrix.Cast<double>().Any(x => double.IsNaN(x) || double.IsInfinity(x)))
|
---|
72 | throw new NotSupportedException("Splines does not support NaN or infinity values in the input dataset.");
|
---|
73 |
|
---|
74 | var inputVars = Problem.ProblemData.AllowedInputVariables.ToArray();
|
---|
75 | if (inputVars.Length > 3) throw new ArgumentException();
|
---|
76 |
|
---|
77 | var y = Problem.ProblemData.TargetVariableTrainingValues.ToArray();
|
---|
78 | if (inputVars.Length == 1) {
|
---|
79 |
|
---|
80 | var x = Problem.ProblemData.Dataset.GetDoubleValues(inputVars.First(), Problem.ProblemData.TrainingIndices).ToArray();
|
---|
81 | alglib.spline1dinterpolant c;
|
---|
82 | var type = ((StringValue)Parameters["Type"].ActualValue).Value;
|
---|
83 | switch (type) {
|
---|
84 | case "Monotone":
|
---|
85 | alglib.spline1dbuildmonotone(x, y, out c); break;
|
---|
86 | case "Akima":
|
---|
87 | alglib.spline1dbuildakima(x, y, out c); break;
|
---|
88 | case "Catmull-Rom":
|
---|
89 | alglib.spline1dbuildcatmullrom(x, y, out c); break;
|
---|
90 | case "Cubic":
|
---|
91 | alglib.spline1dbuildcubic(x, y, out c); break;
|
---|
92 | case "Linear":
|
---|
93 | alglib.spline1dbuildlinear(x, y, out c); break;
|
---|
94 |
|
---|
95 | default: throw new NotSupportedException();
|
---|
96 | }
|
---|
97 |
|
---|
98 | Results.Add(new Result("Solution", new RegressionSolution(new Spline1dModel(c, Problem.ProblemData.TargetVariable, inputVars),
|
---|
99 | (IRegressionProblemData)Problem.ProblemData.Clone())));
|
---|
100 | }
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | // UNFINISHED
|
---|
106 | public class Spline1dModel : NamedItem, IRegressionModel {
|
---|
107 | private alglib.spline1dinterpolant interpolant;
|
---|
108 |
|
---|
109 | public string TargetVariable { get; set; }
|
---|
110 |
|
---|
111 | public IEnumerable<string> VariablesUsedForPrediction { get; private set; }
|
---|
112 |
|
---|
113 | public event EventHandler TargetVariableChanged;
|
---|
114 |
|
---|
115 | public Spline1dModel(Spline1dModel orig, Cloner cloner) : base(orig, cloner) {
|
---|
116 | this.TargetVariable = orig.TargetVariable;
|
---|
117 | this.VariablesUsedForPrediction = orig.VariablesUsedForPrediction.ToArray();
|
---|
118 | this.interpolant = (alglib.spline1dinterpolant)orig.interpolant.make_copy();
|
---|
119 | }
|
---|
120 | public Spline1dModel(alglib.spline1dinterpolant interpolant, string targetVar, string[] inputs) : base("SplineModel", "SplineModel") {
|
---|
121 | this.interpolant = interpolant;
|
---|
122 | this.TargetVariable = targetVar;
|
---|
123 | this.VariablesUsedForPrediction = inputs;
|
---|
124 | }
|
---|
125 |
|
---|
126 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
127 | return new Spline1dModel(this, cloner);
|
---|
128 | }
|
---|
129 |
|
---|
130 | public IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
|
---|
131 | return new RegressionSolution(this, (IRegressionProblemData)problemData.Clone());
|
---|
132 | }
|
---|
133 |
|
---|
134 | public IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
|
---|
135 | foreach (var x in dataset.GetDoubleValues(VariablesUsedForPrediction.First(), rows)) {
|
---|
136 | yield return alglib.spline1dcalc(interpolant, x);
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|
140 | }
|
---|