Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13113 was 13113, checked in by pfleck, 8 years ago

#2269 Some small changes.

File size: 14.5 KB
RevLine 
[11580]1#region License Information
2/* HeuristicLab
[12018]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[11580]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
[11583]22using System.Linq;
23using HeuristicLab.Algorithms.GeneticAlgorithm;
[11580]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;
[13111]32using HeuristicLab.Selection;
[11580]33
34namespace HeuristicLab.Algorithms.ALPS {
35
36  [Item("AlpsGeneticAlgorithmMainLoop", "An ALPS genetic algorithm main loop operator.")]
[11585]37  [StorableClass]
[11583]38  public sealed class AlpsGeneticAlgorithmMainLoop : AlgorithmOperator {
[11580]39    #region Parameter Properties
[12045]40    public ValueLookupParameter<BoolValue> MaximizationParameter {
41      get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
42    }
43    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
44      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
45    }
[11580]46    public ILookupParameter<IOperator> AnalyzerParameter {
47      get { return (ILookupParameter<IOperator>)Parameters["Analyzer"]; }
48    }
49    public ILookupParameter<IOperator> LayerAnalyzerParameter {
50      get { return (ILookupParameter<IOperator>)Parameters["LayerAnalyzer"]; }
51    }
52    #endregion
53
[11583]54    public GeneticAlgorithmMainLoop MainOperator {
55      get { return OperatorGraph.Iterate().OfType<GeneticAlgorithmMainLoop>().First(); }
56    }
57
[11580]58    [StorableConstructor]
59    private AlpsGeneticAlgorithmMainLoop(bool deserializing)
60      : base(deserializing) { }
61    private AlpsGeneticAlgorithmMainLoop(AlpsGeneticAlgorithmMainLoop original, Cloner cloner)
62      : base(original, cloner) { }
63    public override IDeepCloneable Clone(Cloner cloner) {
64      return new AlpsGeneticAlgorithmMainLoop(this, cloner);
65    }
66    public AlpsGeneticAlgorithmMainLoop()
67      : base() {
[12997]68      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false.") { Hidden = true });
[12045]69      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
[11580]70      Parameters.Add(new LookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations that the algorithm should process."));
71      Parameters.Add(new LookupParameter<IOperator>("Analyzer", "The operator used to the analyze all individuals."));
72      Parameters.Add(new LookupParameter<IOperator>("LayerAnalyzer", "The operator used to analyze each layer."));
73
74      var variableCreator = new VariableCreator() { Name = "Initialize" };
75      var initLayerAnalyzerProcessor = new SubScopesProcessor();
[11590]76      var layerVariableCreator = new VariableCreator() { Name = "Initialize Layer" };
[11580]77      var initLayerAnalyzerPlaceholder = new Placeholder() { Name = "LayerAnalyzer (Placeholder)" };
[11609]78      var initAnalyzerPlaceholder = new Placeholder() { Name = "Analyzer (Placeholder)" };
79      var resultsCollector = new ResultsCollector();
[11583]80      var matingPoolCreator = new MatingPoolCreator() { Name = "Create Mating Pools" };
[13113]81      var matingPoolProcessor = new UniformSubScopesProcessor() { Name = "Process Mating Pools" };
[11590]82      var initializeLayer = new Assigner() { Name = "Reset LayerEvaluatedSolutions" };
[13095]83      var mainOperator = AlpsGeneticAlgorithmMainOperator.Create();
[11583]84      var generationsIcrementor = new IntCounter() { Name = "Increment Generations" };
85      var evaluatedSolutionsReducer = new DataReducer() { Name = "Increment EvaluatedSolutions" };
[13111]86      var eldersEmigrator = CreateEldersEmigrator();
87      var layerOpener = CreateLayerOpener();
88      var layerReseeder = CreateReseeder();
[12992]89      var layerAnalyzerProcessor = new UniformSubScopesProcessor();
[12197]90      var layerAnalyzerPlaceholder = new Placeholder() { Name = "LayerAnalyzer (Placeholder)" };
[11580]91      var analyzerPlaceholder = new Placeholder() { Name = "Analyzer (Placeholder)" };
[12531]92      var termination = new TerminationOperator();
[11580]93
94      OperatorGraph.InitialOperator = variableCreator;
95
96      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0)));
[11586]97      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("OpenLayers", new IntValue(1)));
[11609]98      variableCreator.Successor = initLayerAnalyzerProcessor;
[11580]99
[11590]100      initLayerAnalyzerProcessor.Operators.Add(layerVariableCreator);
[11609]101      initLayerAnalyzerProcessor.Successor = initAnalyzerPlaceholder;
[11580]102
[11590]103      layerVariableCreator.CollectedValues.Add(new ValueParameter<IntValue>("LayerEvaluatedSolutions"));
[12071]104      layerVariableCreator.CollectedValues.Add(new ValueParameter<ResultCollection>("LayerResults"));
[11590]105      layerVariableCreator.Successor = initLayerAnalyzerPlaceholder;
106
[11580]107      initLayerAnalyzerPlaceholder.OperatorParameter.ActualName = LayerAnalyzerParameter.Name;
108      initLayerAnalyzerPlaceholder.Successor = null;
109
[11609]110      initAnalyzerPlaceholder.OperatorParameter.ActualName = AnalyzerParameter.Name;
111      initAnalyzerPlaceholder.Successor = resultsCollector;
112
113      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
[12071]114      resultsCollector.CollectedValues.Add(new ScopeTreeLookupParameter<ResultCollection>("LayerResults", "Result set for each layer", "LayerResults"));
[11609]115      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("OpenLayers"));
116      resultsCollector.CopyValue = new BoolValue(false);
[13031]117      resultsCollector.Successor = matingPoolCreator;
[11609]118
[11580]119      matingPoolCreator.Successor = matingPoolProcessor;
120
121      matingPoolProcessor.Parallel.Value = true;
[11590]122      matingPoolProcessor.Operator = initializeLayer;
[11583]123      matingPoolProcessor.Successor = generationsIcrementor;
[11580]124
[11590]125      initializeLayer.LeftSideParameter.ActualName = "LayerEvaluatedSolutions";
126      initializeLayer.RightSideParameter.Value = new IntValue(0);
127      initializeLayer.Successor = mainOperator;
128
[11583]129      generationsIcrementor.ValueParameter.ActualName = "Generations";
130      generationsIcrementor.Increment = new IntValue(1);
131      generationsIcrementor.Successor = evaluatedSolutionsReducer;
132
[11676]133      evaluatedSolutionsReducer.ParameterToReduce.ActualName = "LayerEvaluatedSolutions";
[11583]134      evaluatedSolutionsReducer.TargetParameter.ActualName = "EvaluatedSolutions";
135      evaluatedSolutionsReducer.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum);
136      evaluatedSolutionsReducer.TargetOperation.Value = new ReductionOperation(ReductionOperations.Sum);
137      evaluatedSolutionsReducer.Successor = eldersEmigrator;
138
[13095]139      eldersEmigrator.Successor = layerOpener;
[11580]140
[13095]141      layerOpener.Successor = layerReseeder;
[11580]142
[13095]143      layerReseeder.Successor = layerAnalyzerProcessor;
144
[12197]145      layerAnalyzerProcessor.Operator = layerAnalyzerPlaceholder;
146      layerAnalyzerProcessor.Successor = analyzerPlaceholder;
[11580]147
[12197]148      layerAnalyzerPlaceholder.OperatorParameter.ActualName = LayerAnalyzerParameter.Name;
[11580]149
150      analyzerPlaceholder.OperatorParameter.ActualName = AnalyzerParameter.Name;
[12531]151      analyzerPlaceholder.Successor = termination;
[11580]152
[13031]153      termination.ContinueBranch = matingPoolCreator;
[11583]154    }
[13111]155
156    private static CombinedOperator CreateEldersEmigrator() {
157      var eldersEmigrator = new CombinedOperator() { Name = "Emigrate Elders" };
158      var selectorProsessor = new UniformSubScopesProcessor();
159      var eldersSelector = new EldersSelector();
[13113]160      var shiftToRightMigrator = new UnidirectionalRingMigrator() { Name = "Shift elders to next Layer" };
[13111]161      var mergingProsessor = new UniformSubScopesProcessor();
162      var mergingReducer = new MergingReducer();
163      var subScopesCounter = new SubScopesCounter();
164      var countCalculator = new ExpressionCalculator() { Name = "LayerPopulationSize = Min(LayerPopulationSize, PopulationSize)" };
165      var bestSelector = new BestSelector();
166      var rightReducer = new RightReducer();
167
168      eldersEmigrator.OperatorGraph.InitialOperator = selectorProsessor;
169
170      selectorProsessor.Operator = eldersSelector;
171      selectorProsessor.Successor = shiftToRightMigrator;
172
173      shiftToRightMigrator.ClockwiseMigrationParameter.Value = new BoolValue(true);
174      shiftToRightMigrator.Successor = mergingProsessor;
175
176      mergingProsessor.Operator = mergingReducer;
177
178      mergingReducer.Successor = subScopesCounter;
179
180      subScopesCounter.ValueParameter.ActualName = "LayerPopulationSize";
181      subScopesCounter.AccumulateParameter.Value = new BoolValue(false);
182      subScopesCounter.Successor = countCalculator;
183
184      countCalculator.CollectedValues.Add(new LookupParameter<IntValue>("PopulationSize"));
185      countCalculator.CollectedValues.Add(new LookupParameter<IntValue>("LayerPopulationSize"));
186      countCalculator.ExpressionParameter.Value = new StringValue("LayerPopulationSize PopulationSize LayerPopulationSize PopulationSize < if toint");
187      countCalculator.ExpressionResultParameter.ActualName = "LayerPopulationSize";
188      countCalculator.Successor = bestSelector;
189
190      bestSelector.NumberOfSelectedSubScopesParameter.ActualName = "LayerPopulationSize";
191      bestSelector.CopySelected = new BoolValue(false);
192      bestSelector.Successor = rightReducer;
193
194      return eldersEmigrator;
195    }
196
197    private static CombinedOperator CreateLayerOpener() {
198      var layerOpener = new CombinedOperator() { Name = "Open new Layer if needed" };
199      var maxLayerReached = new Comparator() { Name = "MaxLayersReached = OpenLayers >= NumberOfLayers" };
200      var maxLayerReachedBranch = new ConditionalBranch() { Name = "MaxLayersReached?" };
201      var openNewLayerCalculator = new ExpressionCalculator() { Name = "OpenNewLayer = Generations >= AgeLimits[OpenLayers - 1]" };
202      var openNewLayerBranch = new ConditionalBranch() { Name = "OpenNewLayer?" };
203      var layerCreator = new LayerCreator() { Name = "Create Layer" };
204      var createChildrenViaCrossover = AlpsGeneticAlgorithmMainOperator.Create();
205      var incrEvaluatedSolutionsForNewLayer = new SubScopesCounter() { Name = "Update EvaluatedSolutions" };
206      var incrOpenLayers = new IntCounter() { Name = "Incr. OpenLayers" };
207      var newLayerResultsCollector = new ResultsCollector() { Name = "Collect new Layer Results" };
208
209      layerOpener.OperatorGraph.InitialOperator = maxLayerReached;
210
211      maxLayerReached.LeftSideParameter.ActualName = "OpenLayers";
212      maxLayerReached.RightSideParameter.ActualName = "NumberOfLayers";
213      maxLayerReached.ResultParameter.ActualName = "MaxLayerReached";
214      maxLayerReached.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
215      maxLayerReached.Successor = maxLayerReachedBranch;
216
217      maxLayerReachedBranch.ConditionParameter.ActualName = "MaxLayerReached";
218      maxLayerReachedBranch.FalseBranch = openNewLayerCalculator;
219
220      openNewLayerCalculator.CollectedValues.Add(new LookupParameter<IntArray>("AgeLimits"));
221      openNewLayerCalculator.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
222      openNewLayerCalculator.CollectedValues.Add(new LookupParameter<IntValue>("NumberOfLayers"));
223      openNewLayerCalculator.CollectedValues.Add(new LookupParameter<IntValue>("OpenLayers"));
224      openNewLayerCalculator.ExpressionResultParameter.ActualName = "OpenNewLayer";
225      openNewLayerCalculator.ExpressionParameter.Value = new StringValue("Generations 1 + AgeLimits OpenLayers 1 - [] >");
226      openNewLayerCalculator.Successor = openNewLayerBranch;
227
228      openNewLayerBranch.ConditionParameter.ActualName = "OpenNewLayer";
229      openNewLayerBranch.TrueBranch = layerCreator;
230
231      layerCreator.NewLayerOperator = createChildrenViaCrossover;
232      layerCreator.Successor = incrOpenLayers;
233
234      createChildrenViaCrossover.Successor = incrEvaluatedSolutionsForNewLayer;
235
236      incrEvaluatedSolutionsForNewLayer.ValueParameter.ActualName = "EvaluatedSolutions";
237      incrEvaluatedSolutionsForNewLayer.AccumulateParameter.Value = new BoolValue(true);
238
239      incrOpenLayers.ValueParameter.ActualName = "OpenLayers";
240      incrOpenLayers.Increment = new IntValue(1);
241      incrOpenLayers.Successor = newLayerResultsCollector;
242
243      newLayerResultsCollector.CollectedValues.Add(new ScopeTreeLookupParameter<ResultCollection>("LayerResults", "Result set for each layer", "LayerResults"));
244      newLayerResultsCollector.CopyValue = new BoolValue(false);
245      newLayerResultsCollector.Successor = null;
246
247      return layerOpener;
248    }
249
250    private static CombinedOperator CreateReseeder() {
251      var reseeder = new CombinedOperator() { Name = "Reseed Layer Zero if needed" };
252      var reseedingController = new ReseedingController() { Name = "Reseeding needed (Generation % AgeGap == 0)?" };
253      var removeIndividuals = new SubScopesRemover();
254      var createIndividuals = new SolutionsCreator();
255      var initializeAgeProsessor = new UniformSubScopesProcessor();
256      var initializeAge = new VariableCreator() { Name = "Initialize Age" };
257      var incrEvaluatedSolutionsAfterReseeding = new SubScopesCounter() { Name = "Update EvaluatedSolutions" };
258
259      reseeder.OperatorGraph.InitialOperator = reseedingController;
260
261      reseedingController.FirstLayerOperator = removeIndividuals;
262
263      removeIndividuals.Successor = createIndividuals;
264
265      createIndividuals.NumberOfSolutionsParameter.ActualName = "PopulationSize";
266      createIndividuals.Successor = initializeAgeProsessor;
267
268      initializeAgeProsessor.Operator = initializeAge;
269      initializeAgeProsessor.Successor = incrEvaluatedSolutionsAfterReseeding;
270
271      initializeAge.CollectedValues.Add(new ValueParameter<DoubleValue>("Age", new DoubleValue(0)));
272
273      incrEvaluatedSolutionsAfterReseeding.ValueParameter.ActualName = "EvaluatedSolutions";
274      incrEvaluatedSolutionsAfterReseeding.AccumulateParameter.Value = new BoolValue(true);
275
276      return reseeder;
277    }
[11580]278  }
279}
Note: See TracBrowser for help on using the repository browser.