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.IO;
|
---|
25 | using System.Linq;
|
---|
26 | using System.Text;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 | using HeuristicLab.Problems.DataAnalysis;
|
---|
31 | using SVM;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
34 | /// <summary>
|
---|
35 | /// Represents a neural network ensembel model for regression and classification
|
---|
36 | /// </summary>
|
---|
37 | [StorableClass]
|
---|
38 | [Item("NeuralNetworkEnsembleModel", "Represents a neural network ensemble for regression and classification.")]
|
---|
39 | public sealed class NeuralNetworkEnsembleModel : NamedItem, INeuralNetworkEnsembleModel {
|
---|
40 |
|
---|
41 | private alglib.mlpensemble mlpEnsemble;
|
---|
42 | public alglib.mlpensemble MultiLayerPerceptronEnsemble {
|
---|
43 | get { return mlpEnsemble; }
|
---|
44 | set {
|
---|
45 | if (value != mlpEnsemble) {
|
---|
46 | if (value == null) throw new ArgumentNullException();
|
---|
47 | mlpEnsemble = value;
|
---|
48 | OnChanged(EventArgs.Empty);
|
---|
49 | }
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | [Storable]
|
---|
54 | private string targetVariable;
|
---|
55 | [Storable]
|
---|
56 | private string[] allowedInputVariables;
|
---|
57 | [Storable]
|
---|
58 | private double[] classValues;
|
---|
59 | [StorableConstructor]
|
---|
60 | private NeuralNetworkEnsembleModel(bool deserializing)
|
---|
61 | : base(deserializing) {
|
---|
62 | if (deserializing)
|
---|
63 | mlpEnsemble = new alglib.mlpensemble();
|
---|
64 | }
|
---|
65 | private NeuralNetworkEnsembleModel(NeuralNetworkEnsembleModel original, Cloner cloner)
|
---|
66 | : base(original, cloner) {
|
---|
67 | mlpEnsemble = new alglib.mlpensemble();
|
---|
68 | mlpEnsemble.innerobj.columnmeans = (double[])original.mlpEnsemble.innerobj.columnmeans.Clone();
|
---|
69 | mlpEnsemble.innerobj.columnsigmas = (double[])original.mlpEnsemble.innerobj.columnsigmas.Clone();
|
---|
70 | mlpEnsemble.innerobj.dfdnet = (double[])original.mlpEnsemble.innerobj.dfdnet.Clone();
|
---|
71 | mlpEnsemble.innerobj.ensemblesize = original.mlpEnsemble.innerobj.ensemblesize;
|
---|
72 | mlpEnsemble.innerobj.issoftmax = original.mlpEnsemble.innerobj.issoftmax;
|
---|
73 | mlpEnsemble.innerobj.neurons = (double[])original.mlpEnsemble.innerobj.neurons.Clone();
|
---|
74 | mlpEnsemble.innerobj.nin = original.mlpEnsemble.innerobj.nin;
|
---|
75 | mlpEnsemble.innerobj.nout = original.mlpEnsemble.innerobj.nout;
|
---|
76 | mlpEnsemble.innerobj.postprocessing = original.mlpEnsemble.innerobj.postprocessing;
|
---|
77 | mlpEnsemble.innerobj.serializedlen = original.mlpEnsemble.innerobj.serializedlen;
|
---|
78 | mlpEnsemble.innerobj.serializedmlp = (double[])original.mlpEnsemble.innerobj.serializedmlp.Clone();
|
---|
79 | mlpEnsemble.innerobj.structinfo = (int[])original.mlpEnsemble.innerobj.structinfo.Clone();
|
---|
80 | mlpEnsemble.innerobj.tmpmeans = (double[])original.mlpEnsemble.innerobj.tmpmeans.Clone();
|
---|
81 | mlpEnsemble.innerobj.tmpsigmas = (double[])original.mlpEnsemble.innerobj.tmpsigmas.Clone();
|
---|
82 | mlpEnsemble.innerobj.tmpweights = (double[])original.mlpEnsemble.innerobj.tmpweights.Clone();
|
---|
83 | mlpEnsemble.innerobj.wcount = original.mlpEnsemble.innerobj.wcount;
|
---|
84 | mlpEnsemble.innerobj.weights = (double[])original.mlpEnsemble.innerobj.weights.Clone();
|
---|
85 | mlpEnsemble.innerobj.y = (double[])original.mlpEnsemble.innerobj.y.Clone();
|
---|
86 | targetVariable = original.targetVariable;
|
---|
87 | allowedInputVariables = (string[])original.allowedInputVariables.Clone();
|
---|
88 | if (original.classValues != null)
|
---|
89 | this.classValues = (double[])original.classValues.Clone();
|
---|
90 | }
|
---|
91 | public NeuralNetworkEnsembleModel(alglib.mlpensemble mlpEnsemble, string targetVariable, IEnumerable<string> allowedInputVariables, double[] classValues = null)
|
---|
92 | : base() {
|
---|
93 | this.name = ItemName;
|
---|
94 | this.description = ItemDescription;
|
---|
95 | this.mlpEnsemble = mlpEnsemble;
|
---|
96 | this.targetVariable = targetVariable;
|
---|
97 | this.allowedInputVariables = allowedInputVariables.ToArray();
|
---|
98 | if (classValues != null)
|
---|
99 | this.classValues = (double[])classValues.Clone();
|
---|
100 | }
|
---|
101 |
|
---|
102 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
103 | return new NeuralNetworkEnsembleModel(this, cloner);
|
---|
104 | }
|
---|
105 |
|
---|
106 | public IEnumerable<double> GetEstimatedValues(Dataset dataset, IEnumerable<int> rows) {
|
---|
107 | double[,] inputData = AlglibUtil.PrepareInputMatrix(dataset, allowedInputVariables, rows);
|
---|
108 |
|
---|
109 | int n = inputData.GetLength(0);
|
---|
110 | int columns = inputData.GetLength(1);
|
---|
111 | double[] x = new double[columns];
|
---|
112 | double[] y = new double[1];
|
---|
113 |
|
---|
114 | for (int row = 0; row < n; row++) {
|
---|
115 | for (int column = 0; column < columns; column++) {
|
---|
116 | x[column] = inputData[row, column];
|
---|
117 | }
|
---|
118 | alglib.mlpeprocess(mlpEnsemble, x, ref y);
|
---|
119 | yield return y[0];
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | public IEnumerable<double> GetEstimatedClassValues(Dataset dataset, IEnumerable<int> rows) {
|
---|
124 | double[,] inputData = AlglibUtil.PrepareInputMatrix(dataset, allowedInputVariables, rows);
|
---|
125 |
|
---|
126 | int n = inputData.GetLength(0);
|
---|
127 | int columns = inputData.GetLength(1);
|
---|
128 | double[] x = new double[columns];
|
---|
129 | double[] y = new double[classValues.Length];
|
---|
130 |
|
---|
131 | for (int row = 0; row < n; row++) {
|
---|
132 | for (int column = 0; column < columns; column++) {
|
---|
133 | x[column] = inputData[row, column];
|
---|
134 | }
|
---|
135 | alglib.mlpeprocess(mlpEnsemble, x, ref y);
|
---|
136 | // find class for with the largest probability value
|
---|
137 | int maxProbClassIndex = 0;
|
---|
138 | double maxProb = y[0];
|
---|
139 | for (int i = 1; i < y.Length; i++) {
|
---|
140 | if (maxProb < y[i]) {
|
---|
141 | maxProb = y[i];
|
---|
142 | maxProbClassIndex = i;
|
---|
143 | }
|
---|
144 | }
|
---|
145 | yield return classValues[maxProbClassIndex];
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | #region events
|
---|
150 | public event EventHandler Changed;
|
---|
151 | private void OnChanged(EventArgs e) {
|
---|
152 | var handlers = Changed;
|
---|
153 | if (handlers != null)
|
---|
154 | handlers(this, e);
|
---|
155 | }
|
---|
156 | #endregion
|
---|
157 |
|
---|
158 | #region persistence
|
---|
159 | [Storable]
|
---|
160 | private double[] MultiLayerPerceptronEnsembleColumnMeans {
|
---|
161 | get {
|
---|
162 | return mlpEnsemble.innerobj.columnmeans;
|
---|
163 | }
|
---|
164 | set {
|
---|
165 | mlpEnsemble.innerobj.columnmeans = value;
|
---|
166 | }
|
---|
167 | }
|
---|
168 | [Storable]
|
---|
169 | private double[] MultiLayerPerceptronEnsembleColumnSigmas {
|
---|
170 | get {
|
---|
171 | return mlpEnsemble.innerobj.columnsigmas;
|
---|
172 | }
|
---|
173 | set {
|
---|
174 | mlpEnsemble.innerobj.columnsigmas = value;
|
---|
175 | }
|
---|
176 | }
|
---|
177 | [Storable]
|
---|
178 | private double[] MultiLayerPerceptronEnsembleDfdnet {
|
---|
179 | get {
|
---|
180 | return mlpEnsemble.innerobj.dfdnet;
|
---|
181 | }
|
---|
182 | set {
|
---|
183 | mlpEnsemble.innerobj.dfdnet = value;
|
---|
184 | }
|
---|
185 | }
|
---|
186 | [Storable]
|
---|
187 | private int MultiLayerPerceptronEnsembleSize {
|
---|
188 | get {
|
---|
189 | return mlpEnsemble.innerobj.ensemblesize;
|
---|
190 | }
|
---|
191 | set {
|
---|
192 | mlpEnsemble.innerobj.ensemblesize = value;
|
---|
193 | }
|
---|
194 | }
|
---|
195 | [Storable]
|
---|
196 | private bool MultiLayerPerceptronEnsembleIsSoftMax {
|
---|
197 | get {
|
---|
198 | return mlpEnsemble.innerobj.issoftmax;
|
---|
199 | }
|
---|
200 | set {
|
---|
201 | mlpEnsemble.innerobj.issoftmax = value;
|
---|
202 | }
|
---|
203 | }
|
---|
204 | [Storable]
|
---|
205 | private double[] MultiLayerPerceptronEnsembleNeurons {
|
---|
206 | get {
|
---|
207 | return mlpEnsemble.innerobj.neurons;
|
---|
208 | }
|
---|
209 | set {
|
---|
210 | mlpEnsemble.innerobj.neurons = value;
|
---|
211 | }
|
---|
212 | }
|
---|
213 | [Storable]
|
---|
214 | private int MultiLayerPerceptronEnsembleNin {
|
---|
215 | get {
|
---|
216 | return mlpEnsemble.innerobj.nin;
|
---|
217 | }
|
---|
218 | set {
|
---|
219 | mlpEnsemble.innerobj.nin = value;
|
---|
220 | }
|
---|
221 | }
|
---|
222 | [Storable]
|
---|
223 | private int MultiLayerPerceptronEnsembleNout {
|
---|
224 | get {
|
---|
225 | return mlpEnsemble.innerobj.nout;
|
---|
226 | }
|
---|
227 | set {
|
---|
228 | mlpEnsemble.innerobj.nout = value;
|
---|
229 | }
|
---|
230 | }
|
---|
231 | [Storable]
|
---|
232 | private bool MultiLayerPerceptronEnsemblePostprocessing {
|
---|
233 | get {
|
---|
234 | return mlpEnsemble.innerobj.postprocessing;
|
---|
235 | }
|
---|
236 | set {
|
---|
237 | mlpEnsemble.innerobj.postprocessing = value;
|
---|
238 | }
|
---|
239 | }
|
---|
240 | [Storable]
|
---|
241 | private int MultiLayerPerceptronEnsembleSerializedLen {
|
---|
242 | get {
|
---|
243 | return mlpEnsemble.innerobj.serializedlen;
|
---|
244 | }
|
---|
245 | set {
|
---|
246 | mlpEnsemble.innerobj.serializedlen = value;
|
---|
247 | }
|
---|
248 | }
|
---|
249 | [Storable]
|
---|
250 | private double[] MultiLayerPerceptronEnsembleSerializedMlp {
|
---|
251 | get {
|
---|
252 | return mlpEnsemble.innerobj.serializedmlp;
|
---|
253 | }
|
---|
254 | set {
|
---|
255 | mlpEnsemble.innerobj.serializedmlp = value;
|
---|
256 | }
|
---|
257 | }
|
---|
258 | [Storable]
|
---|
259 | private int[] MultiLayerPerceptronStuctinfo {
|
---|
260 | get {
|
---|
261 | return mlpEnsemble.innerobj.structinfo;
|
---|
262 | }
|
---|
263 | set {
|
---|
264 | mlpEnsemble.innerobj.structinfo = value;
|
---|
265 | }
|
---|
266 | }
|
---|
267 | [Storable]
|
---|
268 | private double[] MultiLayerPerceptronEnsembleTmpMeans {
|
---|
269 | get {
|
---|
270 | return mlpEnsemble.innerobj.tmpmeans;
|
---|
271 | }
|
---|
272 | set {
|
---|
273 | mlpEnsemble.innerobj.tmpmeans = value;
|
---|
274 | }
|
---|
275 | }
|
---|
276 | [Storable]
|
---|
277 | private double[] MultiLayerPerceptronEnsembleTmpSigmas {
|
---|
278 | get {
|
---|
279 | return mlpEnsemble.innerobj.tmpsigmas;
|
---|
280 | }
|
---|
281 | set {
|
---|
282 | mlpEnsemble.innerobj.tmpsigmas = value;
|
---|
283 | }
|
---|
284 | }
|
---|
285 | [Storable]
|
---|
286 | private double[] MultiLayerPerceptronEnsembleTmpWeights {
|
---|
287 | get {
|
---|
288 | return mlpEnsemble.innerobj.tmpweights;
|
---|
289 | }
|
---|
290 | set {
|
---|
291 | mlpEnsemble.innerobj.tmpweights = value;
|
---|
292 | }
|
---|
293 | }
|
---|
294 | [Storable]
|
---|
295 | private int MultiLayerPerceptronEnsembleWCount {
|
---|
296 | get {
|
---|
297 | return mlpEnsemble.innerobj.wcount;
|
---|
298 | }
|
---|
299 | set {
|
---|
300 | mlpEnsemble.innerobj.wcount = value;
|
---|
301 | }
|
---|
302 | }
|
---|
303 |
|
---|
304 | [Storable]
|
---|
305 | private double[] MultiLayerPerceptronWeights {
|
---|
306 | get {
|
---|
307 | return mlpEnsemble.innerobj.weights;
|
---|
308 | }
|
---|
309 | set {
|
---|
310 | mlpEnsemble.innerobj.weights = value;
|
---|
311 | }
|
---|
312 | }
|
---|
313 | [Storable]
|
---|
314 | private double[] MultiLayerPerceptronY {
|
---|
315 | get {
|
---|
316 | return mlpEnsemble.innerobj.y;
|
---|
317 | }
|
---|
318 | set {
|
---|
319 | mlpEnsemble.innerobj.y = value;
|
---|
320 | }
|
---|
321 | }
|
---|
322 | #endregion
|
---|
323 | }
|
---|
324 | }
|
---|