Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2350

  • Implemented elitism by sorting the layer and restricting the random target index.
  • Renamed Layers in LayersScope.
File size: 14.4 KB
Line 
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
22using System.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Optimization.Operators;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Selection;
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 {
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
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() {
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" };
73      var resultsColletor = new ResultsCollector();
74      var layersProcessor = new NamedSubScopeProcessor() { Name = "Process Layers" };
75      var initializeBatchIteration = new Assigner() { Name = "Initialize BatchIterations" };
76      var randomLayerProcessor = new RandomLayerProcessor() { Name = "Select a layer" };
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" };
83      var workingScopeProcessor = new NamedSubScopeProcessor() { Name = "WorkingScope Processor" };
84      var createRandomIndividual = new SolutionsCreator() { Name = "Create random Individual" };
85      var initializeAgeProcessor = new UniformSubScopesProcessor();
86      var initializeAge = new Assigner() { Name = "Initialize Age" };
87      var removeEmptySubscope = new SubScopesRemover() { Name = "Remove empty subscope (from solutioncreator)" };
88      var layerSorter = new SubScopesSorter() { Name = "Sort Layer" };
89      var selectRandomTargetIndex = new RandomIntAssigner();
90      var matingPoolCreator = new SteadyStateMatingPoolCreator() { Name = "Create MatingPool" };
91      var matingPoolProcessor = new NamedSubScopeProcessor() { Name = "Process MatingPool" };
92      var matingPoolSize = new SubScopesCounter() { Name = "MatingPoolSize" };
93      var matingPoolSizeMin2 = new Comparator() { Name = "ValidParents = MatingPoolSize >= 2" };
94      var validParentsBranch = new ConditionalBranch() { Name = "ValidParents?" };
95      var mainOperator = new AlpsSsGeneticAlgorithmMainOperator();
96      var reactivateInit = new Assigner() { Name = "DoInit = true" };
97      var resetNextInit = new Assigner() { Name = "NextInit = 1" };
98      var resetTargetIndex = new Assigner() { Name = "TargetIndex = 0" };
99      var tryMoveUp = new AlpsSsMover() { Name = "Try Move Up" };
100      var incrementEvaluations = new IntCounter() { Name = "Incr. EvaluatedSolutions" };
101      var incrementBatchIterations = new IntCounter() { Name = "Incr. BatchIterations" };
102      var batchFinishedComperator = new Comparator() { Name = "BatchFinished = BatchIterations >= BatchSize" };
103      var batchFinishedBranch = new ConditionalBranch() { Name = "BatchFinished?" };
104      var incrIterations = new IntCounter() { Name = "Incr. Iterations" };
105      var updateResultEvaluatedSolutions = new IntCounter() { Name = "Update ResultEvaluatedSolutions" };
106      var layerAnalyzerProcessor = new UniformSubScopesProcessor();
107      var layerAnalyzer = new Placeholder() { Name = "LayerAnalyzer (Placeholder)" };
108      var analyzer = new Placeholder() { Name = "Analyzer (Placeholder)" };
109      var iterationsComparator = new Comparator() { Name = "Iterations >= MaximumIterations" };
110      var terminateBranch = new ConditionalBranch() { Name = "Terminate?" };
111
112
113      OperatorGraph.InitialOperator = variableCreator;
114
115      variableCreator.CollectedValues.Add(new ValueParameter<BoolValue>("DoInit", new BoolValue(false)));
116      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("NextInit", new IntValue(0)));
117      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("OpenLayers", new IntValue(1)));
118      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("TargetIndex", new IntValue(0)));
119      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Iterations", new IntValue(0)));
120      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("MatingPoolSize", new IntValue(0)));
121      variableCreator.CollectedValues.Add(new ValueParameter<BoolValue>("ValidParents", new BoolValue(false)));
122      variableCreator.CollectedValues.Add(new ValueParameter<BoolValue>("IsLayerZero", new BoolValue(false)));
123      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("BatchIterations", new IntValue(0)));
124      variableCreator.Successor = resultsColletor;
125
126      resultsColletor.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
127      resultsColletor.Successor = layersProcessor;
128
129      layersProcessor.TargetScopeParameter.ActualName = "LayersScope";
130      layersProcessor.Operator = initializeBatchIteration;
131
132      initializeBatchIteration.LeftSideParameter.ActualName = "BatchIterations";
133      initializeBatchIteration.RightSideParameter.Value = new IntValue(0);
134      initializeBatchIteration.Successor = randomLayerProcessor;
135
136      randomLayerProcessor.Operator = isLayerZeroComperator;
137      randomLayerProcessor.Successor = incrementBatchIterations;
138
139      isLayerZeroComperator.LeftSideParameter.ActualName = "Layer";
140      isLayerZeroComperator.RightSideParameter.Value = new IntValue(0);
141      isLayerZeroComperator.ResultParameter.ActualName = "IsLayerZero";
142      isLayerZeroComperator.Comparison = new Comparison(ComparisonType.Equal);
143      isLayerZeroComperator.Successor = isLayerZeroBranch;
144
145      isLayerZeroBranch.ConditionParameter.ActualName = "IsLayerZero";
146      isLayerZeroBranch.TrueBranch = isDoInitBranch;
147      isLayerZeroBranch.FalseBranch = layerSorter;
148      isLayerZeroBranch.Successor = tryMoveUp;
149
150      isDoInitBranch.ConditionParameter.ActualName = "DoInit";
151      isDoInitBranch.TrueBranch = setTargetIndedxToNextInit;
152      isDoInitBranch.FalseBranch = layerSorter;
153
154      setTargetIndedxToNextInit.LeftSideParameter.ActualName = "TargetIndex";
155      setTargetIndedxToNextInit.RightSideParameter.ActualName = "NextInit";
156      setTargetIndedxToNextInit.Successor = incrementNextInit;
157
158      incrementNextInit.ValueParameter.ActualName = "NextInit";
159      incrementNextInit.Increment = new IntValue(1);
160      incrementNextInit.Successor = checkInitFinished;
161
162      checkInitFinished.LeftSideParameter.ActualName = "NextInit";
163      checkInitFinished.RightSideParameter.ActualName = "PopulationSize";
164      checkInitFinished.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
165      checkInitFinished.ResultParameter.ActualName = "DoInit";
166      checkInitFinished.Successor = workingScopeProcessor;
167
168      workingScopeProcessor.Operator = createRandomIndividual;
169      workingScopeProcessor.TargetScopeParameter.ActualName = "WorkingScope";
170
171      createRandomIndividual.NumberOfSolutions = new IntValue(1);
172      createRandomIndividual.Successor = initializeAgeProcessor;
173
174      initializeAgeProcessor.Operator = initializeAge;
175      initializeAgeProcessor.Successor = removeEmptySubscope;
176
177      initializeAge.LeftSideParameter.ActualName = "EvalsCreated";
178      initializeAge.RightSideParameter.ActualName = "EvaluatedSolutions";
179
180      layerSorter.ValueParameter.ActualName = "Quality";
181      layerSorter.DescendingParameter.ActualName = "Maximization";
182      layerSorter.Successor = selectRandomTargetIndex;
183
184      selectRandomTargetIndex.LeftSideParameter.ActualName = "TargetIndex";
185      selectRandomTargetIndex.MinimumParameter.ActualName = "Elites";
186      selectRandomTargetIndex.MinimumParameter.Value = null;
187      selectRandomTargetIndex.MaximumParameter.ActualName = "LayerSize";
188      selectRandomTargetIndex.MaximumParameter.Value = null;
189      selectRandomTargetIndex.Successor = matingPoolCreator;
190
191      matingPoolCreator.Successor = matingPoolProcessor;
192
193      matingPoolProcessor.Operator = matingPoolSize;
194      matingPoolProcessor.TargetScopeParameter.ActualName = "WorkingScope";
195
196      matingPoolSize.ValueParameter.ActualName = "MatingPoolSize";
197      matingPoolSize.AccumulateParameter.Value = new BoolValue(false);
198      matingPoolSize.Successor = matingPoolSizeMin2;
199
200      matingPoolSizeMin2.LeftSideParameter.ActualName = "MatingPoolSize";
201      matingPoolSizeMin2.RightSideParameter.Value = new IntValue(2);
202      matingPoolSizeMin2.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
203      matingPoolSizeMin2.ResultParameter.ActualName = "ValidParents";
204      matingPoolSizeMin2.Successor = validParentsBranch;
205
206      validParentsBranch.ConditionParameter.ActualName = "ValidParents";
207      validParentsBranch.TrueBranch = mainOperator;
208      validParentsBranch.FalseBranch = reactivateInit;
209
210      reactivateInit.Successor = resetNextInit;
211      reactivateInit.LeftSideParameter.ActualName = "DoInit";
212      reactivateInit.RightSideParameter.Value = new BoolValue(true);
213
214      resetNextInit.Successor = resetTargetIndex;
215      resetNextInit.LeftSideParameter.ActualName = "NextInit";
216      resetNextInit.RightSideParameter.Value = new IntValue(1);
217
218      resetTargetIndex.LeftSideParameter.ActualName = "TargetIndex";
219      resetTargetIndex.RightSideParameter.Value = new IntValue(0);
220      resetTargetIndex.Successor = createRandomIndividual;
221
222      tryMoveUp.Successor = incrementEvaluations;
223
224      incrementEvaluations.ValueParameter.ActualName = "EvaluatedSolutions";
225      incrementEvaluations.Increment = new IntValue(1);
226
227      incrementBatchIterations.ValueParameter.ActualName = "BatchIterations";
228      incrementBatchIterations.Increment = new IntValue(1);
229      incrementBatchIterations.Successor = batchFinishedComperator;
230
231      batchFinishedComperator.LeftSideParameter.ActualName = "BatchIterations";
232      batchFinishedComperator.RightSideParameter.ActualName = "BatchSize";
233      batchFinishedComperator.ResultParameter.ActualName = "BatchFinished";
234      batchFinishedComperator.Successor = batchFinishedBranch;
235
236      batchFinishedBranch.ConditionParameter.ActualName = "BatchFinished";
237      batchFinishedBranch.TrueBranch = incrIterations;
238      batchFinishedBranch.FalseBranch = randomLayerProcessor;
239
240      incrIterations.ValueParameter.ActualName = "Iterations";
241      incrIterations.Increment = new IntValue(1);
242      incrIterations.Successor = updateResultEvaluatedSolutions;
243
244      updateResultEvaluatedSolutions.ValueParameter.ActualName = "ResultEvaluatedSolutions";
245      updateResultEvaluatedSolutions.IncrementParameter.ActualName = "BatchSize";
246      updateResultEvaluatedSolutions.IncrementParameter.Value = null;
247      updateResultEvaluatedSolutions.Successor = layerAnalyzerProcessor;
248
249      layerAnalyzerProcessor.Operator = layerAnalyzer;
250      layerAnalyzerProcessor.Successor = analyzer;
251
252      layerAnalyzer.OperatorParameter.ActualName = "LayerAnalyzer";
253
254      analyzer.OperatorParameter.ActualName = "Analyzer";
255      analyzer.Successor = iterationsComparator;
256
257      iterationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
258      iterationsComparator.LeftSideParameter.ActualName = "Iterations";
259      iterationsComparator.RightSideParameter.ActualName = MaximumIterationsParameter.Name;
260      iterationsComparator.ResultParameter.ActualName = "Terminate";
261      iterationsComparator.Successor = terminateBranch;
262
263      terminateBranch.ConditionParameter.ActualName = "Terminate";
264      terminateBranch.FalseBranch = initializeBatchIteration;
265    }
266  }
267}
Note: See TracBrowser for help on using the repository browser.