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