Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added hard-coded algorithm for SVR with best model selection on the validation set. #624 (Algorithm for support vector regression)

File size: 11.9 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.GP.StructureIdentification;
33using HeuristicLab.Logging;
34using HeuristicLab.Operators.Programmable;
35
36namespace HeuristicLab.SupportVectorMachines {
37  public class SupportVectorRegression : ItemBase, IEditable {
38    private SequentialEngine.SequentialEngine engine;
39    public IEngine Engine {
40      get { return engine; }
41    }
42
43    public IOperator ProblemInjector {
44      get;
45      set;
46    }
47    public DoubleArrayData NuList {
48      get { return GetVariableInjector().GetVariable("NuList").GetValue<DoubleArrayData>(); }
49      set { GetVariableInjector().GetVariable("NuList").Value = value; }
50    }
51
52    public int MaxNuIndex {
53      get { return GetVariableInjector().GetVariable("MaxNuIndex").GetValue<IntData>().Data; }
54      set { GetVariableInjector().GetVariable("MaxNuIndex").GetValue<IntData>().Data = value; }
55    }
56
57    public DoubleArrayData CostList {
58      get { return GetVariableInjector().GetVariable("CostList").GetValue<DoubleArrayData>(); }
59      set { GetVariableInjector().GetVariable("CostList").Value = value; }
60    }
61
62    public int MaxCostIndex {
63      get { return GetVariableInjector().GetVariable("MaxCostIndex").GetValue<IntData>().Data; }
64      set { GetVariableInjector().GetVariable("MaxCostIndex").GetValue<IntData>().Data = value; }
65    }
66
67    public SupportVectorRegression() {
68      engine = new SequentialEngine.SequentialEngine();
69      ProblemInjector = new EmptyOperator();
70      CombinedOperator algo = CreateAlgorithm();
71      engine.OperatorGraph.AddOperator(algo);
72      engine.OperatorGraph.InitialOperator = algo;
73      MaxCostIndex = CostList.Data.Length;
74      MaxNuIndex = NuList.Data.Length;
75    }
76
77    private CombinedOperator CreateAlgorithm() {
78      CombinedOperator algo = new CombinedOperator();
79      algo.Name = "SupportVectorRegression";
80      IOperator main = CreateMainLoop();
81      algo.OperatorGraph.AddOperator(main);
82      algo.OperatorGraph.InitialOperator = main;
83      return algo;
84    }
85
86    private IOperator CreateMainLoop() {
87      SequentialProcessor main = new SequentialProcessor();
88      main.AddSubOperator(CreateGlobalInjector());
89      main.AddSubOperator(ProblemInjector);
90
91      SequentialProcessor nuLoop = new SequentialProcessor();
92      nuLoop.Name = "NuLoop";
93      SequentialProcessor costLoop = new SequentialProcessor();
94      costLoop.Name = "CostLoop";
95      main.AddSubOperator(nuLoop);
96      nuLoop.AddSubOperator(CreateResetOperator("CostIndex"));
97      nuLoop.AddSubOperator(costLoop);
98      SubScopesCreater modelScopeCreator = new SubScopesCreater();
99      modelScopeCreator.GetVariableInfo("SubScopes").Local = true;
100      modelScopeCreator.AddVariable(new HeuristicLab.Core.Variable("SubScopes", new IntData(1)));
101      costLoop.AddSubOperator(modelScopeCreator);
102      SequentialSubScopesProcessor subScopesProcessor = new SequentialSubScopesProcessor();
103      costLoop.AddSubOperator(subScopesProcessor);
104      SequentialProcessor modelProcessor = new SequentialProcessor();
105      subScopesProcessor.AddSubOperator(modelProcessor);
106      modelProcessor.AddSubOperator(CreateSetNextParameterValueOperator("Nu"));
107      modelProcessor.AddSubOperator(CreateSetNextParameterValueOperator("Cost"));
108
109      SupportVectorCreator modelCreator = new SupportVectorCreator();
110      modelCreator.GetVariableInfo("SamplesStart").ActualName = "TrainingSamplesStart";
111      modelCreator.GetVariableInfo("SamplesEnd").ActualName = "TrainingSamplesEnd";
112      modelCreator.GetVariableInfo("SVMCost").ActualName = "Cost";
113      modelCreator.GetVariableInfo("SVMGamma").ActualName = "Gamma";
114      modelCreator.GetVariableInfo("SVMKernelType").ActualName = "KernelType";
115      modelCreator.GetVariableInfo("SVMModel").ActualName = "Model";
116      modelCreator.GetVariableInfo("SVMNu").ActualName = "Nu";
117      modelCreator.GetVariableInfo("SVMRangeTransform").ActualName = "RangeTransform";
118      modelCreator.GetVariableInfo("SVMType").ActualName = "Type";
119     
120
121      modelProcessor.AddSubOperator(modelCreator);
122      modelProcessor.AddSubOperator(CreateEvaluator("Training"));
123      modelProcessor.AddSubOperator(CreateEvaluator("Validation"));
124      modelProcessor.AddSubOperator(CreateEvaluator("Test"));
125
126      DataCollector collector = new DataCollector();
127      collector.GetVariableInfo("Values").ActualName = "Log";
128      ((ItemList<StringData>)collector.GetVariable("VariableNames").Value).Add(new StringData("Nu"));
129      ((ItemList<StringData>)collector.GetVariable("VariableNames").Value).Add(new StringData("Cost"));
130      ((ItemList<StringData>)collector.GetVariable("VariableNames").Value).Add(new StringData("ValidationMSE"));
131      modelProcessor.AddSubOperator(collector);
132
133      BestSolutionStorer solStorer = new BestSolutionStorer();
134      solStorer.GetVariableInfo("Quality").ActualName = "ValidationMSE";
135      solStorer.GetVariableInfo("Maximization").Local = true;
136      solStorer.AddVariable(new HeuristicLab.Core.Variable("Maximization", new BoolData(false)));
137
138      costLoop.AddSubOperator(solStorer);
139      SubScopesRemover remover = new SubScopesRemover();
140      costLoop.AddSubOperator(remover);
141      costLoop.AddSubOperator(CreateCounter("Cost"));
142      costLoop.AddSubOperator(CreateComparator("Cost"));
143      ConditionalBranch costBranch = new ConditionalBranch();
144      costBranch.Name = "CostLoop";
145      costBranch.GetVariableInfo("Condition").ActualName = "RepeatCostLoop";
146      costBranch.AddSubOperator(costLoop);
147      costLoop.AddSubOperator(costBranch);
148
149      nuLoop.AddSubOperator(CreateCounter("Nu"));
150      nuLoop.AddSubOperator(CreateComparator("Nu"));
151      ConditionalBranch nuBranch = new ConditionalBranch();
152      nuBranch.Name = "NuLoop";
153      nuBranch.GetVariableInfo("Condition").ActualName = "RepeatNuLoop";
154      nuBranch.AddSubOperator(nuLoop);
155      nuLoop.AddSubOperator(nuBranch);
156      return main;
157    }
158
159    private IOperator CreateComparator(string p) {
160      LessThanComparator comparator = new LessThanComparator();
161      comparator.Name = p + "IndexComparator";
162      comparator.GetVariableInfo("LeftSide").ActualName = p + "Index";
163      comparator.GetVariableInfo("RightSide").ActualName = "Max" + p + "Index";
164      comparator.GetVariableInfo("Result").ActualName = "Repeat" + p + "Loop";
165      return comparator;
166    }
167
168    private IOperator CreateCounter(string p) {
169      Counter c = new Counter();
170      c.GetVariableInfo("Value").ActualName = p + "Index";
171      c.Name = p + "Counter";
172      return c;
173    }
174
175    private IOperator CreateEvaluator(string p) {
176      CombinedOperator op = new CombinedOperator();
177      op.Name = p + "Evaluator";
178      SequentialProcessor seqProc = new SequentialProcessor();
179     
180      SupportVectorEvaluator evaluator = new SupportVectorEvaluator();
181      evaluator.Name = p + "SimpleEvaluator";
182      evaluator.GetVariableInfo("SVMModel").ActualName = "Model";
183      evaluator.GetVariableInfo("SVMRangeTransform").ActualName = "RangeTransform";
184      evaluator.GetVariableInfo("SamplesStart").ActualName = p + "SamplesStart";
185      evaluator.GetVariableInfo("SamplesEnd").ActualName = p + "SamplesEnd";
186      evaluator.GetVariableInfo("Values").ActualName = p + "Values";
187      SimpleMSEEvaluator mseEvaluator = new SimpleMSEEvaluator();
188      mseEvaluator.Name = p + "MseEvaluator";
189      mseEvaluator.GetVariableInfo("Values").ActualName = p + "Values";
190      mseEvaluator.GetVariableInfo("MSE").ActualName = p + "MSE";
191      SimpleR2Evaluator r2Evaluator = new SimpleR2Evaluator();
192      r2Evaluator.Name = p + "R2Evaluator";
193      r2Evaluator.GetVariableInfo("Values").ActualName = p + "Values";
194      r2Evaluator.GetVariableInfo("R2").ActualName = p + "R2";
195
196      seqProc.AddSubOperator(evaluator);
197      seqProc.AddSubOperator(mseEvaluator);
198      seqProc.AddSubOperator(r2Evaluator);
199
200      op.OperatorGraph.AddOperator(seqProc);
201      op.OperatorGraph.InitialOperator = seqProc;
202      return op;
203    }
204
205    private IOperator CreateSetNextParameterValueOperator(string paramName) {
206      ProgrammableOperator progOp = new ProgrammableOperator();
207      progOp.Name = "SetNext" + paramName;
208      progOp.RemoveVariableInfo("Result");
209      progOp.AddVariableInfo(new VariableInfo("Value", "Value", typeof(DoubleData), VariableKind.New));
210      progOp.AddVariableInfo(new VariableInfo("ValueIndex", "ValueIndex", typeof(IntData), VariableKind.In));
211      progOp.AddVariableInfo(new VariableInfo("ValueList", "ValueList", typeof(DoubleArrayData), VariableKind.In));
212      progOp.Code =
213@"
214Value.Data = ValueList.Data[ValueIndex.Data];
215";
216
217      progOp.GetVariableInfo("Value").ActualName = paramName;
218      progOp.GetVariableInfo("ValueIndex").ActualName = paramName + "Index";
219      progOp.GetVariableInfo("ValueList").ActualName = paramName + "List";
220      return progOp;
221    }
222
223    private IOperator CreateResetOperator(string paramName) {
224      ProgrammableOperator progOp = new ProgrammableOperator();
225      progOp.Name = "Reset" + paramName;
226      progOp.RemoveVariableInfo("Result");
227      progOp.AddVariableInfo(new VariableInfo("Value", "Value", typeof(IntData), VariableKind.In | VariableKind.Out));
228      progOp.Code ="Value.Data = 0;";
229      progOp.GetVariableInfo("Value").ActualName = paramName;
230      return progOp;
231    }
232
233    private IOperator CreateGlobalInjector() {
234      VariableInjector injector = new VariableInjector();
235      injector.AddVariable(new HeuristicLab.Core.Variable("CostIndex", new IntData(0)));
236      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 })));
237      injector.AddVariable(new HeuristicLab.Core.Variable("MaxCostIndex", new IntData()));
238      injector.AddVariable(new HeuristicLab.Core.Variable("NuIndex", new IntData(0)));
239      injector.AddVariable(new HeuristicLab.Core.Variable("NuList", new DoubleArrayData(new double[] { 0.01, 0.05, 0.1, 0.5, 0.9 })));
240      injector.AddVariable(new HeuristicLab.Core.Variable("MaxNuIndex", new IntData()));
241      injector.AddVariable(new HeuristicLab.Core.Variable("Log", new ItemList()));
242      injector.AddVariable(new HeuristicLab.Core.Variable("Gamma", new DoubleData(1)));
243      injector.AddVariable(new HeuristicLab.Core.Variable("KernelType", new StringData("RBF")));
244      injector.AddVariable(new HeuristicLab.Core.Variable("Type", new StringData("NU_SVR")));
245
246      return injector;
247    }
248
249    private IOperator GetVariableInjector() {
250      CombinedOperator svm = (CombinedOperator)Engine.OperatorGraph.InitialOperator;
251      return svm.OperatorGraph.InitialOperator.SubOperators[0];
252    }
253
254    public override IView CreateView() {
255      return engine.CreateView();
256    }
257
258    #region IEditable Members
259
260    public IEditor CreateEditor() {
261      return engine.CreateEditor();
262    }
263
264    #endregion
265  }
266}
Note: See TracBrowser for help on using the repository browser.