1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Optimization;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 | using HeuristicLab.Problems.DataAnalysis;
|
---|
30 | using HeuristicLab.Problems.DataAnalysis.Evaluators;
|
---|
31 | using HeuristicLab.Problems.DataAnalysis.Regression.SupportVectorRegression;
|
---|
32 | using HeuristicLab.Problems.DataAnalysis.SupportVectorMachine;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
35 | /// <summary>
|
---|
36 | /// A support vector machine.
|
---|
37 | /// </summary>
|
---|
38 | [Item("Support Vector Machine", "Support vector machine data analysis algorithm.")]
|
---|
39 | [Creatable("Data Analysis")]
|
---|
40 | [StorableClass]
|
---|
41 | public sealed class SupportVectorMachine : EngineAlgorithm, IStorableContent {
|
---|
42 | private const string TrainingSamplesStartParameterName = "Training start";
|
---|
43 | private const string TrainingSamplesEndParameterName = "Training end";
|
---|
44 | private const string DataAnalysisProblemDataParameterName = "DataAnalysisProblemData";
|
---|
45 | private const string SvmTypeParameterName = "SvmType";
|
---|
46 | private const string KernelTypeParameterName = "KernelType";
|
---|
47 | private const string CostParameterName = "Cost";
|
---|
48 | private const string NuParameterName = "Nu";
|
---|
49 | private const string GammaParameterName = "Gamma";
|
---|
50 | private const string EpsilonParameterName = "Epsilon";
|
---|
51 | private const string ModelParameterName = "SupportVectorMachineModel";
|
---|
52 |
|
---|
53 | public string Filename { get; set; }
|
---|
54 |
|
---|
55 | #region Problem Properties
|
---|
56 | public override Type ProblemType {
|
---|
57 | get { return typeof(DataAnalysisProblem); }
|
---|
58 | }
|
---|
59 | public new DataAnalysisProblem Problem {
|
---|
60 | get { return (DataAnalysisProblem)base.Problem; }
|
---|
61 | set { base.Problem = value; }
|
---|
62 | }
|
---|
63 | #endregion
|
---|
64 |
|
---|
65 | #region parameter properties
|
---|
66 | public IValueParameter<IntValue> TrainingSamplesStartParameter {
|
---|
67 | get { return (IValueParameter<IntValue>)Parameters[TrainingSamplesStartParameterName]; }
|
---|
68 | }
|
---|
69 | public IValueParameter<IntValue> TrainingSamplesEndParameter {
|
---|
70 | get { return (IValueParameter<IntValue>)Parameters[TrainingSamplesEndParameterName]; }
|
---|
71 | }
|
---|
72 | public IValueParameter<StringValue> SvmTypeParameter {
|
---|
73 | get { return (IValueParameter<StringValue>)Parameters[SvmTypeParameterName]; }
|
---|
74 | }
|
---|
75 | public IValueParameter<StringValue> KernelTypeParameter {
|
---|
76 | get { return (IValueParameter<StringValue>)Parameters[KernelTypeParameterName]; }
|
---|
77 | }
|
---|
78 | public IValueParameter<DoubleValue> NuParameter {
|
---|
79 | get { return (IValueParameter<DoubleValue>)Parameters[NuParameterName]; }
|
---|
80 | }
|
---|
81 | public IValueParameter<DoubleValue> CostParameter {
|
---|
82 | get { return (IValueParameter<DoubleValue>)Parameters[CostParameterName]; }
|
---|
83 | }
|
---|
84 | public IValueParameter<DoubleValue> GammaParameter {
|
---|
85 | get { return (IValueParameter<DoubleValue>)Parameters[GammaParameterName]; }
|
---|
86 | }
|
---|
87 | public IValueParameter<DoubleValue> EpsilonParameter {
|
---|
88 | get { return (IValueParameter<DoubleValue>)Parameters[EpsilonParameterName]; }
|
---|
89 | }
|
---|
90 | #endregion
|
---|
91 |
|
---|
92 | [Storable]
|
---|
93 | private SupportVectorMachineModelCreator solutionCreator;
|
---|
94 | [Storable]
|
---|
95 | private SupportVectorMachineModelEvaluator evaluator;
|
---|
96 | [Storable]
|
---|
97 | private SimpleMSEEvaluator mseEvaluator;
|
---|
98 | [Storable]
|
---|
99 | private BestSupportVectorRegressionSolutionAnalyzer analyzer;
|
---|
100 | public SupportVectorMachine()
|
---|
101 | : base() {
|
---|
102 | #region svm types
|
---|
103 | StringValue cSvcType = new StringValue("C_SVC").AsReadOnly();
|
---|
104 | StringValue nuSvcType = new StringValue("NU_SVC").AsReadOnly();
|
---|
105 | StringValue eSvrType = new StringValue("EPSILON_SVR").AsReadOnly();
|
---|
106 | StringValue nuSvrType = new StringValue("NU_SVR").AsReadOnly();
|
---|
107 | ItemSet<StringValue> allowedSvmTypes = new ItemSet<StringValue>();
|
---|
108 | allowedSvmTypes.Add(cSvcType);
|
---|
109 | allowedSvmTypes.Add(nuSvcType);
|
---|
110 | allowedSvmTypes.Add(eSvrType);
|
---|
111 | allowedSvmTypes.Add(nuSvrType);
|
---|
112 | #endregion
|
---|
113 | #region kernel types
|
---|
114 | StringValue rbfKernelType = new StringValue("RBF").AsReadOnly();
|
---|
115 | StringValue linearKernelType = new StringValue("LINEAR").AsReadOnly();
|
---|
116 | StringValue polynomialKernelType = new StringValue("POLY").AsReadOnly();
|
---|
117 | StringValue sigmoidKernelType = new StringValue("SIGMOID").AsReadOnly();
|
---|
118 | ItemSet<StringValue> allowedKernelTypes = new ItemSet<StringValue>();
|
---|
119 | allowedKernelTypes.Add(rbfKernelType);
|
---|
120 | allowedKernelTypes.Add(linearKernelType);
|
---|
121 | allowedKernelTypes.Add(polynomialKernelType);
|
---|
122 | allowedKernelTypes.Add(sigmoidKernelType);
|
---|
123 | #endregion
|
---|
124 | Parameters.Add(new ValueParameter<IntValue>(TrainingSamplesStartParameterName, "The first index of the data set partition to use for training."));
|
---|
125 | Parameters.Add(new ValueParameter<IntValue>(TrainingSamplesEndParameterName, "The last index of the data set partition to use for training."));
|
---|
126 | Parameters.Add(new ConstrainedValueParameter<StringValue>(SvmTypeParameterName, "The type of SVM to use.", allowedSvmTypes, nuSvrType));
|
---|
127 | Parameters.Add(new ConstrainedValueParameter<StringValue>(KernelTypeParameterName, "The kernel type to use for the SVM.", allowedKernelTypes, rbfKernelType));
|
---|
128 | Parameters.Add(new ValueParameter<DoubleValue>(NuParameterName, "The value of the nu parameter nu-SVC, one-class SVM and nu-SVR.", new DoubleValue(0.5)));
|
---|
129 | Parameters.Add(new ValueParameter<DoubleValue>(CostParameterName, "The value of the C (cost) parameter of C-SVC, epsilon-SVR and nu-SVR.", new DoubleValue(1.0)));
|
---|
130 | Parameters.Add(new ValueParameter<DoubleValue>(GammaParameterName, "The value of the gamma parameter in the kernel function.", new DoubleValue(1.0)));
|
---|
131 | Parameters.Add(new ValueLookupParameter<DoubleValue>(EpsilonParameterName, "The value of the epsilon parameter (only for epsilon-SVR).", new DoubleValue(1.0)));
|
---|
132 |
|
---|
133 | solutionCreator = new SupportVectorMachineModelCreator();
|
---|
134 | evaluator = new SupportVectorMachineModelEvaluator();
|
---|
135 | mseEvaluator = new SimpleMSEEvaluator();
|
---|
136 | analyzer = new BestSupportVectorRegressionSolutionAnalyzer();
|
---|
137 |
|
---|
138 | OperatorGraph.InitialOperator = solutionCreator;
|
---|
139 | solutionCreator.Successor = evaluator;
|
---|
140 | evaluator.Successor = mseEvaluator;
|
---|
141 | mseEvaluator.Successor = analyzer;
|
---|
142 |
|
---|
143 | Initialize();
|
---|
144 | }
|
---|
145 | [StorableConstructor]
|
---|
146 | private SupportVectorMachine(bool deserializing) : base(deserializing) { }
|
---|
147 | [StorableHook(HookType.AfterDeserialization)]
|
---|
148 | private void AfterDeserialization() {
|
---|
149 | Initialize();
|
---|
150 | }
|
---|
151 |
|
---|
152 | private SupportVectorMachine(SupportVectorMachine original, Cloner cloner)
|
---|
153 | : base(original, cloner) {
|
---|
154 | solutionCreator = cloner.Clone(original.solutionCreator);
|
---|
155 | evaluator = cloner.Clone(original.evaluator);
|
---|
156 | mseEvaluator = cloner.Clone(original.mseEvaluator);
|
---|
157 | analyzer = cloner.Clone(original.analyzer);
|
---|
158 | Initialize();
|
---|
159 | }
|
---|
160 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
161 | return new SupportVectorMachine(this, cloner);
|
---|
162 | }
|
---|
163 |
|
---|
164 | public override void Prepare() {
|
---|
165 | if (Problem != null) base.Prepare();
|
---|
166 | }
|
---|
167 |
|
---|
168 | protected override void Problem_Reset(object sender, EventArgs e) {
|
---|
169 | UpdateAlgorithmParameters();
|
---|
170 | base.Problem_Reset(sender, e);
|
---|
171 | }
|
---|
172 |
|
---|
173 | #region Events
|
---|
174 | protected override void OnProblemChanged() {
|
---|
175 | solutionCreator.DataAnalysisProblemDataParameter.ActualName = Problem.DataAnalysisProblemDataParameter.Name;
|
---|
176 | evaluator.DataAnalysisProblemDataParameter.ActualName = Problem.DataAnalysisProblemDataParameter.Name;
|
---|
177 | analyzer.ProblemDataParameter.ActualName = Problem.DataAnalysisProblemDataParameter.Name;
|
---|
178 | UpdateAlgorithmParameters();
|
---|
179 | Problem.Reset += new EventHandler(Problem_Reset);
|
---|
180 | base.OnProblemChanged();
|
---|
181 | }
|
---|
182 |
|
---|
183 | #endregion
|
---|
184 |
|
---|
185 | #region Helpers
|
---|
186 | private void Initialize() {
|
---|
187 | solutionCreator.SvmTypeParameter.ActualName = SvmTypeParameter.Name;
|
---|
188 | solutionCreator.KernelTypeParameter.ActualName = KernelTypeParameter.Name;
|
---|
189 | solutionCreator.CostParameter.ActualName = CostParameter.Name;
|
---|
190 | solutionCreator.GammaParameter.ActualName = GammaParameter.Name;
|
---|
191 | solutionCreator.NuParameter.ActualName = NuParameter.Name;
|
---|
192 | solutionCreator.SamplesStartParameter.ActualName = TrainingSamplesStartParameter.Name;
|
---|
193 | solutionCreator.SamplesEndParameter.ActualName = TrainingSamplesEndParameter.Name;
|
---|
194 |
|
---|
195 | evaluator.SamplesStartParameter.ActualName = TrainingSamplesStartParameter.Name;
|
---|
196 | evaluator.SamplesEndParameter.ActualName = TrainingSamplesEndParameter.Name;
|
---|
197 | evaluator.SupportVectorMachineModelParameter.ActualName = solutionCreator.SupportVectorMachineModelParameter.ActualName;
|
---|
198 | evaluator.ValuesParameter.ActualName = "Training values";
|
---|
199 |
|
---|
200 | mseEvaluator.ValuesParameter.ActualName = "Training values";
|
---|
201 | mseEvaluator.MeanSquaredErrorParameter.ActualName = "Training MSE";
|
---|
202 |
|
---|
203 | analyzer.SupportVectorRegressionModelParameter.ActualName = solutionCreator.SupportVectorMachineModelParameter.ActualName;
|
---|
204 | analyzer.SupportVectorRegressionModelParameter.Depth = 0;
|
---|
205 | analyzer.QualityParameter.ActualName = mseEvaluator.MeanSquaredErrorParameter.ActualName;
|
---|
206 | analyzer.QualityParameter.Depth = 0;
|
---|
207 |
|
---|
208 | if (Problem != null) {
|
---|
209 | solutionCreator.DataAnalysisProblemDataParameter.ActualName = Problem.DataAnalysisProblemDataParameter.Name;
|
---|
210 | evaluator.DataAnalysisProblemDataParameter.ActualName = Problem.DataAnalysisProblemDataParameter.Name;
|
---|
211 | analyzer.ProblemDataParameter.ActualName = Problem.DataAnalysisProblemDataParameter.Name;
|
---|
212 | Problem.Reset += new EventHandler(Problem_Reset);
|
---|
213 | }
|
---|
214 | }
|
---|
215 |
|
---|
216 | private void UpdateAlgorithmParameters() {
|
---|
217 | TrainingSamplesStartParameter.ActualValue = Problem.DataAnalysisProblemData.TrainingSamplesStart;
|
---|
218 | TrainingSamplesEndParameter.ActualValue = Problem.DataAnalysisProblemData.TrainingSamplesEnd;
|
---|
219 | }
|
---|
220 | #endregion
|
---|
221 | }
|
---|
222 | }
|
---|