1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2008 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.Collections.Generic;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Data;
|
---|
25 | using HeuristicLab.Modeling;
|
---|
26 | using System;
|
---|
27 | using System.Xml;
|
---|
28 | using HeuristicLab.DataAnalysis;
|
---|
29 | using System.Linq;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.ArtificialNeuralNetworks {
|
---|
32 | public class Predictor : PredictorBase {
|
---|
33 | private MultiLayerPerceptron perceptron;
|
---|
34 | public Predictor() : base() { } // for persistence
|
---|
35 | public Predictor(MultiLayerPerceptron perceptron, double lowerPredictionLimit, double upperPredictionLimit)
|
---|
36 | : base(lowerPredictionLimit, upperPredictionLimit) {
|
---|
37 | this.perceptron = perceptron;
|
---|
38 | }
|
---|
39 |
|
---|
40 | public override IEnumerable<double> Predict(Dataset input, int start, int end) {
|
---|
41 |
|
---|
42 | if (start < 0 || end <= start) throw new ArgumentException("start must be larger than zero and strictly smaller than end");
|
---|
43 | if (end > input.Rows) throw new ArgumentOutOfRangeException("number of rows in input is smaller then end");
|
---|
44 |
|
---|
45 | for (int i = 0; i < end - start; i++) {
|
---|
46 | double[] output = new double[1];
|
---|
47 | double[] inputRow = new double[GetInputVariables().Count()];
|
---|
48 | int c = 0;
|
---|
49 | foreach (var inputVariable in GetInputVariables()) {
|
---|
50 | int inputVariableIndex = input.GetVariableIndex(inputVariable);
|
---|
51 | inputRow[c++] = input.GetValue(i + start, inputVariableIndex);
|
---|
52 | }
|
---|
53 | double estimatedValue;
|
---|
54 | try {
|
---|
55 | alglib.mlpbase.multilayerperceptron p = perceptron.Perceptron;
|
---|
56 | alglib.mlpbase.mlpprocess(ref p, ref inputRow, ref output);
|
---|
57 | perceptron.Perceptron = p;
|
---|
58 | estimatedValue = output[0];
|
---|
59 | }
|
---|
60 | catch (ArithmeticException) {
|
---|
61 | estimatedValue = UpperPredictionLimit;
|
---|
62 | }
|
---|
63 | yield return Math.Max(Math.Min(estimatedValue, UpperPredictionLimit), LowerPredictionLimit);
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | public override IEnumerable<string> GetInputVariables() {
|
---|
68 | return perceptron.InputVariables;
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
73 | Predictor clone = (Predictor)base.Clone(clonedObjects);
|
---|
74 | clone.perceptron = (MultiLayerPerceptron)Auxiliary.Clone(perceptron, clonedObjects);
|
---|
75 | return clone;
|
---|
76 | }
|
---|
77 |
|
---|
78 | public override System.Xml.XmlNode GetXmlNode(string name, System.Xml.XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
79 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
80 | node.AppendChild(PersistenceManager.Persist("Perceptron", perceptron, document, persistedObjects));
|
---|
81 | return node;
|
---|
82 | }
|
---|
83 |
|
---|
84 | public override void Populate(System.Xml.XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
85 | base.Populate(node, restoredObjects);
|
---|
86 | perceptron = (MultiLayerPerceptron)PersistenceManager.Restore(node.SelectSingleNode("Perceptron"), restoredObjects);
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|