1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2008 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 System.Xml;
|
---|
28 | using System.Diagnostics;
|
---|
29 | using HeuristicLab.DataAnalysis;
|
---|
30 | using HeuristicLab.Data;
|
---|
31 | using HeuristicLab.Operators;
|
---|
32 | using HeuristicLab.GP.StructureIdentification;
|
---|
33 | using HeuristicLab.Logging;
|
---|
34 | using HeuristicLab.Operators.Programmable;
|
---|
35 | using HeuristicLab.Modeling;
|
---|
36 |
|
---|
37 | namespace HeuristicLab.SupportVectorMachines {
|
---|
38 | public class SupportVectorRegression : ItemBase, IEditable, IAlgorithm {
|
---|
39 |
|
---|
40 | public string Name { get { return "SupportVectorRegression"; } }
|
---|
41 | public string Description { get { return "TODO"; } }
|
---|
42 |
|
---|
43 | private SequentialEngine.SequentialEngine engine;
|
---|
44 | public IEngine Engine {
|
---|
45 | get { return engine; }
|
---|
46 | }
|
---|
47 |
|
---|
48 | public Dataset Dataset {
|
---|
49 | get { return ProblemInjector.GetVariableValue<Dataset>("Dataset", null, false); }
|
---|
50 | set { ProblemInjector.GetVariable("Dataset").Value = value; }
|
---|
51 | }
|
---|
52 |
|
---|
53 | public int TargetVariable {
|
---|
54 | get { return ProblemInjector.GetVariableValue<IntData>("TargetVariable", null, false).Data; }
|
---|
55 | set { ProblemInjector.GetVariableValue<IntData>("TargetVariable", null, false).Data = value; }
|
---|
56 | }
|
---|
57 |
|
---|
58 | public IOperator ProblemInjector {
|
---|
59 | get {
|
---|
60 | IOperator main = GetMainOperator();
|
---|
61 | return main.SubOperators[1];
|
---|
62 | }
|
---|
63 | set {
|
---|
64 | IOperator main = GetMainOperator();
|
---|
65 | main.RemoveSubOperator(1);
|
---|
66 | main.AddSubOperator(value, 1);
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | public IModel Model {
|
---|
71 | get {
|
---|
72 | if (!engine.Terminated) throw new InvalidOperationException("The algorithm is still running. Wait until the algorithm is terminated to retrieve the result.");
|
---|
73 | IScope bestModelScope = engine.GlobalScope.GetVariableValue<IScope>("BestValidationSolution", false);
|
---|
74 | return CreateSVMModel(bestModelScope);
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | public DoubleArrayData NuList {
|
---|
79 | get { return GetVariableInjector().GetVariable("NuList").GetValue<DoubleArrayData>(); }
|
---|
80 | set { GetVariableInjector().GetVariable("NuList").Value = value; }
|
---|
81 | }
|
---|
82 |
|
---|
83 | public int MaxNuIndex {
|
---|
84 | get { return GetVariableInjector().GetVariable("MaxNuIndex").GetValue<IntData>().Data; }
|
---|
85 | set { GetVariableInjector().GetVariable("MaxNuIndex").GetValue<IntData>().Data = value; }
|
---|
86 | }
|
---|
87 |
|
---|
88 | public DoubleArrayData CostList {
|
---|
89 | get { return GetVariableInjector().GetVariable("CostList").GetValue<DoubleArrayData>(); }
|
---|
90 | set { GetVariableInjector().GetVariable("CostList").Value = value; }
|
---|
91 | }
|
---|
92 |
|
---|
93 | public int MaxCostIndex {
|
---|
94 | get { return GetVariableInjector().GetVariable("MaxCostIndex").GetValue<IntData>().Data; }
|
---|
95 | set { GetVariableInjector().GetVariable("MaxCostIndex").GetValue<IntData>().Data = value; }
|
---|
96 | }
|
---|
97 |
|
---|
98 | public SupportVectorRegression() {
|
---|
99 | engine = new SequentialEngine.SequentialEngine();
|
---|
100 | CombinedOperator algo = CreateAlgorithm();
|
---|
101 | engine.OperatorGraph.AddOperator(algo);
|
---|
102 | engine.OperatorGraph.InitialOperator = algo;
|
---|
103 | MaxCostIndex = CostList.Data.Length;
|
---|
104 | MaxNuIndex = NuList.Data.Length;
|
---|
105 | }
|
---|
106 |
|
---|
107 | private CombinedOperator CreateAlgorithm() {
|
---|
108 | CombinedOperator algo = new CombinedOperator();
|
---|
109 | algo.Name = "SupportVectorRegression";
|
---|
110 | IOperator main = CreateMainLoop();
|
---|
111 | algo.OperatorGraph.AddOperator(main);
|
---|
112 | algo.OperatorGraph.InitialOperator = main;
|
---|
113 | return algo;
|
---|
114 | }
|
---|
115 |
|
---|
116 | private IOperator CreateMainLoop() {
|
---|
117 | SequentialProcessor main = new SequentialProcessor();
|
---|
118 | main.AddSubOperator(CreateGlobalInjector());
|
---|
119 | main.AddSubOperator(new ProblemInjector());
|
---|
120 |
|
---|
121 | SequentialProcessor nuLoop = new SequentialProcessor();
|
---|
122 | nuLoop.Name = "NuLoop";
|
---|
123 | SequentialProcessor costLoop = new SequentialProcessor();
|
---|
124 | costLoop.Name = "CostLoop";
|
---|
125 | main.AddSubOperator(nuLoop);
|
---|
126 | nuLoop.AddSubOperator(CreateResetOperator("CostIndex"));
|
---|
127 | nuLoop.AddSubOperator(costLoop);
|
---|
128 | SubScopesCreater modelScopeCreator = new SubScopesCreater();
|
---|
129 | modelScopeCreator.GetVariableInfo("SubScopes").Local = true;
|
---|
130 | modelScopeCreator.AddVariable(new HeuristicLab.Core.Variable("SubScopes", new IntData(1)));
|
---|
131 | costLoop.AddSubOperator(modelScopeCreator);
|
---|
132 | SequentialSubScopesProcessor subScopesProcessor = new SequentialSubScopesProcessor();
|
---|
133 | costLoop.AddSubOperator(subScopesProcessor);
|
---|
134 | SequentialProcessor modelProcessor = new SequentialProcessor();
|
---|
135 | subScopesProcessor.AddSubOperator(modelProcessor);
|
---|
136 | modelProcessor.AddSubOperator(CreateSetNextParameterValueOperator("Nu"));
|
---|
137 | modelProcessor.AddSubOperator(CreateSetNextParameterValueOperator("Cost"));
|
---|
138 |
|
---|
139 | SupportVectorCreator modelCreator = new SupportVectorCreator();
|
---|
140 | modelCreator.GetVariableInfo("SamplesStart").ActualName = "TrainingSamplesStart";
|
---|
141 | modelCreator.GetVariableInfo("SamplesEnd").ActualName = "TrainingSamplesEnd";
|
---|
142 | modelCreator.GetVariableInfo("SVMCost").ActualName = "Cost";
|
---|
143 | modelCreator.GetVariableInfo("SVMGamma").ActualName = "Gamma";
|
---|
144 | modelCreator.GetVariableInfo("SVMKernelType").ActualName = "KernelType";
|
---|
145 | modelCreator.GetVariableInfo("SVMModel").ActualName = "Model";
|
---|
146 | modelCreator.GetVariableInfo("SVMNu").ActualName = "Nu";
|
---|
147 | modelCreator.GetVariableInfo("SVMType").ActualName = "Type";
|
---|
148 |
|
---|
149 | modelProcessor.AddSubOperator(modelCreator);
|
---|
150 | CombinedOperator trainingEvaluator = (CombinedOperator)CreateEvaluator("Training");
|
---|
151 | trainingEvaluator.OperatorGraph.InitialOperator.SubOperators[1].GetVariableInfo("MSE").ActualName = "Quality";
|
---|
152 | modelProcessor.AddSubOperator(trainingEvaluator);
|
---|
153 | modelProcessor.AddSubOperator(CreateEvaluator("Validation"));
|
---|
154 | modelProcessor.AddSubOperator(CreateEvaluator("Test"));
|
---|
155 |
|
---|
156 | DataCollector collector = new DataCollector();
|
---|
157 | collector.GetVariableInfo("Values").ActualName = "Log";
|
---|
158 | ((ItemList<StringData>)collector.GetVariable("VariableNames").Value).Add(new StringData("Nu"));
|
---|
159 | ((ItemList<StringData>)collector.GetVariable("VariableNames").Value).Add(new StringData("Cost"));
|
---|
160 | ((ItemList<StringData>)collector.GetVariable("VariableNames").Value).Add(new StringData("ValidationQuality"));
|
---|
161 | modelProcessor.AddSubOperator(collector);
|
---|
162 |
|
---|
163 | BestSolutionStorer solStorer = new BestSolutionStorer();
|
---|
164 | solStorer.GetVariableInfo("Quality").ActualName = "ValidationQuality";
|
---|
165 | solStorer.GetVariableInfo("Maximization").Local = true;
|
---|
166 | solStorer.GetVariableInfo("BestSolution").ActualName = "BestValidationSolution";
|
---|
167 | solStorer.AddVariable(new HeuristicLab.Core.Variable("Maximization", new BoolData(false)));
|
---|
168 |
|
---|
169 | costLoop.AddSubOperator(solStorer);
|
---|
170 | SubScopesRemover remover = new SubScopesRemover();
|
---|
171 | costLoop.AddSubOperator(remover);
|
---|
172 | costLoop.AddSubOperator(CreateCounter("Cost"));
|
---|
173 | costLoop.AddSubOperator(CreateComparator("Cost"));
|
---|
174 | ConditionalBranch costBranch = new ConditionalBranch();
|
---|
175 | costBranch.Name = "CostLoop";
|
---|
176 | costBranch.GetVariableInfo("Condition").ActualName = "RepeatCostLoop";
|
---|
177 | costBranch.AddSubOperator(costLoop);
|
---|
178 | costLoop.AddSubOperator(costBranch);
|
---|
179 |
|
---|
180 | nuLoop.AddSubOperator(CreateCounter("Nu"));
|
---|
181 | nuLoop.AddSubOperator(CreateComparator("Nu"));
|
---|
182 | ConditionalBranch nuBranch = new ConditionalBranch();
|
---|
183 | nuBranch.Name = "NuLoop";
|
---|
184 | nuBranch.GetVariableInfo("Condition").ActualName = "RepeatNuLoop";
|
---|
185 | nuBranch.AddSubOperator(nuLoop);
|
---|
186 | nuLoop.AddSubOperator(nuBranch);
|
---|
187 | return main;
|
---|
188 | }
|
---|
189 |
|
---|
190 | private IOperator CreateComparator(string p) {
|
---|
191 | LessThanComparator comparator = new LessThanComparator();
|
---|
192 | comparator.Name = p + "IndexComparator";
|
---|
193 | comparator.GetVariableInfo("LeftSide").ActualName = p + "Index";
|
---|
194 | comparator.GetVariableInfo("RightSide").ActualName = "Max" + p + "Index";
|
---|
195 | comparator.GetVariableInfo("Result").ActualName = "Repeat" + p + "Loop";
|
---|
196 | return comparator;
|
---|
197 | }
|
---|
198 |
|
---|
199 | private IOperator CreateCounter(string p) {
|
---|
200 | Counter c = new Counter();
|
---|
201 | c.GetVariableInfo("Value").ActualName = p + "Index";
|
---|
202 | c.Name = p + "Counter";
|
---|
203 | return c;
|
---|
204 | }
|
---|
205 |
|
---|
206 | private IOperator CreateEvaluator(string p) {
|
---|
207 | CombinedOperator op = new CombinedOperator();
|
---|
208 | op.Name = p + "Evaluator";
|
---|
209 | SequentialProcessor seqProc = new SequentialProcessor();
|
---|
210 |
|
---|
211 | SupportVectorEvaluator evaluator = new SupportVectorEvaluator();
|
---|
212 | evaluator.Name = p + "SimpleEvaluator";
|
---|
213 | evaluator.GetVariableInfo("SVMModel").ActualName = "Model";
|
---|
214 | evaluator.GetVariableInfo("SamplesStart").ActualName = p + "SamplesStart";
|
---|
215 | evaluator.GetVariableInfo("SamplesEnd").ActualName = p + "SamplesEnd";
|
---|
216 | evaluator.GetVariableInfo("Values").ActualName = p + "Values";
|
---|
217 | SimpleMSEEvaluator mseEvaluator = new SimpleMSEEvaluator();
|
---|
218 | mseEvaluator.Name = p + "MseEvaluator";
|
---|
219 | mseEvaluator.GetVariableInfo("Values").ActualName = p + "Values";
|
---|
220 | mseEvaluator.GetVariableInfo("MSE").ActualName = p + "Quality";
|
---|
221 | SimpleR2Evaluator r2Evaluator = new SimpleR2Evaluator();
|
---|
222 | r2Evaluator.Name = p + "R2Evaluator";
|
---|
223 | r2Evaluator.GetVariableInfo("Values").ActualName = p + "Values";
|
---|
224 | r2Evaluator.GetVariableInfo("R2").ActualName = p + "R2";
|
---|
225 | SimpleMeanAbsolutePercentageErrorEvaluator mapeEvaluator = new SimpleMeanAbsolutePercentageErrorEvaluator();
|
---|
226 | mapeEvaluator.Name = p + "MAPEEvaluator";
|
---|
227 | mapeEvaluator.GetVariableInfo("Values").ActualName = p + "Values";
|
---|
228 | mapeEvaluator.GetVariableInfo("MAPE").ActualName = p + "MAPE";
|
---|
229 | SimpleMeanAbsolutePercentageOfRangeErrorEvaluator mapreEvaluator = new SimpleMeanAbsolutePercentageOfRangeErrorEvaluator();
|
---|
230 | mapreEvaluator.Name = p + "MAPREEvaluator";
|
---|
231 | mapreEvaluator.GetVariableInfo("Values").ActualName = p + "Values";
|
---|
232 | mapreEvaluator.GetVariableInfo("MAPRE").ActualName = p + "MAPRE";
|
---|
233 | SimpleVarianceAccountedForEvaluator vafEvaluator = new SimpleVarianceAccountedForEvaluator();
|
---|
234 | vafEvaluator.Name = p + "VAFEvaluator";
|
---|
235 | vafEvaluator.GetVariableInfo("Values").ActualName = p + "Values";
|
---|
236 | vafEvaluator.GetVariableInfo("VAF").ActualName = p + "VAF";
|
---|
237 |
|
---|
238 | seqProc.AddSubOperator(evaluator);
|
---|
239 | seqProc.AddSubOperator(mseEvaluator);
|
---|
240 | seqProc.AddSubOperator(r2Evaluator);
|
---|
241 | seqProc.AddSubOperator(mapeEvaluator);
|
---|
242 | seqProc.AddSubOperator(mapreEvaluator);
|
---|
243 | seqProc.AddSubOperator(vafEvaluator);
|
---|
244 |
|
---|
245 | op.OperatorGraph.AddOperator(seqProc);
|
---|
246 | op.OperatorGraph.InitialOperator = seqProc;
|
---|
247 | return op;
|
---|
248 | }
|
---|
249 |
|
---|
250 | private IOperator CreateSetNextParameterValueOperator(string paramName) {
|
---|
251 | ProgrammableOperator progOp = new ProgrammableOperator();
|
---|
252 | progOp.Name = "SetNext" + paramName;
|
---|
253 | progOp.RemoveVariableInfo("Result");
|
---|
254 | progOp.AddVariableInfo(new VariableInfo("Value", "Value", typeof(DoubleData), VariableKind.New));
|
---|
255 | progOp.AddVariableInfo(new VariableInfo("ValueIndex", "ValueIndex", typeof(IntData), VariableKind.In));
|
---|
256 | progOp.AddVariableInfo(new VariableInfo("ValueList", "ValueList", typeof(DoubleArrayData), VariableKind.In));
|
---|
257 | progOp.Code =
|
---|
258 | @"
|
---|
259 | Value.Data = ValueList.Data[ValueIndex.Data];
|
---|
260 | ";
|
---|
261 |
|
---|
262 | progOp.GetVariableInfo("Value").ActualName = paramName;
|
---|
263 | progOp.GetVariableInfo("ValueIndex").ActualName = paramName + "Index";
|
---|
264 | progOp.GetVariableInfo("ValueList").ActualName = paramName + "List";
|
---|
265 | return progOp;
|
---|
266 | }
|
---|
267 |
|
---|
268 | private IOperator CreateResetOperator(string paramName) {
|
---|
269 | ProgrammableOperator progOp = new ProgrammableOperator();
|
---|
270 | progOp.Name = "Reset" + paramName;
|
---|
271 | progOp.RemoveVariableInfo("Result");
|
---|
272 | progOp.AddVariableInfo(new VariableInfo("Value", "Value", typeof(IntData), VariableKind.In | VariableKind.Out));
|
---|
273 | progOp.Code = "Value.Data = 0;";
|
---|
274 | progOp.GetVariableInfo("Value").ActualName = paramName;
|
---|
275 | return progOp;
|
---|
276 | }
|
---|
277 |
|
---|
278 | private IOperator CreateGlobalInjector() {
|
---|
279 | VariableInjector injector = new VariableInjector();
|
---|
280 | injector.AddVariable(new HeuristicLab.Core.Variable("CostIndex", new IntData(0)));
|
---|
281 | injector.AddVariable(new HeuristicLab.Core.Variable("CostList", new DoubleArrayData(new double[] { 0.1, 0.25, 0.5, 1.0, 2.0, 4.0, 8.0, 16.0, 32.0, 64.0, 128.0 })));
|
---|
282 | injector.AddVariable(new HeuristicLab.Core.Variable("MaxCostIndex", new IntData()));
|
---|
283 | injector.AddVariable(new HeuristicLab.Core.Variable("NuIndex", new IntData(0)));
|
---|
284 | injector.AddVariable(new HeuristicLab.Core.Variable("NuList", new DoubleArrayData(new double[] { 0.01, 0.05, 0.1, 0.5, 0.9 })));
|
---|
285 | injector.AddVariable(new HeuristicLab.Core.Variable("MaxNuIndex", new IntData()));
|
---|
286 | injector.AddVariable(new HeuristicLab.Core.Variable("Log", new ItemList()));
|
---|
287 | injector.AddVariable(new HeuristicLab.Core.Variable("Gamma", new DoubleData(1)));
|
---|
288 | injector.AddVariable(new HeuristicLab.Core.Variable("KernelType", new StringData("RBF")));
|
---|
289 | injector.AddVariable(new HeuristicLab.Core.Variable("Type", new StringData("NU_SVR")));
|
---|
290 |
|
---|
291 | return injector;
|
---|
292 | }
|
---|
293 |
|
---|
294 | protected internal virtual Model CreateSVMModel(IScope bestModelScope) {
|
---|
295 | Model model = new Model();
|
---|
296 | model.TrainingMeanSquaredError = bestModelScope.GetVariableValue<DoubleData>("Quality", false).Data;
|
---|
297 | model.ValidationMeanSquaredError = bestModelScope.GetVariableValue<DoubleData>("ValidationQuality", false).Data;
|
---|
298 | model.TestMeanSquaredError = bestModelScope.GetVariableValue<DoubleData>("TestQuality", false).Data;
|
---|
299 | model.TrainingCoefficientOfDetermination = bestModelScope.GetVariableValue<DoubleData>("TrainingR2", false).Data;
|
---|
300 | model.ValidationCoefficientOfDetermination = bestModelScope.GetVariableValue<DoubleData>("ValidationR2", false).Data;
|
---|
301 | model.TestCoefficientOfDetermination = bestModelScope.GetVariableValue<DoubleData>("TestR2", false).Data;
|
---|
302 | model.TrainingMeanAbsolutePercentageError = bestModelScope.GetVariableValue<DoubleData>("TrainingMAPE", false).Data;
|
---|
303 | model.ValidationMeanAbsolutePercentageError = bestModelScope.GetVariableValue<DoubleData>("ValidationMAPE", false).Data;
|
---|
304 | model.TestMeanAbsolutePercentageError = bestModelScope.GetVariableValue<DoubleData>("TestMAPE", false).Data;
|
---|
305 | model.TrainingMeanAbsolutePercentageOfRangeError = bestModelScope.GetVariableValue<DoubleData>("TrainingMAPRE", false).Data;
|
---|
306 | model.ValidationMeanAbsolutePercentageOfRangeError = bestModelScope.GetVariableValue<DoubleData>("ValidationMAPRE", false).Data;
|
---|
307 | model.TestMeanAbsolutePercentageOfRangeError = bestModelScope.GetVariableValue<DoubleData>("TestMAPRE", false).Data;
|
---|
308 | model.TrainingVarianceAccountedFor = bestModelScope.GetVariableValue<DoubleData>("TrainingVAF", false).Data;
|
---|
309 | model.ValidationVarianceAccountedFor = bestModelScope.GetVariableValue<DoubleData>("ValidationVAF", false).Data;
|
---|
310 | model.TestVarianceAccountedFor = bestModelScope.GetVariableValue<DoubleData>("TestVAF", false).Data;
|
---|
311 |
|
---|
312 | model.Data = bestModelScope.GetVariableValue<SVMModel>("BestValidationModel", false);
|
---|
313 | HeuristicLab.DataAnalysis.Dataset ds = bestModelScope.GetVariableValue<Dataset>("Dataset", true);
|
---|
314 | model.Dataset = ds;
|
---|
315 | model.TargetVariable = ds.GetVariableName(bestModelScope.GetVariableValue<IntData>("TargetVariable", true).Data);
|
---|
316 | return model;
|
---|
317 | }
|
---|
318 |
|
---|
319 | private IOperator GetVariableInjector() {
|
---|
320 | return GetMainOperator().SubOperators[0];
|
---|
321 | }
|
---|
322 |
|
---|
323 | private IOperator GetMainOperator() {
|
---|
324 | CombinedOperator svm = (CombinedOperator)Engine.OperatorGraph.InitialOperator;
|
---|
325 | return svm.OperatorGraph.InitialOperator;
|
---|
326 | }
|
---|
327 |
|
---|
328 | public override IView CreateView() {
|
---|
329 | return engine.CreateView();
|
---|
330 | }
|
---|
331 |
|
---|
332 | #region IEditable Members
|
---|
333 |
|
---|
334 | public IEditor CreateEditor() {
|
---|
335 | return engine.CreateEditor();
|
---|
336 | }
|
---|
337 |
|
---|
338 | #endregion
|
---|
339 |
|
---|
340 | }
|
---|
341 | }
|
---|