[5624] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15583] | 3 | * Copyright (C) 2002-2018 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.")]
|
---|
[13941] | 39 | public sealed class SupportVectorMachineModel : ClassificationModel, ISupportVectorMachineModel {
|
---|
| 40 | public override IEnumerable<string> VariablesUsedForPrediction {
|
---|
[13921] | 41 | get { return allowedInputVariables; }
|
---|
| 42 | }
|
---|
[5624] | 43 |
|
---|
[13921] | 44 |
|
---|
[8609] | 45 | private svm_model model;
|
---|
[5624] | 46 | /// <summary>
|
---|
| 47 | /// Gets or sets the SVM model.
|
---|
| 48 | /// </summary>
|
---|
[8609] | 49 | public svm_model Model {
|
---|
[5624] | 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>
|
---|
[8609] | 63 | private RangeTransform rangeTransform;
|
---|
| 64 | public RangeTransform RangeTransform {
|
---|
[5624] | 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 | }
|
---|
[5649] | 74 |
|
---|
[5690] | 75 | public Dataset SupportVectors {
|
---|
| 76 | get {
|
---|
[8609] | 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];
|
---|
[5690] | 80 | for (int j = 0; j < sv.Length; j++) {
|
---|
[8609] | 81 | data[i, j] = sv[j].value;
|
---|
[5690] | 82 | }
|
---|
| 83 | }
|
---|
| 84 | return new Dataset(allowedInputVariables, data);
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[5649] | 88 | [Storable]
|
---|
| 89 | private string[] allowedInputVariables;
|
---|
[5690] | 90 | [Storable]
|
---|
| 91 | private double[] classValues; // only for SVM classification models
|
---|
[5649] | 92 |
|
---|
| 93 | [StorableConstructor]
|
---|
| 94 | private SupportVectorMachineModel(bool deserializing) : base(deserializing) { }
|
---|
| 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;
|
---|
[5690] | 100 | this.allowedInputVariables = (string[])original.allowedInputVariables.Clone();
|
---|
| 101 | if (original.classValues != null)
|
---|
| 102 | this.classValues = (double[])original.classValues.Clone();
|
---|
[5649] | 103 | }
|
---|
[8609] | 104 | public SupportVectorMachineModel(svm_model model, RangeTransform rangeTransform, string targetVariable, IEnumerable<string> allowedInputVariables, IEnumerable<double> classValues)
|
---|
[5690] | 105 | : this(model, rangeTransform, targetVariable, allowedInputVariables) {
|
---|
| 106 | this.classValues = classValues.ToArray();
|
---|
| 107 | }
|
---|
[8609] | 108 | public SupportVectorMachineModel(svm_model model, RangeTransform rangeTransform, string targetVariable, IEnumerable<string> allowedInputVariables)
|
---|
[13941] | 109 | : base(targetVariable) {
|
---|
[5649] | 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 |
|
---|
[5626] | 121 | #region IRegressionModel Members
|
---|
[12509] | 122 | public IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
|
---|
[5649] | 123 | return GetEstimatedValuesHelper(dataset, rows);
|
---|
[5626] | 124 | }
|
---|
[13941] | 125 | public IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
|
---|
[8528] | 126 | return new SupportVectorRegressionSolution(this, new RegressionProblemData(problemData));
|
---|
[6603] | 127 | }
|
---|
[16386] | 128 |
|
---|
| 129 | public bool IsProblemDataCompatible(IRegressionProblemData problemData, out string errorMessage) {
|
---|
| 130 | return RegressionModel.IsProblemDataCompatible(this, problemData, out errorMessage);
|
---|
| 131 | }
|
---|
[5626] | 132 | #endregion
|
---|
[6603] | 133 |
|
---|
[16386] | 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 a regression nor a classification problem data. Instead a " + problemData.GetType().GetPrettyName() + " was provided.", "problemData");
|
---|
| 146 | }
|
---|
| 147 |
|
---|
[5626] | 148 | #region IClassificationModel Members
|
---|
[13941] | 149 | public override IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows) {
|
---|
[5690] | 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 | }
|
---|
[5626] | 167 | }
|
---|
[6604] | 168 |
|
---|
[13941] | 169 | public override IClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
|
---|
[8528] | 170 | return new SupportVectorClassificationSolution(this, new ClassificationProblemData(problemData));
|
---|
[6604] | 171 | }
|
---|
[5626] | 172 | #endregion
|
---|
[16386] | 173 |
|
---|
[12509] | 174 | private IEnumerable<double> GetEstimatedValuesHelper(IDataset dataset, IEnumerable<int> rows) {
|
---|
[7180] | 175 | // calculate predictions for the currently requested rows
|
---|
[15854] | 176 | svm_problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, allowedInputVariables, rows);
|
---|
[8609] | 177 | svm_problem scaledProblem = rangeTransform.Scale(problem);
|
---|
[5624] | 178 |
|
---|
[8609] | 179 | for (int i = 0; i < problem.l; i++) {
|
---|
| 180 | yield return svm.svm_predict(Model, scaledProblem.x[i]);
|
---|
[5690] | 181 | }
|
---|
[5624] | 182 | }
|
---|
[6566] | 183 |
|
---|
[5624] | 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()) {
|
---|
[8609] | 198 | svm.svm_save_model(new StreamWriter(stream), Model);
|
---|
[5624] | 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))) {
|
---|
[8609] | 206 | model = svm.svm_load_model(new StreamReader(stream));
|
---|
[5624] | 207 | }
|
---|
| 208 | }
|
---|
| 209 | }
|
---|
| 210 | [Storable]
|
---|
| 211 | private string RangeTransformAsString {
|
---|
| 212 | get {
|
---|
| 213 | using (MemoryStream stream = new MemoryStream()) {
|
---|
[8609] | 214 | RangeTransform.Write(stream, RangeTransform);
|
---|
[5624] | 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))) {
|
---|
[8609] | 222 | RangeTransform = RangeTransform.Read(stream);
|
---|
[5624] | 223 | }
|
---|
| 224 | }
|
---|
| 225 | }
|
---|
[5861] | 226 | #endregion
|
---|
[5624] | 227 | }
|
---|
| 228 | }
|
---|