1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 | using HeuristicLab.Problems.DataAnalysis;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
34 | /// <summary>
|
---|
35 | /// Support vector machine regression data analysis algorithm.
|
---|
36 | /// </summary>
|
---|
37 | [Item("Support Vector Regression", "Support vector machine regression data analysis algorithm (wrapper for libSVM).")]
|
---|
38 | [Creatable("Data Analysis")]
|
---|
39 | [StorableClass]
|
---|
40 | public sealed class SupportVectorRegression : FixedDataAnalysisAlgorithm<IRegressionProblem> {
|
---|
41 | private const string SvmTypeParameterName = "SvmType";
|
---|
42 | private const string KernelTypeParameterName = "KernelType";
|
---|
43 | private const string CostParameterName = "Cost";
|
---|
44 | private const string NuParameterName = "Nu";
|
---|
45 | private const string GammaParameterName = "Gamma";
|
---|
46 | private const string EpsilonParameterName = "Epsilon";
|
---|
47 |
|
---|
48 | #region parameter properties
|
---|
49 | public IValueParameter<StringValue> SvmTypeParameter {
|
---|
50 | get { return (IValueParameter<StringValue>)Parameters[SvmTypeParameterName]; }
|
---|
51 | }
|
---|
52 | public IValueParameter<StringValue> KernelTypeParameter {
|
---|
53 | get { return (IValueParameter<StringValue>)Parameters[KernelTypeParameterName]; }
|
---|
54 | }
|
---|
55 | public IValueParameter<DoubleValue> NuParameter {
|
---|
56 | get { return (IValueParameter<DoubleValue>)Parameters[NuParameterName]; }
|
---|
57 | }
|
---|
58 | public IValueParameter<DoubleValue> CostParameter {
|
---|
59 | get { return (IValueParameter<DoubleValue>)Parameters[CostParameterName]; }
|
---|
60 | }
|
---|
61 | public IValueParameter<DoubleValue> GammaParameter {
|
---|
62 | get { return (IValueParameter<DoubleValue>)Parameters[GammaParameterName]; }
|
---|
63 | }
|
---|
64 | public IValueParameter<DoubleValue> EpsilonParameter {
|
---|
65 | get { return (IValueParameter<DoubleValue>)Parameters[EpsilonParameterName]; }
|
---|
66 | }
|
---|
67 | #endregion
|
---|
68 | #region properties
|
---|
69 | public StringValue SvmType {
|
---|
70 | get { return SvmTypeParameter.Value; }
|
---|
71 | }
|
---|
72 | public StringValue KernelType {
|
---|
73 | get { return KernelTypeParameter.Value; }
|
---|
74 | }
|
---|
75 | public DoubleValue Nu {
|
---|
76 | get { return NuParameter.Value; }
|
---|
77 | }
|
---|
78 | public DoubleValue Cost {
|
---|
79 | get { return CostParameter.Value; }
|
---|
80 | }
|
---|
81 | public DoubleValue Gamma {
|
---|
82 | get { return GammaParameter.Value; }
|
---|
83 | }
|
---|
84 | public DoubleValue Epsilon {
|
---|
85 | get { return EpsilonParameter.Value; }
|
---|
86 | }
|
---|
87 | #endregion
|
---|
88 | [StorableConstructor]
|
---|
89 | private SupportVectorRegression(bool deserializing) : base(deserializing) { }
|
---|
90 | private SupportVectorRegression(SupportVectorRegression original, Cloner cloner)
|
---|
91 | : base(original, cloner) {
|
---|
92 | }
|
---|
93 | public SupportVectorRegression()
|
---|
94 | : base() {
|
---|
95 | Problem = new RegressionProblem();
|
---|
96 |
|
---|
97 | List<StringValue> svrTypes = (from type in new List<string> { "NU_SVR", "EPSILON_SVR" }
|
---|
98 | select new StringValue(type).AsReadOnly())
|
---|
99 | .ToList();
|
---|
100 | ItemSet<StringValue> svrTypeSet = new ItemSet<StringValue>(svrTypes);
|
---|
101 | List<StringValue> kernelTypes = (from type in new List<string> { "LINEAR", "POLY", "SIGMOID", "RBF" }
|
---|
102 | select new StringValue(type).AsReadOnly())
|
---|
103 | .ToList();
|
---|
104 | ItemSet<StringValue> kernelTypeSet = new ItemSet<StringValue>(kernelTypes);
|
---|
105 | Parameters.Add(new ConstrainedValueParameter<StringValue>(SvmTypeParameterName, "The type of SVM to use.", svrTypeSet, svrTypes[0]));
|
---|
106 | Parameters.Add(new ConstrainedValueParameter<StringValue>(KernelTypeParameterName, "The kernel type to use for the SVM.", kernelTypeSet, kernelTypes[3]));
|
---|
107 | Parameters.Add(new ValueParameter<DoubleValue>(NuParameterName, "The value of the nu parameter of the nu-SVR.", new DoubleValue(0.5)));
|
---|
108 | Parameters.Add(new ValueParameter<DoubleValue>(CostParameterName, "The value of the C (cost) parameter of epsilon-SVR and nu-SVR.", new DoubleValue(1.0)));
|
---|
109 | Parameters.Add(new ValueParameter<DoubleValue>(GammaParameterName, "The value of the gamma parameter in the kernel function.", new DoubleValue(1.0)));
|
---|
110 | Parameters.Add(new ValueParameter<DoubleValue>(EpsilonParameterName, "The value of the epsilon parameter for epsilon-SVR.", new DoubleValue(0.1)));
|
---|
111 | }
|
---|
112 |
|
---|
113 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
114 | return new SupportVectorRegression(this, cloner);
|
---|
115 | }
|
---|
116 |
|
---|
117 | #region support vector regression
|
---|
118 | protected override void Run() {
|
---|
119 | IRegressionProblemData problemData = Problem.ProblemData;
|
---|
120 | IEnumerable<string> selectedInputVariables = problemData.AllowedInputVariables;
|
---|
121 | var solution = CreateSupportVectorRegressionSolution(problemData, selectedInputVariables, SvmType.Value, KernelType.Value, Cost.Value, Nu.Value, Gamma.Value, Epsilon.Value);
|
---|
122 |
|
---|
123 | Results.Add(new Result("Support vector regression solution", "The support vector regression solution.", solution));
|
---|
124 | }
|
---|
125 |
|
---|
126 | public static SupportVectorRegressionSolution CreateSupportVectorRegressionSolution(IRegressionProblemData problemData, IEnumerable<string> allowedInputVariables,
|
---|
127 | string svmType, string kernelType, double cost, double nu, double gamma, double epsilon) {
|
---|
128 | Dataset dataset = problemData.Dataset;
|
---|
129 | string targetVariable = problemData.TargetVariable;
|
---|
130 | IEnumerable<int> rows = problemData.TrainingIndizes;
|
---|
131 |
|
---|
132 | //extract SVM parameters from scope and set them
|
---|
133 | SVM.Parameter parameter = new SVM.Parameter();
|
---|
134 | parameter.SvmType = (SVM.SvmType)Enum.Parse(typeof(SVM.SvmType), svmType, true);
|
---|
135 | parameter.KernelType = (SVM.KernelType)Enum.Parse(typeof(SVM.KernelType), kernelType, true);
|
---|
136 | parameter.C = cost;
|
---|
137 | parameter.Nu = nu;
|
---|
138 | parameter.Gamma = gamma;
|
---|
139 | parameter.P = epsilon;
|
---|
140 | parameter.CacheSize = 500;
|
---|
141 | parameter.Probability = false;
|
---|
142 |
|
---|
143 |
|
---|
144 | SVM.Problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows);
|
---|
145 | SVM.RangeTransform rangeTransform = SVM.RangeTransform.Compute(problem);
|
---|
146 | SVM.Problem scaledProblem = SVM.Scaling.Scale(rangeTransform, problem);
|
---|
147 | var model = new SupportVectorMachineModel(SVM.Training.Train(scaledProblem, parameter), rangeTransform, targetVariable, allowedInputVariables);
|
---|
148 | return new SupportVectorRegressionSolution(model, (IRegressionProblemData)problemData.Clone());
|
---|
149 | }
|
---|
150 | #endregion
|
---|
151 | }
|
---|
152 | }
|
---|