Free cookie consent management tool by TermsFeed Policy Generator

source: branches/plugins/HeuristicLab.GP.StructureIdentification/3.2/OffspringSelectionGP.cs @ 3098

Last change on this file since 3098 was 1202, checked in by gkronber, 16 years ago

fixed a few minor glitches in hard-coded GP algos. #224 (Simple frontend for GP for non-expert users (similar to HeurisicLab.SGA))

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