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 ModelParameterName = "SupportVectorMachineModel";
|
---|
49 |
|
---|
50 | #region parameter properties
|
---|
51 | public IValueLookupParameter<DataAnalysisProblemData> DataAnalysisProblemDataParameter {
|
---|
52 | get { return (IValueLookupParameter<DataAnalysisProblemData>)Parameters[DataAnalysisProblemDataParameterName]; }
|
---|
53 | }
|
---|
54 | public IValueParameter<StringValue> SvmTypeParameter {
|
---|
55 | get { return (IValueParameter<StringValue>)Parameters[SvmTypeParameterName]; }
|
---|
56 | }
|
---|
57 | public IValueParameter<StringValue> KernelTypeParameter {
|
---|
58 | get { return (IValueParameter<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 ILookupParameter<SupportVectorMachineModel> SupportVectorMachineModelParameter {
|
---|
70 | get { return (ILookupParameter<SupportVectorMachineModel>)Parameters[ModelParameterName]; }
|
---|
71 | }
|
---|
72 | #endregion
|
---|
73 | #region properties
|
---|
74 | public DataAnalysisProblemData DataAnalysisProblemData {
|
---|
75 | get { return DataAnalysisProblemDataParameter.ActualValue; }
|
---|
76 | }
|
---|
77 | public StringValue SvmType {
|
---|
78 | get { return SvmTypeParameter.Value; }
|
---|
79 | }
|
---|
80 | public StringValue KernelType {
|
---|
81 | get { return KernelTypeParameter.Value; }
|
---|
82 | }
|
---|
83 | public DoubleValue Nu {
|
---|
84 | get { return NuParameter.ActualValue; }
|
---|
85 | }
|
---|
86 | public DoubleValue Cost {
|
---|
87 | get { return CostParameter.ActualValue; }
|
---|
88 | }
|
---|
89 | public DoubleValue Gamma {
|
---|
90 | get { return GammaParameter.ActualValue; }
|
---|
91 | }
|
---|
92 | #endregion
|
---|
93 |
|
---|
94 | public SupportVectorMachineModelCreator()
|
---|
95 | : base() {
|
---|
96 | #region svm types
|
---|
97 | StringValue cSvcType = new StringValue("C_SVC").AsReadOnly();
|
---|
98 | StringValue nuSvcType = new StringValue("NU_SVC").AsReadOnly();
|
---|
99 | StringValue eSvrType = new StringValue("EPSILON_SVR").AsReadOnly();
|
---|
100 | StringValue nuSvrType = new StringValue("NU_SVR").AsReadOnly();
|
---|
101 | ItemSet<StringValue> allowedSvmTypes = new ItemSet<StringValue>();
|
---|
102 | allowedSvmTypes.Add(cSvcType);
|
---|
103 | allowedSvmTypes.Add(nuSvcType);
|
---|
104 | allowedSvmTypes.Add(eSvrType);
|
---|
105 | allowedSvmTypes.Add(nuSvrType);
|
---|
106 | #endregion
|
---|
107 | #region kernel types
|
---|
108 | StringValue rbfKernelType = new StringValue("RBF").AsReadOnly();
|
---|
109 | StringValue linearKernelType = new StringValue("LINEAR").AsReadOnly();
|
---|
110 | StringValue polynomialKernelType = new StringValue("POLY").AsReadOnly();
|
---|
111 | StringValue sigmoidKernelType = new StringValue("SIGMOID").AsReadOnly();
|
---|
112 | ItemSet<StringValue> allowedKernelTypes = new ItemSet<StringValue>();
|
---|
113 | allowedKernelTypes.Add(rbfKernelType);
|
---|
114 | allowedKernelTypes.Add(linearKernelType);
|
---|
115 | allowedKernelTypes.Add(polynomialKernelType);
|
---|
116 | allowedKernelTypes.Add(sigmoidKernelType);
|
---|
117 | #endregion
|
---|
118 | Parameters.Add(new ValueLookupParameter<DataAnalysisProblemData>(DataAnalysisProblemDataParameterName, "The data analysis problem data to use for training."));
|
---|
119 | Parameters.Add(new ConstrainedValueParameter<StringValue>(SvmTypeParameterName, "The type of SVM to use.", allowedSvmTypes, nuSvrType));
|
---|
120 | Parameters.Add(new ConstrainedValueParameter<StringValue>(KernelTypeParameterName, "The kernel type to use for the SVM.", allowedKernelTypes, rbfKernelType));
|
---|
121 | Parameters.Add(new ValueLookupParameter<DoubleValue>(NuParameterName, "The value of the nu parameter nu-SVC, one-class SVM and nu-SVR."));
|
---|
122 | Parameters.Add(new ValueLookupParameter<DoubleValue>(CostParameterName, "The value of the C (cost) parameter of C-SVC, epsilon-SVR and nu-SVR."));
|
---|
123 | Parameters.Add(new ValueLookupParameter<DoubleValue>(GammaParameterName, "The value of the gamma parameter in the kernel function."));
|
---|
124 | Parameters.Add(new LookupParameter<SupportVectorMachineModel>(ModelParameterName, "The result model generated by the SVM."));
|
---|
125 | }
|
---|
126 |
|
---|
127 | public override IOperation Apply() {
|
---|
128 |
|
---|
129 | SupportVectorMachineModel model = TrainModel(DataAnalysisProblemData,
|
---|
130 | SvmType.Value, KernelType.Value,
|
---|
131 | Cost.Value, Nu.Value, Gamma.Value);
|
---|
132 | SupportVectorMachineModelParameter.ActualValue = model;
|
---|
133 |
|
---|
134 | return base.Apply();
|
---|
135 | }
|
---|
136 |
|
---|
137 | public static SupportVectorMachineModel TrainModel(
|
---|
138 | DataAnalysisProblemData problemData,
|
---|
139 | string svmType, string kernelType,
|
---|
140 | double cost, double nu, double gamma) {
|
---|
141 | int targetVariableIndex = problemData.Dataset.GetVariableIndex(problemData.TargetVariable.Value);
|
---|
142 |
|
---|
143 | //extract SVM parameters from scope and set them
|
---|
144 | SVM.Parameter parameter = new SVM.Parameter();
|
---|
145 | parameter.SvmType = (SVM.SvmType)Enum.Parse(typeof(SVM.SvmType), svmType, true);
|
---|
146 | parameter.KernelType = (SVM.KernelType)Enum.Parse(typeof(SVM.KernelType), kernelType, true);
|
---|
147 | parameter.C = cost;
|
---|
148 | parameter.Nu = nu;
|
---|
149 | parameter.Gamma = gamma;
|
---|
150 | parameter.CacheSize = 500;
|
---|
151 | parameter.Probability = false;
|
---|
152 |
|
---|
153 |
|
---|
154 | SVM.Problem problem = SupportVectorMachineUtil.CreateSvmProblem(problemData, problemData.TrainingSamplesStart.Value, problemData.TrainingSamplesEnd.Value);
|
---|
155 | SVM.RangeTransform rangeTransform = SVM.RangeTransform.Compute(problem);
|
---|
156 | SVM.Problem scaledProblem = Scaling.Scale(rangeTransform, problem);
|
---|
157 | var model = new SupportVectorMachineModel();
|
---|
158 |
|
---|
159 | model.Model = SVM.Training.Train(scaledProblem, parameter);
|
---|
160 | model.RangeTransform = rangeTransform;
|
---|
161 |
|
---|
162 | return model;
|
---|
163 | }
|
---|
164 | }
|
---|
165 | }
|
---|