Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.SupportVectorMachines/3.2/SupportVectorRegression.cs @ 2363

Last change on this file since 2363 was 2363, checked in by gkronber, 15 years ago

Fixed problems with classification algorithms. #746 (CEDMA server is not compatible with new data-modeling algorithms)

File size: 19.8 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Core;
27using System.Xml;
28using System.Diagnostics;
29using HeuristicLab.DataAnalysis;
30using HeuristicLab.Data;
31using HeuristicLab.Operators;
32using HeuristicLab.Logging;
33using HeuristicLab.Operators.Programmable;
34using HeuristicLab.Modeling;
35using HeuristicLab.Random;
36using HeuristicLab.Selection;
37
38namespace HeuristicLab.SupportVectorMachines {
39  public class SupportVectorRegression : ItemBase, IEditable, IAlgorithm {
40
41    public virtual string Name { get { return "SupportVectorRegression"; } }
42    public virtual string Description { get { return "TODO"; } }
43
44    private SequentialEngine.SequentialEngine engine;
45    public IEngine Engine {
46      get { return engine; }
47    }
48
49    public Dataset Dataset {
50      get { return ProblemInjector.GetVariableValue<Dataset>("Dataset", null, false); }
51      set { ProblemInjector.GetVariable("Dataset").Value = value; }
52    }
53
54    public int TargetVariable {
55      get { return ProblemInjector.GetVariableValue<IntData>("TargetVariable", null, false).Data; }
56      set { ProblemInjector.GetVariableValue<IntData>("TargetVariable", null, false).Data = value; }
57    }
58
59    public IOperator ProblemInjector {
60      get {
61        IOperator main = GetMainOperator();
62        CombinedOperator probInject = (CombinedOperator)main.SubOperators[0].SubOperators[2];
63        return probInject.OperatorGraph.InitialOperator.SubOperators[0];
64      }
65      set {
66        IOperator main = GetMainOperator();
67        CombinedOperator probInject = (CombinedOperator)main.SubOperators[0].SubOperators[2];
68        probInject.OperatorGraph.InitialOperator.RemoveSubOperator(0);
69        probInject.OperatorGraph.InitialOperator.AddSubOperator(value, 0);
70      }
71    }
72
73    public IAnalyzerModel Model {
74      get {
75        if (!engine.Terminated) throw new InvalidOperationException("The algorithm is still running. Wait until the algorithm is terminated to retrieve the result.");
76        IScope bestModelScope = engine.GlobalScope.SubScopes[0];
77        return CreateSVMModel(bestModelScope);
78      }
79    }
80
81    public DoubleArrayData NuList {
82      get { return GetVariableInjector().GetVariable("NuList").GetValue<DoubleArrayData>(); }
83      set { GetVariableInjector().GetVariable("NuList").Value = value; }
84    }
85
86    public int MaxNuIndex {
87      get { return GetVariableInjector().GetVariable("MaxNuIndex").GetValue<IntData>().Data; }
88      set { GetVariableInjector().GetVariable("MaxNuIndex").GetValue<IntData>().Data = value; }
89    }
90
91    public DoubleArrayData CostList {
92      get { return GetVariableInjector().GetVariable("CostList").GetValue<DoubleArrayData>(); }
93      set { GetVariableInjector().GetVariable("CostList").Value = value; }
94    }
95
96    public int MaxCostIndex {
97      get { return GetVariableInjector().GetVariable("MaxCostIndex").GetValue<IntData>().Data; }
98      set { GetVariableInjector().GetVariable("MaxCostIndex").GetValue<IntData>().Data = value; }
99    }
100
101    public DoubleArrayData GammaList {
102      get { return GetVariableInjector().GetVariable("GammaList").GetValue<DoubleArrayData>(); }
103      set { GetVariableInjector().GetVariable("GammaList").Value = value; }
104    }
105
106    public int MaxGammaIndex {
107      get { return GetVariableInjector().GetVariable("MaxGammaIndex").GetValue<IntData>().Data; }
108      set { GetVariableInjector().GetVariable("MaxGammaIndex").GetValue<IntData>().Data = value; }
109    }
110
111    public string SvmType {
112      get { return GetVariableInjector().GetVariable("Type").GetValue<StringData>().Data; }
113      set { GetVariableInjector().GetVariable("Type").GetValue<StringData>().Data = value; }
114    }
115
116    public SupportVectorRegression() {
117      engine = new SequentialEngine.SequentialEngine();
118      CombinedOperator algo = CreateAlgorithm();
119      engine.OperatorGraph.AddOperator(algo);
120      engine.OperatorGraph.InitialOperator = algo;
121      MaxCostIndex = CostList.Data.Length;
122      MaxNuIndex = NuList.Data.Length;
123      MaxGammaIndex = GammaList.Data.Length;
124    }
125
126    private CombinedOperator CreateAlgorithm() {
127      CombinedOperator algo = new CombinedOperator();
128      SequentialProcessor seq = new SequentialProcessor();
129      algo.Name = Name;
130      seq.Name = Name;
131
132      IOperator initialization = CreateInitialization();
133      IOperator main = CreateMainLoop();
134      IOperator postProc = CreatePostProcessingOperator();
135
136      seq.AddSubOperator(initialization);
137      seq.AddSubOperator(main);
138      seq.AddSubOperator(postProc);
139
140      algo.OperatorGraph.InitialOperator = seq;
141      algo.OperatorGraph.AddOperator(seq);
142
143      return algo;
144    }
145
146    protected virtual IOperator CreateInitialization() {
147      SequentialProcessor seq = new SequentialProcessor();
148      seq.Name = "Initialization";
149      seq.AddSubOperator(new RandomInjector());
150      seq.AddSubOperator(CreateGlobalInjector());
151      seq.AddSubOperator(CreateProblemInjector());
152      return seq;
153    }
154
155    protected virtual IOperator CreateProblemInjector() {
156      return DefaultRegressionOperators.CreateProblemInjector();
157    }
158
159    private IOperator CreateMainLoop() {
160      SequentialProcessor main = new SequentialProcessor();
161      main.Name = "Main";
162      #region initial solution
163      SubScopesCreater modelScopeCreator = new SubScopesCreater();
164      modelScopeCreator.GetVariableInfo("SubScopes").Local = true;
165      modelScopeCreator.AddVariable(new HeuristicLab.Core.Variable("SubScopes", new IntData(1)));
166      main.AddSubOperator(modelScopeCreator);
167
168      SequentialSubScopesProcessor seqSubScopesProc = new SequentialSubScopesProcessor();
169      IOperator modelProcessor = CreateModelProcessor();
170
171      seqSubScopesProc.AddSubOperator(modelProcessor);
172      main.AddSubOperator(seqSubScopesProc);
173      #endregion
174
175      SequentialProcessor nuLoop = new SequentialProcessor();
176      nuLoop.Name = "NuLoop";
177
178      SequentialProcessor gammaLoop = new SequentialProcessor();
179      gammaLoop.Name = "GammaLoop";
180
181      nuLoop.AddSubOperator(gammaLoop);
182
183      IOperator costCounter = CreateCounter("Cost");
184      IOperator costComparator = CreateComparator("Cost");
185      gammaLoop.AddSubOperator(costCounter);
186      gammaLoop.AddSubOperator(costComparator);
187
188      ConditionalBranch costBranch = new ConditionalBranch();
189      costBranch.Name = "IfValidCostIndex";
190      costBranch.GetVariableInfo("Condition").ActualName = "RepeatCostLoop";
191
192      // build cost loop
193      SequentialProcessor costLoop = new SequentialProcessor();
194      costLoop.Name = "CostLoop";
195
196      #region selection of better solution
197      costLoop.AddSubOperator(modelScopeCreator);
198      SequentialSubScopesProcessor subScopesProcessor = new SequentialSubScopesProcessor();
199      costLoop.AddSubOperator(subScopesProcessor);
200      subScopesProcessor.AddSubOperator(new EmptyOperator());
201      subScopesProcessor.AddSubOperator(modelProcessor);
202
203      Sorter sorter = new Sorter();
204      sorter.GetVariableInfo("Value").ActualName = "ValidationQuality";
205      sorter.GetVariableInfo("Descending").Local = true;
206      sorter.AddVariable(new Variable("Descending", new BoolData(false)));
207      costLoop.AddSubOperator(sorter);
208
209      LeftSelector selector = new LeftSelector();
210      selector.GetVariableInfo("Selected").Local = true;
211      selector.AddVariable(new Variable("Selected", new IntData(1)));
212      costLoop.AddSubOperator(selector);
213
214      RightReducer reducer = new RightReducer();
215      costLoop.AddSubOperator(reducer);
216      #endregion
217
218      costLoop.AddSubOperator(costCounter);
219      costLoop.AddSubOperator(costComparator);
220
221      costBranch.AddSubOperator(costLoop);
222      costLoop.AddSubOperator(costBranch);
223
224      gammaLoop.AddSubOperator(costBranch); // inner loop
225      gammaLoop.AddSubOperator(CreateResetOperator("CostIndex", -1));
226      gammaLoop.AddSubOperator(CreateCounter("Gamma"));
227      gammaLoop.AddSubOperator(CreateComparator("Gamma"));
228
229      ConditionalBranch gammaBranch = new ConditionalBranch();
230      gammaBranch.Name = "GammaLoop";
231      gammaBranch.GetVariableInfo("Condition").ActualName = "RepeatGammaLoop";
232      gammaBranch.AddSubOperator(gammaLoop);
233      gammaLoop.AddSubOperator(gammaBranch);
234
235      nuLoop.AddSubOperator(CreateResetOperator("GammaIndex", 0));
236
237      nuLoop.AddSubOperator(CreateCounter("Nu"));
238      nuLoop.AddSubOperator(CreateComparator("Nu"));
239
240      ConditionalBranch nuBranch = new ConditionalBranch();
241      nuBranch.Name = "NuLoop";
242      nuBranch.GetVariableInfo("Condition").ActualName = "RepeatNuLoop";
243
244      nuBranch.AddSubOperator(nuLoop);
245      nuLoop.AddSubOperator(nuBranch);
246
247      main.AddSubOperator(nuLoop);
248      return main;
249    }
250
251    private IOperator CreateModelProcessor() {
252      SequentialProcessor modelProcessor = new SequentialProcessor();
253      modelProcessor.AddSubOperator(CreateSetNextParameterValueOperator("Nu"));
254      modelProcessor.AddSubOperator(CreateSetNextParameterValueOperator("Cost"));
255      modelProcessor.AddSubOperator(CreateSetNextParameterValueOperator("Gamma"));
256
257      SupportVectorCreator modelCreator = new SupportVectorCreator();
258      modelCreator.GetVariableInfo("SamplesStart").ActualName = "ActualTrainingSamplesStart";
259      modelCreator.GetVariableInfo("SamplesEnd").ActualName = "ActualTrainingSamplesEnd";
260      modelCreator.GetVariableInfo("SVMCost").ActualName = "Cost";
261      modelCreator.GetVariableInfo("SVMGamma").ActualName = "Gamma";
262      modelCreator.GetVariableInfo("SVMKernelType").ActualName = "KernelType";
263      modelCreator.GetVariableInfo("SVMModel").ActualName = "Model";
264      modelCreator.GetVariableInfo("SVMNu").ActualName = "Nu";
265      modelCreator.GetVariableInfo("SVMType").ActualName = "Type";
266
267      modelProcessor.AddSubOperator(modelCreator);
268      CombinedOperator trainingEvaluator = (CombinedOperator)CreateEvaluator("ActualTraining");
269      trainingEvaluator.OperatorGraph.InitialOperator.SubOperators[1].GetVariableInfo("MSE").ActualName = "Quality";
270      modelProcessor.AddSubOperator(trainingEvaluator);
271      modelProcessor.AddSubOperator(CreateEvaluator("Validation"));
272
273      DataCollector collector = new DataCollector();
274      collector.GetVariableInfo("Values").ActualName = "Log";
275      ((ItemList<StringData>)collector.GetVariable("VariableNames").Value).Add(new StringData("Nu"));
276      ((ItemList<StringData>)collector.GetVariable("VariableNames").Value).Add(new StringData("Cost"));
277      ((ItemList<StringData>)collector.GetVariable("VariableNames").Value).Add(new StringData("Gamma"));
278      ((ItemList<StringData>)collector.GetVariable("VariableNames").Value).Add(new StringData("ValidationQuality"));
279      modelProcessor.AddSubOperator(collector);
280      return modelProcessor;
281    }
282
283    private IOperator CreateComparator(string p) {
284      LessThanComparator comparator = new LessThanComparator();
285      comparator.Name = p + "IndexComparator";
286      comparator.GetVariableInfo("LeftSide").ActualName = p + "Index";
287      comparator.GetVariableInfo("RightSide").ActualName = "Max" + p + "Index";
288      comparator.GetVariableInfo("Result").ActualName = "Repeat" + p + "Loop";
289      return comparator;
290    }
291
292    private IOperator CreateCounter(string p) {
293      Counter c = new Counter();
294      c.GetVariableInfo("Value").ActualName = p + "Index";
295      c.Name = p + "Counter";
296      return c;
297    }
298
299    protected virtual IOperator CreateEvaluator(string p) {
300      CombinedOperator op = new CombinedOperator();
301      op.Name = p + "Evaluator";
302      SequentialProcessor seqProc = new SequentialProcessor();
303
304      SupportVectorEvaluator evaluator = new SupportVectorEvaluator();
305      evaluator.Name = p + "SimpleEvaluator";
306      evaluator.GetVariableInfo("SVMModel").ActualName = "Model";
307      evaluator.GetVariableInfo("SamplesStart").ActualName = p + "SamplesStart";
308      evaluator.GetVariableInfo("SamplesEnd").ActualName = p + "SamplesEnd";
309      evaluator.GetVariableInfo("Values").ActualName = p + "Values";
310      SimpleMSEEvaluator mseEvaluator = new SimpleMSEEvaluator();
311      mseEvaluator.Name = p + "MseEvaluator";
312      mseEvaluator.GetVariableInfo("Values").ActualName = p + "Values";
313      mseEvaluator.GetVariableInfo("MSE").ActualName = p + "Quality";
314      SimpleR2Evaluator r2Evaluator = new SimpleR2Evaluator();
315      r2Evaluator.Name = p + "R2Evaluator";
316      r2Evaluator.GetVariableInfo("Values").ActualName = p + "Values";
317      r2Evaluator.GetVariableInfo("R2").ActualName = p + "R2";
318      SimpleMeanAbsolutePercentageErrorEvaluator mapeEvaluator = new SimpleMeanAbsolutePercentageErrorEvaluator();
319      mapeEvaluator.Name = p + "MAPEEvaluator";
320      mapeEvaluator.GetVariableInfo("Values").ActualName = p + "Values";
321      mapeEvaluator.GetVariableInfo("MAPE").ActualName = p + "MAPE";
322      SimpleMeanAbsolutePercentageOfRangeErrorEvaluator mapreEvaluator = new SimpleMeanAbsolutePercentageOfRangeErrorEvaluator();
323      mapreEvaluator.Name = p + "MAPREEvaluator";
324      mapreEvaluator.GetVariableInfo("Values").ActualName = p + "Values";
325      mapreEvaluator.GetVariableInfo("MAPRE").ActualName = p + "MAPRE";
326      SimpleVarianceAccountedForEvaluator vafEvaluator = new SimpleVarianceAccountedForEvaluator();
327      vafEvaluator.Name = p + "VAFEvaluator";
328      vafEvaluator.GetVariableInfo("Values").ActualName = p + "Values";
329      vafEvaluator.GetVariableInfo("VAF").ActualName = p + "VAF";
330
331      seqProc.AddSubOperator(evaluator);
332      seqProc.AddSubOperator(mseEvaluator);
333      seqProc.AddSubOperator(r2Evaluator);
334      seqProc.AddSubOperator(mapeEvaluator);
335      seqProc.AddSubOperator(mapreEvaluator);
336      seqProc.AddSubOperator(vafEvaluator);
337
338      op.OperatorGraph.AddOperator(seqProc);
339      op.OperatorGraph.InitialOperator = seqProc;
340      return op;
341    }
342
343    private IOperator CreateSetNextParameterValueOperator(string paramName) {
344      ProgrammableOperator progOp = new ProgrammableOperator();
345      progOp.Name = "SetNext" + paramName;
346      progOp.RemoveVariableInfo("Result");
347      progOp.AddVariableInfo(new VariableInfo("Value", "Value", typeof(DoubleData), VariableKind.New));
348      progOp.AddVariableInfo(new VariableInfo("ValueIndex", "ValueIndex", typeof(IntData), VariableKind.In));
349      progOp.AddVariableInfo(new VariableInfo("ValueList", "ValueList", typeof(DoubleArrayData), VariableKind.In));
350      progOp.Code =
351@"
352Value.Data = ValueList.Data[ValueIndex.Data];
353";
354
355      progOp.GetVariableInfo("Value").ActualName = paramName;
356      progOp.GetVariableInfo("ValueIndex").ActualName = paramName + "Index";
357      progOp.GetVariableInfo("ValueList").ActualName = paramName + "List";
358      return progOp;
359    }
360
361    private IOperator CreateResetOperator(string paramName, int value) {
362      ProgrammableOperator progOp = new ProgrammableOperator();
363      progOp.Name = "Reset" + paramName;
364      progOp.RemoveVariableInfo("Result");
365      progOp.AddVariableInfo(new VariableInfo("Value", "Value", typeof(IntData), VariableKind.In | VariableKind.Out));
366      progOp.Code = "Value.Data = " + value + ";";
367      progOp.GetVariableInfo("Value").ActualName = paramName;
368      return progOp;
369    }
370
371    protected virtual VariableInjector CreateGlobalInjector() {
372      VariableInjector injector = new VariableInjector();
373      injector.AddVariable(new HeuristicLab.Core.Variable("CostIndex", new IntData(0)));
374      injector.AddVariable(new HeuristicLab.Core.Variable("CostList", new DoubleArrayData(new double[] {
375        Math.Pow(2,-5),
376        Math.Pow(2,-3),
377        Math.Pow(2,-1),
378        2,
379        Math.Pow(2,3),
380        Math.Pow(2,5),
381        Math.Pow(2,7),
382        Math.Pow(2,9),
383        Math.Pow(2,11),
384        Math.Pow(2,13),
385        Math.Pow(2,15)})));
386      injector.AddVariable(new HeuristicLab.Core.Variable("MaxCostIndex", new IntData()));
387      injector.AddVariable(new HeuristicLab.Core.Variable("NuIndex", new IntData(0)));
388      injector.AddVariable(new HeuristicLab.Core.Variable("NuList", new DoubleArrayData(new double[] { 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.9, 0.8, 1 })));
389      injector.AddVariable(new HeuristicLab.Core.Variable("MaxNuIndex", new IntData()));
390      injector.AddVariable(new HeuristicLab.Core.Variable("Log", new ItemList()));
391      injector.AddVariable(new HeuristicLab.Core.Variable("GammaIndex", new IntData(0)));
392      injector.AddVariable(new HeuristicLab.Core.Variable("GammaList", new DoubleArrayData(new double[] {
393        3.0517578125E-05, 0.0001220703125,0.00048828125,0.001953125,
394        0.0078125,0.03125,0.125,0.5,2,4,8})));
395      injector.AddVariable(new HeuristicLab.Core.Variable("MaxGammaIndex", new IntData()));
396      injector.AddVariable(new HeuristicLab.Core.Variable("KernelType", new StringData("RBF")));
397      injector.AddVariable(new HeuristicLab.Core.Variable("Type", new StringData("NU_SVR")));
398      injector.AddVariable(new HeuristicLab.Core.Variable("PunishmentFactor", new DoubleData(1000.0)));
399      return injector;
400    }
401
402    protected virtual IOperator CreatePostProcessingOperator() {
403      CombinedOperator modelAnalyser = new CombinedOperator();
404      modelAnalyser.Name = "Model Analyzer";
405      SequentialSubScopesProcessor seqSubScopeProc = new SequentialSubScopesProcessor();
406      SequentialProcessor seqProc = new SequentialProcessor();
407
408      PredictorBuilder predictorBuilder = new PredictorBuilder();
409      predictorBuilder.GetVariableInfo("SVMModel").ActualName = "Model";
410
411      seqProc.AddSubOperator(CreateEvaluator("Test"));
412      seqProc.AddSubOperator(CreateEvaluator("Training"));
413      seqProc.AddSubOperator(predictorBuilder);
414      seqProc.AddSubOperator(CreateModelAnalyzerOperator());
415
416      seqSubScopeProc.AddSubOperator(seqProc);
417      modelAnalyser.OperatorGraph.InitialOperator = seqSubScopeProc;
418      modelAnalyser.OperatorGraph.AddOperator(seqSubScopeProc);
419      return modelAnalyser;
420    }
421
422    protected virtual IOperator CreateModelAnalyzerOperator() {
423      return DefaultRegressionOperators.CreatePostProcessingOperator();
424    }
425
426
427    protected virtual IAnalyzerModel CreateSVMModel(IScope bestModelScope) {
428      AnalyzerModel model = new AnalyzerModel();
429      model.SetMetaData("Cost", bestModelScope.GetVariableValue<DoubleData>("Cost", false).Data);
430      model.SetMetaData("Nu", bestModelScope.GetVariableValue<DoubleData>("Nu", false).Data);
431      DefaultRegressionOperators.PopulateAnalyzerModel(bestModelScope, model);
432
433      return model;
434    }
435
436    protected IOperator GetVariableInjector() {
437      return GetMainOperator().SubOperators[0].SubOperators[1];
438    }
439
440    protected IOperator GetMainOperator() {
441      CombinedOperator svm = (CombinedOperator)Engine.OperatorGraph.InitialOperator;
442      return svm.OperatorGraph.InitialOperator;
443    }
444
445    public override IView CreateView() {
446      return engine.CreateView();
447    }
448
449    #region IEditable Members
450
451    public IEditor CreateEditor() {
452      return engine.CreateEditor();
453    }
454
455    #endregion
456  }
457}
Note: See TracBrowser for help on using the repository browser.