#region License Information
/* HeuristicLab
* Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using HeuristicLab.Core;
using System.Globalization;
using System.IO;
namespace HeuristicLab.Data {
public class SVMModel : ItemBase {
private SVM.Model model;
///
/// Gets or sets the SVM model.
///
public SVM.Model Model {
get { return model; }
set { model = value; }
}
///
/// Gets or sets the range transformation for the model.
///
private SVM.RangeTransform rangeTransform;
public SVM.RangeTransform RangeTransform {
get { return rangeTransform; }
set { rangeTransform = value; }
}
public override IView CreateView() {
return new HeuristicLab.SupportVectorMachines.SVMModelView(this);
}
///
/// Clones the current instance and adds it to the dictionary .
///
/// Dictionary of all already cloned objects.
/// The cloned instance as .
public override object Clone(IDictionary clonedObjects) {
SVMModel clone = new SVMModel();
clonedObjects.Add(Guid, clone);
// beware we are only using a shallow copy here! (gkronber)
clone.Model = Model;
clone.RangeTransform = RangeTransform;
return clone;
}
///
/// Saves the current instance as in the specified .
///
/// The actual model is saved in the node's inner text as string,
/// its format depending on the local culture info and its number format.
/// The (tag)name of the .
/// The where the data is saved.
/// A dictionary of all already persisted objects. (Needed to avoid cycles.)
/// The saved .
public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary persistedObjects) {
XmlNode node = base.GetXmlNode(name, document, persistedObjects);
XmlNode model = document.CreateElement("Model");
using (MemoryStream stream = new MemoryStream()) {
SVM.Model.Write(stream, Model);
stream.Seek(0, System.IO.SeekOrigin.Begin);
StreamReader reader = new StreamReader(stream);
model.InnerText = reader.ReadToEnd();
node.AppendChild(model);
}
XmlNode rangeTransform = document.CreateElement("RangeTransform");
using (MemoryStream stream = new MemoryStream()) {
SVM.RangeTransform.Write(stream, RangeTransform);
stream.Seek(0, System.IO.SeekOrigin.Begin);
StreamReader reader = new StreamReader(stream);
rangeTransform.InnerText = reader.ReadToEnd();
node.AppendChild(rangeTransform);
}
return node;
}
///
/// Loads the persisted SVM model from the specified .
///
/// The serialized SVM model must be saved in the node's inner text as a string
/// (see ).
/// The where the SVM model is saved.
/// A dictionary of all already restored objects. (Needed to avoid cycles.)
public override void Populate(XmlNode node, IDictionary restoredObjects) {
base.Populate(node, restoredObjects);
XmlNode model = node.SelectSingleNode("Model");
using (MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(model.InnerText))) {
Model = SVM.Model.Read(stream);
}
XmlNode rangeTransform = node.SelectSingleNode("RangeTransform");
using (MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(rangeTransform.InnerText))) {
RangeTransform = SVM.RangeTransform.Read(stream);
}
}
}
}