Free cookie consent management tool by TermsFeed Policy Generator

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

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

Worked on different dispatching of deterministic and non-deterministic modeling algorithms. #635

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