source: branches/CEDMA-Refactoring-Ticket419/HeuristicLab.GP.StructureIdentification/OffspringSelectionGP.cs @ 1156

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

worked on #419

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