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