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.Xml;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.DataAnalysis;
|
---|
28 | using HeuristicLab.Evolutionary;
|
---|
29 | using HeuristicLab.GP.Interfaces;
|
---|
30 | using HeuristicLab.Logging;
|
---|
31 | using HeuristicLab.Modeling;
|
---|
32 | using HeuristicLab.Operators;
|
---|
33 | using HeuristicLab.Random;
|
---|
34 | using HeuristicLab.Selection;
|
---|
35 |
|
---|
36 | namespace HeuristicLab.GP.StructureIdentification {
|
---|
37 | public abstract class AlgorithmBase : ItemBase, IAlgorithm, IStochasticAlgorithm {
|
---|
38 | public virtual string Name { get { return "GP"; } }
|
---|
39 | public virtual string Description { get { return "TODO"; } }
|
---|
40 |
|
---|
41 | public abstract Dataset Dataset { get; set; }
|
---|
42 | public abstract int TargetVariable { get; set; }
|
---|
43 |
|
---|
44 | public virtual double MutationRate {
|
---|
45 | get { return GetVariableInjector().GetVariable("MutationRate").GetValue<DoubleData>().Data; }
|
---|
46 | set { GetVariableInjector().GetVariable("MutationRate").GetValue<DoubleData>().Data = value; }
|
---|
47 | }
|
---|
48 | public virtual int PopulationSize {
|
---|
49 | get { return GetVariableInjector().GetVariable("PopulationSize").GetValue<IntData>().Data; }
|
---|
50 | set { GetVariableInjector().GetVariable("PopulationSize").GetValue<IntData>().Data = value; }
|
---|
51 | }
|
---|
52 |
|
---|
53 | public virtual bool SetSeedRandomly {
|
---|
54 | get { return GetRandomInjector().GetVariable("SetSeedRandomly").GetValue<BoolData>().Data; }
|
---|
55 | set { GetRandomInjector().GetVariable("SetSeedRandomly").GetValue<BoolData>().Data = value; }
|
---|
56 | }
|
---|
57 |
|
---|
58 | public virtual int RandomSeed {
|
---|
59 | get { return GetRandomInjector().GetVariable("Seed").GetValue<IntData>().Data; }
|
---|
60 | set { GetRandomInjector().GetVariable("Seed").GetValue<IntData>().Data = value; }
|
---|
61 | }
|
---|
62 |
|
---|
63 | public virtual IOperator ProblemInjector {
|
---|
64 | get { return algorithm.SubOperators[1]; }
|
---|
65 | set {
|
---|
66 | value.Name = "ProblemInjector";
|
---|
67 | algorithm.RemoveSubOperator(1);
|
---|
68 | algorithm.AddSubOperator(value, 1);
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | private IModel model;
|
---|
73 | public virtual IModel Model {
|
---|
74 | get {
|
---|
75 | if (!engine.Terminated) throw new InvalidOperationException("The algorithm is still running. Wait until the algorithm is terminated to retrieve the result.");
|
---|
76 | if (model == null) {
|
---|
77 | IScope bestModelScope = engine.GlobalScope.GetVariableValue<IScope>("BestValidationSolution", false);
|
---|
78 | model = CreateGPModel(bestModelScope);
|
---|
79 | }
|
---|
80 | return model;
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | public virtual int Elites {
|
---|
85 | get { return GetVariableInjector().GetVariable("Elites").GetValue<IntData>().Data; }
|
---|
86 | set { GetVariableInjector().GetVariable("Elites").GetValue<IntData>().Data = value; }
|
---|
87 | }
|
---|
88 |
|
---|
89 | public virtual int MaxTreeSize {
|
---|
90 | get { return GetVariableInjector().GetVariable("MaxTreeSize").GetValue<IntData>().Data; }
|
---|
91 | set { GetVariableInjector().GetVariable("MaxTreeSize").GetValue<IntData>().Data = value; }
|
---|
92 | }
|
---|
93 |
|
---|
94 | public virtual int MaxTreeHeight {
|
---|
95 | get { return GetVariableInjector().GetVariable("MaxTreeHeight").GetValue<IntData>().Data; }
|
---|
96 | set { GetVariableInjector().GetVariable("MaxTreeHeight").GetValue<IntData>().Data = value; }
|
---|
97 | }
|
---|
98 |
|
---|
99 | public virtual int Parents {
|
---|
100 | get { return GetVariableInjector().GetVariable("Parents").GetValue<IntData>().Data; }
|
---|
101 | set { GetVariableInjector().GetVariable("Parents").GetValue<IntData>().Data = value; }
|
---|
102 | }
|
---|
103 |
|
---|
104 | public virtual double PunishmentFactor {
|
---|
105 | get { return GetVariableInjector().GetVariable("PunishmentFactor").GetValue<DoubleData>().Data; }
|
---|
106 | set { GetVariableInjector().GetVariable("PunishmentFactor").GetValue<DoubleData>().Data = value; }
|
---|
107 | }
|
---|
108 |
|
---|
109 | public virtual bool UseEstimatedTargetValue {
|
---|
110 | get { return GetVariableInjector().GetVariable("UseEstimatedTargetValue").GetValue<BoolData>().Data; }
|
---|
111 | set { GetVariableInjector().GetVariable("UseEstimatedTargetValue").GetValue<BoolData>().Data = value; }
|
---|
112 | }
|
---|
113 |
|
---|
114 | private IOperator algorithm;
|
---|
115 |
|
---|
116 | private SequentialEngine.SequentialEngine engine;
|
---|
117 | public IEngine Engine {
|
---|
118 | get { return engine; }
|
---|
119 | protected set { engine = (SequentialEngine.SequentialEngine)value; }
|
---|
120 | }
|
---|
121 |
|
---|
122 | public AlgorithmBase() {
|
---|
123 | engine = new SequentialEngine.SequentialEngine();
|
---|
124 | CombinedOperator algo = CreateAlgorithm();
|
---|
125 | engine.OperatorGraph.AddOperator(algo);
|
---|
126 | engine.OperatorGraph.InitialOperator = algo;
|
---|
127 | SetSeedRandomly = true;
|
---|
128 | Elites = 1;
|
---|
129 | MutationRate = 0.15;
|
---|
130 | PopulationSize = 1000;
|
---|
131 | MaxTreeSize = 100;
|
---|
132 | MaxTreeHeight = 10;
|
---|
133 | Parents = 2000;
|
---|
134 | PunishmentFactor = 10;
|
---|
135 | UseEstimatedTargetValue = false;
|
---|
136 | }
|
---|
137 |
|
---|
138 | protected internal virtual CombinedOperator CreateAlgorithm() {
|
---|
139 | CombinedOperator algo = new CombinedOperator();
|
---|
140 | algo.Name = "GP";
|
---|
141 | SequentialProcessor seq = new SequentialProcessor();
|
---|
142 | IOperator problemInjector = CreateProblemInjector();
|
---|
143 |
|
---|
144 | RandomInjector randomInjector = new RandomInjector();
|
---|
145 | randomInjector.Name = "Random Injector";
|
---|
146 |
|
---|
147 | IOperator globalInjector = CreateGlobalInjector();
|
---|
148 | IOperator initialization = CreateInitialization();
|
---|
149 | IOperator funLibInjector = CreateFunctionLibraryInjector();
|
---|
150 |
|
---|
151 | IOperator mainLoop = CreateMainLoop();
|
---|
152 | mainLoop.Name = "Main loop";
|
---|
153 |
|
---|
154 | IOperator treeCreator = CreateTreeCreator();
|
---|
155 |
|
---|
156 | MeanSquaredErrorEvaluator evaluator = new MeanSquaredErrorEvaluator();
|
---|
157 | evaluator.GetVariableInfo("MSE").ActualName = "Quality";
|
---|
158 | evaluator.GetVariableInfo("SamplesStart").ActualName = "ActualTrainingSamplesStart";
|
---|
159 | evaluator.GetVariableInfo("SamplesEnd").ActualName = "ActualTrainingSamplesEnd";
|
---|
160 | evaluator.Name = "Evaluator";
|
---|
161 |
|
---|
162 | IOperator crossover = CreateCrossover();
|
---|
163 | IOperator manipulator = CreateManipulator();
|
---|
164 |
|
---|
165 | IOperator selector = CreateSelector();
|
---|
166 | LeftReducer cleanUp = new LeftReducer();
|
---|
167 |
|
---|
168 | seq.AddSubOperator(randomInjector);
|
---|
169 | seq.AddSubOperator(problemInjector);
|
---|
170 | seq.AddSubOperator(globalInjector);
|
---|
171 | seq.AddSubOperator(funLibInjector);
|
---|
172 | seq.AddSubOperator(initialization);
|
---|
173 | seq.AddSubOperator(mainLoop);
|
---|
174 | seq.AddSubOperator(cleanUp);
|
---|
175 |
|
---|
176 | initialization.AddSubOperator(treeCreator);
|
---|
177 | initialization.AddSubOperator(evaluator);
|
---|
178 |
|
---|
179 | mainLoop.AddSubOperator(selector);
|
---|
180 | mainLoop.AddSubOperator(crossover);
|
---|
181 | mainLoop.AddSubOperator(manipulator);
|
---|
182 | mainLoop.AddSubOperator(evaluator);
|
---|
183 | algo.OperatorGraph.AddOperator(seq);
|
---|
184 | algo.OperatorGraph.InitialOperator = seq;
|
---|
185 | this.algorithm = seq;
|
---|
186 | return algo;
|
---|
187 | }
|
---|
188 |
|
---|
189 | protected internal virtual IOperator CreateProblemInjector() {
|
---|
190 | return new EmptyOperator();
|
---|
191 | }
|
---|
192 |
|
---|
193 | protected internal abstract IOperator CreateSelector();
|
---|
194 |
|
---|
195 | protected internal abstract IOperator CreateCrossover();
|
---|
196 |
|
---|
197 | protected internal abstract IOperator CreateTreeCreator();
|
---|
198 |
|
---|
199 | protected internal abstract IOperator CreateFunctionLibraryInjector();
|
---|
200 |
|
---|
201 | protected internal virtual IOperator CreateGlobalInjector() {
|
---|
202 | VariableInjector injector = new VariableInjector();
|
---|
203 | injector.Name = "Global Injector";
|
---|
204 | injector.AddVariable(new HeuristicLab.Core.Variable("Generations", new IntData(0)));
|
---|
205 | injector.AddVariable(new HeuristicLab.Core.Variable("MutationRate", new DoubleData()));
|
---|
206 | injector.AddVariable(new HeuristicLab.Core.Variable("PopulationSize", new IntData()));
|
---|
207 | injector.AddVariable(new HeuristicLab.Core.Variable("Elites", new IntData()));
|
---|
208 | injector.AddVariable(new HeuristicLab.Core.Variable("Maximization", new BoolData(false)));
|
---|
209 | injector.AddVariable(new HeuristicLab.Core.Variable("MaxTreeHeight", new IntData()));
|
---|
210 | injector.AddVariable(new HeuristicLab.Core.Variable("MaxTreeSize", new IntData()));
|
---|
211 | injector.AddVariable(new HeuristicLab.Core.Variable("EvaluatedSolutions", new IntData(0)));
|
---|
212 | injector.AddVariable(new HeuristicLab.Core.Variable("TotalEvaluatedNodes", new DoubleData(0)));
|
---|
213 | injector.AddVariable(new HeuristicLab.Core.Variable("Parents", new IntData()));
|
---|
214 | injector.AddVariable(new HeuristicLab.Core.Variable("PunishmentFactor", new DoubleData()));
|
---|
215 | injector.AddVariable(new HeuristicLab.Core.Variable("UseEstimatedTargetValue", new BoolData()));
|
---|
216 | injector.AddVariable(new HeuristicLab.Core.Variable("TreeEvaluator", new HL2TreeEvaluator()));
|
---|
217 | return injector;
|
---|
218 | }
|
---|
219 |
|
---|
220 | protected internal abstract IOperator CreateManipulator();
|
---|
221 |
|
---|
222 | protected internal virtual IOperator CreateInitialization() {
|
---|
223 | CombinedOperator init = new CombinedOperator();
|
---|
224 | init.Name = "Initialization";
|
---|
225 | SequentialProcessor seq = new SequentialProcessor();
|
---|
226 | SubScopesCreater subScopesCreater = new SubScopesCreater();
|
---|
227 | subScopesCreater.GetVariableInfo("SubScopes").ActualName = "PopulationSize";
|
---|
228 | UniformSequentialSubScopesProcessor subScopesProc = new UniformSequentialSubScopesProcessor();
|
---|
229 | SequentialProcessor individualSeq = new SequentialProcessor();
|
---|
230 | OperatorExtractor treeCreater = new OperatorExtractor();
|
---|
231 | treeCreater.Name = "Tree generator (extr.)";
|
---|
232 | treeCreater.GetVariableInfo("Operator").ActualName = "Tree generator";
|
---|
233 | OperatorExtractor evaluator = new OperatorExtractor();
|
---|
234 | evaluator.Name = "Evaluator (extr.)";
|
---|
235 | evaluator.GetVariableInfo("Operator").ActualName = "Evaluator";
|
---|
236 | MeanSquaredErrorEvaluator validationEvaluator = new MeanSquaredErrorEvaluator();
|
---|
237 | validationEvaluator.GetVariableInfo("MSE").ActualName = "ValidationQuality";
|
---|
238 | validationEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
|
---|
239 | validationEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
|
---|
240 | Counter evalCounter = new Counter();
|
---|
241 | evalCounter.GetVariableInfo("Value").ActualName = "EvaluatedSolutions";
|
---|
242 | Sorter sorter = new Sorter();
|
---|
243 | sorter.GetVariableInfo("Descending").ActualName = "Maximization";
|
---|
244 | sorter.GetVariableInfo("Value").ActualName = "Quality";
|
---|
245 |
|
---|
246 | seq.AddSubOperator(subScopesCreater);
|
---|
247 | seq.AddSubOperator(subScopesProc);
|
---|
248 | seq.AddSubOperator(sorter);
|
---|
249 |
|
---|
250 | subScopesProc.AddSubOperator(individualSeq);
|
---|
251 | individualSeq.AddSubOperator(treeCreater);
|
---|
252 | individualSeq.AddSubOperator(evaluator);
|
---|
253 | individualSeq.AddSubOperator(validationEvaluator);
|
---|
254 | individualSeq.AddSubOperator(evalCounter);
|
---|
255 |
|
---|
256 | init.OperatorGraph.AddOperator(seq);
|
---|
257 | init.OperatorGraph.InitialOperator = seq;
|
---|
258 | return init;
|
---|
259 | }
|
---|
260 |
|
---|
261 | protected internal virtual IOperator CreateMainLoop() {
|
---|
262 | CombinedOperator main = new CombinedOperator();
|
---|
263 | SequentialProcessor seq = new SequentialProcessor();
|
---|
264 | IOperator childCreater = CreateChildCreater();
|
---|
265 | IOperator replacement = CreateReplacement();
|
---|
266 |
|
---|
267 | BestSolutionStorer solutionStorer = new BestSolutionStorer();
|
---|
268 | solutionStorer.GetVariableInfo("Quality").ActualName = "ValidationQuality";
|
---|
269 | solutionStorer.GetVariableInfo("BestSolution").ActualName = "BestValidationSolution";
|
---|
270 | solutionStorer.AddSubOperator(CreateBestSolutionProcessor());
|
---|
271 |
|
---|
272 | BestAverageWorstQualityCalculator qualityCalculator = new BestAverageWorstQualityCalculator();
|
---|
273 | BestAverageWorstQualityCalculator validationQualityCalculator = new BestAverageWorstQualityCalculator();
|
---|
274 | validationQualityCalculator.Name = "BestAverageWorstValidationQualityCalculator";
|
---|
275 | validationQualityCalculator.GetVariableInfo("Quality").ActualName = "ValidationQuality";
|
---|
276 | validationQualityCalculator.GetVariableInfo("BestQuality").ActualName = "BestValidationQuality";
|
---|
277 | validationQualityCalculator.GetVariableInfo("AverageQuality").ActualName = "AverageValidationQuality";
|
---|
278 | validationQualityCalculator.GetVariableInfo("WorstQuality").ActualName = "WorstValidationQuality";
|
---|
279 | IOperator loggingOperator = CreateLoggingOperator();
|
---|
280 | Counter counter = new Counter();
|
---|
281 | counter.GetVariableInfo("Value").ActualName = "Generations";
|
---|
282 | IOperator loopCondition = CreateLoopCondition(seq);
|
---|
283 |
|
---|
284 | seq.AddSubOperator(childCreater);
|
---|
285 | seq.AddSubOperator(replacement);
|
---|
286 | seq.AddSubOperator(solutionStorer);
|
---|
287 | seq.AddSubOperator(qualityCalculator);
|
---|
288 | seq.AddSubOperator(validationQualityCalculator);
|
---|
289 | seq.AddSubOperator(loggingOperator);
|
---|
290 | seq.AddSubOperator(counter);
|
---|
291 | seq.AddSubOperator(loopCondition);
|
---|
292 |
|
---|
293 | main.OperatorGraph.AddOperator(seq);
|
---|
294 | main.OperatorGraph.InitialOperator = seq;
|
---|
295 | return main;
|
---|
296 | }
|
---|
297 |
|
---|
298 | protected internal virtual IOperator CreateLoggingOperator() {
|
---|
299 | return new EmptyOperator();
|
---|
300 | }
|
---|
301 |
|
---|
302 | protected internal virtual IOperator CreateLoopCondition(IOperator loop) {
|
---|
303 | SequentialProcessor seq = new SequentialProcessor();
|
---|
304 | seq.Name = "Loop Condition";
|
---|
305 | LessThanComparator comparator = new LessThanComparator();
|
---|
306 | comparator.GetVariableInfo("LeftSide").ActualName = "Generations";
|
---|
307 | comparator.GetVariableInfo("RightSide").ActualName = "MaxGenerations";
|
---|
308 | comparator.GetVariableInfo("Result").ActualName = "GenerationsCondition";
|
---|
309 | ConditionalBranch cond = new ConditionalBranch();
|
---|
310 | cond.GetVariableInfo("Condition").ActualName = "GenerationsCondition";
|
---|
311 |
|
---|
312 | seq.AddSubOperator(comparator);
|
---|
313 | seq.AddSubOperator(cond);
|
---|
314 |
|
---|
315 | cond.AddSubOperator(loop);
|
---|
316 | return seq;
|
---|
317 | }
|
---|
318 |
|
---|
319 | protected internal virtual IOperator CreateBestSolutionProcessor() {
|
---|
320 | return new EmptyOperator();
|
---|
321 | }
|
---|
322 |
|
---|
323 | protected internal virtual IOperator CreateReplacement() {
|
---|
324 | CombinedOperator replacement = new CombinedOperator();
|
---|
325 | replacement.Name = "Replacement";
|
---|
326 | SequentialProcessor seq = new SequentialProcessor();
|
---|
327 | SequentialSubScopesProcessor seqScopeProc = new SequentialSubScopesProcessor();
|
---|
328 | SequentialProcessor selectedProc = new SequentialProcessor();
|
---|
329 | LeftSelector leftSelector = new LeftSelector();
|
---|
330 | leftSelector.GetVariableInfo("Selected").ActualName = "Elites";
|
---|
331 | RightReducer rightReducer = new RightReducer();
|
---|
332 |
|
---|
333 | SequentialProcessor remainingProc = new SequentialProcessor();
|
---|
334 | RightSelector rightSelector = new RightSelector();
|
---|
335 | rightSelector.GetVariableInfo("Selected").ActualName = "Elites";
|
---|
336 | LeftReducer leftReducer = new LeftReducer();
|
---|
337 | MergingReducer mergingReducer = new MergingReducer();
|
---|
338 | Sorter sorter = new Sorter();
|
---|
339 | sorter.GetVariableInfo("Descending").ActualName = "Maximization";
|
---|
340 | sorter.GetVariableInfo("Value").ActualName = "Quality";
|
---|
341 |
|
---|
342 | seq.AddSubOperator(seqScopeProc);
|
---|
343 | seqScopeProc.AddSubOperator(selectedProc);
|
---|
344 | selectedProc.AddSubOperator(leftSelector);
|
---|
345 | selectedProc.AddSubOperator(rightReducer);
|
---|
346 |
|
---|
347 | seqScopeProc.AddSubOperator(remainingProc);
|
---|
348 | remainingProc.AddSubOperator(rightSelector);
|
---|
349 | remainingProc.AddSubOperator(leftReducer);
|
---|
350 | seq.AddSubOperator(mergingReducer);
|
---|
351 | seq.AddSubOperator(sorter);
|
---|
352 | replacement.OperatorGraph.AddOperator(seq);
|
---|
353 | replacement.OperatorGraph.InitialOperator = seq;
|
---|
354 | return replacement;
|
---|
355 | }
|
---|
356 |
|
---|
357 | protected internal virtual IOperator CreateChildCreater() {
|
---|
358 | CombinedOperator childCreater = new CombinedOperator();
|
---|
359 | childCreater.Name = "Create children";
|
---|
360 | SequentialProcessor seq = new SequentialProcessor();
|
---|
361 | OperatorExtractor selector = new OperatorExtractor();
|
---|
362 | selector.Name = "Selector (extr.)";
|
---|
363 | selector.GetVariableInfo("Operator").ActualName = "Selector";
|
---|
364 |
|
---|
365 | SequentialSubScopesProcessor seqScopesProc = new SequentialSubScopesProcessor();
|
---|
366 | EmptyOperator emptyOpt = new EmptyOperator();
|
---|
367 | SequentialProcessor selectedProc = new SequentialProcessor();
|
---|
368 | ChildrenInitializer childInitializer = new ChildrenInitializer();
|
---|
369 | ((IntData)childInitializer.GetVariable("ParentsPerChild").Value).Data = 2;
|
---|
370 |
|
---|
371 | OperatorExtractor crossover = new OperatorExtractor();
|
---|
372 | crossover.Name = "Crossover (extr.)";
|
---|
373 | crossover.GetVariableInfo("Operator").ActualName = "Crossover";
|
---|
374 | UniformSequentialSubScopesProcessor individualProc = new UniformSequentialSubScopesProcessor();
|
---|
375 | SequentialProcessor individualSeqProc = new SequentialProcessor();
|
---|
376 | StochasticBranch cond = new StochasticBranch();
|
---|
377 | cond.GetVariableInfo("Probability").ActualName = "MutationRate";
|
---|
378 | OperatorExtractor manipulator = new OperatorExtractor();
|
---|
379 | manipulator.Name = "Manipulator (extr.)";
|
---|
380 | manipulator.GetVariableInfo("Operator").ActualName = "Manipulator";
|
---|
381 | OperatorExtractor evaluator = new OperatorExtractor();
|
---|
382 | evaluator.Name = "Evaluator (extr.)";
|
---|
383 | evaluator.GetVariableInfo("Operator").ActualName = "Evaluator";
|
---|
384 | MeanSquaredErrorEvaluator validationEvaluator = new MeanSquaredErrorEvaluator();
|
---|
385 | validationEvaluator.GetVariableInfo("MSE").ActualName = "ValidationQuality";
|
---|
386 | validationEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
|
---|
387 | validationEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
|
---|
388 | Counter evalCounter = new Counter();
|
---|
389 | evalCounter.GetVariableInfo("Value").ActualName = "EvaluatedSolutions";
|
---|
390 | SubScopesRemover parentRefRemover = new SubScopesRemover();
|
---|
391 |
|
---|
392 | Sorter sorter = new Sorter();
|
---|
393 | sorter.GetVariableInfo("Descending").ActualName = "Maximization";
|
---|
394 | sorter.GetVariableInfo("Value").ActualName = "Quality";
|
---|
395 |
|
---|
396 |
|
---|
397 | seq.AddSubOperator(selector);
|
---|
398 | seq.AddSubOperator(seqScopesProc);
|
---|
399 | seqScopesProc.AddSubOperator(emptyOpt);
|
---|
400 | seqScopesProc.AddSubOperator(selectedProc);
|
---|
401 | selectedProc.AddSubOperator(childInitializer);
|
---|
402 | selectedProc.AddSubOperator(individualProc);
|
---|
403 | individualProc.AddSubOperator(individualSeqProc);
|
---|
404 | individualSeqProc.AddSubOperator(crossover);
|
---|
405 | individualSeqProc.AddSubOperator(cond);
|
---|
406 | cond.AddSubOperator(manipulator);
|
---|
407 | individualSeqProc.AddSubOperator(evaluator);
|
---|
408 | individualSeqProc.AddSubOperator(validationEvaluator);
|
---|
409 | individualSeqProc.AddSubOperator(evalCounter);
|
---|
410 | individualSeqProc.AddSubOperator(parentRefRemover);
|
---|
411 | selectedProc.AddSubOperator(sorter);
|
---|
412 |
|
---|
413 | childCreater.OperatorGraph.AddOperator(seq);
|
---|
414 | childCreater.OperatorGraph.InitialOperator = seq;
|
---|
415 | return childCreater;
|
---|
416 | }
|
---|
417 |
|
---|
418 | protected internal virtual Model CreateGPModel(IScope bestModelScope) {
|
---|
419 | Engine.GlobalScope.AddSubScope(bestModelScope);
|
---|
420 | Model model = new Model();
|
---|
421 | Dataset ds = bestModelScope.GetVariableValue<Dataset>("Dataset", true);
|
---|
422 | model.Data = bestModelScope.GetVariableValue<IGeneticProgrammingModel>("FunctionTree", false);
|
---|
423 | model.Dataset = ds;
|
---|
424 | model.TargetVariable = ds.GetVariableName(bestModelScope.GetVariableValue<IntData>("TargetVariable", true).Data);
|
---|
425 | model.TrainingMeanSquaredError = bestModelScope.GetVariableValue<DoubleData>("Quality", false).Data;
|
---|
426 | model.ValidationMeanSquaredError = bestModelScope.GetVariableValue<DoubleData>("ValidationQuality", false).Data;
|
---|
427 | // calculate and set variable impacts
|
---|
428 | VariableEvaluationImpactCalculator evaluationImpactCalculator = new VariableEvaluationImpactCalculator();
|
---|
429 | evaluationImpactCalculator.GetVariableInfo("TrainingSamplesStart").ActualName = "ActualTrainingSamplesStart";
|
---|
430 | evaluationImpactCalculator.GetVariableInfo("TrainingSamplesEnd").ActualName = "ActualTrainingSamplesEnd";
|
---|
431 | VariableQualityImpactCalculator qualityImpactCalculator = new VariableQualityImpactCalculator();
|
---|
432 | qualityImpactCalculator.GetVariableInfo("TrainingSamplesStart").ActualName = "ActualTrainingSamplesStart";
|
---|
433 | qualityImpactCalculator.GetVariableInfo("TrainingSamplesEnd").ActualName = "ActualTrainingSamplesEnd";
|
---|
434 |
|
---|
435 |
|
---|
436 | evaluationImpactCalculator.Apply(bestModelScope);
|
---|
437 | qualityImpactCalculator.Apply(bestModelScope);
|
---|
438 |
|
---|
439 | ItemList evaluationImpacts = bestModelScope.GetVariableValue<ItemList>("VariableEvaluationImpacts", false);
|
---|
440 | ItemList qualityImpacts = bestModelScope.GetVariableValue<ItemList>("VariableQualityImpacts", false);
|
---|
441 | foreach (ItemList row in evaluationImpacts) {
|
---|
442 | string variableName = ((StringData)row[0]).Data;
|
---|
443 | double impact = ((DoubleData)row[1]).Data;
|
---|
444 | model.SetVariableEvaluationImpact(variableName, impact);
|
---|
445 | model.AddInputVariables(variableName);
|
---|
446 | }
|
---|
447 | foreach (ItemList row in qualityImpacts) {
|
---|
448 | string variableName = ((StringData)row[0]).Data;
|
---|
449 | double impact = ((DoubleData)row[1]).Data;
|
---|
450 | model.SetVariableQualityImpact(variableName, impact);
|
---|
451 | model.AddInputVariables(variableName);
|
---|
452 | }
|
---|
453 | Engine.GlobalScope.RemoveSubScope(bestModelScope);
|
---|
454 | return model;
|
---|
455 | }
|
---|
456 |
|
---|
457 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
458 | AlgorithmBase clone = (AlgorithmBase)base.Clone(clonedObjects);
|
---|
459 | clonedObjects.Add(Guid, clone);
|
---|
460 | clone.engine = (SequentialEngine.SequentialEngine)Auxiliary.Clone(Engine, clonedObjects);
|
---|
461 | return clone;
|
---|
462 | }
|
---|
463 |
|
---|
464 | protected VariableInjector GetVariableInjector() {
|
---|
465 | CombinedOperator co1 = (CombinedOperator)Engine.OperatorGraph.InitialOperator;
|
---|
466 | // SequentialProcessor in GP
|
---|
467 | algorithm = (SequentialProcessor)co1.OperatorGraph.InitialOperator;
|
---|
468 | return (VariableInjector)algorithm.SubOperators[2];
|
---|
469 | }
|
---|
470 |
|
---|
471 | protected RandomInjector GetRandomInjector() {
|
---|
472 | CombinedOperator co1 = (CombinedOperator)Engine.OperatorGraph.InitialOperator;
|
---|
473 | // SequentialProcessor in GP
|
---|
474 | algorithm = (SequentialProcessor)co1.OperatorGraph.InitialOperator;
|
---|
475 | return (RandomInjector)algorithm.SubOperators[0];
|
---|
476 | }
|
---|
477 |
|
---|
478 | #region Persistence Methods
|
---|
479 | public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
480 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
481 | node.AppendChild(PersistenceManager.Persist("Engine", Engine, document, persistedObjects));
|
---|
482 | return node;
|
---|
483 | }
|
---|
484 | public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
485 | base.Populate(node, restoredObjects);
|
---|
486 | engine = (SequentialEngine.SequentialEngine)PersistenceManager.Restore(node.SelectSingleNode("Engine"), restoredObjects);
|
---|
487 | }
|
---|
488 | #endregion
|
---|
489 |
|
---|
490 |
|
---|
491 | }
|
---|
492 | }
|
---|