Free cookie consent management tool by TermsFeed Policy Generator

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

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