Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Operator Architecture Refactoring/HeuristicLab.SupportVectorMachines/3.2/SVMModel.cs @ 1992

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

Added interface IModel and a standard implementation and changed SVM models to include the range transform in the model. #650 (IAlgorithm and derived interfaces should provide properties to retrieve results)

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