[5624] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16453] | 3 | * Copyright (C) 2002-2019 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;
|
---|
[5777] | 23 | using System.Collections.Generic;
|
---|
[5624] | 24 | using System.Linq;
|
---|
[14523] | 25 | using System.Threading;
|
---|
[5624] | 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Data;
|
---|
| 29 | using HeuristicLab.Optimization;
|
---|
| 30 | using HeuristicLab.Parameters;
|
---|
[16559] | 31 | using HEAL.Attic;
|
---|
[5624] | 32 | using HeuristicLab.Problems.DataAnalysis;
|
---|
[8609] | 33 | using LibSVM;
|
---|
[5624] | 34 |
|
---|
| 35 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
| 36 | /// <summary>
|
---|
| 37 | /// Support vector machine regression data analysis algorithm.
|
---|
| 38 | /// </summary>
|
---|
[13238] | 39 | [Item("Support Vector Regression (SVM)", "Support vector machine regression data analysis algorithm (wrapper for libSVM).")]
|
---|
[12504] | 40 | [Creatable(CreatableAttribute.Categories.DataAnalysisRegression, Priority = 110)]
|
---|
[16462] | 41 | [StorableType("645A21E5-EF07-46BF-AA04-A616165F0EF4")]
|
---|
[5624] | 42 | public sealed class SupportVectorRegression : FixedDataAnalysisAlgorithm<IRegressionProblem> {
|
---|
| 43 | private const string SvmTypeParameterName = "SvmType";
|
---|
| 44 | private const string KernelTypeParameterName = "KernelType";
|
---|
| 45 | private const string CostParameterName = "Cost";
|
---|
| 46 | private const string NuParameterName = "Nu";
|
---|
| 47 | private const string GammaParameterName = "Gamma";
|
---|
| 48 | private const string EpsilonParameterName = "Epsilon";
|
---|
[8613] | 49 | private const string DegreeParameterName = "Degree";
|
---|
[12934] | 50 | private const string CreateSolutionParameterName = "CreateSolution";
|
---|
[5624] | 51 |
|
---|
| 52 | #region parameter properties
|
---|
[8121] | 53 | public IConstrainedValueParameter<StringValue> SvmTypeParameter {
|
---|
| 54 | get { return (IConstrainedValueParameter<StringValue>)Parameters[SvmTypeParameterName]; }
|
---|
[5624] | 55 | }
|
---|
[8121] | 56 | public IConstrainedValueParameter<StringValue> KernelTypeParameter {
|
---|
| 57 | get { return (IConstrainedValueParameter<StringValue>)Parameters[KernelTypeParameterName]; }
|
---|
[5624] | 58 | }
|
---|
| 59 | public IValueParameter<DoubleValue> NuParameter {
|
---|
| 60 | get { return (IValueParameter<DoubleValue>)Parameters[NuParameterName]; }
|
---|
| 61 | }
|
---|
| 62 | public IValueParameter<DoubleValue> CostParameter {
|
---|
| 63 | get { return (IValueParameter<DoubleValue>)Parameters[CostParameterName]; }
|
---|
| 64 | }
|
---|
| 65 | public IValueParameter<DoubleValue> GammaParameter {
|
---|
| 66 | get { return (IValueParameter<DoubleValue>)Parameters[GammaParameterName]; }
|
---|
| 67 | }
|
---|
| 68 | public IValueParameter<DoubleValue> EpsilonParameter {
|
---|
| 69 | get { return (IValueParameter<DoubleValue>)Parameters[EpsilonParameterName]; }
|
---|
| 70 | }
|
---|
[8613] | 71 | public IValueParameter<IntValue> DegreeParameter {
|
---|
| 72 | get { return (IValueParameter<IntValue>)Parameters[DegreeParameterName]; }
|
---|
| 73 | }
|
---|
[12934] | 74 | public IFixedValueParameter<BoolValue> CreateSolutionParameter {
|
---|
| 75 | get { return (IFixedValueParameter<BoolValue>)Parameters[CreateSolutionParameterName]; }
|
---|
| 76 | }
|
---|
[5624] | 77 | #endregion
|
---|
| 78 | #region properties
|
---|
| 79 | public StringValue SvmType {
|
---|
| 80 | get { return SvmTypeParameter.Value; }
|
---|
[8121] | 81 | set { SvmTypeParameter.Value = value; }
|
---|
[5624] | 82 | }
|
---|
| 83 | public StringValue KernelType {
|
---|
| 84 | get { return KernelTypeParameter.Value; }
|
---|
[8121] | 85 | set { KernelTypeParameter.Value = value; }
|
---|
[5624] | 86 | }
|
---|
| 87 | public DoubleValue Nu {
|
---|
| 88 | get { return NuParameter.Value; }
|
---|
| 89 | }
|
---|
| 90 | public DoubleValue Cost {
|
---|
| 91 | get { return CostParameter.Value; }
|
---|
| 92 | }
|
---|
| 93 | public DoubleValue Gamma {
|
---|
| 94 | get { return GammaParameter.Value; }
|
---|
| 95 | }
|
---|
| 96 | public DoubleValue Epsilon {
|
---|
| 97 | get { return EpsilonParameter.Value; }
|
---|
| 98 | }
|
---|
[8613] | 99 | public IntValue Degree {
|
---|
| 100 | get { return DegreeParameter.Value; }
|
---|
| 101 | }
|
---|
[12934] | 102 | public bool CreateSolution {
|
---|
| 103 | get { return CreateSolutionParameter.Value.Value; }
|
---|
| 104 | set { CreateSolutionParameter.Value.Value = value; }
|
---|
| 105 | }
|
---|
[5624] | 106 | #endregion
|
---|
| 107 | [StorableConstructor]
|
---|
[16462] | 108 | private SupportVectorRegression(StorableConstructorFlag _) : base(_) { }
|
---|
[5624] | 109 | private SupportVectorRegression(SupportVectorRegression original, Cloner cloner)
|
---|
| 110 | : base(original, cloner) {
|
---|
| 111 | }
|
---|
| 112 | public SupportVectorRegression()
|
---|
| 113 | : base() {
|
---|
[5649] | 114 | Problem = new RegressionProblem();
|
---|
| 115 |
|
---|
[5626] | 116 | List<StringValue> svrTypes = (from type in new List<string> { "NU_SVR", "EPSILON_SVR" }
|
---|
| 117 | select new StringValue(type).AsReadOnly())
|
---|
| 118 | .ToList();
|
---|
| 119 | ItemSet<StringValue> svrTypeSet = new ItemSet<StringValue>(svrTypes);
|
---|
| 120 | List<StringValue> kernelTypes = (from type in new List<string> { "LINEAR", "POLY", "SIGMOID", "RBF" }
|
---|
| 121 | select new StringValue(type).AsReadOnly())
|
---|
| 122 | .ToList();
|
---|
[5649] | 123 | ItemSet<StringValue> kernelTypeSet = new ItemSet<StringValue>(kernelTypes);
|
---|
[5626] | 124 | Parameters.Add(new ConstrainedValueParameter<StringValue>(SvmTypeParameterName, "The type of SVM to use.", svrTypeSet, svrTypes[0]));
|
---|
| 125 | Parameters.Add(new ConstrainedValueParameter<StringValue>(KernelTypeParameterName, "The kernel type to use for the SVM.", kernelTypeSet, kernelTypes[3]));
|
---|
| 126 | Parameters.Add(new ValueParameter<DoubleValue>(NuParameterName, "The value of the nu parameter of the nu-SVR.", new DoubleValue(0.5)));
|
---|
| 127 | Parameters.Add(new ValueParameter<DoubleValue>(CostParameterName, "The value of the C (cost) parameter of epsilon-SVR and nu-SVR.", new DoubleValue(1.0)));
|
---|
[5624] | 128 | Parameters.Add(new ValueParameter<DoubleValue>(GammaParameterName, "The value of the gamma parameter in the kernel function.", new DoubleValue(1.0)));
|
---|
[5626] | 129 | Parameters.Add(new ValueParameter<DoubleValue>(EpsilonParameterName, "The value of the epsilon parameter for epsilon-SVR.", new DoubleValue(0.1)));
|
---|
[8613] | 130 | Parameters.Add(new ValueParameter<IntValue>(DegreeParameterName, "The degree parameter for the polynomial kernel function.", new IntValue(3)));
|
---|
[12934] | 131 | Parameters.Add(new FixedValueParameter<BoolValue>(CreateSolutionParameterName, "Flag that indicates if a solution should be produced at the end of the run", new BoolValue(true)));
|
---|
| 132 | Parameters[CreateSolutionParameterName].Hidden = true;
|
---|
[5624] | 133 | }
|
---|
| 134 | [StorableHook(HookType.AfterDeserialization)]
|
---|
[8613] | 135 | private void AfterDeserialization() {
|
---|
| 136 | #region backwards compatibility (change with 3.4)
|
---|
[12934] | 137 |
|
---|
| 138 | if (!Parameters.ContainsKey(DegreeParameterName)) {
|
---|
| 139 | Parameters.Add(new ValueParameter<IntValue>(DegreeParameterName,
|
---|
| 140 | "The degree parameter for the polynomial kernel function.", new IntValue(3)));
|
---|
| 141 | }
|
---|
| 142 | if (!Parameters.ContainsKey(CreateSolutionParameterName)) {
|
---|
| 143 | Parameters.Add(new FixedValueParameter<BoolValue>(CreateSolutionParameterName, "Flag that indicates if a solution should be produced at the end of the run", new BoolValue(true)));
|
---|
| 144 | Parameters[CreateSolutionParameterName].Hidden = true;
|
---|
| 145 | }
|
---|
[8613] | 146 | #endregion
|
---|
| 147 | }
|
---|
[5624] | 148 |
|
---|
| 149 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 150 | return new SupportVectorRegression(this, cloner);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | #region support vector regression
|
---|
[14523] | 154 | protected override void Run(CancellationToken cancellationToken) {
|
---|
[5624] | 155 | IRegressionProblemData problemData = Problem.ProblemData;
|
---|
[5649] | 156 | IEnumerable<string> selectedInputVariables = problemData.AllowedInputVariables;
|
---|
[7306] | 157 | int nSv;
|
---|
[12934] | 158 | ISupportVectorMachineModel model;
|
---|
| 159 | Run(problemData, selectedInputVariables, SvmType.Value, KernelType.Value, Cost.Value, Nu.Value, Gamma.Value, Epsilon.Value, Degree.Value, out model, out nSv);
|
---|
[5624] | 160 |
|
---|
[12934] | 161 | if (CreateSolution) {
|
---|
| 162 | var solution = new SupportVectorRegressionSolution((SupportVectorMachineModel)model, (IRegressionProblemData)problemData.Clone());
|
---|
| 163 | Results.Add(new Result("Support vector regression solution", "The support vector regression solution.", solution));
|
---|
| 164 | }
|
---|
| 165 |
|
---|
[7306] | 166 | Results.Add(new Result("Number of support vectors", "The number of support vectors of the SVR solution.", new IntValue(nSv)));
|
---|
[12934] | 167 |
|
---|
| 168 |
|
---|
| 169 | {
|
---|
| 170 | // calculate regression model metrics
|
---|
| 171 | var ds = problemData.Dataset;
|
---|
| 172 | var trainRows = problemData.TrainingIndices;
|
---|
| 173 | var testRows = problemData.TestIndices;
|
---|
| 174 | var yTrain = ds.GetDoubleValues(problemData.TargetVariable, trainRows);
|
---|
| 175 | var yTest = ds.GetDoubleValues(problemData.TargetVariable, testRows);
|
---|
| 176 | var yPredTrain = model.GetEstimatedValues(ds, trainRows).ToArray();
|
---|
| 177 | var yPredTest = model.GetEstimatedValues(ds, testRows).ToArray();
|
---|
| 178 |
|
---|
| 179 | OnlineCalculatorError error;
|
---|
| 180 | var trainMse = OnlineMeanSquaredErrorCalculator.Calculate(yPredTrain, yTrain, out error);
|
---|
| 181 | if (error != OnlineCalculatorError.None) trainMse = double.MaxValue;
|
---|
| 182 | var testMse = OnlineMeanSquaredErrorCalculator.Calculate(yPredTest, yTest, out error);
|
---|
| 183 | if (error != OnlineCalculatorError.None) testMse = double.MaxValue;
|
---|
| 184 |
|
---|
| 185 | Results.Add(new Result("Mean squared error (training)", "The mean of squared errors of the SVR solution on the training partition.", new DoubleValue(trainMse)));
|
---|
| 186 | Results.Add(new Result("Mean squared error (test)", "The mean of squared errors of the SVR solution on the test partition.", new DoubleValue(testMse)));
|
---|
| 187 |
|
---|
| 188 |
|
---|
| 189 | var trainMae = OnlineMeanAbsoluteErrorCalculator.Calculate(yPredTrain, yTrain, out error);
|
---|
| 190 | if (error != OnlineCalculatorError.None) trainMae = double.MaxValue;
|
---|
| 191 | var testMae = OnlineMeanAbsoluteErrorCalculator.Calculate(yPredTest, yTest, out error);
|
---|
| 192 | if (error != OnlineCalculatorError.None) testMae = double.MaxValue;
|
---|
| 193 |
|
---|
| 194 | Results.Add(new Result("Mean absolute error (training)", "The mean of absolute errors of the SVR solution on the training partition.", new DoubleValue(trainMae)));
|
---|
| 195 | Results.Add(new Result("Mean absolute error (test)", "The mean of absolute errors of the SVR solution on the test partition.", new DoubleValue(testMae)));
|
---|
| 196 |
|
---|
| 197 |
|
---|
| 198 | var trainRelErr = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(yPredTrain, yTrain, out error);
|
---|
| 199 | if (error != OnlineCalculatorError.None) trainRelErr = double.MaxValue;
|
---|
| 200 | var testRelErr = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(yPredTest, yTest, out error);
|
---|
| 201 | if (error != OnlineCalculatorError.None) testRelErr = double.MaxValue;
|
---|
| 202 |
|
---|
| 203 | Results.Add(new Result("Average relative error (training)", "The mean of relative errors of the SVR solution on the training partition.", new DoubleValue(trainRelErr)));
|
---|
| 204 | Results.Add(new Result("Average relative error (test)", "The mean of relative errors of the SVR solution on the test partition.", new DoubleValue(testRelErr)));
|
---|
| 205 | }
|
---|
[5624] | 206 | }
|
---|
| 207 |
|
---|
[12934] | 208 | // BackwardsCompatibility3.4
|
---|
| 209 | #region Backwards compatible code, remove with 3.5
|
---|
| 210 | // for compatibility with old API
|
---|
| 211 | public static SupportVectorRegressionSolution CreateSupportVectorRegressionSolution(
|
---|
| 212 | IRegressionProblemData problemData, IEnumerable<string> allowedInputVariables,
|
---|
[8613] | 213 | string svmType, string kernelType, double cost, double nu, double gamma, double epsilon, int degree,
|
---|
[7306] | 214 | out double trainingR2, out double testR2, out int nSv) {
|
---|
[12934] | 215 | ISupportVectorMachineModel model;
|
---|
| 216 | Run(problemData, allowedInputVariables, svmType, kernelType, cost, nu, gamma, epsilon, degree, out model, out nSv);
|
---|
| 217 |
|
---|
| 218 | var solution = new SupportVectorRegressionSolution((SupportVectorMachineModel)model, (IRegressionProblemData)problemData.Clone());
|
---|
| 219 | trainingR2 = solution.TrainingRSquared;
|
---|
| 220 | testR2 = solution.TestRSquared;
|
---|
| 221 | return solution;
|
---|
| 222 | }
|
---|
| 223 | #endregion
|
---|
| 224 |
|
---|
| 225 | public static void Run(IRegressionProblemData problemData, IEnumerable<string> allowedInputVariables,
|
---|
| 226 | string svmType, string kernelType, double cost, double nu, double gamma, double epsilon, int degree,
|
---|
| 227 | out ISupportVectorMachineModel model, out int nSv) {
|
---|
[12509] | 228 | var dataset = problemData.Dataset;
|
---|
[5624] | 229 | string targetVariable = problemData.TargetVariable;
|
---|
[8139] | 230 | IEnumerable<int> rows = problemData.TrainingIndices;
|
---|
[5624] | 231 |
|
---|
[12934] | 232 | svm_parameter parameter = new svm_parameter {
|
---|
| 233 | svm_type = GetSvmType(svmType),
|
---|
| 234 | kernel_type = GetKernelType(kernelType),
|
---|
| 235 | C = cost,
|
---|
| 236 | nu = nu,
|
---|
| 237 | gamma = gamma,
|
---|
| 238 | p = epsilon,
|
---|
| 239 | cache_size = 500,
|
---|
| 240 | probability = 0,
|
---|
| 241 | eps = 0.001,
|
---|
| 242 | degree = degree,
|
---|
| 243 | shrinking = 1,
|
---|
| 244 | coef0 = 0
|
---|
| 245 | };
|
---|
[5624] | 246 |
|
---|
[8609] | 247 | svm_problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows);
|
---|
| 248 | RangeTransform rangeTransform = RangeTransform.Compute(problem);
|
---|
| 249 | svm_problem scaledProblem = rangeTransform.Scale(problem);
|
---|
| 250 | var svmModel = svm.svm_train(scaledProblem, parameter);
|
---|
| 251 | nSv = svmModel.SV.Length;
|
---|
[12934] | 252 |
|
---|
| 253 | model = new SupportVectorMachineModel(svmModel, rangeTransform, targetVariable, allowedInputVariables);
|
---|
[5624] | 254 | }
|
---|
[8609] | 255 |
|
---|
| 256 | private static int GetSvmType(string svmType) {
|
---|
| 257 | if (svmType == "NU_SVR") return svm_parameter.NU_SVR;
|
---|
| 258 | if (svmType == "EPSILON_SVR") return svm_parameter.EPSILON_SVR;
|
---|
| 259 | throw new ArgumentException("Unknown SVM type");
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | private static int GetKernelType(string kernelType) {
|
---|
| 263 | if (kernelType == "LINEAR") return svm_parameter.LINEAR;
|
---|
| 264 | if (kernelType == "POLY") return svm_parameter.POLY;
|
---|
| 265 | if (kernelType == "SIGMOID") return svm_parameter.SIGMOID;
|
---|
| 266 | if (kernelType == "RBF") return svm_parameter.RBF;
|
---|
| 267 | throw new ArgumentException("Unknown kernel type");
|
---|
| 268 | }
|
---|
[5624] | 269 | #endregion
|
---|
| 270 | }
|
---|
| 271 | }
|
---|