Free cookie consent management tool by TermsFeed Policy Generator

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

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

Fixed bugs in text export/import of SVM models. #772.

File size: 6.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    /// <summary>
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
51    public override IView CreateView() {
52      return new SVMModelView(this);
53    }
54
55    /// <summary>
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)
64      clone.Model = Model;
65      clone.RangeTransform = RangeTransform;
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);
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      }
88
89      XmlNode rangeTransform = document.CreateElement("RangeTransform");
90      using (MemoryStream stream = new MemoryStream()) {
91        SVM.RangeTransform.Write(stream, RangeTransform);
92        stream.Seek(0, System.IO.SeekOrigin.Begin);
93        StreamReader reader = new StreamReader(stream);
94        rangeTransform.InnerText = reader.ReadToEnd();
95        node.AppendChild(rangeTransform);
96      }
97
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);
109      XmlNode model = node.SelectSingleNode("Model");
110      using (MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(model.InnerText))) {
111        Model = SVM.Model.Read(stream);
112      }
113      XmlNode rangeTransform = node.SelectSingleNode("RangeTransform");
114      using (MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(rangeTransform.InnerText))) {
115        RangeTransform = SVM.RangeTransform.Read(stream);
116      }
117    }
118
119    public static void Export(SVMModel model, Stream s) {
120      StreamWriter writer = new StreamWriter(s);
121      writer.WriteLine("RangeTransform:");
122      writer.Flush();
123      using (MemoryStream memStream = new MemoryStream()) {
124        SVM.RangeTransform.Write(memStream, model.RangeTransform);
125        memStream.Seek(0, SeekOrigin.Begin);
126        memStream.WriteTo(s);
127      }
128      writer.WriteLine("Model:");
129      writer.Flush();
130      using (MemoryStream memStream = new MemoryStream()) {
131        SVM.Model.Write(memStream, model.Model);
132        memStream.Seek(0, SeekOrigin.Begin);
133        memStream.WriteTo(s);
134      }
135      s.Flush();
136    }
137
138    public static SVMModel Import(TextReader reader) {
139      SVMModel model = new SVMModel();
140      while (reader.ReadLine().Trim() != "RangeTransform:") ; // read until line "RangeTransform";
141      model.RangeTransform = SVM.RangeTransform.Read(reader);
142      // read until "Model:"
143      while (reader.ReadLine().Trim() != "Model:") ;
144      model.Model = SVM.Model.Read(reader);
145      return model;
146    }
147  }
148}
Note: See TracBrowser for help on using the repository browser.