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 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("ValidationMSE"));
|
---|
143 | modelProcessor.AddSubOperator(collector);
|
---|
144 |
|
---|
145 | BestSolutionStorer solStorer = new BestSolutionStorer();
|
---|
146 | solStorer.GetVariableInfo("Quality").ActualName = "ValidationMSE";
|
---|
147 | solStorer.GetVariableInfo("Maximization").Local = true;
|
---|
148 | solStorer.AddVariable(new HeuristicLab.Core.Variable("Maximization", new BoolData(false)));
|
---|
149 |
|
---|
150 | costLoop.AddSubOperator(solStorer);
|
---|
151 | SubScopesRemover remover = new SubScopesRemover();
|
---|
152 | costLoop.AddSubOperator(remover);
|
---|
153 | costLoop.AddSubOperator(CreateCounter("Cost"));
|
---|
154 | costLoop.AddSubOperator(CreateComparator("Cost"));
|
---|
155 | ConditionalBranch costBranch = new ConditionalBranch();
|
---|
156 | costBranch.Name = "CostLoop";
|
---|
157 | costBranch.GetVariableInfo("Condition").ActualName = "RepeatCostLoop";
|
---|
158 | costBranch.AddSubOperator(costLoop);
|
---|
159 | costLoop.AddSubOperator(costBranch);
|
---|
160 |
|
---|
161 | nuLoop.AddSubOperator(CreateCounter("Nu"));
|
---|
162 | nuLoop.AddSubOperator(CreateComparator("Nu"));
|
---|
163 | ConditionalBranch nuBranch = new ConditionalBranch();
|
---|
164 | nuBranch.Name = "NuLoop";
|
---|
165 | nuBranch.GetVariableInfo("Condition").ActualName = "RepeatNuLoop";
|
---|
166 | nuBranch.AddSubOperator(nuLoop);
|
---|
167 | nuLoop.AddSubOperator(nuBranch);
|
---|
168 | return main;
|
---|
169 | }
|
---|
170 |
|
---|
171 | private IOperator CreateComparator(string p) {
|
---|
172 | LessThanComparator comparator = new LessThanComparator();
|
---|
173 | comparator.Name = p + "IndexComparator";
|
---|
174 | comparator.GetVariableInfo("LeftSide").ActualName = p + "Index";
|
---|
175 | comparator.GetVariableInfo("RightSide").ActualName = "Max" + p + "Index";
|
---|
176 | comparator.GetVariableInfo("Result").ActualName = "Repeat" + p + "Loop";
|
---|
177 | return comparator;
|
---|
178 | }
|
---|
179 |
|
---|
180 | private IOperator CreateCounter(string p) {
|
---|
181 | Counter c = new Counter();
|
---|
182 | c.GetVariableInfo("Value").ActualName = p + "Index";
|
---|
183 | c.Name = p + "Counter";
|
---|
184 | return c;
|
---|
185 | }
|
---|
186 |
|
---|
187 | private IOperator CreateEvaluator(string p) {
|
---|
188 | CombinedOperator op = new CombinedOperator();
|
---|
189 | op.Name = p + "Evaluator";
|
---|
190 | SequentialProcessor seqProc = new SequentialProcessor();
|
---|
191 |
|
---|
192 | SupportVectorEvaluator evaluator = new SupportVectorEvaluator();
|
---|
193 | evaluator.Name = p + "SimpleEvaluator";
|
---|
194 | evaluator.GetVariableInfo("SVMModel").ActualName = "Model";
|
---|
195 | evaluator.GetVariableInfo("SVMRangeTransform").ActualName = "RangeTransform";
|
---|
196 | evaluator.GetVariableInfo("SamplesStart").ActualName = p + "SamplesStart";
|
---|
197 | evaluator.GetVariableInfo("SamplesEnd").ActualName = p + "SamplesEnd";
|
---|
198 | evaluator.GetVariableInfo("Values").ActualName = p + "Values";
|
---|
199 | SimpleMSEEvaluator mseEvaluator = new SimpleMSEEvaluator();
|
---|
200 | mseEvaluator.Name = p + "MseEvaluator";
|
---|
201 | mseEvaluator.GetVariableInfo("Values").ActualName = p + "Values";
|
---|
202 | mseEvaluator.GetVariableInfo("MSE").ActualName = p + "MSE";
|
---|
203 | SimpleR2Evaluator r2Evaluator = new SimpleR2Evaluator();
|
---|
204 | r2Evaluator.Name = p + "R2Evaluator";
|
---|
205 | r2Evaluator.GetVariableInfo("Values").ActualName = p + "Values";
|
---|
206 | r2Evaluator.GetVariableInfo("R2").ActualName = p + "R2";
|
---|
207 |
|
---|
208 | seqProc.AddSubOperator(evaluator);
|
---|
209 | seqProc.AddSubOperator(mseEvaluator);
|
---|
210 | seqProc.AddSubOperator(r2Evaluator);
|
---|
211 |
|
---|
212 | op.OperatorGraph.AddOperator(seqProc);
|
---|
213 | op.OperatorGraph.InitialOperator = seqProc;
|
---|
214 | return op;
|
---|
215 | }
|
---|
216 |
|
---|
217 | private IOperator CreateSetNextParameterValueOperator(string paramName) {
|
---|
218 | ProgrammableOperator progOp = new ProgrammableOperator();
|
---|
219 | progOp.Name = "SetNext" + paramName;
|
---|
220 | progOp.RemoveVariableInfo("Result");
|
---|
221 | progOp.AddVariableInfo(new VariableInfo("Value", "Value", typeof(DoubleData), VariableKind.New));
|
---|
222 | progOp.AddVariableInfo(new VariableInfo("ValueIndex", "ValueIndex", typeof(IntData), VariableKind.In));
|
---|
223 | progOp.AddVariableInfo(new VariableInfo("ValueList", "ValueList", typeof(DoubleArrayData), VariableKind.In));
|
---|
224 | progOp.Code =
|
---|
225 | @"
|
---|
226 | Value.Data = ValueList.Data[ValueIndex.Data];
|
---|
227 | ";
|
---|
228 |
|
---|
229 | progOp.GetVariableInfo("Value").ActualName = paramName;
|
---|
230 | progOp.GetVariableInfo("ValueIndex").ActualName = paramName + "Index";
|
---|
231 | progOp.GetVariableInfo("ValueList").ActualName = paramName + "List";
|
---|
232 | return progOp;
|
---|
233 | }
|
---|
234 |
|
---|
235 | private IOperator CreateResetOperator(string paramName) {
|
---|
236 | ProgrammableOperator progOp = new ProgrammableOperator();
|
---|
237 | progOp.Name = "Reset" + paramName;
|
---|
238 | progOp.RemoveVariableInfo("Result");
|
---|
239 | progOp.AddVariableInfo(new VariableInfo("Value", "Value", typeof(IntData), VariableKind.In | VariableKind.Out));
|
---|
240 | progOp.Code ="Value.Data = 0;";
|
---|
241 | progOp.GetVariableInfo("Value").ActualName = paramName;
|
---|
242 | return progOp;
|
---|
243 | }
|
---|
244 |
|
---|
245 | private IOperator CreateGlobalInjector() {
|
---|
246 | VariableInjector injector = new VariableInjector();
|
---|
247 | injector.AddVariable(new HeuristicLab.Core.Variable("CostIndex", new IntData(0)));
|
---|
248 | 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 })));
|
---|
249 | injector.AddVariable(new HeuristicLab.Core.Variable("MaxCostIndex", new IntData()));
|
---|
250 | injector.AddVariable(new HeuristicLab.Core.Variable("NuIndex", new IntData(0)));
|
---|
251 | injector.AddVariable(new HeuristicLab.Core.Variable("NuList", new DoubleArrayData(new double[] { 0.01, 0.05, 0.1, 0.5, 0.9 })));
|
---|
252 | injector.AddVariable(new HeuristicLab.Core.Variable("MaxNuIndex", new IntData()));
|
---|
253 | injector.AddVariable(new HeuristicLab.Core.Variable("Log", new ItemList()));
|
---|
254 | injector.AddVariable(new HeuristicLab.Core.Variable("Gamma", new DoubleData(1)));
|
---|
255 | injector.AddVariable(new HeuristicLab.Core.Variable("KernelType", new StringData("RBF")));
|
---|
256 | injector.AddVariable(new HeuristicLab.Core.Variable("Type", new StringData("NU_SVR")));
|
---|
257 |
|
---|
258 | return injector;
|
---|
259 | }
|
---|
260 |
|
---|
261 | private IOperator GetVariableInjector() {
|
---|
262 | return GetMainOperator().SubOperators[0];
|
---|
263 | }
|
---|
264 |
|
---|
265 | private IOperator GetMainOperator() {
|
---|
266 | CombinedOperator svm = (CombinedOperator)Engine.OperatorGraph.InitialOperator;
|
---|
267 | return svm.OperatorGraph.InitialOperator;
|
---|
268 | }
|
---|
269 |
|
---|
270 | public override IView CreateView() {
|
---|
271 | return engine.CreateView();
|
---|
272 | }
|
---|
273 |
|
---|
274 | #region IEditable Members
|
---|
275 |
|
---|
276 | public IEditor CreateEditor() {
|
---|
277 | return engine.CreateEditor();
|
---|
278 | }
|
---|
279 |
|
---|
280 | #endregion
|
---|
281 | }
|
---|
282 | }
|
---|