Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ArtificialNeuralNetworks/3.2/MultiLayerPerceptron.cs @ 3293

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

Fixed bugs in MLP operators, extended operators to work for time series prognosis and added pre-configured engine for time series prognosis with MLP. #882 (Artificial neural networks engine for time series prognosis)

File size: 4.6 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
21using System;
22using System.Collections.Generic;
23using System.Text;
24using HeuristicLab.PluginInfrastructure;
25using HeuristicLab.Core;
26using System.Xml;
27using System.Linq;
28using System.Globalization;
29
30namespace HeuristicLab.ArtificialNeuralNetworks {
31
32  public class MultiLayerPerceptron : ItemBase {
33    private alglib.mlpbase.multilayerperceptron perceptron;
34    public alglib.mlpbase.multilayerperceptron Perceptron {
35      get { return perceptron; }
36      internal set { perceptron = value; }
37    }
38
39    private List<string> inputVariables;
40    public IEnumerable<string> InputVariables {
41      get {
42        return inputVariables;
43      }
44    }
45   
46    private int minTimeOffset;
47    public int MinTimeOffset {
48      get { return minTimeOffset; }
49    }
50    private int maxTimeOffset;
51    public int MaxTimeOffset {
52      get { return maxTimeOffset; }
53    }
54
55    public MultiLayerPerceptron() : base() { } // for persistence;
56
57    public MultiLayerPerceptron(alglib.mlpbase.multilayerperceptron perceptron, IEnumerable<string> inputVariables,
58      int minTimeOffset, int maxTimeOffset)
59      : base() {
60      this.perceptron = perceptron;
61      this.minTimeOffset = minTimeOffset;
62      this.maxTimeOffset = maxTimeOffset;
63      this.inputVariables = new List<string>(inputVariables);
64    }
65
66    public override object Clone(IDictionary<Guid, object> clonedObjects) {
67      MultiLayerPerceptron clone = (MultiLayerPerceptron)base.Clone(clonedObjects);
68
69      clone.inputVariables = new List<string>(inputVariables);
70      clone.minTimeOffset = MinTimeOffset;
71      clone.maxTimeOffset = MaxTimeOffset;
72
73      double[] ra = null;
74      int rlen = 0;
75      alglib.mlpbase.mlpserialize(ref perceptron, ref ra, ref rlen); // output: ra, rlen
76      alglib.mlpbase.mlpunserialize(ref ra, ref clone.perceptron); // output clone.perceptron
77      return clone;
78    }
79
80    public override System.Xml.XmlNode GetXmlNode(string name, System.Xml.XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
81      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
82      var minTimeOffsetAttr = document.CreateAttribute("MinTimeOffset");
83      minTimeOffsetAttr.Value = minTimeOffset.ToString();
84      var maxTimeOffsetAttr = document.CreateAttribute("MaxTimeOffset");
85      maxTimeOffsetAttr.Value = maxTimeOffset.ToString();
86      node.Attributes.Append(minTimeOffsetAttr);
87      node.Attributes.Append(maxTimeOffsetAttr);
88      XmlNode networkInformation = document.CreateElement("NetworkInformation");
89      double[] ra = null;
90      int rlen = 0;
91      alglib.mlpbase.mlpserialize(ref perceptron, ref ra, ref rlen);
92      networkInformation.InnerText = String.Join(";", ra.Select(x => x.ToString("r", CultureInfo.InvariantCulture)).ToArray()); // culture invariant & round trip
93      node.AppendChild(networkInformation);
94
95      XmlNode inputVariablesNode = document.CreateElement("InputVariables");
96      inputVariablesNode.InnerText = String.Join(";", inputVariables.ToArray());
97      node.AppendChild(inputVariablesNode);
98      return node;
99    }
100
101    public override void Populate(System.Xml.XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
102      base.Populate(node, restoredObjects);
103      minTimeOffset = XmlConvert.ToInt32(node.Attributes["MinTimeOffset"].Value);
104      maxTimeOffset = XmlConvert.ToInt32(node.Attributes["MaxTimeOffset"].Value);
105      double[] ra = (from s in node.SelectSingleNode("NetworkInformation").InnerText.Split(';')
106                     select double.Parse(s, CultureInfo.InvariantCulture)).ToArray();
107      alglib.mlpbase.mlpunserialize(ref ra, ref perceptron);
108
109      inputVariables = new List<string>(node.SelectSingleNode("InputVariables").InnerText.Split(';'));
110    }
111  }
112}
Note: See TracBrowser for help on using the repository browser.