source: branches/ALPS/HeuristicLab.Algorithms.ALPS/3.3/AlpsGeneticAlgorithmMainLoop.cs @ 11676

Last change on this file since 11676 was 11676, checked in by pfleck, 5 years ago

#2269 Added LayerPopulationSize variable (for PopulationSizeAnalyzer)

File size: 10.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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.Linq;
23using HeuristicLab.Algorithms.GeneticAlgorithm;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Optimization.Operators;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.Selection;
33
34namespace HeuristicLab.Algorithms.ALPS {
35
36  [Item("AlpsGeneticAlgorithmMainLoop", "An ALPS genetic algorithm main loop operator.")]
37  [StorableClass]
38  public sealed class AlpsGeneticAlgorithmMainLoop : AlgorithmOperator {
39    #region Parameter Properties
40    public ILookupParameter<IntValue> MaximumGenerationsParameter {
41      get { return (ILookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
42    }
43    public ILookupParameter<IOperator> AnalyzerParameter {
44      get { return (ILookupParameter<IOperator>)Parameters["Analyzer"]; }
45    }
46    public ILookupParameter<IOperator> LayerAnalyzerParameter {
47      get { return (ILookupParameter<IOperator>)Parameters["LayerAnalyzer"]; }
48    }
49    #endregion
50
51    public GeneticAlgorithmMainLoop MainOperator {
52      get { return OperatorGraph.Iterate().OfType<GeneticAlgorithmMainLoop>().First(); }
53    }
54    public EldersEmigrator EldersEmigrator {
55      get { return OperatorGraph.Iterate().OfType<EldersEmigrator>().First(); }
56    }
57    public LayerUpdator LayerUpdator {
58      get { return OperatorGraph.Iterate().OfType<LayerUpdator>().First(); }
59    }
60
61    [StorableConstructor]
62    private AlpsGeneticAlgorithmMainLoop(bool deserializing)
63      : base(deserializing) { }
64    private AlpsGeneticAlgorithmMainLoop(AlpsGeneticAlgorithmMainLoop original, Cloner cloner)
65      : base(original, cloner) { }
66    public override IDeepCloneable Clone(Cloner cloner) {
67      return new AlpsGeneticAlgorithmMainLoop(this, cloner);
68    }
69    public AlpsGeneticAlgorithmMainLoop()
70      : base() {
71      Parameters.Add(new LookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations that the algorithm should process."));
72      Parameters.Add(new LookupParameter<IOperator>("Analyzer", "The operator used to the analyze all individuals."));
73      Parameters.Add(new LookupParameter<IOperator>("LayerAnalyzer", "The operator used to analyze each layer."));
74
75      var variableCreator = new VariableCreator() { Name = "Initialize" };
76      var initLayerAnalyzerProcessor = new SubScopesProcessor();
77      var layerVariableCreator = new VariableCreator() { Name = "Initialize Layer" };
78      var initLayerAnalyzerPlaceholder = new Placeholder() { Name = "LayerAnalyzer (Placeholder)" };
79      var initAnalyzerPlaceholder = new Placeholder() { Name = "Analyzer (Placeholder)" };
80      var resultsCollector = new ResultsCollector();
81      var matingPoolCreator = new MatingPoolCreator() { Name = "Create Mating Pools" };
82      var matingPoolProcessor = new UniformSubScopesProcessor();
83      var initializeLayer = new Assigner() { Name = "Reset LayerEvaluatedSolutions" };
84      var mainOperator = CreatePreparedGeneticAlgorithmMainLoop();
85      var layerAnalyzerPlaceholder = new Placeholder() { Name = "LayerAnalyzer (Placeholder)" };
86      var generationsIcrementor = new IntCounter() { Name = "Increment Generations" };
87      var evaluatedSolutionsReducer = new DataReducer() { Name = "Increment EvaluatedSolutions" };
88      var eldersEmigrator = new EldersEmigrator() { Name = "Emigrate Elders" };
89      var layerUpdator = new LayerUpdator(mainOperator) { Name = "Update Layers" };
90      var analyzerPlaceholder = new Placeholder() { Name = "Analyzer (Placeholder)" };
91      var generationsComparator = new Comparator() { Name = "Generations >= MaximumGenerations" };
92      var terminateBranch = new ConditionalBranch() { Name = "Terminate?" };
93
94      OperatorGraph.InitialOperator = variableCreator;
95
96      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0)));
97      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("GenerationsSinceLastRefresh", new IntValue(0)));
98      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("OpenLayers", new IntValue(1)));
99      variableCreator.Successor = initLayerAnalyzerProcessor;
100
101      initLayerAnalyzerProcessor.Operators.Add(layerVariableCreator);
102      initLayerAnalyzerProcessor.Successor = initAnalyzerPlaceholder;
103
104      layerVariableCreator.CollectedValues.Add(new ValueParameter<IntValue>("LayerEvaluatedSolutions"));
105      layerVariableCreator.CollectedValues.Add(new ValueParameter<ResultCollection>("Results"));
106      layerVariableCreator.Successor = initLayerAnalyzerPlaceholder;
107
108      initLayerAnalyzerPlaceholder.OperatorParameter.ActualName = LayerAnalyzerParameter.Name;
109      initLayerAnalyzerPlaceholder.Successor = null;
110
111      initAnalyzerPlaceholder.OperatorParameter.ActualName = AnalyzerParameter.Name;
112      initAnalyzerPlaceholder.Successor = resultsCollector;
113
114      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
115      resultsCollector.CollectedValues.Add(new ScopeTreeLookupParameter<ResultCollection>("LayerResults", "Result set for each layer", "Results"));
116      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("OpenLayers"));
117      resultsCollector.CopyValue = new BoolValue(false);
118      resultsCollector.Successor = matingPoolCreator;
119
120      matingPoolCreator.Successor = matingPoolProcessor;
121
122      matingPoolProcessor.Parallel.Value = true;
123      matingPoolProcessor.Operator = initializeLayer;
124      matingPoolProcessor.Successor = generationsIcrementor;
125
126      initializeLayer.LeftSideParameter.ActualName = "LayerEvaluatedSolutions";
127      initializeLayer.RightSideParameter.Value = new IntValue(0);
128      initializeLayer.Successor = mainOperator;
129
130      generationsIcrementor.ValueParameter.ActualName = "Generations";
131      generationsIcrementor.Increment = new IntValue(1);
132      generationsIcrementor.Successor = evaluatedSolutionsReducer;
133
134      evaluatedSolutionsReducer.ParameterToReduce.ActualName = "LayerEvaluatedSolutions";
135      evaluatedSolutionsReducer.TargetParameter.ActualName = "EvaluatedSolutions";
136      evaluatedSolutionsReducer.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum);
137      evaluatedSolutionsReducer.TargetOperation.Value = new ReductionOperation(ReductionOperations.Sum);
138      evaluatedSolutionsReducer.Successor = eldersEmigrator;
139
140      mainOperator.Successor = layerAnalyzerPlaceholder;
141
142      layerAnalyzerPlaceholder.OperatorParameter.ActualName = LayerAnalyzerParameter.Name;
143      layerAnalyzerPlaceholder.Successor = null;
144
145      eldersEmigrator.Successor = layerUpdator;
146
147      layerUpdator.Successor = analyzerPlaceholder;
148
149      analyzerPlaceholder.OperatorParameter.ActualName = AnalyzerParameter.Name;
150      analyzerPlaceholder.Successor = generationsComparator;
151
152      generationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
153      generationsComparator.LeftSideParameter.ActualName = "Generations";
154      generationsComparator.RightSideParameter.ActualName = MaximumGenerationsParameter.Name;
155      generationsComparator.ResultParameter.ActualName = "TerminateGenerations";
156      generationsComparator.Successor = terminateBranch;
157
158      terminateBranch.ConditionParameter.ActualName = "TerminateGenerations";
159      terminateBranch.FalseBranch = matingPoolCreator;
160    }
161
162    private GeneticAlgorithmMainLoop CreatePreparedGeneticAlgorithmMainLoop() {
163      var mainLoop = new GeneticAlgorithmMainLoop();
164      var selector = mainLoop.OperatorGraph.Iterate().OfType<Placeholder>().First(o => o.OperatorParameter.ActualName == "Selector");
165      var crossover = mainLoop.OperatorGraph.Iterate().OfType<Placeholder>().First(o => o.OperatorParameter.ActualName == "Crossover");
166      var subScopesCounter = mainLoop.OperatorGraph.Iterate().OfType<SubScopesCounter>().First();
167      var elitesMerger = mainLoop.OperatorGraph.Iterate().OfType<MergingReducer>().First();
168
169      // Operator starts with selector
170      mainLoop.OperatorGraph.InitialOperator = selector;
171
172      // Insert AgeCalculator between crossover and its successor
173      var crossoverSuccessor = crossover.Successor;
174      var ageCalculator = new DataReducer() { Name = "Calculate Age" };
175      ageCalculator.ParameterToReduce.ActualName = "Age";
176      ageCalculator.TargetParameter.ActualName = "Age";
177      ageCalculator.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Max);
178      ageCalculator.TargetOperation.Value = new ReductionOperation(ReductionOperations.Assign);
179      crossover.Successor = ageCalculator;
180      ageCalculator.Successor = crossoverSuccessor;
181
182      // When counting the evaluated solutions, write in LayerEvaluatedSolutions
183      subScopesCounter.ValueParameter.ActualName = "LayerEvaluatedSolutions";
184      subScopesCounter.AccumulateParameter.Value = new BoolValue(false);
185
186      // Instead of generational loop after merging of elites, increment ages of all individuals
187      var processor = new UniformSubScopesProcessor();
188      var incrementor = new IntCounter() { Name = "Increment Age" };
189      processor.Operator = incrementor;
190      processor.Successor = null;
191      incrementor.ValueParameter.ActualName = "Age";
192      incrementor.Increment = new IntValue(1);
193      incrementor.Successor = null;
194      elitesMerger.Successor = processor;
195
196      // Parameterize
197      foreach (var stochasticOperator in mainLoop.OperatorGraph.Iterate().OfType<IStochasticOperator>())
198        stochasticOperator.RandomParameter.ActualName = "LocalRandom";
199      foreach (var stochasticBranch in mainLoop.OperatorGraph.Iterate().OfType<StochasticBranch>())
200        stochasticBranch.RandomParameter.ActualName = "LocalRandom";
201
202      return mainLoop;
203    }
204  }
205}
Note: See TracBrowser for help on using the repository browser.