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