Free cookie consent management tool by TermsFeed Policy Generator

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

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

Fixed #721.

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