Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ArtificialNeuralNetworks/3.2/Predictor.cs @ 2562

Last change on this file since 2562 was 2562, checked in by gkronber, 14 years ago

Added project for ANN. #751 (Plugin for for data-modeling with ANN (integrated into CEDMA))

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