Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.SupportVectorMachines/3.2/SVMModel.cs @ 1841

Last change on this file since 1841 was 1837, checked in by gkronber, 16 years ago

Implemented #627 (Datatypes of SVM should be wrapped into dedicated classes that implement IItem).

File size: 4.0 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;
23using System.Collections.Generic;
24using System.Text;
25using System.Xml;
26using HeuristicLab.Core;
27using System.Globalization;
28using System.IO;
29
30namespace HeuristicLab.Data {
31  public class SVMModel : ObjectData {
32    /// <summary>
33    /// Gets or sets the SVM model.
34    /// </summary>
35    /// <remarks>Uses property <see cref="ObjectData.Data"/> of base class <see cref="ObjectData"></see>.
36    /// No own data storage present.</remarks>
37    public new SVM.Model Data {
38      get { return (SVM.Model)base.Data; }
39      set { base.Data = value; }
40    }
41
42    /// <summary>
43    /// Clones the current instance and adds it to the dictionary <paramref name="clonedObjects"/>.
44    /// </summary>
45    /// <param name="clonedObjects">Dictionary of all already cloned objects.</param>
46    /// <returns>The cloned instance as <see cref="DoubleData"/>.</returns>
47    public override object Clone(IDictionary<Guid, object> clonedObjects) {
48      SVMModel clone = new SVMModel();
49      clonedObjects.Add(Guid, clone);
50      // beware we are only using a shallow copy here! (gkronber)
51      clone.Data = Data;
52      return clone;
53    }
54
55    /// <summary>
56    /// Saves the current instance as <see cref="XmlNode"/> in the specified <paramref name="document"/>.
57    /// </summary>
58    /// <remarks>The actual model is saved in the node's inner text as string,
59    /// its format depending on the local culture info and its number format.</remarks>
60    /// <param name="name">The (tag)name of the <see cref="XmlNode"/>.</param>
61    /// <param name="document">The <see cref="XmlDocument"/> where the data is saved.</param>
62    /// <param name="persistedObjects">A dictionary of all already persisted objects. (Needed to avoid cycles.)</param>
63    /// <returns>The saved <see cref="XmlNode"/>.</returns>
64    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
65      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
66      XmlNode data = document.CreateElement("Data");
67
68      using (MemoryStream stream = new MemoryStream()) {
69        SVM.Model.Write(stream, Data);
70        stream.Seek(0, System.IO.SeekOrigin.Begin);
71        StreamReader reader = new StreamReader(stream);
72        data.InnerText = reader.ReadToEnd();
73        node.AppendChild(data);
74      }
75      return node;
76    }
77    /// <summary>
78    /// Loads the persisted SVM model from the specified <paramref name="node"/>.
79    /// </summary>
80    /// <remarks>The serialized SVM model must be saved in the node's inner text as a string 
81    /// (see <see cref="GetXmlNode"/>).</remarks>
82    /// <param name="node">The <see cref="XmlNode"/> where the SVM model is saved.</param>
83    /// <param name="restoredObjects">A dictionary of all already restored objects. (Needed to avoid cycles.)</param>
84    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
85      base.Populate(node, restoredObjects);
86      XmlNode data = node.SelectSingleNode("Data");
87      using (MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(data.InnerText))) {
88        Data = SVM.Model.Read(stream);
89      }
90    }
91  }
92}
Note: See TracBrowser for help on using the repository browser.