Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/SupportVectorMachine/SupportVectorMachineModelCreator.cs @ 4543

Last change on this file since 4543 was 4543, checked in by gkronber, 14 years ago

Adapted SVM classes to work correctly for overlapping training / test partitions. #1226

File size: 8.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Operators;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using SVM;
29using System.Collections.Generic;
30using System.Linq;
31
32namespace HeuristicLab.Problems.DataAnalysis.SupportVectorMachine {
33  /// <summary>
34  /// Represents an operator that creates a support vector machine model.
35  /// </summary>
36  [StorableClass]
37  [Item("SupportVectorMachineModelCreator", "Represents an operator that creates a support vector machine model.")]
38  public class SupportVectorMachineModelCreator : SingleSuccessorOperator {
39    private const string DataAnalysisProblemDataParameterName = "DataAnalysisProblemData";
40    private const string SvmTypeParameterName = "SvmType";
41    private const string KernelTypeParameterName = "KernelType";
42    private const string CostParameterName = "Cost";
43    private const string NuParameterName = "Nu";
44    private const string GammaParameterName = "Gamma";
45    private const string EpsilonParameterName = "Epsilon";
46    private const string SamplesStartParameterName = "SamplesStart";
47    private const string SamplesEndParameterName = "SamplesEnd";
48    private const string ModelParameterName = "SupportVectorMachineModel";
49
50    #region parameter properties
51    public IValueLookupParameter<DataAnalysisProblemData> DataAnalysisProblemDataParameter {
52      get { return (IValueLookupParameter<DataAnalysisProblemData>)Parameters[DataAnalysisProblemDataParameterName]; }
53    }
54    public IValueLookupParameter<StringValue> SvmTypeParameter {
55      get { return (IValueLookupParameter<StringValue>)Parameters[SvmTypeParameterName]; }
56    }
57    public IValueLookupParameter<StringValue> KernelTypeParameter {
58      get { return (IValueLookupParameter<StringValue>)Parameters[KernelTypeParameterName]; }
59    }
60    public IValueLookupParameter<DoubleValue> NuParameter {
61      get { return (IValueLookupParameter<DoubleValue>)Parameters[NuParameterName]; }
62    }
63    public IValueLookupParameter<DoubleValue> CostParameter {
64      get { return (IValueLookupParameter<DoubleValue>)Parameters[CostParameterName]; }
65    }
66    public IValueLookupParameter<DoubleValue> GammaParameter {
67      get { return (IValueLookupParameter<DoubleValue>)Parameters[GammaParameterName]; }
68    }
69    public IValueLookupParameter<DoubleValue> EpsilonParameter {
70      get { return (IValueLookupParameter<DoubleValue>)Parameters[EpsilonParameterName]; }
71    }
72    public IValueLookupParameter<IntValue> SamplesStartParameter {
73      get { return (IValueLookupParameter<IntValue>)Parameters[SamplesStartParameterName]; }
74    }
75    public IValueLookupParameter<IntValue> SamplesEndParameter {
76      get { return (IValueLookupParameter<IntValue>)Parameters[SamplesEndParameterName]; }
77    }
78    public ILookupParameter<SupportVectorMachineModel> SupportVectorMachineModelParameter {
79      get { return (ILookupParameter<SupportVectorMachineModel>)Parameters[ModelParameterName]; }
80    }
81    #endregion
82    #region properties
83    public DataAnalysisProblemData DataAnalysisProblemData {
84      get { return DataAnalysisProblemDataParameter.ActualValue; }
85    }
86    public StringValue SvmType {
87      get { return SvmTypeParameter.Value; }
88    }
89    public StringValue KernelType {
90      get { return KernelTypeParameter.Value; }
91    }
92    public DoubleValue Nu {
93      get { return NuParameter.ActualValue; }
94    }
95    public DoubleValue Cost {
96      get { return CostParameter.ActualValue; }
97    }
98    public DoubleValue Gamma {
99      get { return GammaParameter.ActualValue; }
100    }
101    public DoubleValue Epsilon {
102      get { return EpsilonParameter.ActualValue; }
103    }
104    public IntValue SamplesStart {
105      get { return SamplesStartParameter.ActualValue; }
106    }
107    public IntValue SamplesEnd {
108      get { return SamplesEndParameter.ActualValue; }
109    }
110    #endregion
111
112    public SupportVectorMachineModelCreator()
113      : base() {
114      StringValue nuSvrType = new StringValue("NU_SVR").AsReadOnly();
115      StringValue rbfKernelType = new StringValue("RBF").AsReadOnly();
116      Parameters.Add(new ValueLookupParameter<DataAnalysisProblemData>(DataAnalysisProblemDataParameterName, "The data analysis problem data to use for training."));
117      Parameters.Add(new ValueLookupParameter<StringValue>(SvmTypeParameterName, "The type of SVM to use.", nuSvrType));
118      Parameters.Add(new ValueLookupParameter<StringValue>(KernelTypeParameterName, "The kernel type to use for the SVM.", rbfKernelType));
119      Parameters.Add(new ValueLookupParameter<DoubleValue>(NuParameterName, "The value of the nu parameter nu-SVC, one-class SVM and nu-SVR."));
120      Parameters.Add(new ValueLookupParameter<DoubleValue>(CostParameterName, "The value of the C (cost) parameter of C-SVC, epsilon-SVR and nu-SVR."));
121      Parameters.Add(new ValueLookupParameter<DoubleValue>(GammaParameterName, "The value of the gamma parameter in the kernel function."));
122      Parameters.Add(new ValueLookupParameter<DoubleValue>(EpsilonParameterName, "The value of the epsilon parameter for epsilon-SVR."));
123      Parameters.Add(new ValueLookupParameter<IntValue>(SamplesStartParameterName, "The first index of the data set partition the support vector machine should use for training."));
124      Parameters.Add(new ValueLookupParameter<IntValue>(SamplesEndParameterName, "The last index of the data set partition the support vector machine should use for training."));
125      Parameters.Add(new LookupParameter<SupportVectorMachineModel>(ModelParameterName, "The result model generated by the SVM."));
126    }
127
128    public override IOperation Apply() {
129      int start = SamplesStart.Value;
130      int end = SamplesEnd.Value;
131      IEnumerable<int> rows =
132        Enumerable.Range(start, end-start)
133        .Where(i => i < DataAnalysisProblemData.TestSamplesStart.Value || DataAnalysisProblemData.TestSamplesEnd.Value <= i);
134
135      SupportVectorMachineModel model = TrainModel(DataAnalysisProblemData,
136                             rows,
137                             SvmType.Value, KernelType.Value,
138                             Cost.Value, Nu.Value, Gamma.Value, Epsilon.Value);
139      SupportVectorMachineModelParameter.ActualValue = model;
140
141      return base.Apply();
142    }
143
144    private static SupportVectorMachineModel TrainModel(
145      DataAnalysisProblemData problemData,
146      string svmType, string kernelType,
147      double cost, double nu, double gamma, double epsilon) {
148      return TrainModel(problemData, problemData.TrainingIndizes, svmType, kernelType, cost, nu, gamma, epsilon);
149    }
150
151    public static SupportVectorMachineModel TrainModel(
152      DataAnalysisProblemData problemData,
153      IEnumerable<int> trainingIndizes,
154      string svmType, string kernelType,
155      double cost, double nu, double gamma, double epsilon) {
156      int targetVariableIndex = problemData.Dataset.GetVariableIndex(problemData.TargetVariable.Value);
157
158      //extract SVM parameters from scope and set them
159      SVM.Parameter parameter = new SVM.Parameter();
160      parameter.SvmType = (SVM.SvmType)Enum.Parse(typeof(SVM.SvmType), svmType, true);
161      parameter.KernelType = (SVM.KernelType)Enum.Parse(typeof(SVM.KernelType), kernelType, true);
162      parameter.C = cost;
163      parameter.Nu = nu;
164      parameter.Gamma = gamma;
165      parameter.P = epsilon;
166      parameter.CacheSize = 500;
167      parameter.Probability = false;
168
169
170      SVM.Problem problem = SupportVectorMachineUtil.CreateSvmProblem(problemData, trainingIndizes);
171      SVM.RangeTransform rangeTransform = SVM.RangeTransform.Compute(problem);
172      SVM.Problem scaledProblem = Scaling.Scale(rangeTransform, problem);
173      var model = new SupportVectorMachineModel();
174      model.Model = SVM.Training.Train(scaledProblem, parameter);
175      model.RangeTransform = rangeTransform;
176
177      return model;
178    }
179  }
180}
Note: See TracBrowser for help on using the repository browser.