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 System.Linq;
|
---|
23 | using HeuristicLab.Algorithms.GeneticAlgorithm;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Operators;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Optimization.Operators;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 | using HeuristicLab.Selection;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Algorithms.ALPS {
|
---|
35 |
|
---|
36 | [Item("AlpsGeneticAlgorithmMainLoop", "An ALPS genetic algorithm main loop operator.")]
|
---|
37 | [StorableClass]
|
---|
38 | public sealed class AlpsGeneticAlgorithmMainLoop : AlgorithmOperator {
|
---|
39 | #region Parameter Properties
|
---|
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 | }
|
---|
46 | public ILookupParameter<IntValue> MaximumGenerationsParameter {
|
---|
47 | get { return (ILookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
|
---|
48 | }
|
---|
49 | public ILookupParameter<IOperator> AnalyzerParameter {
|
---|
50 | get { return (ILookupParameter<IOperator>)Parameters["Analyzer"]; }
|
---|
51 | }
|
---|
52 | public ILookupParameter<IOperator> LayerAnalyzerParameter {
|
---|
53 | get { return (ILookupParameter<IOperator>)Parameters["LayerAnalyzer"]; }
|
---|
54 | }
|
---|
55 | #endregion
|
---|
56 |
|
---|
57 | public GeneticAlgorithmMainLoop MainOperator {
|
---|
58 | get { return OperatorGraph.Iterate().OfType<GeneticAlgorithmMainLoop>().First(); }
|
---|
59 | }
|
---|
60 | public EldersEmigrator EldersEmigrator {
|
---|
61 | get { return OperatorGraph.Iterate().OfType<EldersEmigrator>().First(); }
|
---|
62 | }
|
---|
63 | public LayerUpdator LayerUpdator {
|
---|
64 | get { return OperatorGraph.Iterate().OfType<LayerUpdator>().First(); }
|
---|
65 | }
|
---|
66 |
|
---|
67 | [StorableConstructor]
|
---|
68 | private AlpsGeneticAlgorithmMainLoop(bool deserializing)
|
---|
69 | : base(deserializing) { }
|
---|
70 | private AlpsGeneticAlgorithmMainLoop(AlpsGeneticAlgorithmMainLoop original, Cloner cloner)
|
---|
71 | : base(original, cloner) { }
|
---|
72 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
73 | return new AlpsGeneticAlgorithmMainLoop(this, cloner);
|
---|
74 | }
|
---|
75 | public AlpsGeneticAlgorithmMainLoop()
|
---|
76 | : base() {
|
---|
77 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
|
---|
78 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
|
---|
79 | Parameters.Add(new LookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations that the algorithm should process."));
|
---|
80 | Parameters.Add(new LookupParameter<IOperator>("Analyzer", "The operator used to the analyze all individuals."));
|
---|
81 | Parameters.Add(new LookupParameter<IOperator>("LayerAnalyzer", "The operator used to analyze each layer."));
|
---|
82 |
|
---|
83 | var variableCreator = new VariableCreator() { Name = "Initialize" };
|
---|
84 | var initLayerAnalyzerProcessor = new SubScopesProcessor();
|
---|
85 | var layerVariableCreator = new VariableCreator() { Name = "Initialize Layer" };
|
---|
86 | var initLayerAnalyzerPlaceholder = new Placeholder() { Name = "LayerAnalyzer (Placeholder)" };
|
---|
87 | var initAnalyzerPlaceholder = new Placeholder() { Name = "Analyzer (Placeholder)" };
|
---|
88 | var resultsCollector = new ResultsCollector();
|
---|
89 | var matingPoolPreProcessor = new UniformSubScopesProcessor() { Name = "MatingPoolPreProcessor" };
|
---|
90 | var matingPoolPreSorter = new SubScopesSorter() { Name = "MatingPoolPreSorter" };
|
---|
91 | var matingPoolCreator = new MatingPoolCreator() { Name = "Create Mating Pools" };
|
---|
92 | var matingPoolProcessor = new LayerUniformSubScopesProcessor();
|
---|
93 | var initializeLayer = new Assigner() { Name = "Reset LayerEvaluatedSolutions" };
|
---|
94 | var mainOperator = CreatePreparedGeneticAlgorithmMainLoop();
|
---|
95 | var generationsIcrementor = new IntCounter() { Name = "Increment Generations" };
|
---|
96 | var evaluatedSolutionsReducer = new DataReducer() { Name = "Increment EvaluatedSolutions" };
|
---|
97 | var eldersEmigrator = new EldersEmigrator() { Name = "Emigrate Elders" };
|
---|
98 | var layerUpdator = new LayerUpdator(mainOperator) { Name = "Update Layers" };
|
---|
99 | var layerAnalyzerProcessor = new LayerUniformSubScopesProcessor();
|
---|
100 | var layerAnalyzerPlaceholder = new Placeholder() { Name = "LayerAnalyzer (Placeholder)" };
|
---|
101 | var analyzerPlaceholder = new Placeholder() { Name = "Analyzer (Placeholder)" };
|
---|
102 | var termination = new TerminationOperator();
|
---|
103 |
|
---|
104 | OperatorGraph.InitialOperator = variableCreator;
|
---|
105 |
|
---|
106 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0)));
|
---|
107 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("GenerationsSinceLastRefresh", new IntValue(0)));
|
---|
108 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("OpenLayers", new IntValue(1)));
|
---|
109 | variableCreator.Successor = initLayerAnalyzerProcessor;
|
---|
110 |
|
---|
111 | initLayerAnalyzerProcessor.Operators.Add(layerVariableCreator);
|
---|
112 | initLayerAnalyzerProcessor.Successor = initAnalyzerPlaceholder;
|
---|
113 |
|
---|
114 | layerVariableCreator.CollectedValues.Add(new ValueParameter<IntValue>("LayerEvaluatedSolutions"));
|
---|
115 | layerVariableCreator.CollectedValues.Add(new ValueParameter<ResultCollection>("LayerResults"));
|
---|
116 | layerVariableCreator.Successor = initLayerAnalyzerPlaceholder;
|
---|
117 |
|
---|
118 | initLayerAnalyzerPlaceholder.OperatorParameter.ActualName = LayerAnalyzerParameter.Name;
|
---|
119 | initLayerAnalyzerPlaceholder.Successor = null;
|
---|
120 |
|
---|
121 | initAnalyzerPlaceholder.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
122 | initAnalyzerPlaceholder.Successor = resultsCollector;
|
---|
123 |
|
---|
124 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
|
---|
125 | resultsCollector.CollectedValues.Add(new ScopeTreeLookupParameter<ResultCollection>("LayerResults", "Result set for each layer", "LayerResults"));
|
---|
126 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("OpenLayers"));
|
---|
127 | resultsCollector.CopyValue = new BoolValue(false);
|
---|
128 | resultsCollector.Successor = matingPoolPreProcessor;
|
---|
129 |
|
---|
130 | matingPoolPreProcessor.Operator = matingPoolPreSorter;
|
---|
131 | matingPoolPreProcessor.Successor = matingPoolCreator;
|
---|
132 |
|
---|
133 | matingPoolPreSorter.ValueParameter.ActualName = QualityParameter.Name;
|
---|
134 | matingPoolPreSorter.DescendingParameter.ActualName = MaximizationParameter.Name;
|
---|
135 |
|
---|
136 | matingPoolCreator.Successor = matingPoolProcessor;
|
---|
137 |
|
---|
138 | matingPoolProcessor.Parallel.Value = true;
|
---|
139 | matingPoolProcessor.Operator = initializeLayer;
|
---|
140 | matingPoolProcessor.Successor = generationsIcrementor;
|
---|
141 |
|
---|
142 | initializeLayer.LeftSideParameter.ActualName = "LayerEvaluatedSolutions";
|
---|
143 | initializeLayer.RightSideParameter.Value = new IntValue(0);
|
---|
144 | initializeLayer.Successor = mainOperator;
|
---|
145 |
|
---|
146 | generationsIcrementor.ValueParameter.ActualName = "Generations";
|
---|
147 | generationsIcrementor.Increment = new IntValue(1);
|
---|
148 | generationsIcrementor.Successor = evaluatedSolutionsReducer;
|
---|
149 |
|
---|
150 | evaluatedSolutionsReducer.ParameterToReduce.ActualName = "LayerEvaluatedSolutions";
|
---|
151 | evaluatedSolutionsReducer.TargetParameter.ActualName = "EvaluatedSolutions";
|
---|
152 | evaluatedSolutionsReducer.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum);
|
---|
153 | evaluatedSolutionsReducer.TargetOperation.Value = new ReductionOperation(ReductionOperations.Sum);
|
---|
154 | evaluatedSolutionsReducer.Successor = eldersEmigrator;
|
---|
155 |
|
---|
156 | eldersEmigrator.Successor = layerUpdator;
|
---|
157 |
|
---|
158 | layerUpdator.Successor = layerAnalyzerProcessor;
|
---|
159 |
|
---|
160 | layerAnalyzerProcessor.Operator = layerAnalyzerPlaceholder;
|
---|
161 | layerAnalyzerProcessor.Successor = analyzerPlaceholder;
|
---|
162 |
|
---|
163 | layerAnalyzerPlaceholder.OperatorParameter.ActualName = LayerAnalyzerParameter.Name;
|
---|
164 |
|
---|
165 | analyzerPlaceholder.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
166 | analyzerPlaceholder.Successor = termination;
|
---|
167 |
|
---|
168 | termination.ContinueBranch = matingPoolPreProcessor;
|
---|
169 | }
|
---|
170 |
|
---|
171 | private GeneticAlgorithmMainLoop CreatePreparedGeneticAlgorithmMainLoop() {
|
---|
172 | var mainLoop = new GeneticAlgorithmMainLoop();
|
---|
173 |
|
---|
174 | var selector = mainLoop.OperatorGraph.Iterate().OfType<Placeholder>().First(o => o.OperatorParameter.ActualName == "Selector");
|
---|
175 | var crossover = mainLoop.OperatorGraph.Iterate().OfType<Placeholder>().First(o => o.OperatorParameter.ActualName == "Crossover");
|
---|
176 | var subScopesCounter = mainLoop.OperatorGraph.Iterate().OfType<SubScopesCounter>().First();
|
---|
177 | var elitesMerger = mainLoop.OperatorGraph.Iterate().OfType<MergingReducer>().First();
|
---|
178 |
|
---|
179 | // Operator starts with calculating number of selected scopes base on plus/comma-selection replacement scheme
|
---|
180 | var numberOfSubScopesBranch = new ConditionalBranch() { Name = "PlusSelection?" };
|
---|
181 | var numberOfSelectedSubScopesPlusCalculator = new ExpressionCalculator() { Name = "NumberOfSelectedSubScopes = PopulationSize * 2" };
|
---|
182 | var numberOfSelectedSubScopesCalculator = new ExpressionCalculator() { Name = "NumberOfSelectedSubScopes = (PopulationSize - Elites) * 2" };
|
---|
183 | var replacementBranch = new ConditionalBranch() { Name = "PlusSelection?" };
|
---|
184 |
|
---|
185 | mainLoop.OperatorGraph.InitialOperator = numberOfSubScopesBranch;
|
---|
186 |
|
---|
187 | numberOfSubScopesBranch.ConditionParameter.ActualName = "PlusSelection";
|
---|
188 | numberOfSubScopesBranch.TrueBranch = numberOfSelectedSubScopesPlusCalculator;
|
---|
189 | numberOfSubScopesBranch.FalseBranch = numberOfSelectedSubScopesCalculator;
|
---|
190 | numberOfSubScopesBranch.Successor = selector;
|
---|
191 |
|
---|
192 | numberOfSelectedSubScopesPlusCalculator.CollectedValues.Add(new LookupParameter<IntValue>("PopulationSize"));
|
---|
193 | numberOfSelectedSubScopesPlusCalculator.ExpressionResultParameter.ActualName = "NumberOfSelectedSubScopes";
|
---|
194 | numberOfSelectedSubScopesPlusCalculator.ExpressionParameter.Value = new StringValue("PopulationSize 2 * toint");
|
---|
195 |
|
---|
196 | numberOfSelectedSubScopesCalculator.CollectedValues.Add(new LookupParameter<IntValue>("PopulationSize"));
|
---|
197 | numberOfSelectedSubScopesCalculator.CollectedValues.Add(new LookupParameter<IntValue>("Elites"));
|
---|
198 | numberOfSelectedSubScopesCalculator.ExpressionResultParameter.ActualName = "NumberOfSelectedSubScopes";
|
---|
199 | numberOfSelectedSubScopesCalculator.ExpressionParameter.Value = new StringValue("PopulationSize Elites - 2 * toint");
|
---|
200 |
|
---|
201 | // Use Elitism or Plus-Selection as replacement strategy
|
---|
202 | var selectedProcessor = (SubScopesProcessor)selector.Successor;
|
---|
203 | var elitismReplacement = selectedProcessor.Successor;
|
---|
204 | selectedProcessor.Successor = replacementBranch;
|
---|
205 | replacementBranch.ConditionParameter.ActualName = "PlusSelection";
|
---|
206 | replacementBranch.FalseBranch = elitismReplacement;
|
---|
207 |
|
---|
208 | // Plus selection replacement
|
---|
209 | var replacementMergingReducer = new MergingReducer();
|
---|
210 | var replacementBestSelector = new BestSelector();
|
---|
211 | var replacementRightReducer = new RightReducer();
|
---|
212 | replacementBranch.TrueBranch = replacementMergingReducer;
|
---|
213 |
|
---|
214 | replacementMergingReducer.Successor = replacementBestSelector;
|
---|
215 |
|
---|
216 | replacementBestSelector.NumberOfSelectedSubScopesParameter.ActualName = "PopulationSize";
|
---|
217 | replacementBestSelector.CopySelected = new BoolValue(false);
|
---|
218 | replacementBestSelector.Successor = replacementRightReducer;
|
---|
219 |
|
---|
220 | replacementRightReducer.Successor = null;
|
---|
221 |
|
---|
222 | // Increment ages of all individuals after replacement
|
---|
223 | var incrementAgeProcessor = new UniformSubScopesProcessor();
|
---|
224 | var ageIncrementor = new IntCounter() { Name = "Increment Age" };
|
---|
225 | replacementBranch.Successor = incrementAgeProcessor;
|
---|
226 | incrementAgeProcessor.Operator = ageIncrementor;
|
---|
227 | incrementAgeProcessor.Successor = null;
|
---|
228 | ageIncrementor.ValueParameter.ActualName = "Age";
|
---|
229 | ageIncrementor.Increment = new IntValue(1);
|
---|
230 | //ageIncrementor.Successor = null;
|
---|
231 |
|
---|
232 | // Insert AgeCalculator between crossover and its successor
|
---|
233 | var crossoverSuccessor = crossover.Successor;
|
---|
234 | var ageCalculator = new DataReducer() { Name = "Calculate Age" };
|
---|
235 | crossover.Successor = ageCalculator;
|
---|
236 |
|
---|
237 | ageCalculator.ParameterToReduce.ActualName = "Age";
|
---|
238 | ageCalculator.TargetParameter.ActualName = "Age";
|
---|
239 | ageCalculator.ReductionOperation.Value = null;
|
---|
240 | ageCalculator.ReductionOperation.ActualName = "AgeInheritanceReduction";
|
---|
241 | ageCalculator.TargetOperation.Value = new ReductionOperation(ReductionOperations.Assign);
|
---|
242 | ageCalculator.Successor = crossoverSuccessor;
|
---|
243 |
|
---|
244 | // When counting the evaluated solutions, write in LayerEvaluatedSolutions
|
---|
245 | subScopesCounter.ValueParameter.ActualName = "LayerEvaluatedSolutions";
|
---|
246 | subScopesCounter.AccumulateParameter.Value = new BoolValue(false);
|
---|
247 |
|
---|
248 | // Instead of generational loop after merging of elites, stop
|
---|
249 | elitesMerger.Successor = null;
|
---|
250 |
|
---|
251 | // Parameterize
|
---|
252 | foreach (var stochasticOperator in mainLoop.OperatorGraph.Iterate().OfType<IStochasticOperator>())
|
---|
253 | stochasticOperator.RandomParameter.ActualName = "LocalRandom";
|
---|
254 | foreach (var stochasticBranch in mainLoop.OperatorGraph.Iterate().OfType<StochasticBranch>())
|
---|
255 | stochasticBranch.RandomParameter.ActualName = "LocalRandom";
|
---|
256 |
|
---|
257 | return mainLoop;
|
---|
258 | }
|
---|
259 | }
|
---|
260 | } |
---|