1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.IO;
|
---|
25 | using System.Linq;
|
---|
26 | using System.Text;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 | using SVM;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Problems.DataAnalysis.SupportVectorMachine {
|
---|
33 | /// <summary>
|
---|
34 | /// Represents a support vector machine model.
|
---|
35 | /// </summary>
|
---|
36 | [StorableClass]
|
---|
37 | [Item("SupportVectorMachineModel", "Represents a support vector machine model.")]
|
---|
38 | public class SupportVectorMachineModel : NamedItem, IDataAnalysisModel {
|
---|
39 | public SupportVectorMachineModel()
|
---|
40 | : base() {
|
---|
41 | }
|
---|
42 |
|
---|
43 | private SVM.Model model;
|
---|
44 | /// <summary>
|
---|
45 | /// Gets or sets the SVM model.
|
---|
46 | /// </summary>
|
---|
47 | public SVM.Model Model {
|
---|
48 | get { return model; }
|
---|
49 | set {
|
---|
50 | if (value != model) {
|
---|
51 | if (value == null) throw new ArgumentNullException();
|
---|
52 | model = value;
|
---|
53 | OnChanged(EventArgs.Empty);
|
---|
54 | }
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | /// <summary>
|
---|
59 | /// Gets or sets the range transformation for the model.
|
---|
60 | /// </summary>
|
---|
61 | private SVM.RangeTransform rangeTransform;
|
---|
62 | public SVM.RangeTransform RangeTransform {
|
---|
63 | get { return rangeTransform; }
|
---|
64 | set {
|
---|
65 | if (value != rangeTransform) {
|
---|
66 | if (value == null) throw new ArgumentNullException();
|
---|
67 | rangeTransform = value;
|
---|
68 | OnChanged(EventArgs.Empty);
|
---|
69 | }
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | public IEnumerable<double> GetEstimatedValues(DataAnalysisProblemData problemData, int start, int end) {
|
---|
74 | SVM.Problem problem = SupportVectorMachineUtil.CreateSvmProblem(problemData, start, end);
|
---|
75 | SVM.Problem scaledProblem = Scaling.Scale(RangeTransform, problem);
|
---|
76 |
|
---|
77 | return (from row in Enumerable.Range(0, scaledProblem.Count)
|
---|
78 | select SVM.Prediction.Predict(Model, scaledProblem.X[row])).ToList();
|
---|
79 | }
|
---|
80 |
|
---|
81 | #region events
|
---|
82 | public event EventHandler Changed;
|
---|
83 | private void OnChanged(EventArgs e) {
|
---|
84 | var handlers = Changed;
|
---|
85 | if (handlers != null)
|
---|
86 | handlers(this, e);
|
---|
87 | }
|
---|
88 | #endregion
|
---|
89 |
|
---|
90 | #region persistence
|
---|
91 | [Storable]
|
---|
92 | private int[] SupportVectorIndizes {
|
---|
93 | get { return this.Model.SupportVectorIndizes; }
|
---|
94 | set { this.Model.SupportVectorIndizes = value; }
|
---|
95 | }
|
---|
96 |
|
---|
97 | [Storable]
|
---|
98 | private string ModelAsString {
|
---|
99 | get {
|
---|
100 | using (MemoryStream stream = new MemoryStream()) {
|
---|
101 | SVM.Model.Write(stream, Model);
|
---|
102 | stream.Seek(0, System.IO.SeekOrigin.Begin);
|
---|
103 | StreamReader reader = new StreamReader(stream);
|
---|
104 | return reader.ReadToEnd();
|
---|
105 | }
|
---|
106 | }
|
---|
107 | set {
|
---|
108 | using (MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(value))) {
|
---|
109 | model = SVM.Model.Read(stream);
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|
113 | [Storable]
|
---|
114 | private string RangeTransformAsString {
|
---|
115 | get {
|
---|
116 | using (MemoryStream stream = new MemoryStream()) {
|
---|
117 | SVM.RangeTransform.Write(stream, RangeTransform);
|
---|
118 | stream.Seek(0, System.IO.SeekOrigin.Begin);
|
---|
119 | StreamReader reader = new StreamReader(stream);
|
---|
120 | return reader.ReadToEnd();
|
---|
121 | }
|
---|
122 | }
|
---|
123 | set {
|
---|
124 | using (MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(value))) {
|
---|
125 | RangeTransform = SVM.RangeTransform.Read(stream);
|
---|
126 | }
|
---|
127 | }
|
---|
128 | }
|
---|
129 | #endregion
|
---|
130 |
|
---|
131 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
132 | SupportVectorMachineModel clone = (SupportVectorMachineModel)base.Clone(cloner);
|
---|
133 | // beware we are only using a shallow copy here! (gkronber)
|
---|
134 | clone.model = model;
|
---|
135 | clone.rangeTransform = rangeTransform;
|
---|
136 | return clone;
|
---|
137 | }
|
---|
138 |
|
---|
139 | /// <summary>
|
---|
140 | /// Exports the <paramref name="model"/> in string representation to stream <paramref name="s"/>
|
---|
141 | /// </summary>
|
---|
142 | /// <param name="model">The support vector regression model to export</param>
|
---|
143 | /// <param name="s">The stream to export the model to</param>
|
---|
144 | public static void Export(SupportVectorMachineModel model, Stream s) {
|
---|
145 | StreamWriter writer = new StreamWriter(s);
|
---|
146 | writer.WriteLine("RangeTransform:");
|
---|
147 | writer.Flush();
|
---|
148 | using (MemoryStream memStream = new MemoryStream()) {
|
---|
149 | SVM.RangeTransform.Write(memStream, model.RangeTransform);
|
---|
150 | memStream.Seek(0, SeekOrigin.Begin);
|
---|
151 | memStream.WriteTo(s);
|
---|
152 | }
|
---|
153 | writer.WriteLine("Model:");
|
---|
154 | writer.Flush();
|
---|
155 | using (MemoryStream memStream = new MemoryStream()) {
|
---|
156 | SVM.Model.Write(memStream, model.Model);
|
---|
157 | memStream.Seek(0, SeekOrigin.Begin);
|
---|
158 | memStream.WriteTo(s);
|
---|
159 | }
|
---|
160 | s.Flush();
|
---|
161 | }
|
---|
162 |
|
---|
163 | /// <summary>
|
---|
164 | /// Imports a support vector machine model given as string representation.
|
---|
165 | /// </summary>
|
---|
166 | /// <param name="reader">The reader to retrieve the string representation from</param>
|
---|
167 | /// <returns>The imported support vector machine model.</returns>
|
---|
168 | public static SupportVectorMachineModel Import(TextReader reader) {
|
---|
169 | SupportVectorMachineModel model = new SupportVectorMachineModel();
|
---|
170 | while (reader.ReadLine().Trim() != "RangeTransform:") ; // read until line "RangeTransform";
|
---|
171 | model.RangeTransform = SVM.RangeTransform.Read(reader);
|
---|
172 | // read until "Model:"
|
---|
173 | while (reader.ReadLine().Trim() != "Model:") ;
|
---|
174 | model.Model = SVM.Model.Read(reader);
|
---|
175 | return model;
|
---|
176 | }
|
---|
177 | }
|
---|
178 | }
|
---|