Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorMachineModel.cs @ 17399

Last change on this file since 17399 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 8.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.IO;
25using System.Linq;
26using System.Text;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HEAL.Attic;
30using HeuristicLab.Problems.DataAnalysis;
31using LibSVM;
32
33namespace HeuristicLab.Algorithms.DataAnalysis {
34  /// <summary>
35  /// Represents a support vector machine model.
36  /// </summary>
37  [StorableType("669B0522-A27B-4DA6-B655-27C374D88C95")]
38  [Item("SupportVectorMachineModel", "Represents a support vector machine model.")]
39  public sealed class SupportVectorMachineModel : ClassificationModel, ISupportVectorMachineModel {
40    public override IEnumerable<string> VariablesUsedForPrediction {
41      get { return allowedInputVariables; }
42    }
43
44
45    private svm_model model;
46    /// <summary>
47    /// Gets or sets the SVM model.
48    /// </summary>
49    public svm_model Model {
50      get { return model; }
51      set {
52        if (value != model) {
53          if (value == null) throw new ArgumentNullException();
54          model = value;
55          OnChanged(EventArgs.Empty);
56        }
57      }
58    }
59
60    /// <summary>
61    /// Gets or sets the range transformation for the model.
62    /// </summary>
63    private RangeTransform rangeTransform;
64    public RangeTransform RangeTransform {
65      get { return rangeTransform; }
66      set {
67        if (value != rangeTransform) {
68          if (value == null) throw new ArgumentNullException();
69          rangeTransform = value;
70          OnChanged(EventArgs.Empty);
71        }
72      }
73    }
74
75    public Dataset SupportVectors {
76      get {
77        var data = new double[Model.sv_coef.Length, allowedInputVariables.Count()];
78        for (int i = 0; i < Model.sv_coef.Length; i++) {
79          var sv = Model.SV[i];
80          for (int j = 0; j < sv.Length; j++) {
81            data[i, j] = sv[j].value;
82          }
83        }
84        return new Dataset(allowedInputVariables, data);
85      }
86    }
87
88    [Storable]
89    private string[] allowedInputVariables;
90    [Storable]
91    private double[] classValues; // only for SVM classification models
92
93    [StorableConstructor]
94    private SupportVectorMachineModel(StorableConstructorFlag _) : base(_) { }
95    private SupportVectorMachineModel(SupportVectorMachineModel original, Cloner cloner)
96      : base(original, cloner) {
97      // only using a shallow copy here! (gkronber)
98      this.model = original.model;
99      this.rangeTransform = original.rangeTransform;
100      this.allowedInputVariables = (string[])original.allowedInputVariables.Clone();
101      if (original.classValues != null)
102        this.classValues = (double[])original.classValues.Clone();
103    }
104    public SupportVectorMachineModel(svm_model model, RangeTransform rangeTransform, string targetVariable, IEnumerable<string> allowedInputVariables, IEnumerable<double> classValues)
105      : this(model, rangeTransform, targetVariable, allowedInputVariables) {
106      this.classValues = classValues.ToArray();
107    }
108    public SupportVectorMachineModel(svm_model model, RangeTransform rangeTransform, string targetVariable, IEnumerable<string> allowedInputVariables)
109      : base(targetVariable) {
110      this.name = ItemName;
111      this.description = ItemDescription;
112      this.model = model;
113      this.rangeTransform = rangeTransform;
114      this.allowedInputVariables = allowedInputVariables.ToArray();
115    }
116
117    public override IDeepCloneable Clone(Cloner cloner) {
118      return new SupportVectorMachineModel(this, cloner);
119    }
120
121    #region IRegressionModel Members
122    public IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
123      return GetEstimatedValuesHelper(dataset, rows);
124    }
125    public IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
126      return new SupportVectorRegressionSolution(this, new RegressionProblemData(problemData));
127    }
128
129    public bool IsProblemDataCompatible(IRegressionProblemData problemData, out string errorMessage) {
130      return RegressionModel.IsProblemDataCompatible(this, problemData, out errorMessage);
131    }
132    #endregion
133
134    public override bool IsProblemDataCompatible(IDataAnalysisProblemData problemData, out string errorMessage) {
135      if (problemData == null) throw new ArgumentNullException("problemData", "The provided problemData is null.");
136
137      var regressionProblemData = problemData as IRegressionProblemData;
138      if (regressionProblemData != null)
139        return IsProblemDataCompatible(regressionProblemData, out errorMessage);
140
141      var classificationProblemData = problemData as IClassificationProblemData;
142      if (classificationProblemData != null)
143        return IsProblemDataCompatible(classificationProblemData, out errorMessage);
144
145      throw new ArgumentException("The problem data is not compatible with this SVM. Instead a " + problemData.GetType().GetPrettyName() + " was provided.", "problemData");
146    }
147
148    #region IClassificationModel Members
149    public override IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows) {
150      if (classValues == null) throw new NotSupportedException();
151      // return the original class value instead of the predicted value of the model
152      // svm classification only works for integer classes
153      foreach (var estimated in GetEstimatedValuesHelper(dataset, rows)) {
154        // find closest class
155        double bestDist = double.MaxValue;
156        double bestClass = -1;
157        for (int i = 0; i < classValues.Length; i++) {
158          double d = Math.Abs(estimated - classValues[i]);
159          if (d < bestDist) {
160            bestDist = d;
161            bestClass = classValues[i];
162            if (d.IsAlmost(0.0)) break; // exact match no need to look further
163          }
164        }
165        yield return bestClass;
166      }
167    }
168
169    public override IClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
170      return new SupportVectorClassificationSolution(this, new ClassificationProblemData(problemData));
171    }
172    #endregion
173
174    private IEnumerable<double> GetEstimatedValuesHelper(IDataset dataset, IEnumerable<int> rows) {
175      // calculate predictions for the currently requested rows
176      svm_problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, allowedInputVariables, rows);
177      svm_problem scaledProblem = rangeTransform.Scale(problem);
178
179      for (int i = 0; i < problem.l; i++) {
180        yield return svm.svm_predict(Model, scaledProblem.x[i]);
181      }
182    }
183
184    #region events
185    public event EventHandler Changed;
186    private void OnChanged(EventArgs e) {
187      var handlers = Changed;
188      if (handlers != null)
189        handlers(this, e);
190    }
191    #endregion
192
193    #region persistence
194    [Storable]
195    private string ModelAsString {
196      get {
197        using (MemoryStream stream = new MemoryStream()) {
198          svm.svm_save_model(new StreamWriter(stream), Model);
199          stream.Seek(0, System.IO.SeekOrigin.Begin);
200          StreamReader reader = new StreamReader(stream);
201          return reader.ReadToEnd();
202        }
203      }
204      set {
205        using (MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(value))) {
206          model = svm.svm_load_model(new StreamReader(stream));
207        }
208      }
209    }
210    [Storable]
211    private string RangeTransformAsString {
212      get {
213        using (MemoryStream stream = new MemoryStream()) {
214          RangeTransform.Write(stream, RangeTransform);
215          stream.Seek(0, System.IO.SeekOrigin.Begin);
216          StreamReader reader = new StreamReader(stream);
217          return reader.ReadToEnd();
218        }
219      }
220      set {
221        using (MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(value))) {
222          RangeTransform = RangeTransform.Read(stream);
223        }
224      }
225    }
226    #endregion
227  }
228}
Note: See TracBrowser for help on using the repository browser.