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