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;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Analysis;
|
---|
26 | using HeuristicLab.Collections;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Data;
|
---|
30 | using HeuristicLab.Operators;
|
---|
31 | using HeuristicLab.Optimization;
|
---|
32 | using HeuristicLab.Optimization.Operators;
|
---|
33 | using HeuristicLab.Parameters;
|
---|
34 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
35 | using HeuristicLab.PluginInfrastructure;
|
---|
36 | using HeuristicLab.Random;
|
---|
37 | using HeuristicLab.Selection;
|
---|
38 |
|
---|
39 | namespace HeuristicLab.Algorithms.ALPS {
|
---|
40 | [Item("ALPS Genetic Algorithm", "A genetic algorithm within an age-layered population structure as described in Gregory S. Hornby. 2006. ALPS: the age-layered population structure for reducing the problem of premature convergence. In Proceedings of the 8th annual conference on Genetic and evolutionary computation (GECCO '06). 815-822.")]
|
---|
41 | [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 160)]
|
---|
42 | [StorableClass]
|
---|
43 | public sealed class AlpsGeneticAlgorithm : HeuristicOptimizationEngineAlgorithm, IStorableContent {
|
---|
44 | public string Filename { get; set; }
|
---|
45 |
|
---|
46 | #region Problem Properties
|
---|
47 | public override Type ProblemType {
|
---|
48 | get { return typeof(ISingleObjectiveHeuristicOptimizationProblem); }
|
---|
49 | }
|
---|
50 | public new ISingleObjectiveHeuristicOptimizationProblem Problem {
|
---|
51 | get { return (ISingleObjectiveHeuristicOptimizationProblem)base.Problem; }
|
---|
52 | set { base.Problem = value; }
|
---|
53 | }
|
---|
54 | #endregion
|
---|
55 |
|
---|
56 | #region Parameter Properties
|
---|
57 | private IValueParameter<IntValue> SeedParameter {
|
---|
58 | get { return (IValueParameter<IntValue>)Parameters["Seed"]; }
|
---|
59 | }
|
---|
60 | private IValueParameter<BoolValue> SetSeedRandomlyParameter {
|
---|
61 | get { return (IValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
|
---|
62 | }
|
---|
63 |
|
---|
64 | private IFixedValueParameter<MultiAnalyzer> AnalyzerParameter {
|
---|
65 | get { return (IFixedValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
|
---|
66 | }
|
---|
67 | private IFixedValueParameter<MultiAnalyzer> LayerAnalyzerParameter {
|
---|
68 | get { return (IFixedValueParameter<MultiAnalyzer>)Parameters["LayerAnalyzer"]; }
|
---|
69 | }
|
---|
70 |
|
---|
71 | private IValueParameter<IntValue> NumberOfLayersParameter {
|
---|
72 | get { return (IValueParameter<IntValue>)Parameters["NumberOfLayers"]; }
|
---|
73 | }
|
---|
74 | private IValueParameter<IntValue> PopulationSizeParameter {
|
---|
75 | get { return (IValueParameter<IntValue>)Parameters["PopulationSize"]; }
|
---|
76 | }
|
---|
77 |
|
---|
78 | public IConstrainedValueParameter<ISelector> SelectorParameter {
|
---|
79 | get { return (IConstrainedValueParameter<ISelector>)Parameters["Selector"]; }
|
---|
80 | }
|
---|
81 | public IConstrainedValueParameter<ICrossover> CrossoverParameter {
|
---|
82 | get { return (IConstrainedValueParameter<ICrossover>)Parameters["Crossover"]; }
|
---|
83 | }
|
---|
84 | public IConstrainedValueParameter<IManipulator> MutatorParameter {
|
---|
85 | get { return (IConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; }
|
---|
86 | }
|
---|
87 | private IValueParameter<PercentValue> MutationProbabilityParameter {
|
---|
88 | get { return (IValueParameter<PercentValue>)Parameters["MutationProbability"]; }
|
---|
89 | }
|
---|
90 | private IValueParameter<IntValue> ElitesParameter {
|
---|
91 | get { return (IValueParameter<IntValue>)Parameters["Elites"]; }
|
---|
92 | }
|
---|
93 | private IFixedValueParameter<BoolValue> ReevaluateElitesParameter {
|
---|
94 | get { return (IFixedValueParameter<BoolValue>)Parameters["ReevaluateElites"]; }
|
---|
95 | }
|
---|
96 | private IValueParameter<BoolValue> PlusSelectionParameter {
|
---|
97 | get { return (IValueParameter<BoolValue>)Parameters["PlusSelection"]; }
|
---|
98 | }
|
---|
99 |
|
---|
100 | private IValueParameter<EnumValue<AgingScheme>> AgingSchemeParameter {
|
---|
101 | get { return (IValueParameter<EnumValue<AgingScheme>>)Parameters["AgingScheme"]; }
|
---|
102 | }
|
---|
103 | private IValueParameter<IntValue> AgeGapParameter {
|
---|
104 | get { return (IValueParameter<IntValue>)Parameters["AgeGap"]; }
|
---|
105 | }
|
---|
106 | private IValueParameter<EnumValue<AgeInheritance>> AgeInheritanceParameter {
|
---|
107 | get { return (IValueParameter<EnumValue<AgeInheritance>>)Parameters["AgeInheritance"]; }
|
---|
108 | }
|
---|
109 | private IValueParameter<ReductionOperation> AgeInheritanceReductionParameter {
|
---|
110 | get { return (IValueParameter<ReductionOperation>)Parameters["AgeInheritanceReduction"]; }
|
---|
111 | }
|
---|
112 | private IValueParameter<IntArray> AgeLimitsParameter {
|
---|
113 | get { return (IValueParameter<IntArray>)Parameters["AgeLimits"]; }
|
---|
114 | }
|
---|
115 |
|
---|
116 | private IValueParameter<IntValue> MatingPoolRangeParameter {
|
---|
117 | get { return (IValueParameter<IntValue>)Parameters["MatingPoolRange"]; }
|
---|
118 | }
|
---|
119 | private IValueParameter<PercentValue> MatingPoolSelectionPercentageParameter {
|
---|
120 | get { return (IValueLookupParameter<PercentValue>)Parameters["MatingPoolSelectionPercentage"]; }
|
---|
121 | }
|
---|
122 |
|
---|
123 | private IValueParameter<MultiTerminator> TerminatorParameter {
|
---|
124 | get { return (IValueParameter<MultiTerminator>)Parameters["Terminator"]; }
|
---|
125 | }
|
---|
126 | #endregion
|
---|
127 |
|
---|
128 | #region Properties
|
---|
129 | public IntValue Seed {
|
---|
130 | get { return SeedParameter.Value; }
|
---|
131 | set { SeedParameter.Value = value; }
|
---|
132 | }
|
---|
133 | public BoolValue SetSeedRandomly {
|
---|
134 | get { return SetSeedRandomlyParameter.Value; }
|
---|
135 | set { SetSeedRandomlyParameter.Value = value; }
|
---|
136 | }
|
---|
137 |
|
---|
138 | public MultiAnalyzer Analyzer {
|
---|
139 | get { return AnalyzerParameter.Value; }
|
---|
140 | }
|
---|
141 | public MultiAnalyzer LayerAnalyzer {
|
---|
142 | get { return LayerAnalyzerParameter.Value; }
|
---|
143 | }
|
---|
144 |
|
---|
145 | public IntValue NumberOfLayers {
|
---|
146 | get { return NumberOfLayersParameter.Value; }
|
---|
147 | set { NumberOfLayersParameter.Value = value; }
|
---|
148 | }
|
---|
149 | public IntValue PopulationSize {
|
---|
150 | get { return PopulationSizeParameter.Value; }
|
---|
151 | set { PopulationSizeParameter.Value = value; }
|
---|
152 | }
|
---|
153 |
|
---|
154 | public ISelector Selector {
|
---|
155 | get { return SelectorParameter.Value; }
|
---|
156 | set { SelectorParameter.Value = value; }
|
---|
157 | }
|
---|
158 | public ICrossover Crossover {
|
---|
159 | get { return CrossoverParameter.Value; }
|
---|
160 | set { CrossoverParameter.Value = value; }
|
---|
161 | }
|
---|
162 | public IManipulator Mutator {
|
---|
163 | get { return MutatorParameter.Value; }
|
---|
164 | set { MutatorParameter.Value = value; }
|
---|
165 | }
|
---|
166 | public PercentValue MutationProbability {
|
---|
167 | get { return MutationProbabilityParameter.Value; }
|
---|
168 | set { MutationProbabilityParameter.Value = value; }
|
---|
169 | }
|
---|
170 | public IntValue Elites {
|
---|
171 | get { return ElitesParameter.Value; }
|
---|
172 | set { ElitesParameter.Value = value; }
|
---|
173 | }
|
---|
174 | public bool ReevaluteElites {
|
---|
175 | get { return ReevaluateElitesParameter.Value.Value; }
|
---|
176 | set { ReevaluateElitesParameter.Value.Value = value; }
|
---|
177 | }
|
---|
178 | public bool PlusSelection {
|
---|
179 | get { return PlusSelectionParameter.Value.Value; }
|
---|
180 | set { PlusSelectionParameter.Value.Value = value; }
|
---|
181 | }
|
---|
182 |
|
---|
183 | public EnumValue<AgingScheme> AgingScheme {
|
---|
184 | get { return AgingSchemeParameter.Value; }
|
---|
185 | set { AgingSchemeParameter.Value = value; }
|
---|
186 | }
|
---|
187 | public IntValue AgeGap {
|
---|
188 | get { return AgeGapParameter.Value; }
|
---|
189 | set { AgeGapParameter.Value = value; }
|
---|
190 | }
|
---|
191 | public EnumValue<AgeInheritance> AgeInheritance {
|
---|
192 | get { return AgeInheritanceParameter.Value; }
|
---|
193 | set { AgeInheritanceParameter.Value = value; }
|
---|
194 | }
|
---|
195 | private ReductionOperation AgeInheritanceReduction {
|
---|
196 | get { return AgeInheritanceReductionParameter.Value; }
|
---|
197 | set { AgeInheritanceReductionParameter.Value = value; }
|
---|
198 | }
|
---|
199 | public IntArray AgeLimits {
|
---|
200 | get { return AgeLimitsParameter.Value; }
|
---|
201 | set { AgeLimitsParameter.Value = value; }
|
---|
202 | }
|
---|
203 |
|
---|
204 | public IntValue MatingPoolRange {
|
---|
205 | get { return MatingPoolRangeParameter.Value; }
|
---|
206 | set { MatingPoolRangeParameter.Value = value; }
|
---|
207 | }
|
---|
208 | public PercentValue MatingPoolSelectionPercentage {
|
---|
209 | get { return MatingPoolSelectionPercentageParameter.Value; }
|
---|
210 | set { MatingPoolSelectionPercentageParameter.Value = value; }
|
---|
211 | }
|
---|
212 |
|
---|
213 | public MultiTerminator Terminators {
|
---|
214 | get { return TerminatorParameter.Value; }
|
---|
215 | }
|
---|
216 |
|
---|
217 | public int MaximumGenerations {
|
---|
218 | get { return generationsTerminator.Threshold.Value; }
|
---|
219 | set { generationsTerminator.Threshold.Value = value; }
|
---|
220 | }
|
---|
221 | #endregion
|
---|
222 |
|
---|
223 | #region Helper Properties
|
---|
224 | private RandomCreator GlobalRandomCreator {
|
---|
225 | get { return (RandomCreator)OperatorGraph.InitialOperator; }
|
---|
226 | }
|
---|
227 | private SolutionsCreator SolutionsCreator {
|
---|
228 | get { return OperatorGraph.Iterate().OfType<SolutionsCreator>().First(); }
|
---|
229 | }
|
---|
230 | private AlpsGeneticAlgorithmMainLoop MainLoop {
|
---|
231 | get { return OperatorGraph.Iterate().OfType<AlpsGeneticAlgorithmMainLoop>().First(); }
|
---|
232 | }
|
---|
233 | #endregion
|
---|
234 |
|
---|
235 | #region Preconfigured Analyzers
|
---|
236 | [Storable]
|
---|
237 | private BestAverageWorstQualityAnalyzer qualityAnalyzer;
|
---|
238 | [Storable]
|
---|
239 | private BestAverageWorstQualityAnalyzer layerQualityAnalyzer;
|
---|
240 | #endregion
|
---|
241 |
|
---|
242 | #region Preconfigured Terminators
|
---|
243 | [Storable]
|
---|
244 | private ComparisonTerminator<IntValue> generationsTerminator;
|
---|
245 | [Storable]
|
---|
246 | private ComparisonTerminator<IntValue> evaluationsTerminator;
|
---|
247 | [Storable]
|
---|
248 | private SingleObjectiveQualityTerminator qualityTerminator;
|
---|
249 | [Storable]
|
---|
250 | private ExecutionTimeTerminator executionTimeTerminator;
|
---|
251 | #endregion
|
---|
252 |
|
---|
253 | [StorableConstructor]
|
---|
254 | private AlpsGeneticAlgorithm(bool deserializing)
|
---|
255 | : base(deserializing) { }
|
---|
256 | [StorableHook(HookType.AfterDeserialization)]
|
---|
257 | private void AfterDeserialization() {
|
---|
258 | Initialize();
|
---|
259 | }
|
---|
260 | private AlpsGeneticAlgorithm(AlpsGeneticAlgorithm original, Cloner cloner)
|
---|
261 | : base(original, cloner) {
|
---|
262 | qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
|
---|
263 | layerQualityAnalyzer = cloner.Clone(original.layerQualityAnalyzer);
|
---|
264 | generationsTerminator = cloner.Clone(original.generationsTerminator);
|
---|
265 | evaluationsTerminator = cloner.Clone(original.evaluationsTerminator);
|
---|
266 | qualityTerminator = cloner.Clone(original.qualityTerminator);
|
---|
267 | executionTimeTerminator = cloner.Clone(original.executionTimeTerminator);
|
---|
268 | Initialize();
|
---|
269 | }
|
---|
270 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
271 | return new AlpsGeneticAlgorithm(this, cloner);
|
---|
272 | }
|
---|
273 | public AlpsGeneticAlgorithm()
|
---|
274 | : base() {
|
---|
275 | #region Add parameters
|
---|
276 | Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
|
---|
277 | Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
278 |
|
---|
279 | Parameters.Add(new FixedValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze the islands.", new MultiAnalyzer()));
|
---|
280 | Parameters.Add(new FixedValueParameter<MultiAnalyzer>("LayerAnalyzer", "The operator used to analyze each layer.", new MultiAnalyzer()));
|
---|
281 |
|
---|
282 | Parameters.Add(new ValueParameter<IntValue>("NumberOfLayers", "The number of layers.", new IntValue(10)));
|
---|
283 | Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "The size of the population of solutions each layer.", new IntValue(100)));
|
---|
284 |
|
---|
285 | Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
|
---|
286 | Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
|
---|
287 | Parameters.Add(new OptionalConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
|
---|
288 | Parameters.Add(new ValueParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new PercentValue(0.05)));
|
---|
289 | Parameters.Add(new ValueParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation.", new IntValue(1)));
|
---|
290 | Parameters.Add(new FixedValueParameter<BoolValue>("ReevaluateElites", "Flag to determine if elite individuals should be reevaluated (i.e., if stochastic fitness functions are used.)", new BoolValue(false)) { Hidden = true });
|
---|
291 | Parameters.Add(new ValueParameter<BoolValue>("PlusSelection", "Include the parents in the selection of the invividuals for the next generation.", new BoolValue(false)) { Hidden = true });
|
---|
292 |
|
---|
293 | Parameters.Add(new ValueParameter<EnumValue<AgingScheme>>("AgingScheme", "The aging scheme for setting the age-limits for the layers.", new EnumValue<AgingScheme>(ALPS.AgingScheme.Polynomial)));
|
---|
294 | Parameters.Add(new ValueParameter<IntValue>("AgeGap", "The frequency of reseeding the lowest layer and scaling factor for the age-limits for the layers", new IntValue(20)));
|
---|
295 | Parameters.Add(new ValueParameter<EnumValue<AgeInheritance>>("AgeInheritance", "The operator for determining the age of an offspring based the parents' age.", new EnumValue<AgeInheritance>(ALPS.AgeInheritance.Older)));
|
---|
296 | Parameters.Add(new ValueParameter<ReductionOperation>("AgeInheritanceReduction") { Hidden = true });
|
---|
297 | Parameters.Add(new ValueParameter<IntArray>("AgeLimits", new IntArray(new int[0])) { Hidden = true });
|
---|
298 |
|
---|
299 | Parameters.Add(new ValueParameter<IntValue>("MatingPoolRange", "The range of layers used for creating a mating pool. (1 = current + previous layer)", new IntValue(1)) { Hidden = true });
|
---|
300 | Parameters.Add(new ValueParameter<PercentValue>("MatingPoolSelectionPercentage", "Percentage of the previous layers used for creating a mating pool.", new PercentValue(1.0, restrictToUnitInterval: true)) { Hidden = true });
|
---|
301 |
|
---|
302 | Parameters.Add(new ValueParameter<MultiTerminator>("Terminator", "The termination criteria which sould be checked.", new MultiTerminator()));
|
---|
303 | #endregion
|
---|
304 |
|
---|
305 | #region Create operators
|
---|
306 | var globalRandomCreator = new RandomCreator();
|
---|
307 | var layer0Creator = new SubScopesCreator() { Name = "Create Layer Zero" };
|
---|
308 | var layer0Processor = new UniformSubScopesProcessor();
|
---|
309 | var localRandomCreator = new LocalRandomCreator();
|
---|
310 | var layerVariableCreator = new VariableCreator();
|
---|
311 | var layerSolutionsCreator = new SolutionsCreator();
|
---|
312 | var initializeAgeProcessor = new UniformSubScopesProcessor();
|
---|
313 | var initializeAge = new VariableCreator() { Name = "Initialize Age" };
|
---|
314 | var initializeLayerPopulationSize = new SubScopesCounter() { Name = "Init LayerPopulationCounter" };
|
---|
315 | var initializeLocalEvaluatedSolutions = new Assigner() { Name = "Initialize LayerEvaluatedSolutions" };
|
---|
316 | var initializeGlobalEvaluatedSolutions = new DataReducer() { Name = "Initialize EvaluatedSolutions" };
|
---|
317 | var resultsCollector = new ResultsCollector();
|
---|
318 | var mainLoop = new AlpsGeneticAlgorithmMainLoop();
|
---|
319 | #endregion
|
---|
320 |
|
---|
321 | #region Create and parameterize operator graph
|
---|
322 | OperatorGraph.InitialOperator = globalRandomCreator;
|
---|
323 |
|
---|
324 | globalRandomCreator.RandomParameter.ActualName = "GlobalRandom";
|
---|
325 | globalRandomCreator.SeedParameter.Value = null;
|
---|
326 | globalRandomCreator.SetSeedRandomlyParameter.Value = null;
|
---|
327 | globalRandomCreator.Successor = layer0Creator;
|
---|
328 |
|
---|
329 | layer0Creator.NumberOfSubScopesParameter.Value = new IntValue(1);
|
---|
330 | layer0Creator.Successor = layer0Processor;
|
---|
331 |
|
---|
332 | layer0Processor.Operator = localRandomCreator;
|
---|
333 | layer0Processor.Successor = initializeGlobalEvaluatedSolutions;
|
---|
334 |
|
---|
335 | localRandomCreator.Successor = layerVariableCreator;
|
---|
336 |
|
---|
337 | layerVariableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Layer", new IntValue(0)));
|
---|
338 | layerVariableCreator.Successor = layerSolutionsCreator;
|
---|
339 |
|
---|
340 | layerSolutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
|
---|
341 | layerSolutionsCreator.Successor = initializeAgeProcessor;
|
---|
342 |
|
---|
343 | initializeAgeProcessor.Operator = initializeAge;
|
---|
344 | initializeAgeProcessor.Successor = initializeLayerPopulationSize;
|
---|
345 |
|
---|
346 | initializeLayerPopulationSize.ValueParameter.ActualName = "LayerPopulationSize";
|
---|
347 | initializeLayerPopulationSize.Successor = initializeLocalEvaluatedSolutions;
|
---|
348 |
|
---|
349 | initializeAge.CollectedValues.Add(new ValueParameter<IntValue>("Age", new IntValue(0)));
|
---|
350 | initializeAge.Successor = null;
|
---|
351 |
|
---|
352 | initializeLocalEvaluatedSolutions.LeftSideParameter.ActualName = "LayerEvaluatedSolutions";
|
---|
353 | initializeLocalEvaluatedSolutions.RightSideParameter.ActualName = "LayerPopulationSize";
|
---|
354 | initializeLocalEvaluatedSolutions.Successor = null;
|
---|
355 |
|
---|
356 | initializeGlobalEvaluatedSolutions.ReductionOperation.Value.Value = ReductionOperations.Sum;
|
---|
357 | initializeGlobalEvaluatedSolutions.TargetOperation.Value.Value = ReductionOperations.Assign;
|
---|
358 | initializeGlobalEvaluatedSolutions.ParameterToReduce.ActualName = "LayerEvaluatedSolutions";
|
---|
359 | initializeGlobalEvaluatedSolutions.TargetParameter.ActualName = "EvaluatedSolutions";
|
---|
360 | initializeGlobalEvaluatedSolutions.Successor = resultsCollector;
|
---|
361 |
|
---|
362 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
|
---|
363 | resultsCollector.Successor = mainLoop;
|
---|
364 | #endregion
|
---|
365 |
|
---|
366 | #region Set selectors
|
---|
367 | foreach (var selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(s => !(s is IMultiObjectiveSelector)).OrderBy(s => Name))
|
---|
368 | SelectorParameter.ValidValues.Add(selector);
|
---|
369 | var defaultSelector = SelectorParameter.ValidValues.OfType<GeneralizedRankSelector>().FirstOrDefault();
|
---|
370 | if (defaultSelector != null) {
|
---|
371 | defaultSelector.PressureParameter.Value = new DoubleValue(4);
|
---|
372 | SelectorParameter.Value = defaultSelector;
|
---|
373 | }
|
---|
374 | #endregion
|
---|
375 |
|
---|
376 | #region Create analyzers
|
---|
377 | qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
|
---|
378 | layerQualityAnalyzer = new BestAverageWorstQualityAnalyzer();
|
---|
379 | #endregion
|
---|
380 |
|
---|
381 | #region Create terminators
|
---|
382 | generationsTerminator = new ComparisonTerminator<IntValue>("Generations", ComparisonType.Less, new IntValue(1000)) { Name = "Generations" };
|
---|
383 | evaluationsTerminator = new ComparisonTerminator<IntValue>("EvaluatedSolutions", ComparisonType.Less, new IntValue(int.MaxValue)) { Name = "Evaluations" };
|
---|
384 | qualityTerminator = new SingleObjectiveQualityTerminator() { Name = "Quality" };
|
---|
385 | executionTimeTerminator = new ExecutionTimeTerminator(this, new TimeSpanValue(TimeSpan.FromMinutes(5)));
|
---|
386 | #endregion
|
---|
387 |
|
---|
388 | #region Parameterize
|
---|
389 | ParameterizeAnalyzers();
|
---|
390 | UpdateAnalyzers();
|
---|
391 |
|
---|
392 | ParameterizeSelectors();
|
---|
393 |
|
---|
394 | UpdateTerminators();
|
---|
395 |
|
---|
396 | ParameterizeAgeLimits();
|
---|
397 | ParameterizeAgeInheritanceReduction();
|
---|
398 | #endregion
|
---|
399 |
|
---|
400 | Initialize();
|
---|
401 | }
|
---|
402 |
|
---|
403 | #region Events
|
---|
404 | public override void Prepare() {
|
---|
405 | if (Problem != null)
|
---|
406 | base.Prepare();
|
---|
407 | }
|
---|
408 | protected override void OnProblemChanged() {
|
---|
409 | base.OnProblemChanged();
|
---|
410 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
411 | ParameterizeStochasticOperatorForLayer(Problem.Evaluator);
|
---|
412 | foreach (var @operator in Problem.Operators.OfType<IOperator>())
|
---|
413 | ParameterizeStochasticOperator(@operator);
|
---|
414 |
|
---|
415 | ParameterizeIterationBasedOperators();
|
---|
416 |
|
---|
417 | ParameterizeSolutionsCreator();
|
---|
418 | ParameterizeMainLoop();
|
---|
419 | ParameterizeAnalyzers();
|
---|
420 | ParameterizeSelectors();
|
---|
421 | ParameterizeTerminators();
|
---|
422 |
|
---|
423 | Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
|
---|
424 |
|
---|
425 | UpdateAnalyzers();
|
---|
426 | UpdateCrossovers();
|
---|
427 | UpdateMutators();
|
---|
428 | UpdateTerminators();
|
---|
429 | }
|
---|
430 |
|
---|
431 | protected override void RegisterProblemEvents() {
|
---|
432 | base.RegisterProblemEvents();
|
---|
433 | var maximizationParameter = (IValueParameter<BoolValue>)Problem.MaximizationParameter;
|
---|
434 | if (maximizationParameter != null) maximizationParameter.ValueChanged += new EventHandler(MaximizationParameter_ValueChanged);
|
---|
435 | }
|
---|
436 | protected override void DeregisterProblemEvents() {
|
---|
437 | var maximizationParameter = (IValueParameter<BoolValue>)Problem.MaximizationParameter;
|
---|
438 | if (maximizationParameter != null) maximizationParameter.ValueChanged -= new EventHandler(MaximizationParameter_ValueChanged);
|
---|
439 | base.DeregisterProblemEvents();
|
---|
440 | }
|
---|
441 |
|
---|
442 | protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
|
---|
443 | base.Problem_SolutionCreatorChanged(sender, e);
|
---|
444 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
445 | ParameterizeStochasticOperatorForLayer(Problem.Evaluator);
|
---|
446 |
|
---|
447 | Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
|
---|
448 |
|
---|
449 | ParameterizeSolutionsCreator();
|
---|
450 | ParameterizeAnalyzers();
|
---|
451 | }
|
---|
452 | protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
|
---|
453 | base.Problem_EvaluatorChanged(sender, e);
|
---|
454 | foreach (var @operator in Problem.Operators.OfType<IOperator>())
|
---|
455 | ParameterizeStochasticOperator(@operator);
|
---|
456 |
|
---|
457 | UpdateAnalyzers();
|
---|
458 |
|
---|
459 | ParameterizeSolutionsCreator();
|
---|
460 | ParameterizeMainLoop();
|
---|
461 | ParameterizeSelectors();
|
---|
462 | }
|
---|
463 | protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
|
---|
464 | base.Problem_OperatorsChanged(sender, e);
|
---|
465 | ParameterizeIterationBasedOperators();
|
---|
466 | UpdateCrossovers();
|
---|
467 | UpdateMutators();
|
---|
468 | UpdateTerminators();
|
---|
469 | }
|
---|
470 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
471 | ParameterizeMainLoop();
|
---|
472 | ParameterizeAnalyzers();
|
---|
473 | ParameterizeSelectors();
|
---|
474 | }
|
---|
475 | private void MaximizationParameter_ValueChanged(object sender, EventArgs e) {
|
---|
476 | ParameterizeTerminators();
|
---|
477 | }
|
---|
478 | private void QualityAnalyzer_CurrentBestQualityParameter_NameChanged(object sender, EventArgs e) {
|
---|
479 | ParameterizeTerminators();
|
---|
480 | }
|
---|
481 |
|
---|
482 | private void AgeGapParameter_ValueChanged(object sender, EventArgs e) {
|
---|
483 | AgeGap.ValueChanged += AgeGap_ValueChanged;
|
---|
484 | ParameterizeAgeLimits();
|
---|
485 | }
|
---|
486 | private void AgeGap_ValueChanged(object sender, EventArgs e) {
|
---|
487 | ParameterizeAgeLimits();
|
---|
488 | }
|
---|
489 | private void AgingSchemeParameter_ValueChanged(object sender, EventArgs e) {
|
---|
490 | AgingScheme.ValueChanged += AgingScheme_ValueChanged;
|
---|
491 | ParameterizeAgeLimits();
|
---|
492 | }
|
---|
493 | private void AgingScheme_ValueChanged(object sender, EventArgs e) {
|
---|
494 | ParameterizeAgeLimits();
|
---|
495 | }
|
---|
496 | private void NumberOfLayersParameter_ValueChanged(object sender, EventArgs e) {
|
---|
497 | NumberOfLayers.ValueChanged += NumberOfLayers_ValueChanged;
|
---|
498 | ParameterizeAgeLimits();
|
---|
499 | }
|
---|
500 | private void NumberOfLayers_ValueChanged(object sender, EventArgs e) {
|
---|
501 | ParameterizeAgeLimits();
|
---|
502 | }
|
---|
503 | private void AgeInheritanceParameter_ValueChanged(object sender, EventArgs e) {
|
---|
504 | AgeInheritance.ValueChanged += AgeInheritance_ValueChanged;
|
---|
505 | ParameterizeAgeInheritanceReduction();
|
---|
506 | }
|
---|
507 | private void AgeInheritance_ValueChanged(object sender, EventArgs e) {
|
---|
508 | ParameterizeAgeInheritanceReduction();
|
---|
509 | }
|
---|
510 |
|
---|
511 | private void AnalyzerOperators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IAnalyzer>> e) {
|
---|
512 | foreach (var analyzer in e.Items) {
|
---|
513 | foreach (var parameter in analyzer.Value.Parameters.OfType<IScopeTreeLookupParameter>()) {
|
---|
514 | parameter.Depth = 2;
|
---|
515 | }
|
---|
516 | }
|
---|
517 | }
|
---|
518 | private void LayerAnalyzerOperators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IAnalyzer>> e) {
|
---|
519 | foreach (var analyzer in e.Items) {
|
---|
520 | IParameter resultParameter;
|
---|
521 | if (analyzer.Value.Parameters.TryGetValue("Results", out resultParameter)) {
|
---|
522 | var lookupParameter = resultParameter as ILookupParameter;
|
---|
523 | if (lookupParameter != null)
|
---|
524 | lookupParameter.ActualName = "LayerResults";
|
---|
525 | }
|
---|
526 | foreach (var parameter in analyzer.Value.Parameters.OfType<IScopeTreeLookupParameter>()) {
|
---|
527 | parameter.Depth = 1;
|
---|
528 | }
|
---|
529 | }
|
---|
530 | }
|
---|
531 | #endregion
|
---|
532 |
|
---|
533 | #region Parameterization
|
---|
534 | private void Initialize() {
|
---|
535 | if (Problem != null)
|
---|
536 | Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
|
---|
537 |
|
---|
538 | NumberOfLayersParameter.ValueChanged += NumberOfLayersParameter_ValueChanged;
|
---|
539 | NumberOfLayers.ValueChanged += NumberOfLayers_ValueChanged;
|
---|
540 |
|
---|
541 | Analyzer.Operators.ItemsAdded += AnalyzerOperators_ItemsAdded;
|
---|
542 | LayerAnalyzer.Operators.ItemsAdded += LayerAnalyzerOperators_ItemsAdded;
|
---|
543 |
|
---|
544 | AgeGapParameter.ValueChanged += AgeGapParameter_ValueChanged;
|
---|
545 | AgeGap.ValueChanged += AgeGap_ValueChanged;
|
---|
546 | AgingSchemeParameter.ValueChanged += AgingSchemeParameter_ValueChanged;
|
---|
547 | AgingScheme.ValueChanged += AgingScheme_ValueChanged;
|
---|
548 | AgeInheritanceParameter.ValueChanged += AgeInheritanceParameter_ValueChanged;
|
---|
549 | AgeInheritance.ValueChanged += AgeInheritance_ValueChanged;
|
---|
550 |
|
---|
551 | qualityAnalyzer.CurrentBestQualityParameter.NameChanged += new EventHandler(QualityAnalyzer_CurrentBestQualityParameter_NameChanged);
|
---|
552 | }
|
---|
553 | private void ParameterizeSolutionsCreator() {
|
---|
554 | SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
555 | SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
|
---|
556 | MainLoop.LayerUpdator.SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
557 | MainLoop.LayerUpdator.SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
|
---|
558 | }
|
---|
559 | private void ParameterizeMainLoop() {
|
---|
560 | MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
561 | MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
562 | MainLoop.MainOperator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
563 | MainLoop.MainOperator.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
564 | MainLoop.MainOperator.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
565 | MainLoop.LayerUpdator.SolutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
|
---|
566 | }
|
---|
567 | private void ParameterizeAnalyzers() {
|
---|
568 | qualityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
569 | qualityAnalyzer.ResultsParameter.Hidden = true;
|
---|
570 | qualityAnalyzer.QualityParameter.Depth = 2;
|
---|
571 | layerQualityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
572 | layerQualityAnalyzer.ResultsParameter.Hidden = true;
|
---|
573 | layerQualityAnalyzer.QualityParameter.Depth = 1;
|
---|
574 | if (Problem != null) {
|
---|
575 | qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
576 | qualityAnalyzer.MaximizationParameter.Hidden = true;
|
---|
577 | qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
578 | qualityAnalyzer.QualityParameter.Hidden = true;
|
---|
579 | qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
580 | qualityAnalyzer.BestKnownQualityParameter.Hidden = true;
|
---|
581 | layerQualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
582 | layerQualityAnalyzer.MaximizationParameter.Hidden = true;
|
---|
583 | layerQualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
584 | layerQualityAnalyzer.QualityParameter.Hidden = true;
|
---|
585 | layerQualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
586 | layerQualityAnalyzer.BestKnownQualityParameter.Hidden = true;
|
---|
587 | }
|
---|
588 | }
|
---|
589 | private void ParameterizeSelectors() {
|
---|
590 | foreach (var selector in SelectorParameter.ValidValues) {
|
---|
591 | selector.CopySelected = new BoolValue(true);
|
---|
592 | // Explicit setting of NumberOfSelectedSubScopesParameter is not required anymore because the NumberOfSelectedSubScopesCalculator calculates it itself
|
---|
593 | //selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * (PopulationSize - Elites.Value));
|
---|
594 | selector.NumberOfSelectedSubScopesParameter.Hidden = true;
|
---|
595 | ParameterizeStochasticOperatorForLayer(selector);
|
---|
596 | }
|
---|
597 | if (Problem != null) {
|
---|
598 | foreach (var selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
|
---|
599 | selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
600 | selector.MaximizationParameter.Hidden = true;
|
---|
601 | selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
602 | selector.QualityParameter.Hidden = true;
|
---|
603 | }
|
---|
604 | }
|
---|
605 | }
|
---|
606 | private void ParameterizeTerminators() {
|
---|
607 | qualityTerminator.Parameterize(qualityAnalyzer.CurrentBestQualityParameter, Problem);
|
---|
608 | }
|
---|
609 | private void ParameterizeIterationBasedOperators() {
|
---|
610 | if (Problem != null) {
|
---|
611 | foreach (var @operator in Problem.Operators.OfType<IIterationBasedOperator>()) {
|
---|
612 | @operator.IterationsParameter.ActualName = "Generations";
|
---|
613 | @operator.IterationsParameter.Hidden = true;
|
---|
614 | @operator.MaximumIterationsParameter.ActualName = generationsTerminator.ThresholdParameter.Name;
|
---|
615 | @operator.MaximumIterationsParameter.Hidden = true;
|
---|
616 | }
|
---|
617 | }
|
---|
618 | }
|
---|
619 | private void ParameterizeAgeInheritanceReduction() {
|
---|
620 | AgeInheritanceReduction.Value = GetAgeInheritanceReduction(AgeInheritance.Value);
|
---|
621 | }
|
---|
622 | private void ParameterizeAgeLimits() {
|
---|
623 | var scheme = AgingScheme.Value;
|
---|
624 | int ageGap = AgeGap.Value;
|
---|
625 | int numberOfLayers = NumberOfLayers.Value;
|
---|
626 | AgeLimits = scheme.CalculateAgeLimits(ageGap, numberOfLayers);
|
---|
627 | }
|
---|
628 |
|
---|
629 | private void ParameterizeStochasticOperator(IOperator @operator) {
|
---|
630 | var stochasticOperator = @operator as IStochasticOperator;
|
---|
631 | if (stochasticOperator != null) {
|
---|
632 | stochasticOperator.RandomParameter.ActualName = "GlobalRandom";
|
---|
633 | stochasticOperator.RandomParameter.Hidden = true;
|
---|
634 | }
|
---|
635 | }
|
---|
636 |
|
---|
637 | private void ParameterizeStochasticOperatorForLayer(IOperator @operator) {
|
---|
638 | var stochasticOperator = @operator as IStochasticOperator;
|
---|
639 | if (stochasticOperator != null) {
|
---|
640 | stochasticOperator.RandomParameter.ActualName = "LocalRandom";
|
---|
641 | stochasticOperator.RandomParameter.Hidden = true;
|
---|
642 | }
|
---|
643 | }
|
---|
644 |
|
---|
645 | private ReductionOperations GetAgeInheritanceReduction(AgeInheritance ageInheritance) {
|
---|
646 | switch (ageInheritance) {
|
---|
647 | case ALPS.AgeInheritance.Older: return ReductionOperations.Max;
|
---|
648 | case ALPS.AgeInheritance.Agerage: return ReductionOperations.Avg;
|
---|
649 | case ALPS.AgeInheritance.Younger: return ReductionOperations.Min;
|
---|
650 | default: throw new NotSupportedException("AgeInheritance " + ageInheritance + " is not supported.");
|
---|
651 | }
|
---|
652 | }
|
---|
653 | #endregion
|
---|
654 |
|
---|
655 | #region Updates
|
---|
656 | private void UpdateAnalyzers() {
|
---|
657 | Analyzer.Operators.Clear();
|
---|
658 | LayerAnalyzer.Operators.Clear();
|
---|
659 |
|
---|
660 | Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
|
---|
661 | LayerAnalyzer.Operators.Add(layerQualityAnalyzer, layerQualityAnalyzer.EnabledByDefault);
|
---|
662 |
|
---|
663 | if (Problem != null) {
|
---|
664 | foreach (var analyzer in Problem.Operators.OfType<IAnalyzer>()) {
|
---|
665 | Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
|
---|
666 | }
|
---|
667 | }
|
---|
668 | }
|
---|
669 | private void UpdateCrossovers() {
|
---|
670 | var oldCrossover = CrossoverParameter.Value;
|
---|
671 | var defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
|
---|
672 | CrossoverParameter.ValidValues.Clear();
|
---|
673 | foreach (var crossover in Problem.Operators.OfType<ICrossover>().OrderBy(c => c.Name)) {
|
---|
674 | ParameterizeStochasticOperatorForLayer(crossover);
|
---|
675 | CrossoverParameter.ValidValues.Add(crossover);
|
---|
676 | }
|
---|
677 | if (oldCrossover != null) {
|
---|
678 | var crossover = CrossoverParameter.ValidValues.FirstOrDefault(c => c.GetType() == oldCrossover.GetType());
|
---|
679 | if (crossover != null)
|
---|
680 | CrossoverParameter.Value = crossover;
|
---|
681 | else
|
---|
682 | oldCrossover = null;
|
---|
683 | }
|
---|
684 | if (oldCrossover == null && defaultCrossover != null)
|
---|
685 | CrossoverParameter.Value = defaultCrossover;
|
---|
686 | }
|
---|
687 | private void UpdateMutators() {
|
---|
688 | var oldMutator = MutatorParameter.Value;
|
---|
689 | MutatorParameter.ValidValues.Clear();
|
---|
690 | foreach (var mutator in Problem.Operators.OfType<IManipulator>().OrderBy(m => m.Name)) {
|
---|
691 | ParameterizeStochasticOperatorForLayer(mutator);
|
---|
692 | MutatorParameter.ValidValues.Add(mutator);
|
---|
693 | }
|
---|
694 | if (oldMutator != null) {
|
---|
695 | var mutator = MutatorParameter.ValidValues.FirstOrDefault(m => m.GetType() == oldMutator.GetType());
|
---|
696 | if (mutator != null)
|
---|
697 | MutatorParameter.Value = mutator;
|
---|
698 | }
|
---|
699 | }
|
---|
700 | private void UpdateTerminators() {
|
---|
701 | var newTerminators = new Dictionary<ITerminator, bool> {
|
---|
702 | {generationsTerminator, !Terminators.Operators.Contains(generationsTerminator) || Terminators.Operators.ItemChecked(generationsTerminator)},
|
---|
703 | {evaluationsTerminator, Terminators.Operators.Contains(evaluationsTerminator) && Terminators.Operators.ItemChecked(evaluationsTerminator)},
|
---|
704 | {qualityTerminator, Terminators.Operators.Contains(qualityTerminator) && Terminators.Operators.ItemChecked(qualityTerminator) },
|
---|
705 | {executionTimeTerminator, Terminators.Operators.Contains(executionTimeTerminator) && Terminators.Operators.ItemChecked(executionTimeTerminator)}
|
---|
706 | };
|
---|
707 | if (Problem != null) {
|
---|
708 | foreach (var terminator in Problem.Operators.OfType<ITerminator>())
|
---|
709 | newTerminators.Add(terminator, !Terminators.Operators.Contains(terminator) || Terminators.Operators.ItemChecked(terminator));
|
---|
710 | }
|
---|
711 |
|
---|
712 | Terminators.Operators.Clear();
|
---|
713 |
|
---|
714 | foreach (var newTerminator in newTerminators)
|
---|
715 | Terminators.Operators.Add(newTerminator.Key, newTerminator.Value);
|
---|
716 | }
|
---|
717 | #endregion
|
---|
718 | }
|
---|
719 | } |
---|