source: branches/CEDMA-Refactoring-Ticket419/HeuristicLab.GP.StructureIdentification/StandardGP.cs @ 1115

Last change on this file since 1115 was 1115, checked in by gkronber, 12 years ago

Removed calculation of R2 for the best (validation) model and added calculation of MAPE for training in the hard-coded StandardGP.
Changed Executer to store all model attributes. Changed parameters for StandardGP CEDMA runs for testing. (#419)

File size: 23.9 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Core;
27using System.Xml;
28using System.Diagnostics;
29using HeuristicLab.DataAnalysis;
30using HeuristicLab.Operators;
31using HeuristicLab.Random;
32using HeuristicLab.Selection;
33using HeuristicLab.Logging;
34using HeuristicLab.Data;
35using HeuristicLab.Operators.Programmable;
36
37namespace HeuristicLab.GP.StructureIdentification {
38  public class StandardGP : ItemBase, IEditable {
39    private IntData maxGenerations = new IntData();
40    public int MaxGenerations {
41      get { return maxGenerations.Data; }
42      set { maxGenerations.Data = value; }
43    }
44
45    private IntData tournamentSize = new IntData();
46    public int TournamentSize {
47      get { return tournamentSize.Data; }
48      set { tournamentSize.Data = value; }
49    }
50    private DoubleData mutationRate = new DoubleData();
51    public double MutationRate {
52      get { return mutationRate.Data; }
53      set { mutationRate.Data = value; }
54    }
55    private IntData parents = new IntData();
56    private IntData populationSize = new IntData();
57    public int PopulationSize {
58      get { return populationSize.Data; }
59      set {
60        populationSize.Data = value;
61        parents.Data = value * 2;
62      }
63    }
64
65    private BoolData setSeedRandomly = new BoolData();
66    public bool SetSeedRandomly {
67      get { return setSeedRandomly.Data; }
68      set { setSeedRandomly.Data = value; }
69    }
70
71    private IntData seed = new IntData();
72    public int Seed {
73      get { return seed.Data; }
74      set { seed.Data = value; }
75    }
76
77    public IOperator ProblemInjector {
78      get { return algorithm.SubOperators[0]; }
79      set {
80        value.Name = "ProblemInjector";
81        algorithm.RemoveSubOperator(0);
82        algorithm.AddSubOperator(value, 0);
83      }
84    }
85
86    private IntData elites = new IntData();
87    public int Elites {
88      get { return elites.Data; }
89      set { elites.Data = value; }
90    }
91
92    private int maxTreeSize = 50;
93    private int maxTreeHeight = 8;
94    private double punishmentFactor = 10.0;
95    private bool useEstimatedTargetValue = false;
96    private double fullTreeShakingFactor = 0.1;
97    private double onepointShakingFactor = 1.0;
98    private IOperator algorithm;
99
100    private SequentialEngine.SequentialEngine engine;
101    public IEngine Engine {
102      get { return engine; }
103    }
104
105    public StandardGP() {
106      PopulationSize = 10000;
107      MaxGenerations = 100;
108      TournamentSize = 7;
109      MutationRate = 0.15;
110      Elites = 1;
111      engine = new SequentialEngine.SequentialEngine();
112      CombinedOperator algo = CreateAlgorithm();
113      engine.OperatorGraph.AddOperator(algo);
114      engine.OperatorGraph.InitialOperator = algo;
115    }
116
117    private CombinedOperator CreateAlgorithm() {
118      CombinedOperator algo = new CombinedOperator();
119      algo.Name = "StandardGP";
120      SequentialProcessor seq = new SequentialProcessor();
121      EmptyOperator problemInjectorPlaceholder = new EmptyOperator();
122      RandomInjector randomInjector = new RandomInjector();
123      randomInjector.GetVariable("SetSeedRandomly").Value = setSeedRandomly;
124      randomInjector.GetVariable("Seed").Value = seed;
125      randomInjector.Name = "Random Injector";
126      VariableInjector globalInjector = CreateGlobalInjector();
127      CombinedOperator initialization = CreateInialization();
128      initialization.Name = "Initialization";
129      FunctionLibraryInjector funLibInjector = new FunctionLibraryInjector();
130      CombinedOperator mainLoop = CreateMainLoop();
131      mainLoop.Name = "Main loop";
132
133      ProbabilisticTreeCreator treeCreator = new ProbabilisticTreeCreator();
134      treeCreator.Name = "Tree generator";
135      treeCreator.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
136      treeCreator.GetVariableInfo("MinTreeSize").Local = true;
137      treeCreator.AddVariable(new HeuristicLab.Core.Variable("MinTreeSize", new IntData(3)));
138      MeanSquaredErrorEvaluator evaluator = new MeanSquaredErrorEvaluator();
139      evaluator.GetVariableInfo("MSE").ActualName = "Quality";
140      evaluator.GetVariableInfo("SamplesStart").ActualName = "TrainingSamplesStart";
141      evaluator.GetVariableInfo("SamplesEnd").ActualName = "TrainingSamplesEnd";
142      evaluator.Name = "Evaluator";
143      StandardCrossOver crossover = new StandardCrossOver();
144      crossover.Name = "Crossover";
145      crossover.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
146      CombinedOperator manipulator = CreateManipulator();
147      manipulator.Name = "Manipulator";
148      TournamentSelector selector = new TournamentSelector();
149      selector.Name = "Selector";
150      selector.GetVariableInfo("Selected").ActualName = "Parents";
151      selector.GetVariableInfo("GroupSize").Local = false;
152      selector.RemoveVariable("GroupSize");
153      selector.GetVariableInfo("GroupSize").ActualName = "TournamentSize";
154      LeftReducer cleanUp = new LeftReducer();
155
156      seq.AddSubOperator(problemInjectorPlaceholder);
157      seq.AddSubOperator(randomInjector);
158      seq.AddSubOperator(globalInjector);
159      seq.AddSubOperator(funLibInjector);
160      seq.AddSubOperator(initialization);
161      seq.AddSubOperator(mainLoop);
162      seq.AddSubOperator(cleanUp);
163
164      initialization.AddSubOperator(treeCreator);
165      initialization.AddSubOperator(evaluator);
166
167      mainLoop.AddSubOperator(selector);
168      mainLoop.AddSubOperator(crossover);
169      mainLoop.AddSubOperator(manipulator);
170      mainLoop.AddSubOperator(evaluator);
171      algo.OperatorGraph.AddOperator(seq);
172      algo.OperatorGraph.InitialOperator = seq;
173      this.algorithm = seq;
174      return algo;
175    }
176
177    private VariableInjector CreateGlobalInjector() {
178      VariableInjector injector = new VariableInjector();
179      injector.Name = "Global Injector";
180      injector.AddVariable(new HeuristicLab.Core.Variable("Generations", new IntData(0)));
181      injector.AddVariable(new HeuristicLab.Core.Variable("MaxGenerations", maxGenerations));
182      injector.AddVariable(new HeuristicLab.Core.Variable("MutationRate", mutationRate));
183      injector.AddVariable(new HeuristicLab.Core.Variable("PopulationSize", populationSize));
184      injector.AddVariable(new HeuristicLab.Core.Variable("Parents", parents));
185      injector.AddVariable(new HeuristicLab.Core.Variable("Elites", elites));
186      injector.AddVariable(new HeuristicLab.Core.Variable("TournamentSize", tournamentSize));
187      injector.AddVariable(new HeuristicLab.Core.Variable("Maximization", new BoolData(false)));
188      injector.AddVariable(new HeuristicLab.Core.Variable("MaxTreeHeight", new IntData(maxTreeHeight)));
189      injector.AddVariable(new HeuristicLab.Core.Variable("MaxTreeSize", new IntData(maxTreeSize)));
190      injector.AddVariable(new HeuristicLab.Core.Variable("EvaluatedSolutions", new IntData(0)));
191      injector.AddVariable(new HeuristicLab.Core.Variable("TotalEvaluatedNodes", new DoubleData(0)));
192      injector.AddVariable(new HeuristicLab.Core.Variable("PunishmentFactor", new DoubleData(punishmentFactor)));
193      injector.AddVariable(new HeuristicLab.Core.Variable("UseEstimatedTargetValue", new BoolData(useEstimatedTargetValue)));
194      return injector;
195    }
196
197    private CombinedOperator CreateManipulator() {
198      CombinedOperator manipulator = new CombinedOperator();
199      StochasticMultiBranch multibranch = new StochasticMultiBranch();
200      FullTreeShaker fullTreeShaker = new FullTreeShaker();
201      fullTreeShaker.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
202      fullTreeShaker.GetVariableInfo("ShakingFactor").Local = true;
203      fullTreeShaker.AddVariable(new HeuristicLab.Core.Variable("ShakingFactor", new DoubleData(fullTreeShakingFactor)));
204
205      OnePointShaker onepointShaker = new OnePointShaker();
206      onepointShaker.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
207      onepointShaker.GetVariableInfo("ShakingFactor").Local = true;
208      onepointShaker.AddVariable(new HeuristicLab.Core.Variable("ShakingFactor", new DoubleData(onepointShakingFactor)));
209      ChangeNodeTypeManipulation changeNodeTypeManipulation = new ChangeNodeTypeManipulation();
210      changeNodeTypeManipulation.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
211      CutOutNodeManipulation cutOutNodeManipulation = new CutOutNodeManipulation();
212      cutOutNodeManipulation.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
213      DeleteSubTreeManipulation deleteSubTreeManipulation = new DeleteSubTreeManipulation();
214      deleteSubTreeManipulation.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
215      SubstituteSubTreeManipulation substituteSubTreeManipulation = new SubstituteSubTreeManipulation();
216      substituteSubTreeManipulation.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
217
218      IOperator[] manipulators = new IOperator[] {
219        onepointShaker, fullTreeShaker,
220        changeNodeTypeManipulation,
221        cutOutNodeManipulation,
222        deleteSubTreeManipulation,
223        substituteSubTreeManipulation};
224
225      DoubleArrayData probabilities = new DoubleArrayData(new double[manipulators.Length]);
226      for(int i=0;i<manipulators.Length;i++) {
227        probabilities.Data[i] = 1.0;
228        multibranch.AddSubOperator(manipulators[i]);
229      }
230      multibranch.GetVariableInfo("Probabilities").Local = true;
231      multibranch.AddVariable(new HeuristicLab.Core.Variable("Probabilities", probabilities));
232
233      manipulator.OperatorGraph.AddOperator(multibranch);
234      manipulator.OperatorGraph.InitialOperator = multibranch;
235      return manipulator;
236    }
237
238    private CombinedOperator CreateInialization() {
239      CombinedOperator init = new CombinedOperator();
240      SequentialProcessor seq = new SequentialProcessor();
241      SubScopesCreater subScopesCreater = new SubScopesCreater();
242      subScopesCreater.GetVariableInfo("SubScopes").ActualName = "PopulationSize";
243      UniformSequentialSubScopesProcessor subScopesProc = new UniformSequentialSubScopesProcessor();
244      SequentialProcessor individualSeq = new SequentialProcessor();
245      OperatorExtractor treeCreater = new OperatorExtractor();
246      treeCreater.Name = "Tree generator (extr.)";
247      treeCreater.GetVariableInfo("Operator").ActualName = "Tree generator";
248      OperatorExtractor evaluator = new OperatorExtractor();
249      evaluator.Name = "Evaluator (extr.)";
250      evaluator.GetVariableInfo("Operator").ActualName = "Evaluator";
251      MeanSquaredErrorEvaluator validationEvaluator = new MeanSquaredErrorEvaluator();
252      validationEvaluator.GetVariableInfo("MSE").ActualName = "ValidationQuality";
253      validationEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
254      validationEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
255      Counter evalCounter = new Counter();
256      evalCounter.GetVariableInfo("Value").ActualName = "EvaluatedSolutions";
257
258      seq.AddSubOperator(subScopesCreater);
259      seq.AddSubOperator(subScopesProc);
260      subScopesProc.AddSubOperator(individualSeq);
261      individualSeq.AddSubOperator(treeCreater);
262      individualSeq.AddSubOperator(evaluator);
263      individualSeq.AddSubOperator(validationEvaluator);
264      individualSeq.AddSubOperator(evalCounter);
265
266      init.OperatorGraph.AddOperator(seq);
267      init.OperatorGraph.InitialOperator = seq;
268      return init;
269    }
270
271    private CombinedOperator CreateMainLoop() {
272      CombinedOperator main = new CombinedOperator();
273      SequentialProcessor seq = new SequentialProcessor();
274      CombinedOperator childCreater = CreateChildCreater();
275      childCreater.Name = "Create children";
276      CombinedOperator replacement = CreateReplacement();
277      replacement.Name = "Replacement";
278      BestSolutionStorer solutionStorer = CreateBestSolutionStorer();
279      BestAverageWorstQualityCalculator qualityCalculator = new BestAverageWorstQualityCalculator();
280      BestAverageWorstQualityCalculator validationQualityCalculator = new BestAverageWorstQualityCalculator();
281      validationQualityCalculator.Name = "ValidationQualityCalculator";
282      validationQualityCalculator.GetVariableInfo("BestQuality").ActualName = "BestValidationQuality";
283      validationQualityCalculator.GetVariableInfo("AverageQuality").ActualName = "AverageValidationQuality";
284      validationQualityCalculator.GetVariableInfo("WorstQuality").ActualName = "WorstValidationQuality";
285      DataCollector collector = new DataCollector();
286      ItemList<StringData> names = collector.GetVariable("VariableNames").GetValue<ItemList<StringData>>();
287      names.Add(new StringData("BestQuality"));
288      names.Add(new StringData("AverageQuality"));
289      names.Add(new StringData("WorstQuality"));
290      names.Add(new StringData("BestValidationQuality"));
291      names.Add(new StringData("AverageValidationQuality"));
292      names.Add(new StringData("WorstValidationQuality"));
293      LinechartInjector lineChartInjector = new LinechartInjector();
294      lineChartInjector.GetVariableInfo("Linechart").ActualName = "Quality Linechart";
295      lineChartInjector.GetVariable("NumberOfLines").GetValue<IntData>().Data = 6;
296      QualityLogger qualityLogger = new QualityLogger();
297      QualityLogger validationQualityLogger = new QualityLogger();
298      validationQualityCalculator.Name = "ValidationQualityLogger";
299      validationQualityLogger.GetVariableInfo("Quality").ActualName = "ValidationQuality";
300      validationQualityLogger.GetVariableInfo("QualityLog").ActualName = "ValidationQualityLog";
301      Counter counter = new Counter();
302      counter.GetVariableInfo("Value").ActualName = "Generations";
303      LessThanComparator comparator = new LessThanComparator();
304      comparator.GetVariableInfo("LeftSide").ActualName = "Generations";
305      comparator.GetVariableInfo("RightSide").ActualName = "MaxGenerations";
306      comparator.GetVariableInfo("Result").ActualName = "GenerationsCondition";
307      ConditionalBranch cond = new ConditionalBranch();
308      cond.GetVariableInfo("Condition").ActualName = "GenerationsCondition";
309
310      seq.AddSubOperator(childCreater);
311      seq.AddSubOperator(replacement);
312      seq.AddSubOperator(solutionStorer);
313      seq.AddSubOperator(qualityCalculator);
314      seq.AddSubOperator(validationQualityCalculator);
315      seq.AddSubOperator(collector);
316      seq.AddSubOperator(lineChartInjector);
317      seq.AddSubOperator(qualityLogger);
318      seq.AddSubOperator(validationQualityLogger);
319      seq.AddSubOperator(counter);
320      seq.AddSubOperator(comparator);
321      seq.AddSubOperator(cond);
322      cond.AddSubOperator(seq);
323
324      main.OperatorGraph.AddOperator(seq);
325      main.OperatorGraph.InitialOperator = seq;
326      return main;
327    }
328
329    private BestSolutionStorer CreateBestSolutionStorer() {
330      BestSolutionStorer solutionStorer = new BestSolutionStorer();
331      solutionStorer.GetVariableInfo("Quality").ActualName = "ValidationQuality";
332      solutionStorer.GetVariableInfo("BestSolution").ActualName = "BestValidationSolution";
333      SequentialProcessor bestSolutionProcessor = new SequentialProcessor();
334      MeanAbsolutePercentageErrorEvaluator trainingMapeEvaluator = new MeanAbsolutePercentageErrorEvaluator();
335      trainingMapeEvaluator.Name = "ValidationMapeEvaluator";
336      trainingMapeEvaluator.GetVariableInfo("MAPE").ActualName = "TrainingMAPE";
337      trainingMapeEvaluator.GetVariableInfo("SamplesStart").ActualName = "TrainingSamplesStart";
338      trainingMapeEvaluator.GetVariableInfo("SamplesEnd").ActualName = "TrainingSamplesEnd";
339      MeanAbsolutePercentageErrorEvaluator validationMapeEvaluator = new MeanAbsolutePercentageErrorEvaluator();
340      validationMapeEvaluator.Name = "ValidationMapeEvaluator";
341      validationMapeEvaluator.GetVariableInfo("MAPE").ActualName = "ValidationMAPE";
342      validationMapeEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
343      validationMapeEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
344      ProgrammableOperator progOperator = new ProgrammableOperator();
345      progOperator.RemoveVariableInfo("Result");
346      progOperator.AddVariableInfo(new HeuristicLab.Core.VariableInfo("EvaluatedSolutions", "", typeof(IntData), VariableKind.In));
347      progOperator.Code = @"
348int evalSolutions = EvaluatedSolutions.Data;
349scope.AddVariable(new Variable(""EvaluatedSolutions"", new IntData(evalSolutions)));
350";
351      solutionStorer.AddSubOperator(bestSolutionProcessor);
352      bestSolutionProcessor.AddSubOperator(trainingMapeEvaluator);
353      bestSolutionProcessor.AddSubOperator(validationMapeEvaluator);
354      bestSolutionProcessor.AddSubOperator(progOperator);
355      return solutionStorer;
356    }
357
358    private CombinedOperator CreateReplacement() {
359      CombinedOperator replacement = new CombinedOperator();
360      SequentialProcessor seq = new SequentialProcessor();
361      SequentialSubScopesProcessor seqScopeProc = new SequentialSubScopesProcessor();
362      SequentialProcessor selectedProc = new SequentialProcessor();
363      LeftSelector leftSelector = new LeftSelector();
364      leftSelector.GetVariableInfo("Selected").ActualName = "Elites";
365      RightReducer rightReducer = new RightReducer();
366
367      SequentialProcessor remainingProc = new SequentialProcessor();
368      RightSelector rightSelector = new RightSelector();
369      rightSelector.GetVariableInfo("Selected").ActualName = "Elites";
370      LeftReducer leftReducer = new LeftReducer();
371      MergingReducer mergingReducer = new MergingReducer();
372      Sorter sorter = new Sorter();
373      sorter.GetVariableInfo("Descending").ActualName = "Maximization";
374      sorter.GetVariableInfo("Value").ActualName = "Quality";
375
376      seq.AddSubOperator(seqScopeProc);
377      seqScopeProc.AddSubOperator(selectedProc);
378      selectedProc.AddSubOperator(leftSelector);
379      selectedProc.AddSubOperator(rightReducer);
380
381      seqScopeProc.AddSubOperator(remainingProc);
382      remainingProc.AddSubOperator(rightSelector);
383      remainingProc.AddSubOperator(leftReducer);
384      seq.AddSubOperator(mergingReducer);
385      seq.AddSubOperator(sorter);
386      replacement.OperatorGraph.AddOperator(seq);
387      replacement.OperatorGraph.InitialOperator = seq;
388      return replacement;
389    }
390
391    private CombinedOperator CreateChildCreater() {
392      CombinedOperator childCreater = new CombinedOperator();
393      SequentialProcessor seq = new SequentialProcessor();
394      OperatorExtractor selector = new OperatorExtractor();
395      selector.Name = "Selector (extr.)";
396      selector.GetVariableInfo("Operator").ActualName = "Selector";
397      SequentialSubScopesProcessor seqScopesProc = new SequentialSubScopesProcessor();
398      EmptyOperator emptyOpt = new EmptyOperator();
399      SequentialProcessor selectedProc = new SequentialProcessor();
400      OperatorExtractor crossover = new OperatorExtractor();
401      crossover.Name = "Crossover (extr.)";
402      crossover.GetVariableInfo("Operator").ActualName = "Crossover";
403      UniformSequentialSubScopesProcessor individualProc = new UniformSequentialSubScopesProcessor();
404      SequentialProcessor individualSeqProc = new SequentialProcessor();
405      StochasticBranch cond = new StochasticBranch();
406      cond.GetVariableInfo("Probability").ActualName = "MutationRate";
407      OperatorExtractor manipulator = new OperatorExtractor();
408      manipulator.Name = "Manipulator (extr.)";
409      manipulator.GetVariableInfo("Operator").ActualName = "Manipulator";
410      OperatorExtractor evaluator = new OperatorExtractor();
411      evaluator.Name = "Evaluator (extr.)";
412      evaluator.GetVariableInfo("Operator").ActualName = "Evaluator";
413      MeanSquaredErrorEvaluator validationEvaluator = new MeanSquaredErrorEvaluator();
414      validationEvaluator.GetVariableInfo("MSE").ActualName = "ValidationQuality";
415      validationEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
416      validationEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
417      Counter evalCounter = new Counter();
418      evalCounter.GetVariableInfo("Value").ActualName = "EvaluatedSolutions";
419
420      Sorter sorter = new Sorter();
421      sorter.GetVariableInfo("Descending").ActualName = "Maximization";
422      sorter.GetVariableInfo("Value").ActualName = "Quality";
423
424
425      seq.AddSubOperator(selector);
426      seq.AddSubOperator(seqScopesProc);
427      seqScopesProc.AddSubOperator(emptyOpt);
428      seqScopesProc.AddSubOperator(selectedProc);
429      selectedProc.AddSubOperator(crossover);
430      selectedProc.AddSubOperator(individualProc);
431      individualProc.AddSubOperator(individualSeqProc);
432      individualSeqProc.AddSubOperator(cond);
433      cond.AddSubOperator(manipulator);
434      individualSeqProc.AddSubOperator(evaluator);
435      individualSeqProc.AddSubOperator(validationEvaluator);
436      individualSeqProc.AddSubOperator(evalCounter);
437      selectedProc.AddSubOperator(sorter);
438
439      childCreater.OperatorGraph.AddOperator(seq);
440      childCreater.OperatorGraph.InitialOperator = seq;
441      return childCreater;
442    }
443
444    public IEditor CreateEditor() {
445      return new StandardGpEditor(this);
446    }
447
448    public override IView CreateView() {
449      return new StandardGpEditor(this);
450    }
451
452    public override object Clone(IDictionary<Guid, object> clonedObjects) {
453      StandardGP clone = new StandardGP();
454      clonedObjects.Add(Guid, clone);
455      clone.engine = (SequentialEngine.SequentialEngine)Auxiliary.Clone(Engine, clonedObjects);
456      return clone;
457    }
458    #region SetReferences Method
459    private void SetReferences() {
460      // SGA
461      CombinedOperator co1 = (CombinedOperator)Engine.OperatorGraph.InitialOperator;
462      // SequentialProcessor in SGA
463      algorithm = (SequentialProcessor)co1.OperatorGraph.InitialOperator;
464      // RandomInjector
465      RandomInjector ri = (RandomInjector)algorithm.SubOperators[1];
466      setSeedRandomly = ri.GetVariable("SetSeedRandomly").GetValue<BoolData>();
467      seed = ri.GetVariable("Seed").GetValue<IntData>();
468      // VariableInjector
469      VariableInjector vi = (VariableInjector)algorithm.SubOperators[2];
470      populationSize = vi.GetVariable("PopulationSize").GetValue<IntData>();
471      parents = vi.GetVariable("Parents").GetValue<IntData>();
472      maxGenerations = vi.GetVariable("MaxGenerations").GetValue<IntData>();
473      mutationRate = vi.GetVariable("MutationRate").GetValue<DoubleData>();
474      elites = vi.GetVariable("Elites").GetValue<IntData>();
475    }
476    #endregion
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      SetReferences();
488    }
489    #endregion
490  }
491}
Note: See TracBrowser for help on using the repository browser.