source: branches/IslandALPS/IslandALPS/3.3/IslandALPSAlgorthmsMainLoop.cs @ 13604

Last change on this file since 13604 was 13604, checked in by pkuelzer, 6 years ago

#2558 Include LayerNumber in GroupResults
Added Migration

File size: 35.1 KB
Line 
1using HeuristicLab.Algorithms.ALPS;
2using HeuristicLab.Common;
3using HeuristicLab.Core;
4using HeuristicLab.Data;
5using HeuristicLab.Operators;
6using HeuristicLab.Optimization;
7using HeuristicLab.Optimization.Operators;
8using HeuristicLab.Parameters;
9using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
10using HeuristicLab.Selection;
11
12namespace HeuristicLab.Algorithms.IslandALPS {
13  [Item("IslandALPSAlgorithmMainLoop", "An island ALPS main loop operator.")]
14  [StorableClass]
15  public class IslandAlpsAlgorthmsMainLoop : AlgorithmOperator{
16
17    [StorableConstructor]
18    private IslandAlpsAlgorthmsMainLoop(bool deserializing) : base(deserializing) { }
19
20    #region Parameter Properties
21    public ValueLookupParameter<IRandom> GlobalRandomParameter {
22      get { return (ValueLookupParameter<IRandom>)Parameters["GlobalRandom"]; }
23    }
24    public ValueLookupParameter<IRandom> IslandRandomParameter {
25      get { return (ValueLookupParameter<IRandom>)Parameters["IslandRandom"]; }
26    }
27    public ValueLookupParameter<IRandom> LayerRandomParameter {
28      get { return (ValueLookupParameter<IRandom>)Parameters["LayerRandom"]; }
29    }
30
31    public ValueLookupParameter<BoolValue> MaximizationParameter {
32      get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
33    }
34    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
35      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
36    }
37    public ValueLookupParameter<DoubleValue> BestKnownQualityParameter {
38      get { return (ValueLookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
39    }
40   
41    public ValueLookupParameter<IntValue> PopulationSizeParameter {
42      get { return (ValueLookupParameter<IntValue>)Parameters["PopulationSize"]; }
43    }
44
45    public ValueLookupParameter<IOperator> SelectorParameter {
46      get { return (ValueLookupParameter<IOperator>)Parameters["Selector"]; }
47    }
48    public ValueLookupParameter<IOperator> CrossoverParameter {
49      get { return (ValueLookupParameter<IOperator>)Parameters["Crossover"]; }
50    }
51    public ValueLookupParameter<PercentValue> MutationProbabilityParameter {
52      get { return (ValueLookupParameter<PercentValue>)Parameters["MutationProbability"]; }
53    }
54    public ValueLookupParameter<IOperator> MutatorParameter {
55      get { return (ValueLookupParameter<IOperator>)Parameters["Mutator"]; }
56    }
57    public ValueLookupParameter<IOperator> EvaluatorParameter {
58      get { return (ValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
59    }
60    public ValueLookupParameter<IntValue> ElitesParameter {
61      get { return (ValueLookupParameter<IntValue>)Parameters["Elites"]; }
62    }
63    public IValueLookupParameter<BoolValue> ReevaluateElitesParameter {
64      get { return (IValueLookupParameter<BoolValue>)Parameters["ReevaluateElites"]; }
65    }
66    public ValueLookupParameter<ResultCollection> ResultsParameter {
67      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
68    }
69    public ILookupParameter<ITerminator> TerminatorParameter {
70      get { return (ILookupParameter<ITerminator>)Parameters["Terminator"]; }
71    }
72
73    public ValueLookupParameter<IOperator> AnalyzerParameter {
74      get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
75    }
76    public ValueLookupParameter<IOperator> IslandAnalyzerParameter {
77      get { return (ValueLookupParameter<IOperator>)Parameters["IslandAnalyzer"]; }
78    }
79    public ValueLookupParameter<IOperator> LayerAnalyzerParameter {
80      get { return (ValueLookupParameter<IOperator>)Parameters["LayerAnalyzer"]; }
81    }
82
83    public ValueLookupParameter<IOperator> GroupAnalyzerParameter {
84      get { return (ValueLookupParameter<IOperator>)Parameters["GroupAnalyzer"]; }
85    }
86
87    public LookupParameter<IntValue> LayerEvaluatedSolutionsParameter {
88      get { return (LookupParameter<IntValue>)Parameters["LayerEvaluatedSolutions"]; }
89    }
90    public LookupParameter<IntValue> EvaluatedSolutions {
91      get { return (LookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
92    }
93    #region island
94    public ValueLookupParameter<IntValue> NumberOfIslandsParameter {
95      get { return (ValueLookupParameter<IntValue>)Parameters["NumberOfIslands"]; }
96    }
97    public ValueLookupParameter<IntValue> MigrationIntervalParameter {
98      get { return (ValueLookupParameter<IntValue>)Parameters["MigrationInterval"]; }
99    }
100    public ValueLookupParameter<PercentValue> MigrationRateParameter {
101      get { return (ValueLookupParameter<PercentValue>)Parameters["MigrationRate"]; }
102    }
103    public ValueLookupParameter<IOperator> MigratorParameter {
104      get { return (ValueLookupParameter<IOperator>)Parameters["Migrator"]; }
105    }
106    public ValueLookupParameter<IOperator> EmigrantsSelectorParameter {
107      get { return (ValueLookupParameter<IOperator>)Parameters["EmigrantsSelector"]; }
108    }
109    public ValueLookupParameter<IOperator> ImmigrationReplacerParameter {
110      get { return (ValueLookupParameter<IOperator>)Parameters["ImmigrationReplacer"]; }
111    }
112    public ValueLookupParameter<BoolValue> Migrate {
113      get { return (ValueLookupParameter<BoolValue>)Parameters["Migrate"]; }
114    }
115    public LookupParameter<IntValue> IslandEvaluatedSolutions {
116      get { return (LookupParameter<IntValue>)Parameters["IslandEvaluatedSolutions"]; }
117    }
118    public LookupParameter<IntValue> IslandGenerations {
119      get { return (LookupParameter<IntValue>)Parameters["IslandGenerations"]; }
120    }
121    #endregion
122
123    #region alps
124    public ILookupParameter<IntValue> CurrentPopulationSizeParameter {
125      get { return (ILookupParameter<IntValue>)Parameters["CurrentPopulationSize"]; }
126    }
127    public IValueLookupParameter<IntValue> NumberOfLayersParameter {
128      get { return (IValueLookupParameter<IntValue>)Parameters["NumberOfLayers"]; }
129    }
130    public IScopeTreeLookupParameter<DoubleValue> AgeParameter {
131      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Age"]; }
132    }
133    public IValueLookupParameter<IntValue> AgeGapParameter {
134      get { return (IValueLookupParameter<IntValue>)Parameters["AgeGap"]; }
135    }
136    public IValueLookupParameter<DoubleValue> AgeInheritanceParameter {
137      get { return (IValueLookupParameter<DoubleValue>)Parameters["AgeInheritance"]; }
138    }
139    public IValueLookupParameter<IntArray> AgeLimitsParameter {
140      get { return (IValueLookupParameter<IntArray>)Parameters["AgeLimits"]; }
141    }
142    public IValueLookupParameter<IntValue> MatingPoolRangeParameter {
143      get { return (IValueLookupParameter<IntValue>)Parameters["MatingPoolRange"]; }
144    }
145    public IValueLookupParameter<BoolValue> ReduceToPopulationSizeParameter {
146      get { return (IValueLookupParameter<BoolValue>)Parameters["ReduceToPopulationSize"]; }
147    }
148    public IValueLookupParameter<BoolValue> PlusSelectionParameter {
149      get { return (IValueLookupParameter<BoolValue>)Parameters["PlusSelection"]; }
150    }
151    #endregion
152
153    #endregion
154
155    private IslandAlpsAlgorthmsMainLoop(IslandAlpsAlgorthmsMainLoop original, Cloner cloner) :
156      base(original,cloner){ }
157    public override IDeepCloneable Clone(Cloner cloner) {
158      return new IslandAlpsAlgorthmsMainLoop(this,cloner);
159    }
160
161    public IslandAlpsAlgorthmsMainLoop() : base() {
162
163      #region Create parameters
164      Parameters.Add(new ValueLookupParameter<IRandom>("GlobalRandom", "A pseudo random number generator."));
165      Parameters.Add(new ValueLookupParameter<IRandom>("IslandRandom", "A pseudo random number generator."));
166      Parameters.Add(new ValueLookupParameter<IRandom>("LayerRandom", "A pseudo random number generator."));
167
168      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
169      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
170      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
171
172      Parameters.Add(new ValueLookupParameter<IntValue>("NumberOfLayers", "The number of Layers per Island."));
173      Parameters.Add(new ValueLookupParameter<IntValue>("NumberOfIslands", "The number of islands."));
174      Parameters.Add(new ValueLookupParameter<IntValue>("MigrationInterval", "The number of generations that should pass between migration phases."));
175      Parameters.Add(new ValueLookupParameter<PercentValue>("MigrationRate", "The proportion of individuals that should migrate between the islands."));
176      Parameters.Add(new ValueLookupParameter<IOperator>("Migrator", "The migration strategy."));
177      Parameters.Add(new ValueLookupParameter<IOperator>("EmigrantsSelector", "Selects the individuals that will be migrated."));
178      Parameters.Add(new ValueLookupParameter<IOperator>("ImmigrationReplacer", "Replaces some of the original population with the immigrants."));
179      Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize", "The size of the population of solutions."));
180      Parameters.Add(new ValueLookupParameter<IntValue>("CurrentPopulationSize", "The current size of the population of solutions."));
181
182      Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
183      Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
184      Parameters.Add(new ValueLookupParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
185      Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
186      Parameters.Add(new ValueLookupParameter<IOperator>("Evaluator", "The operator used to evaluate solutions."));
187      Parameters.Add(new ValueLookupParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation."));
188      Parameters.Add(new ValueLookupParameter<BoolValue>("ReevaluateElites", "Flag to determine if elite individuals should be reevaluated (i.e., if stochastic fitness functions are used.)"));
189      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The results collection to store the results."));
190      Parameters.Add(new ValueLookupParameter<ITerminator>("Terminator", "The termination criteria that defines if the algorithm should continue or stop"));
191
192      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Age", "The age of individuals."));
193      Parameters.Add(new ValueLookupParameter<IntValue>("AgeGap", "The frequency of reseeding the lowest layer and scaling factor for the age-limits for the layers."));
194      Parameters.Add(new ValueLookupParameter<DoubleValue>("AgeInheritance", "A weight that determines the age of a child after crossover based on the older (1.0) and younger (0.0) parent."));
195      Parameters.Add(new ValueLookupParameter<IntArray>("AgeLimits", "The maximum age an individual is allowed to reach in a certain layer."));
196      Parameters.Add(new ValueLookupParameter<BoolValue>("PlusSelection", "Include the parents in the selection of the invividuals for the next generation."));
197      Parameters.Add(new ValueLookupParameter<IntValue>("MatingPoolRange", "The range of sub - populations used for creating a mating pool. (1 = current + previous sub-population)"));
198      Parameters.Add(new ValueLookupParameter<BoolValue>("ReduceToPopulationSize", "Reduce the CurrentPopulationSize after elder migration to PopulationSize"));
199
200      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to the analyze the islands."));
201      Parameters.Add(new ValueLookupParameter<IOperator>("IslandAnalyzer", "The operator used to analyze each island."));
202      Parameters.Add(new ValueLookupParameter<IOperator>("LayerAnalyzer", "The operator used to analyze each Layer."));
203      Parameters.Add(new ValueLookupParameter<IOperator>("GroupAnalyzer", "The operator used to analyze each Layergroup."));
204
205      Parameters.Add(new LookupParameter<IntValue>("LayerEvaluatedSolutions", "The number of times a solution has been evaluated."));
206      Parameters.Add(new LookupParameter<IntValue>("IslandGenerations", "The number of generations calculated on one island."));
207      //TODO: make long ?
208      Parameters.Add(new LookupParameter<IntValue>("IslandEvaluatedSolutions", "The number of times a solution has been evaluated on one island."));
209      Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of times a solution has been evaluated on one island."));
210
211      Parameters.Add(new ValueLookupParameter<BoolValue>("Migrate", "Migrate the island?"));
212
213      #endregion
214      var globalVariableCreator = new VariableCreator() { Name = "Create global values" };
215      var islandVarCreatorUssp = new UniformSubScopesProcessor();
216      var islandVariableCreator = new VariableCreator() {Name = "Initialize Results"};
217      var groupVariableCreator = new VariableCreator() {Name = "Initialize Group Results"};
218
219      var layerUssp = new UniformSubScopesProcessor();
220      var layerVariableCreator = new VariableCreator() {};
221
222      var initLayerAnalyzerPlaceholder = new Placeholder() {Name = "Layer Analyzer (Placeholder)"};
223      var initIslandAnalyzerPlaceholder = new Placeholder() {Name = "Island Analyzer (Placeholder)"};
224      var initGlobalAnalyzerPlacerholder =new Placeholder() {Name = "Global Analyzer (Placeholder)"};
225      var resultsCollector = new ResultsCollector();
226
227      var processIslandsUssp = new UniformSubScopesProcessor() {Name = "Process Islands", Parallel = new BoolValue(true)};
228      var initIslandGenerationsAss = new Assigner() {Name = "Init Island Generations"};
229      var initIslandEvaluationsAss = new Assigner() {Name = "Init Island Evaluations"};
230
231      var incrementGenerationDr = new DataReducer() { Name = "Increment Generations" };
232      var incrementEvaluatedSolutionDr = new DataReducer() { Name = "Increment Evaluated Solutions" };
233
234      var elderMigrator = CreateEldersEmigrator();
235      var newLayerOpener = CreateLayerOpener();
236      var reseeder = CreateReseeder();
237
238      var analyzeLayerUssp = new UniformSubScopesProcessor();
239
240      var layerAnalyzerPlaceholder = new Placeholder() { Name = "Layer Analyzer (Placeholder)" };
241      var layerResultsCollector = new ResultsCollector() {Name = "Collect Additional Infos"};
242      var islandAnalyzerPlaceholder = new Placeholder() { Name = "Island Analyzer (Placeholder)" };
243      var islandResultsCollector = new ResultsCollector() { Name = "Collect layer results into IslandResults" };
244
245      var globalAnalyzerPlacerholder = new Placeholder() { Name = "Global Analyzer (Placeholder)" };
246
247      var groupingOperator = new GroupedLayerOperator() {Name = "Grouped Analyzer"};
248      var groupAnalyzer = new Placeholder() {Name = "Group Analyzer (Placeholder)"};
249      var groupResultsExtractor = new ResultsExtractor() {Name = "Collect group results into variable"};
250      var groupResultsCollector = new ResultsCollector() {Name = "Collect group results into into global results"};
251      var groupLayerNumberResultsCollector = new ResultsCollector() {Name = "Add Layernumber to Results"};
252
253      var matingPoolCreator = new MatingPoolCreator();
254      var matingPoolUssp = new UniformSubScopesProcessor() {Name = "Process Mating Pools"};
255      var alpsMainOperator = new AlpsGeneticAlgorithmMainOperator();
256
257      var incrementIslandGeneration = new IntCounter() {Name = "incrementIslandGeneration" };
258      var setIslandEvaluatedSolutions = new DataReducer() {Name = "Set IslandEvaluatedSolutions" };
259       
260      var migrationCounter = new IntCounter() {Name = "Increment  Migrations"};
261      var selectforMigrationUssp = new UniformSubScopesProcessor();
262      var emigrantSelector = new Placeholder() {Name = "Emigrant Selector (Placeholder)"};
263      var migrator = new LayerMigrator();
264      var replaceforMigrationUssp = new UniformSubScopesProcessor();
265      var immigrantSelector = new Placeholder() { Name = "Immigrant Replacer (Placeholder)" };
266
267      var migrateComparator = new Comparator();
268      var migrateCondBranch = new ConditionalBranch() {Name = "Migrate?"};
269
270      var terminator = new TerminationOperator();
271     
272      globalVariableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations",new IntValue(0)));
273      globalVariableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Migrations",new IntValue(0)));
274
275      islandVariableCreator.CollectedValues.Add(new ValueParameter<IntValue>(IslandGenerations.Name, new IntValue(0)));
276      islandVariableCreator.CollectedValues.Add(new ValueParameter<IntValue>("OpenLayers", new IntValue(1)));
277      islandVariableCreator.CollectedValues.Add(new ValueParameter<ResultCollection>("IslandResults"));
278
279      layerVariableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Layer", new IntValue(0)));
280      layerVariableCreator.CollectedValues.Add(new ValueParameter<ResultCollection>("LayerResults"));
281
282      groupVariableCreator.CollectedValues.Add((new ValueParameter<ResultCollection>("GroupResults")));
283      //TODO: groupAnalyze
284      initLayerAnalyzerPlaceholder.OperatorParameter.ActualName = LayerAnalyzerParameter.Name;
285      initIslandAnalyzerPlaceholder.OperatorParameter.ActualName = IslandAnalyzerParameter.Name;
286      initGlobalAnalyzerPlacerholder.OperatorParameter.ActualName = AnalyzerParameter.Name;
287
288      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
289      resultsCollector.CollectedValues.Add(new ScopeTreeLookupParameter<ResultCollection>("IslandResults","","IslandResults"));
290      resultsCollector.CollectedValues.Add(new ScopeTreeLookupParameter<ResultCollection>("GroupResults","","GroupResults"));
291      resultsCollector.CollectedValues.Add(new ValueLookupParameter<IntValue>("Migrations"));
292      resultsCollector.CopyValue = new BoolValue(false);
293
294      initIslandGenerationsAss.LeftSideParameter.ActualName = IslandGenerations.Name;
295      initIslandGenerationsAss.RightSideParameter.Value = new IntValue(0);
296
297      initIslandEvaluationsAss.LeftSideParameter.ActualName = IslandEvaluatedSolutions.Name;
298      initIslandEvaluationsAss.RightSideParameter.Value = new IntValue(0);
299
300      incrementGenerationDr.TargetParameter.ActualName = "Generations";
301      incrementGenerationDr.ParameterToReduce.ActualName = IslandGenerations.Name;
302      incrementGenerationDr.TargetOperation.Value = new ReductionOperation(ReductionOperations.Sum);
303      incrementGenerationDr.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Min);
304
305      incrementEvaluatedSolutionDr.ParameterToReduce.ActualName = IslandEvaluatedSolutions.Name;
306      incrementEvaluatedSolutionDr.TargetParameter.ActualName = EvaluatedSolutions.Name;
307      incrementEvaluatedSolutionDr.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum);
308      incrementEvaluatedSolutionDr.TargetOperation.Value = new ReductionOperation(ReductionOperations.Sum);
309
310      incrementIslandGeneration.ValueParameter.ActualName = IslandGenerations.Name;
311      setIslandEvaluatedSolutions.ParameterToReduce.ActualName = LayerEvaluatedSolutionsParameter.Name;
312      setIslandEvaluatedSolutions.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum);
313      setIslandEvaluatedSolutions.TargetOperation.Value = new ReductionOperation(ReductionOperations.Assign);
314      setIslandEvaluatedSolutions.TargetParameter.ActualName = IslandEvaluatedSolutions.Name;
315
316      migrateComparator.Comparison.Value = ComparisonType.GreaterOrEqual;
317      migrateComparator.LeftSideParameter.ActualName = IslandGenerations.Name;
318      migrateComparator.RightSideParameter.ActualName = MigrationIntervalParameter.Name;
319      migrateComparator.ResultParameter.ActualName = "Migrate";
320
321      migrateCondBranch.ConditionParameter.ActualName = "Migrate";
322
323      migrationCounter.ValueParameter.ActualName = "Migrations";
324      selectforMigrationUssp.Depth = new IntValue(2);
325      emigrantSelector.OperatorParameter.ActualName = EmigrantsSelectorParameter.Name;
326
327      replaceforMigrationUssp.Depth = new IntValue(2);
328      immigrantSelector.OperatorParameter.ActualName = ImmigrationReplacerParameter.Name;
329
330      groupAnalyzer.OperatorParameter.ActualName = GroupAnalyzerParameter.Name;
331      groupLayerNumberResultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("LayerNumber"));
332      groupLayerNumberResultsCollector.ResultsParameter.ActualName = "GroupResults";
333
334      layerAnalyzerPlaceholder.OperatorParameter.ActualName = LayerAnalyzerParameter.Name;
335      layerResultsCollector.ResultsParameter.ActualName = "LayerResults";
336      layerResultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Layer"));
337      islandAnalyzerPlaceholder.OperatorParameter.ActualName = IslandAnalyzerParameter.Name;
338      islandResultsCollector.ResultsParameter.ActualName = "IslandResults";
339      islandResultsCollector.CollectedValues.Add(new ScopeTreeLookupParameter<ResultCollection>("LayerResults"));
340      globalAnalyzerPlacerholder.OperatorParameter.ActualName = AnalyzerParameter.Name;
341
342      //TODO: implement check for grouped layer analyzer
343      groupResultsExtractor.SourceNameParameter.Value = new StringValue("GroupScope");
344      groupResultsExtractor.TargetNameParameter.Value = new StringValue("GroupResults");
345      groupResultsCollector.CollectedValues.Add(new LookupParameter<ItemArray<ResultCollection>>("GroupResults"));
346      matingPoolCreator.MatingPoolRangeParameter.ActualName = MatingPoolRangeParameter.Name;
347
348      alpsMainOperator.RandomParameter.ActualName = LayerRandomParameter.Name;
349      alpsMainOperator.EvaluatorParameter.ActualName = EvaluatorParameter.Name;
350      alpsMainOperator.EvaluatedSolutionsParameter.ActualName = LayerEvaluatedSolutionsParameter.Name;
351      alpsMainOperator.QualityParameter.ActualName = QualityParameter.Name;
352      alpsMainOperator.MaximizationParameter.ActualName = MaximizationParameter.Name;
353      alpsMainOperator.PopulationSizeParameter.ActualName = PopulationSizeParameter.Name;
354      alpsMainOperator.SelectorParameter.ActualName = SelectorParameter.Name;
355      alpsMainOperator.CrossoverParameter.ActualName = CrossoverParameter.Name;
356      alpsMainOperator.MutatorParameter.ActualName = MutatorParameter.ActualName;
357      alpsMainOperator.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
358      alpsMainOperator.ElitesParameter.ActualName = ElitesParameter.Name;
359      alpsMainOperator.ReevaluateElitesParameter.ActualName = ReevaluateElitesParameter.Name;
360      alpsMainOperator.PlusSelectionParameter.ActualName = PlusSelectionParameter.Name;
361      alpsMainOperator.AgeParameter.ActualName = AgeParameter.Name;
362      alpsMainOperator.AgeInheritanceParameter.ActualName = AgeInheritanceParameter.Name;
363      alpsMainOperator.AgeIncrementParameter.Value = new DoubleValue(1.0);
364
365      //flow
366      OperatorGraph.InitialOperator = globalVariableCreator;
367      globalVariableCreator.Successor = islandVarCreatorUssp;
368      islandVarCreatorUssp.Operator = islandVariableCreator;
369      islandVariableCreator.Successor = layerUssp;
370      layerUssp.Operator = layerVariableCreator;
371      layerVariableCreator.Successor = initLayerAnalyzerPlaceholder;
372      initLayerAnalyzerPlaceholder.Successor = null;
373      layerUssp.Successor = initIslandAnalyzerPlaceholder;
374      initLayerAnalyzerPlaceholder.Successor = null;
375      islandVarCreatorUssp.Successor = initGlobalAnalyzerPlacerholder;
376      initGlobalAnalyzerPlacerholder.Successor = resultsCollector;
377      resultsCollector.Successor = processIslandsUssp;
378      processIslandsUssp.Operator = initIslandGenerationsAss;
379      initIslandGenerationsAss.Successor = initIslandEvaluationsAss;
380      initIslandEvaluationsAss.Successor = matingPoolCreator;
381      matingPoolCreator.Successor = matingPoolUssp;
382      matingPoolUssp.Operator = alpsMainOperator;
383      alpsMainOperator.Successor = null;
384      matingPoolUssp.Successor = elderMigrator;
385      elderMigrator.Successor = newLayerOpener;
386      newLayerOpener.Successor = reseeder;
387      reseeder.Successor = analyzeLayerUssp;
388      analyzeLayerUssp.Operator = layerAnalyzerPlaceholder;
389      layerAnalyzerPlaceholder.Successor = layerResultsCollector;
390      layerResultsCollector.Successor = null;
391
392      analyzeLayerUssp.Successor = islandAnalyzerPlaceholder;
393      islandAnalyzerPlaceholder.Successor = islandResultsCollector;
394      islandResultsCollector.Successor = setIslandEvaluatedSolutions;
395      setIslandEvaluatedSolutions.Successor = incrementIslandGeneration;
396      incrementIslandGeneration.Successor = migrateComparator;
397      migrateComparator.Successor = migrateCondBranch;
398      migrateCondBranch.FalseBranch = matingPoolCreator;
399      migrateCondBranch.TrueBranch = null;
400
401      processIslandsUssp.Successor = incrementGenerationDr;
402      incrementGenerationDr.Successor = incrementEvaluatedSolutionDr;
403      incrementEvaluatedSolutionDr.Successor = migrationCounter;
404     
405      migrationCounter.Successor = selectforMigrationUssp;
406      selectforMigrationUssp.Operator = emigrantSelector;
407      emigrantSelector.Successor = null;
408      selectforMigrationUssp.Successor = migrator;
409
410      migrator.Successor = replaceforMigrationUssp;
411      replaceforMigrationUssp.Operator = immigrantSelector;
412      immigrantSelector.Successor = null;
413      replaceforMigrationUssp.Successor = globalAnalyzerPlacerholder;
414
415      globalAnalyzerPlacerholder.Successor = groupingOperator;
416      groupingOperator.Operator = groupAnalyzer;
417      groupAnalyzer.Successor = groupLayerNumberResultsCollector;
418      groupLayerNumberResultsCollector.Successor = null;
419      groupingOperator.Successor = groupResultsExtractor;
420      groupResultsExtractor.Successor = groupResultsCollector;
421      groupResultsCollector.Successor = terminator;
422      terminator.ContinueBranch = processIslandsUssp;
423
424    }
425
426    private CombinedOperator CreateEldersEmigrator() {
427      var eldersEmigrator = new CombinedOperator() { Name = "Emigrate Elders" };
428      var selectorProsessor = new UniformSubScopesProcessor();
429      var eldersSelector = new EldersSelector();
430      var shiftToRightMigrator = new UnidirectionalRingMigrator() { Name = "Shift elders to next layer" };
431      var mergingProsessor = new UniformSubScopesProcessor();
432      var mergingReducer = new MergingReducer();
433      var subScopesCounter = new SubScopesCounter();
434      var reduceToPopulationSizeBranch = new ConditionalBranch() { Name = "ReduceToPopulationSize?" };
435      var countCalculator = new ExpressionCalculator() { Name = "CurrentPopulationSize = Min(CurrentPopulationSize, PopulationSize)" };
436      var bestSelector = new BestSelector();
437      var rightReducer = new RightReducer();
438
439      eldersEmigrator.OperatorGraph.InitialOperator = selectorProsessor;
440
441      selectorProsessor.Operator = eldersSelector;
442      selectorProsessor.Successor = shiftToRightMigrator;
443
444      eldersSelector.AgeParameter.ActualName = AgeParameter.Name;
445      eldersSelector.AgeLimitsParameter.ActualName = AgeLimitsParameter.Name;
446      eldersSelector.NumberOfLayersParameter.ActualName = NumberOfLayersParameter.Name;
447      eldersSelector.LayerParameter.ActualName = "Layer";
448      eldersSelector.Successor = null;
449
450      shiftToRightMigrator.ClockwiseMigrationParameter.Value = new BoolValue(true);
451      shiftToRightMigrator.Successor = mergingProsessor;
452
453      mergingProsessor.Operator = mergingReducer;
454
455      mergingReducer.Successor = subScopesCounter;
456
457      subScopesCounter.ValueParameter.ActualName = CurrentPopulationSizeParameter.Name;
458      subScopesCounter.AccumulateParameter.Value = new BoolValue(false);
459      subScopesCounter.Successor = reduceToPopulationSizeBranch;
460
461      reduceToPopulationSizeBranch.ConditionParameter.ActualName = ReduceToPopulationSizeParameter.Name;
462      reduceToPopulationSizeBranch.TrueBranch = countCalculator;
463
464      countCalculator.CollectedValues.Add(new LookupParameter<IntValue>(PopulationSizeParameter.Name));
465      countCalculator.CollectedValues.Add(new LookupParameter<IntValue>(CurrentPopulationSizeParameter.Name));
466      countCalculator.ExpressionParameter.Value = new StringValue("CurrentPopulationSize PopulationSize CurrentPopulationSize PopulationSize < if toint");
467      countCalculator.ExpressionResultParameter.ActualName = CurrentPopulationSizeParameter.Name;
468      countCalculator.Successor = bestSelector;
469
470      bestSelector.NumberOfSelectedSubScopesParameter.ActualName = CurrentPopulationSizeParameter.Name;
471      bestSelector.CopySelected = new BoolValue(false);
472      bestSelector.Successor = rightReducer;
473
474      return eldersEmigrator;
475    }
476
477    private CombinedOperator CreateLayerOpener() {
478      var layerOpener = new CombinedOperator() { Name = "Open new Layer if needed" };
479      var maxLayerReached = new Comparator() { Name = "MaxLayersReached = OpenLayers >= NumberOfLayers" };
480      var maxLayerReachedBranch = new ConditionalBranch() { Name = "MaxLayersReached?" };
481      var openNewLayerCalculator = new ExpressionCalculator() { Name = "OpenNewLayer = Generations >= AgeLimits[OpenLayers - 1]" };
482      var openNewLayerBranch = new ConditionalBranch() { Name = "OpenNewLayer?" };
483      var layerCreator = new LastLayerCloner() { Name = "Create Layer" };
484      var updateLayerNumber = new Assigner() { Name = "Layer = OpenLayers" };
485      var historyWiper = new ResultsHistoryWiper() { Name = "Clear History in Results" };
486      var createChildrenViaCrossover = new AlpsGeneticAlgorithmMainOperator();
487      var incrEvaluatedSolutionsForNewLayer = new SubScopesCounter() { Name = "Update EvaluatedSolutions" };
488      var incrOpenLayers = new IntCounter() { Name = "Incr. OpenLayers" };
489      //var newLayerResultsCollector = new ResultsCollector() { Name = "Collect new Layer Results" };
490
491      layerOpener.OperatorGraph.InitialOperator = maxLayerReached;
492
493      maxLayerReached.LeftSideParameter.ActualName = "OpenLayers";
494      maxLayerReached.RightSideParameter.ActualName = NumberOfLayersParameter.Name;
495      maxLayerReached.ResultParameter.ActualName = "MaxLayerReached";
496      maxLayerReached.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
497      maxLayerReached.Successor = maxLayerReachedBranch;
498
499      maxLayerReachedBranch.ConditionParameter.ActualName = "MaxLayerReached";
500      maxLayerReachedBranch.FalseBranch = openNewLayerCalculator;
501
502      openNewLayerCalculator.CollectedValues.Add(new LookupParameter<IntArray>(AgeLimitsParameter.Name));
503      openNewLayerCalculator.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
504      openNewLayerCalculator.CollectedValues.Add(new LookupParameter<IntValue>(NumberOfLayersParameter.Name));
505      openNewLayerCalculator.CollectedValues.Add(new LookupParameter<IntValue>("OpenLayers"));
506      openNewLayerCalculator.ExpressionResultParameter.ActualName = "OpenNewLayer";
507      openNewLayerCalculator.ExpressionParameter.Value = new StringValue("Generations 1 + AgeLimits OpenLayers 1 - [] >");
508      openNewLayerCalculator.Successor = openNewLayerBranch;
509
510      openNewLayerBranch.ConditionParameter.ActualName = "OpenNewLayer";
511      openNewLayerBranch.TrueBranch = layerCreator;
512
513      layerCreator.NewLayerOperator = updateLayerNumber;
514      layerCreator.Successor = incrOpenLayers;
515
516      updateLayerNumber.LeftSideParameter.ActualName = "Layer";
517      updateLayerNumber.RightSideParameter.ActualName = "OpenLayers";
518      updateLayerNumber.Successor = historyWiper;
519
520      historyWiper.ResultsParameter.ActualName = "LayerResults";
521      historyWiper.Successor = createChildrenViaCrossover;
522
523      // Maybe use only crossover and no elitism instead of "default operator"
524      createChildrenViaCrossover.RandomParameter.ActualName = LayerRandomParameter.Name;
525      createChildrenViaCrossover.EvaluatorParameter.ActualName = EvaluatorParameter.Name;
526      createChildrenViaCrossover.EvaluatedSolutionsParameter.ActualName = "LayerEvaluatedSolutions";
527      createChildrenViaCrossover.QualityParameter.ActualName = QualityParameter.Name;
528      createChildrenViaCrossover.MaximizationParameter.ActualName = MaximizationParameter.Name;
529      createChildrenViaCrossover.PopulationSizeParameter.ActualName = PopulationSizeParameter.Name;
530      createChildrenViaCrossover.SelectorParameter.ActualName = SelectorParameter.Name;
531      createChildrenViaCrossover.CrossoverParameter.ActualName = CrossoverParameter.Name;
532      createChildrenViaCrossover.MutatorParameter.ActualName = MutatorParameter.Name;
533      createChildrenViaCrossover.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
534      createChildrenViaCrossover.ElitesParameter.ActualName = ElitesParameter.Name;
535      createChildrenViaCrossover.ReevaluateElitesParameter.ActualName = ReevaluateElitesParameter.Name;
536      createChildrenViaCrossover.PlusSelectionParameter.ActualName = PlusSelectionParameter.Name;
537      createChildrenViaCrossover.AgeParameter.ActualName = AgeParameter.Name;
538      createChildrenViaCrossover.AgeInheritanceParameter.ActualName = AgeInheritanceParameter.Name;
539      createChildrenViaCrossover.AgeIncrementParameter.Value = new DoubleValue(0.0);
540      createChildrenViaCrossover.Successor = incrEvaluatedSolutionsForNewLayer;
541
542      incrEvaluatedSolutionsForNewLayer.ValueParameter.ActualName = LayerEvaluatedSolutionsParameter.Name;
543      incrEvaluatedSolutionsForNewLayer.AccumulateParameter.Value = new BoolValue(true);
544
545      incrOpenLayers.ValueParameter.ActualName = "OpenLayers";
546      incrOpenLayers.Increment = new IntValue(1);
547
548      return layerOpener;
549    }
550
551    private CombinedOperator CreateReseeder() {
552      var reseeder = new CombinedOperator() { Name = "Reseed Layer Zero if needed" };
553      var reseedingController = new ReseedingController() { Name = "Reseeding needed (Generation % AgeGap == 0)?" };
554      var removeIndividuals = new SubScopesRemover();
555      var createIndividuals = new SolutionsCreator();
556      var initializeAgeProsessor = new UniformSubScopesProcessor();
557      var initializeAge = new VariableCreator() { Name = "Initialize Age" };
558      var incrEvaluatedSolutionsAfterReseeding = new SubScopesCounter() { Name = "Update EvaluatedSolutions" };
559
560      reseeder.OperatorGraph.InitialOperator = reseedingController;
561
562      reseedingController.GenerationsParameter.ActualName = "Generations";
563      reseedingController.AgeGapParameter.ActualName = AgeGapParameter.Name;
564      reseedingController.FirstLayerOperator = removeIndividuals;
565      reseedingController.Successor = null;
566
567      removeIndividuals.Successor = createIndividuals;
568
569      createIndividuals.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
570      createIndividuals.Successor = initializeAgeProsessor;
571
572      initializeAgeProsessor.Operator = initializeAge;
573      initializeAgeProsessor.Successor = incrEvaluatedSolutionsAfterReseeding;
574
575      initializeAge.CollectedValues.Add(new ValueParameter<DoubleValue>(AgeParameter.Name, new DoubleValue(0)));
576
577      incrEvaluatedSolutionsAfterReseeding.ValueParameter.ActualName = LayerEvaluatedSolutionsParameter.Name;
578      incrEvaluatedSolutionsAfterReseeding.AccumulateParameter.Value = new BoolValue(true);
579
580      return reseeder;
581    }
582  }
583}
Note: See TracBrowser for help on using the repository browser.