[1837] | 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;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Text;
|
---|
| 25 | using System.Xml;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using System.Globalization;
|
---|
| 28 | using System.IO;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Data {
|
---|
[1906] | 31 | public class SVMModel : ItemBase {
|
---|
| 32 | private SVM.Model model;
|
---|
[1837] | 33 | /// <summary>
|
---|
| 34 | /// Gets or sets the SVM model.
|
---|
| 35 | /// </summary>
|
---|
[1906] | 36 | public SVM.Model Model {
|
---|
| 37 | get { return model; }
|
---|
| 38 | set { model = value; }
|
---|
[1837] | 39 | }
|
---|
| 40 |
|
---|
[1906] | 41 |
|
---|
[1837] | 42 | /// <summary>
|
---|
[1906] | 43 | /// Gets or sets the range transformation for the model.
|
---|
| 44 | /// </summary>
|
---|
| 45 | private SVM.RangeTransform rangeTransform;
|
---|
| 46 | public SVM.RangeTransform RangeTransform {
|
---|
| 47 | get { return rangeTransform; }
|
---|
| 48 | set { rangeTransform = value; }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[2156] | 51 | public override IView CreateView() {
|
---|
| 52 | return new HeuristicLab.SupportVectorMachines.SVMModelView(this);
|
---|
| 53 | }
|
---|
| 54 |
|
---|
[1906] | 55 | /// <summary>
|
---|
[1837] | 56 | /// Clones the current instance and adds it to the dictionary <paramref name="clonedObjects"/>.
|
---|
| 57 | /// </summary>
|
---|
| 58 | /// <param name="clonedObjects">Dictionary of all already cloned objects.</param>
|
---|
| 59 | /// <returns>The cloned instance as <see cref="DoubleData"/>.</returns>
|
---|
| 60 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
| 61 | SVMModel clone = new SVMModel();
|
---|
| 62 | clonedObjects.Add(Guid, clone);
|
---|
| 63 | // beware we are only using a shallow copy here! (gkronber)
|
---|
[1906] | 64 | clone.Model = Model;
|
---|
| 65 | clone.RangeTransform = RangeTransform;
|
---|
[1837] | 66 | return clone;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | /// <summary>
|
---|
| 70 | /// Saves the current instance as <see cref="XmlNode"/> in the specified <paramref name="document"/>.
|
---|
| 71 | /// </summary>
|
---|
| 72 | /// <remarks>The actual model is saved in the node's inner text as string,
|
---|
| 73 | /// its format depending on the local culture info and its number format.</remarks>
|
---|
| 74 | /// <param name="name">The (tag)name of the <see cref="XmlNode"/>.</param>
|
---|
| 75 | /// <param name="document">The <see cref="XmlDocument"/> where the data is saved.</param>
|
---|
| 76 | /// <param name="persistedObjects">A dictionary of all already persisted objects. (Needed to avoid cycles.)</param>
|
---|
| 77 | /// <returns>The saved <see cref="XmlNode"/>.</returns>
|
---|
| 78 | public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
| 79 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
[1906] | 80 | XmlNode model = document.CreateElement("Model");
|
---|
| 81 | using (MemoryStream stream = new MemoryStream()) {
|
---|
| 82 | SVM.Model.Write(stream, Model);
|
---|
| 83 | stream.Seek(0, System.IO.SeekOrigin.Begin);
|
---|
| 84 | StreamReader reader = new StreamReader(stream);
|
---|
| 85 | model.InnerText = reader.ReadToEnd();
|
---|
| 86 | node.AppendChild(model);
|
---|
| 87 | }
|
---|
[1837] | 88 |
|
---|
[1906] | 89 | XmlNode rangeTransform = document.CreateElement("RangeTransform");
|
---|
[1837] | 90 | using (MemoryStream stream = new MemoryStream()) {
|
---|
[1906] | 91 | SVM.RangeTransform.Write(stream, RangeTransform);
|
---|
[1837] | 92 | stream.Seek(0, System.IO.SeekOrigin.Begin);
|
---|
| 93 | StreamReader reader = new StreamReader(stream);
|
---|
[1906] | 94 | rangeTransform.InnerText = reader.ReadToEnd();
|
---|
| 95 | node.AppendChild(rangeTransform);
|
---|
[1837] | 96 | }
|
---|
[1906] | 97 |
|
---|
[1837] | 98 | return node;
|
---|
| 99 | }
|
---|
| 100 | /// <summary>
|
---|
| 101 | /// Loads the persisted SVM model from the specified <paramref name="node"/>.
|
---|
| 102 | /// </summary>
|
---|
| 103 | /// <remarks>The serialized SVM model must be saved in the node's inner text as a string
|
---|
| 104 | /// (see <see cref="GetXmlNode"/>).</remarks>
|
---|
| 105 | /// <param name="node">The <see cref="XmlNode"/> where the SVM model is saved.</param>
|
---|
| 106 | /// <param name="restoredObjects">A dictionary of all already restored objects. (Needed to avoid cycles.)</param>
|
---|
| 107 | public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
| 108 | base.Populate(node, restoredObjects);
|
---|
[1906] | 109 | XmlNode model = node.SelectSingleNode("Model");
|
---|
| 110 | using (MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(model.InnerText))) {
|
---|
| 111 | Model = SVM.Model.Read(stream);
|
---|
[1837] | 112 | }
|
---|
[1906] | 113 | XmlNode rangeTransform = node.SelectSingleNode("RangeTransform");
|
---|
| 114 | using (MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(rangeTransform.InnerText))) {
|
---|
| 115 | RangeTransform = SVM.RangeTransform.Read(stream);
|
---|
| 116 | }
|
---|
[1837] | 117 | }
|
---|
| 118 | }
|
---|
| 119 | }
|
---|