Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1201 was 1201, checked in by gkronber, 15 years ago

implemented hard-coded GP with offspring selection for regression problems. #224 (Simple frontend for GP for non-expert users (similar to HeurisicLab.SGA))

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