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