1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 | using HeuristicLab.Problems.DataAnalysis;
|
---|
32 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
33 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
|
---|
34 | using HeuristicLab.Parameters;
|
---|
35 |
|
---|
36 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
37 | /// <summary>
|
---|
38 | /// Neural network regression data analysis algorithm.
|
---|
39 | /// </summary>
|
---|
40 | [Item("Neural Network Regression", "Neural network regression data analysis algorithm (wrapper for ALGLIB).")]
|
---|
41 | [Creatable("Data Analysis")]
|
---|
42 | [StorableClass]
|
---|
43 | public sealed class NeuralNetworkRegression : FixedDataAnalysisAlgorithm<IRegressionProblem> {
|
---|
44 | private const string NeuralNetworkRegressionModelResultName = "Neural network regression solution";
|
---|
45 | [StorableConstructor]
|
---|
46 | private NeuralNetworkRegression(bool deserializing) : base(deserializing) { }
|
---|
47 | private NeuralNetworkRegression(NeuralNetworkRegression original, Cloner cloner)
|
---|
48 | : base(original, cloner) {
|
---|
49 | }
|
---|
50 | public NeuralNetworkRegression()
|
---|
51 | : base() {
|
---|
52 | Problem = new RegressionProblem();
|
---|
53 | }
|
---|
54 | [StorableHook(HookType.AfterDeserialization)]
|
---|
55 | private void AfterDeserialization() { }
|
---|
56 |
|
---|
57 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
58 | return new NeuralNetworkRegression(this, cloner);
|
---|
59 | }
|
---|
60 |
|
---|
61 | #region neural network
|
---|
62 | protected override void Run() {
|
---|
63 | double decay = 0.01;
|
---|
64 | int nLayers = 2;
|
---|
65 | int nHidden1 = 10;
|
---|
66 | int nHidden2 = 10;
|
---|
67 | int nRestarts = 5;
|
---|
68 | double rmsError, avgRelError;
|
---|
69 | var solution = CreateNeuralNetworkRegressionSolution(Problem.ProblemData, nLayers, nHidden1, nHidden2, decay, nRestarts, out rmsError, out avgRelError);
|
---|
70 | Results.Add(new Result(NeuralNetworkRegressionModelResultName, "The neural network regression solution.", solution));
|
---|
71 | Results.Add(new Result("Root mean square error", "The root of the mean of squared errors of the neural network regression solution on the training set.", new DoubleValue(rmsError)));
|
---|
72 | Results.Add(new Result("Average relative error", "The average of relative errors of the neural network regression solution on the training set.", new PercentValue(avgRelError)));
|
---|
73 | }
|
---|
74 |
|
---|
75 | public static IRegressionSolution CreateNeuralNetworkRegressionSolution(IRegressionProblemData problemData, int nLayers, int nHiddenNodes1, int nHiddenNodes2, double decay, int restarts,
|
---|
76 | out double rmsError, out double avgRelError) {
|
---|
77 | Dataset dataset = problemData.Dataset;
|
---|
78 | string targetVariable = problemData.TargetVariable;
|
---|
79 | IEnumerable<string> allowedInputVariables = problemData.AllowedInputVariables;
|
---|
80 | IEnumerable<int> rows = problemData.TrainingIndizes;
|
---|
81 | double[,] inputMatrix = AlglibUtil.PrepareInputMatrix(dataset, allowedInputVariables.Concat(new string[] { targetVariable }), rows);
|
---|
82 | if (inputMatrix.Cast<double>().Any(x => double.IsNaN(x) || double.IsInfinity(x)))
|
---|
83 | throw new NotSupportedException("Neural network regression does not support NaN or infinity values in the input dataset.");
|
---|
84 |
|
---|
85 | double targetMin = problemData.Dataset.GetEnumeratedVariableValues(targetVariable).Min();
|
---|
86 | targetMin = targetMin - targetMin * 0.1; // -10%
|
---|
87 | double targetMax = problemData.Dataset.GetEnumeratedVariableValues(targetVariable).Max();
|
---|
88 | targetMax = targetMax + targetMax * 0.1; // + 10%
|
---|
89 |
|
---|
90 | alglib.multilayerperceptron multiLayerPerceptron = null;
|
---|
91 | if (nLayers == 0) {
|
---|
92 | alglib.mlpcreater0(allowedInputVariables.Count(), 1, targetMin, targetMax, out multiLayerPerceptron);
|
---|
93 | } else if (nLayers == 1) {
|
---|
94 | alglib.mlpcreater1(allowedInputVariables.Count(), nHiddenNodes1, 1, targetMin, targetMax, out multiLayerPerceptron);
|
---|
95 | } else if (nLayers == 2) {
|
---|
96 | alglib.mlpcreater2(allowedInputVariables.Count(), nHiddenNodes1, nHiddenNodes2, 1, targetMin, targetMax, out multiLayerPerceptron);
|
---|
97 | } else throw new ArgumentException("Number of layers must be zero, one, or two.", "nLayers");
|
---|
98 | alglib.mlpreport rep;
|
---|
99 | int nRows = inputMatrix.GetLength(0);
|
---|
100 |
|
---|
101 | int info;
|
---|
102 | // using mlptrainlm instead of mlptraines or mlptrainbfgs because only one parameter is necessary
|
---|
103 | alglib.mlptrainlm(multiLayerPerceptron, inputMatrix, nRows, decay, restarts, out info, out rep);
|
---|
104 | if (info != 2) throw new ArgumentException("Error in calculation of neural network regression solution");
|
---|
105 |
|
---|
106 | rmsError = alglib.mlprmserror(multiLayerPerceptron, inputMatrix, nRows);
|
---|
107 | avgRelError = alglib.mlpavgerror(multiLayerPerceptron, inputMatrix, nRows);
|
---|
108 |
|
---|
109 | return new NeuralNetworkRegressionSolution(problemData, new NeuralNetworkModel(multiLayerPerceptron, targetVariable, allowedInputVariables));
|
---|
110 | }
|
---|
111 | #endregion
|
---|
112 | }
|
---|
113 | }
|
---|