Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2350 Fixed Bugs in Steady-State ALPS OperatorGraph and Mover.

File size: 12.7 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 randomScopeProcessor = new RandomLayerProcessor() { Name = "Select a layer" };
74      var layerPopulationSizeCounter = new SubScopesCounter() { Name = "Count LayerPopulationSize" };
75      var isLayerZeroComperator = new Comparator() { Name = "IsLayerZero = Layer == 0" };
76      var isLayerZeroBranch = new ConditionalBranch() { Name = "IsLayerZero?" };
77      var isDoInitBranch = new ConditionalBranch() { Name = "DoInit?" };
78      var setTargetIndedxToNextInit = new Assigner() { Name = "TargetIndex = NextInit" };
79      var incrementNextInit = new IntCounter() { Name = "Incr. NextInit" };
80      var checkInitFinished = new Comparator() { Name = "DoInit = NextInit >= PopulationSize" };
81      var createWorkingScope = new BestSelector();
82      var workingScopeProcessor = new SubScopesProcessor() { Name = "Working Scope Processor" };
83      var createRandomIndividual = new SolutionsCreator() { Name = "Create random Individual" };
84      var initializeAgeProcessor = new UniformSubScopesProcessor();
85      var initializeAge = new Assigner() { Name = "Initialize Age" };
86      var selectRandomTargetIndex = new RandomIntAssigner();
87      var copyLayer = new BestSelector();
88      var copyLayerProcessor = new SubScopesProcessor();
89      var matingPoolCreator = new SteadyStateMatingPoolCreator() { Name = "Create MatingPool" };
90      var matingPoolSize = new SubScopesCounter() { Name = "MatingPoolSize" };
91      var matingPoolSizeMin2 = new Comparator() { Name = "ValidParents = MatingPoolSize >= 2" };
92      var validParentsBranch = new ConditionalBranch() { Name = "ValidParents?" };
93      var mainOperator = new AlpsSsGeneticAlgorithmMainOperator();
94      var reactivateInit = new Assigner() { Name = "DoInit = true" };
95      var resetNextIndex = new Assigner() { Name = "NextInit = 1" };
96      var resetTargetIndex = new Assigner() { Name = "TargetIndex = 0" };
97      var clearMatingPool = new SubScopesRemover() { Name = "Clear WorkingScope" };
98      var tryMoveUp = new AlpsSsMover() { Name = "Try Move Up" };
99      var removeWorkingScope = new LeftReducer() { Name = "Remove Working Scope" };
100      var incrementEvaluations = new IntCounter() { Name = "incr. EvaluatedSolutions" };
101      var incrIterations = new IntCounter() { Name = "Incr. Iterations" };
102      var layerAnalyzerProcessor = new UniformSubScopesProcessor();
103      var layerAnalyzer = new Placeholder() { Name = "LayerAnalyzer (Placeholder)" };
104      var analyzer = new Placeholder() { Name = "Analyzer (Placeholder)" };
105      var iterationsComparator = new Comparator() { Name = "Iterations >= MaximumIterations" };
106      var terminateBranch = new ConditionalBranch() { Name = "Terminate?" };
107
108
109      OperatorGraph.InitialOperator = variableCreator;
110
111      variableCreator.CollectedValues.Add(new ValueParameter<BoolValue>("DoInit", new BoolValue(false)));
112      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("NextInit", new IntValue(0)));
113      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("OpenLayers", new IntValue(1)));
114      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("TargetIndex", new IntValue(0)));
115      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Iterations", new IntValue(0)));
116      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("MatingPoolSize", new IntValue(0)));
117      variableCreator.CollectedValues.Add(new ValueParameter<BoolValue>("ValidParents", new BoolValue(false)));
118      variableCreator.Successor = randomScopeProcessor;
119
120      randomScopeProcessor.Operator = layerPopulationSizeCounter;
121      randomScopeProcessor.Successor = incrIterations;
122
123      layerPopulationSizeCounter.ValueParameter.ActualName = "LayerPopulationSize";
124      layerPopulationSizeCounter.AccumulateParameter.Value =  new BoolValue(false);
125      layerPopulationSizeCounter.Successor = isLayerZeroComperator;
126
127      isLayerZeroComperator.LeftSideParameter.ActualName = "Layer";
128      isLayerZeroComperator.RightSideParameter.Value = new IntValue(0);
129      isLayerZeroComperator.ResultParameter.ActualName = "IsLayerZero";
130      isLayerZeroComperator.Comparison = new Comparison(ComparisonType.Equal);
131      isLayerZeroComperator.Successor = isLayerZeroBranch;
132
133      isLayerZeroBranch.ConditionParameter.ActualName = "IsLayerZero";
134      isLayerZeroBranch.TrueBranch = isDoInitBranch;
135      isLayerZeroBranch.FalseBranch = selectRandomTargetIndex;
136      isLayerZeroBranch.Successor = tryMoveUp;
137
138      isDoInitBranch.ConditionParameter.ActualName = "DoInit";
139      isDoInitBranch.TrueBranch = setTargetIndedxToNextInit;
140      isDoInitBranch.FalseBranch = selectRandomTargetIndex;
141
142      setTargetIndedxToNextInit.LeftSideParameter.ActualName = "TargetIndex";
143      setTargetIndedxToNextInit.RightSideParameter.ActualName = "NextInit";
144      setTargetIndedxToNextInit.Successor = incrementNextInit;
145
146      incrementNextInit.ValueParameter.ActualName = "NextInit";
147      incrementNextInit.Increment = new IntValue(1);
148      incrementNextInit.Successor = checkInitFinished;
149
150      checkInitFinished.LeftSideParameter.ActualName = "NextInit";
151      checkInitFinished.RightSideParameter.ActualName = "PopulationSize";
152      checkInitFinished.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
153      checkInitFinished.ResultParameter.ActualName = "DoInit";
154      checkInitFinished.Successor = createWorkingScope;
155
156      createWorkingScope.NumberOfSelectedSubScopesParameter.Value = new IntValue(0);
157      createWorkingScope.CopySelected = new BoolValue(false);
158      createWorkingScope.Successor = workingScopeProcessor;
159
160      workingScopeProcessor.Operators.Add(createRandomIndividual);
161      workingScopeProcessor.Operators.Add(new EmptyOperator());
162
163      createRandomIndividual.NumberOfSolutions = new IntValue(1);
164      createRandomIndividual.Successor = initializeAgeProcessor;
165
166      initializeAgeProcessor.Operator = initializeAge;
167
168      initializeAge.LeftSideParameter.ActualName = "EvalsCreated";
169      initializeAge.RightSideParameter.ActualName = "EvaluatedSolutions";
170
171      selectRandomTargetIndex.LeftSideParameter.ActualName = "TargetIndex";
172      selectRandomTargetIndex.MinimumParameter.Value = new IntValue(0);
173      selectRandomTargetIndex.MaximumParameter.ActualName = "LayerPopulationSize";
174      selectRandomTargetIndex.MaximumParameter.Value = null;
175      selectRandomTargetIndex.Successor = copyLayer;
176
177      copyLayer.NumberOfSelectedSubScopesParameter.ActualName = "LayerPopulationSize";
178      copyLayer.CopySelected = new BoolValue(true);
179      copyLayer.Successor = copyLayerProcessor;
180
181      copyLayerProcessor.Operators.Add(new EmptyOperator());
182      copyLayerProcessor.Operators.Add(matingPoolCreator);
183
184      matingPoolCreator.Successor = matingPoolSize;
185
186      matingPoolSize.ValueParameter.ActualName = "MatingPoolSize";
187      matingPoolSize.AccumulateParameter.Value = new BoolValue(false);
188      matingPoolSize.Successor = matingPoolSizeMin2;
189
190      matingPoolSizeMin2.LeftSideParameter.ActualName = "MatingPoolSize";
191      matingPoolSizeMin2.RightSideParameter.Value = new IntValue(2);
192      matingPoolSizeMin2.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
193      matingPoolSizeMin2.ResultParameter.ActualName = "ValidParents";
194      matingPoolSizeMin2.Successor = validParentsBranch;
195
196      validParentsBranch.ConditionParameter.ActualName = "ValidParents";
197      validParentsBranch.TrueBranch = mainOperator;
198      validParentsBranch.FalseBranch = reactivateInit;
199
200      reactivateInit.Successor = resetNextIndex;
201      reactivateInit.LeftSideParameter.ActualName = "DoInit";
202      reactivateInit.RightSideParameter.Value = new BoolValue(true);
203
204      resetNextIndex.Successor = resetTargetIndex;
205      resetNextIndex.LeftSideParameter.ActualName = "NextIndex";
206      resetNextIndex.RightSideParameter.Value = new IntValue(1);
207
208      resetTargetIndex.Successor = clearMatingPool;
209      resetTargetIndex.LeftSideParameter.ActualName = "TargetIndex";
210      resetTargetIndex.RightSideParameter.Value = new IntValue(0);
211
212      clearMatingPool.Successor = createRandomIndividual;
213
214      tryMoveUp.Successor = removeWorkingScope;
215
216      removeWorkingScope.Successor = incrementEvaluations;
217
218      incrementEvaluations.Increment = new IntValue(1);
219      incrementEvaluations.ValueParameter.ActualName = "EvaluatedSolutions";
220
221      incrIterations.ValueParameter.ActualName = "Iterations";
222      incrIterations.Increment = new IntValue(1);
223      incrIterations.Successor = layerAnalyzerProcessor;
224
225      layerAnalyzerProcessor.Operator = layerAnalyzer;
226      layerAnalyzerProcessor.Successor = analyzer;
227
228      layerAnalyzer.OperatorParameter.ActualName = "LayerAnalyzer";
229
230      analyzer.OperatorParameter.ActualName = "Analyzer";
231      analyzer.Successor = iterationsComparator;
232
233      iterationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
234      iterationsComparator.LeftSideParameter.ActualName = "Iterations";
235      iterationsComparator.RightSideParameter.ActualName = MaximumIterationsParameter.Name;
236      iterationsComparator.ResultParameter.ActualName = "Terminate";
237      iterationsComparator.Successor = terminateBranch;
238
239      terminateBranch.ConditionParameter.ActualName = "Terminate";
240      terminateBranch.FalseBranch = randomScopeProcessor;
241    }
242  }
243}
Note: See TracBrowser for help on using the repository browser.