Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4451 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

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