Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorRegression.cs @ 7097

Last change on this file since 7097 was 7097, checked in by gkronber, 12 years ago

#1081: reverse merged r6802, r6807-6808, r6811, r6974, r7058 to prepare for the 3.3.6 release

File size: 7.4 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Problems.DataAnalysis;
32
33namespace 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    [StorableHook(HookType.AfterDeserialization)]
113    private void AfterDeserialization() { }
114
115    public override IDeepCloneable Clone(Cloner cloner) {
116      return new SupportVectorRegression(this, cloner);
117    }
118
119    #region support vector regression
120    protected override void Run() {
121      IRegressionProblemData problemData = Problem.ProblemData;
122      IEnumerable<string> selectedInputVariables = problemData.AllowedInputVariables;
123      var solution = CreateSupportVectorRegressionSolution(problemData, selectedInputVariables, SvmType.Value, KernelType.Value, Cost.Value, Nu.Value, Gamma.Value, Epsilon.Value);
124
125      Results.Add(new Result("Support vector regression solution", "The support vector regression solution.", solution));
126    }
127
128    public static SupportVectorRegressionSolution CreateSupportVectorRegressionSolution(IRegressionProblemData problemData, IEnumerable<string> allowedInputVariables,
129      string svmType, string kernelType, double cost, double nu, double gamma, double epsilon) {
130      Dataset dataset = problemData.Dataset;
131      string targetVariable = problemData.TargetVariable;
132      IEnumerable<int> rows = problemData.TrainingIndizes;
133
134      //extract SVM parameters from scope and set them
135      SVM.Parameter parameter = new SVM.Parameter();
136      parameter.SvmType = (SVM.SvmType)Enum.Parse(typeof(SVM.SvmType), svmType, true);
137      parameter.KernelType = (SVM.KernelType)Enum.Parse(typeof(SVM.KernelType), kernelType, true);
138      parameter.C = cost;
139      parameter.Nu = nu;
140      parameter.Gamma = gamma;
141      parameter.P = epsilon;
142      parameter.CacheSize = 500;
143      parameter.Probability = false;
144
145
146      SVM.Problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows);
147      SVM.RangeTransform rangeTransform = SVM.RangeTransform.Compute(problem);
148      SVM.Problem scaledProblem = SVM.Scaling.Scale(rangeTransform, problem);
149      var model = new SupportVectorMachineModel(SVM.Training.Train(scaledProblem, parameter), rangeTransform, targetVariable, allowedInputVariables);
150      return new SupportVectorRegressionSolution(model, (IRegressionProblemData)problemData.Clone());
151    }
152    #endregion
153  }
154}
Note: See TracBrowser for help on using the repository browser.