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 |
|
---|
22 | using System;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Data;
|
---|
25 | using HeuristicLab.Operators;
|
---|
26 | using HeuristicLab.Parameters;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 | using SVM;
|
---|
29 |
|
---|
30 | namespace 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";
|
---|
43 | private const string EpsilonParameterName = "Epsilon";
|
---|
44 | private const string SamplesStartParameterName = "SamplesStart";
|
---|
45 | private const string SamplesEndParameterName = "SamplesEnd";
|
---|
46 | private const string ModelParameterName = "SupportVectorMachineModel";
|
---|
47 |
|
---|
48 | #region parameter properties
|
---|
49 | public IValueLookupParameter<DataAnalysisProblemData> DataAnalysisProblemDataParameter {
|
---|
50 | get { return (IValueLookupParameter<DataAnalysisProblemData>)Parameters[DataAnalysisProblemDataParameterName]; }
|
---|
51 | }
|
---|
52 | public IValueLookupParameter<StringValue> SvmTypeParameter {
|
---|
53 | get { return (IValueLookupParameter<StringValue>)Parameters[SvmTypeParameterName]; }
|
---|
54 | }
|
---|
55 | public IValueLookupParameter<StringValue> KernelTypeParameter {
|
---|
56 | get { return (IValueLookupParameter<StringValue>)Parameters[KernelTypeParameterName]; }
|
---|
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 | }
|
---|
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 | }
|
---|
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 | }
|
---|
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 | }
|
---|
108 | #endregion
|
---|
109 |
|
---|
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."));
|
---|
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));
|
---|
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."));
|
---|
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."));
|
---|
123 | Parameters.Add(new LookupParameter<SupportVectorMachineModel>(ModelParameterName, "The result model generated by the SVM."));
|
---|
124 | }
|
---|
125 |
|
---|
126 | public override IOperation Apply() {
|
---|
127 | SupportVectorMachineModel model = TrainModel(DataAnalysisProblemData,
|
---|
128 | SamplesStart.Value, SamplesEnd.Value,
|
---|
129 | SvmType.Value, KernelType.Value,
|
---|
130 | Cost.Value, Nu.Value, Gamma.Value, Epsilon.Value);
|
---|
131 | SupportVectorMachineModelParameter.ActualValue = model;
|
---|
132 |
|
---|
133 | return base.Apply();
|
---|
134 | }
|
---|
135 |
|
---|
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 |
|
---|
143 | public static SupportVectorMachineModel TrainModel(
|
---|
144 | DataAnalysisProblemData problemData,
|
---|
145 | int start, int end,
|
---|
146 | string svmType, string kernelType,
|
---|
147 | double cost, double nu, double gamma, double epsilon) {
|
---|
148 | int targetVariableIndex = problemData.Dataset.GetVariableIndex(problemData.TargetVariable.Value);
|
---|
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;
|
---|
157 | parameter.P = epsilon;
|
---|
158 | parameter.CacheSize = 500;
|
---|
159 | parameter.Probability = false;
|
---|
160 |
|
---|
161 |
|
---|
162 | SVM.Problem problem = SupportVectorMachineUtil.CreateSvmProblem(problemData, start, end);
|
---|
163 | SVM.RangeTransform rangeTransform = SVM.RangeTransform.Compute(problem);
|
---|
164 | SVM.Problem scaledProblem = Scaling.Scale(rangeTransform, problem);
|
---|
165 | var model = new SupportVectorMachineModel();
|
---|
166 | model.Model = SVM.Training.Train(scaledProblem, parameter);
|
---|
167 | model.RangeTransform = rangeTransform;
|
---|
168 |
|
---|
169 | return model;
|
---|
170 | }
|
---|
171 | }
|
---|
172 | }
|
---|