Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2619 was 2619, checked in by gkronber, 15 years ago

Implemented #834 (IPredictor.Predict() should return an IEnumerable<double> instead of an double[]).

File size: 3.3 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 IEnumerable<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
44      for (int i = 0; i < end - start; i++) {
45        double[] output = new double[1];
46        double[] inputRow = new double[input.Columns - 1];
47        for (int c = 1; c < inputRow.Length; c++) {
48          inputRow[c - 1] = input.GetValue(i + start, c);
49        }
50        alglib.mlpbase.multilayerperceptron p = perceptron.Perceptron;
51        alglib.mlpbase.mlpprocess(ref p, ref inputRow, ref output);
52        perceptron.Perceptron = p;
53        yield return Math.Max(Math.Min(output[0], UpperPredictionLimit), LowerPredictionLimit);
54      }
55    }
56
57    public override IEnumerable<string> GetInputVariables() {
58      return perceptron.InputVariables;
59    }
60
61
62    public override object Clone(IDictionary<Guid, object> clonedObjects) {
63      Predictor clone = (Predictor)base.Clone(clonedObjects);
64      clone.perceptron = (MultiLayerPerceptron)Auxiliary.Clone(perceptron, clonedObjects);
65      return clone;
66    }
67
68    public override System.Xml.XmlNode GetXmlNode(string name, System.Xml.XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
69      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
70      node.AppendChild(PersistenceManager.Persist("Perceptron", perceptron, document, persistedObjects));
71      return node;
72    }
73
74    public override void Populate(System.Xml.XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
75      base.Populate(node, restoredObjects);
76      perceptron = (MultiLayerPerceptron)PersistenceManager.Restore(node.SelectSingleNode("Perceptron"), restoredObjects);
77    }
78  }
79}
Note: See TracBrowser for help on using the repository browser.