Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ALPS/HeuristicLab.Algorithms.ALPS.SteadyState/3.3/AlpsSsGeneticAlgorithmMainLoop.cs @ 12192

Last change on this file since 12192 was 12192, checked in by pfleck, 9 years ago

#2350 Improved performance of steady state ALPS.

  • Introduced a batch cycle in the main loop to avoid too frequent updates of some variables and analyzers.
  • No cloning of individuals when creating the mating pool.
File size: 14.1 KB
RevLine 
[12120]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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
[12138]22using System.Linq;
[12120]23using HeuristicLab.Common;
24using HeuristicLab.Core;
[12136]25using HeuristicLab.Data;
[12120]26using HeuristicLab.Operators;
[12138]27using HeuristicLab.Optimization;
[12136]28using HeuristicLab.Optimization.Operators;
29using HeuristicLab.Parameters;
[12120]30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[12136]31using HeuristicLab.Selection;
[12120]32
33namespace HeuristicLab.Algorithms.ALPS.SteadyState {
34  [Item("AlpsSsGeneticAlgorithmMainLoop", "An ALPS steady-state genetic algorithm main loop operator.")]
35  [StorableClass]
36  public class AlpsSsGeneticAlgorithmMainLoop : AlgorithmOperator {
[12136]37    #region Parameter Properties
38    public ValueLookupParameter<BoolValue> MaximizationParameter {
39      get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
40    }
41    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
42      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
43    }
44    public ILookupParameter<IntValue> MaximumIterationsParameter {
45      get { return (ILookupParameter<IntValue>)Parameters["MaximumIterations"]; }
46    }
47    public ILookupParameter<IOperator> AnalyzerParameter {
48      get { return (ILookupParameter<IOperator>)Parameters["Analyzer"]; }
49    }
50    public ILookupParameter<IOperator> LayerAnalyzerParameter {
51      get { return (ILookupParameter<IOperator>)Parameters["LayerAnalyzer"]; }
52    }
53    #endregion
54
[12120]55    [StorableConstructor]
56    private AlpsSsGeneticAlgorithmMainLoop(bool deserializing)
57      : base(deserializing) { }
58    private AlpsSsGeneticAlgorithmMainLoop(AlpsSsGeneticAlgorithmMainLoop original, Cloner cloner)
59      : base(original, cloner) { }
60    public override IDeepCloneable Clone(Cloner cloner) {
61      return new AlpsSsGeneticAlgorithmMainLoop(this, cloner);
62    }
63
64    public AlpsSsGeneticAlgorithmMainLoop()
65      : base() {
[12136]66      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
67      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
68      Parameters.Add(new LookupParameter<IntValue>("MaximumIterations", "The maximum number of iterations that the algorithm should process."));
69      Parameters.Add(new LookupParameter<IOperator>("Analyzer", "The operator used to the analyze all individuals."));
70      Parameters.Add(new LookupParameter<IOperator>("LayerAnalyzer", "The operator used to analyze each layer."));
71
72      var variableCreator = new VariableCreator() { Name = "Initialize" };
[12192]73      var resultsColletor = new ResultsCollector();
[12159]74      var layersProcessor = new NamedSubScopeProcessor() { Name = "Process Layers" };
[12192]75      var initializeBatchIteration = new Assigner() { Name = "Initialize BatchIterations" };
76      var randomLayerProcessor = new RandomLayerProcessor() { Name = "Select a layer" };
[12136]77      var isLayerZeroComperator = new Comparator() { Name = "IsLayerZero = Layer == 0" };
78      var isLayerZeroBranch = new ConditionalBranch() { Name = "IsLayerZero?" };
79      var isDoInitBranch = new ConditionalBranch() { Name = "DoInit?" };
80      var setTargetIndedxToNextInit = new Assigner() { Name = "TargetIndex = NextInit" };
81      var incrementNextInit = new IntCounter() { Name = "Incr. NextInit" };
82      var checkInitFinished = new Comparator() { Name = "DoInit = NextInit >= PopulationSize" };
[12171]83      var workingScopeProcessor = new NamedSubScopeProcessor() { Name = "WorkingScope Processor" };
[12136]84      var createRandomIndividual = new SolutionsCreator() { Name = "Create random Individual" };
85      var initializeAgeProcessor = new UniformSubScopesProcessor();
86      var initializeAge = new Assigner() { Name = "Initialize Age" };
[12171]87      var removeEmptySubscope = new SubScopesRemover() { Name = "Remove empty subscope (from solutioncreator)" };
[12136]88      var selectRandomTargetIndex = new RandomIntAssigner();
89      var matingPoolCreator = new SteadyStateMatingPoolCreator() { Name = "Create MatingPool" };
[12159]90      var matingPoolProcessor = new NamedSubScopeProcessor() { Name = "Process MatingPool" };
[12136]91      var matingPoolSize = new SubScopesCounter() { Name = "MatingPoolSize" };
92      var matingPoolSizeMin2 = new Comparator() { Name = "ValidParents = MatingPoolSize >= 2" };
93      var validParentsBranch = new ConditionalBranch() { Name = "ValidParents?" };
[12138]94      var mainOperator = new AlpsSsGeneticAlgorithmMainOperator();
[12136]95      var reactivateInit = new Assigner() { Name = "DoInit = true" };
[12171]96      var resetNextInit = new Assigner() { Name = "NextInit = 1" };
[12136]97      var resetTargetIndex = new Assigner() { Name = "TargetIndex = 0" };
[12142]98      var tryMoveUp = new AlpsSsMover() { Name = "Try Move Up" };
[12192]99      var incrementEvaluations = new IntCounter() { Name = "Incr. EvaluatedSolutions" };
100      var incrementBatchIterations = new IntCounter() { Name = "Incr. BatchIterations" };
101      var batchFinishedComperator = new Comparator() { Name = "BatchFinished = BatchIterations >= BatchSize" };
102      var batchFinishedBranch = new ConditionalBranch() { Name = "BatchFinished?" };
[12138]103      var incrIterations = new IntCounter() { Name = "Incr. Iterations" };
[12192]104      var updateResultEvaluatedSolutions = new IntCounter() { Name = "Update ResultEvaluatedSolutions" };
[12142]105      var layerAnalyzerProcessor = new UniformSubScopesProcessor();
106      var layerAnalyzer = new Placeholder() { Name = "LayerAnalyzer (Placeholder)" };
[12138]107      var analyzer = new Placeholder() { Name = "Analyzer (Placeholder)" };
[12136]108      var iterationsComparator = new Comparator() { Name = "Iterations >= MaximumIterations" };
109      var terminateBranch = new ConditionalBranch() { Name = "Terminate?" };
110
111
112      OperatorGraph.InitialOperator = variableCreator;
113
114      variableCreator.CollectedValues.Add(new ValueParameter<BoolValue>("DoInit", new BoolValue(false)));
115      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("NextInit", new IntValue(0)));
116      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("OpenLayers", new IntValue(1)));
117      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("TargetIndex", new IntValue(0)));
[12142]118      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Iterations", new IntValue(0)));
119      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("MatingPoolSize", new IntValue(0)));
120      variableCreator.CollectedValues.Add(new ValueParameter<BoolValue>("ValidParents", new BoolValue(false)));
[12159]121      variableCreator.CollectedValues.Add(new ValueParameter<BoolValue>("IsLayerZero", new BoolValue(false)));
[12192]122      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("BatchIterations", new IntValue(0)));
123      variableCreator.Successor = resultsColletor;
[12136]124
[12192]125      resultsColletor.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
126      resultsColletor.Successor = layersProcessor;
127
[12159]128      layersProcessor.TargetScopeParameter.ActualName = "Layers";
[12192]129      layersProcessor.Operator = initializeBatchIteration;
[12159]130
[12192]131      initializeBatchIteration.LeftSideParameter.ActualName = "BatchIterations";
132      initializeBatchIteration.RightSideParameter.Value = new IntValue(0);
133      initializeBatchIteration.Successor = randomLayerProcessor;
[12136]134
[12192]135      randomLayerProcessor.Operator = isLayerZeroComperator;
136      randomLayerProcessor.Successor = incrementBatchIterations;
137
[12136]138      isLayerZeroComperator.LeftSideParameter.ActualName = "Layer";
139      isLayerZeroComperator.RightSideParameter.Value = new IntValue(0);
140      isLayerZeroComperator.ResultParameter.ActualName = "IsLayerZero";
141      isLayerZeroComperator.Comparison = new Comparison(ComparisonType.Equal);
142      isLayerZeroComperator.Successor = isLayerZeroBranch;
143
144      isLayerZeroBranch.ConditionParameter.ActualName = "IsLayerZero";
145      isLayerZeroBranch.TrueBranch = isDoInitBranch;
146      isLayerZeroBranch.FalseBranch = selectRandomTargetIndex;
147      isLayerZeroBranch.Successor = tryMoveUp;
148
149      isDoInitBranch.ConditionParameter.ActualName = "DoInit";
150      isDoInitBranch.TrueBranch = setTargetIndedxToNextInit;
151      isDoInitBranch.FalseBranch = selectRandomTargetIndex;
152
153      setTargetIndedxToNextInit.LeftSideParameter.ActualName = "TargetIndex";
154      setTargetIndedxToNextInit.RightSideParameter.ActualName = "NextInit";
155      setTargetIndedxToNextInit.Successor = incrementNextInit;
156
157      incrementNextInit.ValueParameter.ActualName = "NextInit";
158      incrementNextInit.Increment = new IntValue(1);
159      incrementNextInit.Successor = checkInitFinished;
160
161      checkInitFinished.LeftSideParameter.ActualName = "NextInit";
162      checkInitFinished.RightSideParameter.ActualName = "PopulationSize";
163      checkInitFinished.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
164      checkInitFinished.ResultParameter.ActualName = "DoInit";
[12171]165      checkInitFinished.Successor = workingScopeProcessor;
[12136]166
[12171]167      workingScopeProcessor.Operator = createRandomIndividual;
168      workingScopeProcessor.TargetScopeParameter.ActualName = "WorkingScope";
[12136]169
170      createRandomIndividual.NumberOfSolutions = new IntValue(1);
171      createRandomIndividual.Successor = initializeAgeProcessor;
172
173      initializeAgeProcessor.Operator = initializeAge;
[12171]174      initializeAgeProcessor.Successor = removeEmptySubscope;
[12136]175
176      initializeAge.LeftSideParameter.ActualName = "EvalsCreated";
177      initializeAge.RightSideParameter.ActualName = "EvaluatedSolutions";
178
179      selectRandomTargetIndex.LeftSideParameter.ActualName = "TargetIndex";
180      selectRandomTargetIndex.MinimumParameter.Value = new IntValue(0);
[12159]181      selectRandomTargetIndex.MaximumParameter.ActualName = "LayerSize";
[12142]182      selectRandomTargetIndex.MaximumParameter.Value = null;
[12159]183      selectRandomTargetIndex.Successor = matingPoolCreator;
[12136]184
[12159]185      matingPoolCreator.Successor = matingPoolProcessor;
[12136]186
[12159]187      matingPoolProcessor.Operator = matingPoolSize;
188      matingPoolProcessor.TargetScopeParameter.ActualName = "WorkingScope";
[12136]189
190      matingPoolSize.ValueParameter.ActualName = "MatingPoolSize";
[12150]191      matingPoolSize.AccumulateParameter.Value = new BoolValue(false);
[12136]192      matingPoolSize.Successor = matingPoolSizeMin2;
193
194      matingPoolSizeMin2.LeftSideParameter.ActualName = "MatingPoolSize";
195      matingPoolSizeMin2.RightSideParameter.Value = new IntValue(2);
196      matingPoolSizeMin2.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
197      matingPoolSizeMin2.ResultParameter.ActualName = "ValidParents";
198      matingPoolSizeMin2.Successor = validParentsBranch;
199
[12138]200      validParentsBranch.ConditionParameter.ActualName = "ValidParents";
[12136]201      validParentsBranch.TrueBranch = mainOperator;
202      validParentsBranch.FalseBranch = reactivateInit;
203
[12171]204      reactivateInit.Successor = resetNextInit;
[12136]205      reactivateInit.LeftSideParameter.ActualName = "DoInit";
206      reactivateInit.RightSideParameter.Value = new BoolValue(true);
207
[12171]208      resetNextInit.Successor = resetTargetIndex;
209      resetNextInit.LeftSideParameter.ActualName = "NextInit";
210      resetNextInit.RightSideParameter.Value = new IntValue(1);
[12136]211
212      resetTargetIndex.LeftSideParameter.ActualName = "TargetIndex";
213      resetTargetIndex.RightSideParameter.Value = new IntValue(0);
[12171]214      resetTargetIndex.Successor = createRandomIndividual;
[12136]215
[12162]216      tryMoveUp.Successor = incrementEvaluations;
[12136]217
[12192]218      incrementEvaluations.ValueParameter.ActualName = "EvaluatedSolutions";
[12150]219      incrementEvaluations.Increment = new IntValue(1);
220
[12192]221      incrementBatchIterations.ValueParameter.ActualName = "BatchIterations";
222      incrementBatchIterations.Increment = new IntValue(1);
223      incrementBatchIterations.Successor = batchFinishedComperator;
224
225      batchFinishedComperator.LeftSideParameter.ActualName = "BatchIterations";
226      batchFinishedComperator.RightSideParameter.ActualName = "BatchSize";
227      batchFinishedComperator.ResultParameter.ActualName = "BatchFinished";
228      batchFinishedComperator.Successor = batchFinishedBranch;
229
230      batchFinishedBranch.ConditionParameter.ActualName = "BatchFinished";
231      batchFinishedBranch.TrueBranch = incrIterations;
232      batchFinishedBranch.FalseBranch = randomLayerProcessor;
233
[12138]234      incrIterations.ValueParameter.ActualName = "Iterations";
235      incrIterations.Increment = new IntValue(1);
[12192]236      incrIterations.Successor = updateResultEvaluatedSolutions;
[12138]237
[12192]238      updateResultEvaluatedSolutions.ValueParameter.ActualName = "ResultEvaluatedSolutions";
239      updateResultEvaluatedSolutions.IncrementParameter.ActualName = "BatchSize";
240      updateResultEvaluatedSolutions.IncrementParameter.Value = null;
241      updateResultEvaluatedSolutions.Successor = layerAnalyzerProcessor;
242
[12142]243      layerAnalyzerProcessor.Operator = layerAnalyzer;
244      layerAnalyzerProcessor.Successor = analyzer;
245
246      layerAnalyzer.OperatorParameter.ActualName = "LayerAnalyzer";
247
[12138]248      analyzer.OperatorParameter.ActualName = "Analyzer";
249      analyzer.Successor = iterationsComparator;
250
[12136]251      iterationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
252      iterationsComparator.LeftSideParameter.ActualName = "Iterations";
253      iterationsComparator.RightSideParameter.ActualName = MaximumIterationsParameter.Name;
254      iterationsComparator.ResultParameter.ActualName = "Terminate";
255      iterationsComparator.Successor = terminateBranch;
256
257      terminateBranch.ConditionParameter.ActualName = "Terminate";
[12192]258      terminateBranch.FalseBranch = initializeBatchIteration;
[12120]259    }
260  }
261}
Note: See TracBrowser for help on using the repository browser.