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