[2562] | 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 | using System;
|
---|
| 22 | using System.Collections.Generic;
|
---|
| 23 | using System.Text;
|
---|
| 24 | using HeuristicLab.PluginInfrastructure;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using System.Xml;
|
---|
| 27 | using System.Linq;
|
---|
| 28 | using System.Globalization;
|
---|
| 29 |
|
---|
| 30 | namespace 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 |
|
---|
| 71 | double[] ra = null;
|
---|
| 72 | int rlen = 0;
|
---|
| 73 | alglib.mlpbase.mlpserialize(ref perceptron, ref ra, ref rlen); // output: ra, rlen
|
---|
| 74 | alglib.mlpbase.mlpunserialize(ref ra, ref clone.perceptron); // output clone.perceptron
|
---|
| 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 |
|
---|
| 81 | XmlNode networkInformation = document.CreateElement("NetworkInformation");
|
---|
| 82 | double[] ra = null;
|
---|
| 83 | int rlen = 0;
|
---|
| 84 | alglib.mlpbase.mlpserialize(ref perceptron, ref ra, ref rlen);
|
---|
| 85 | networkInformation.InnerText = String.Join(";", ra.Select(x => x.ToString("r", CultureInfo.InvariantCulture)).ToArray()); // culture invariant & round trip
|
---|
| 86 | node.AppendChild(networkInformation);
|
---|
| 87 |
|
---|
| 88 | XmlNode inputVariablesNode = document.CreateElement("InputVariables");
|
---|
| 89 | inputVariablesNode.InnerText = String.Join(";", inputVariables.ToArray());
|
---|
| 90 | node.AppendChild(inputVariablesNode);
|
---|
| 91 | return node;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | public override void Populate(System.Xml.XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
| 95 | base.Populate(node, restoredObjects);
|
---|
| 96 | double[] ra = (from s in node.SelectSingleNode("NetworkInformation").InnerText.Split(';')
|
---|
| 97 | select double.Parse(s, CultureInfo.InvariantCulture)).ToArray();
|
---|
| 98 | alglib.mlpbase.mlpunserialize(ref ra, ref perceptron);
|
---|
| 99 |
|
---|
| 100 | inputVariables = new List<string>(node.SelectSingleNode("InputVariables").InnerText.Split(';'));
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 | } |
---|