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.Linq;
|
---|
24 | using HeuristicLab.Analysis;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Operators;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Optimization.Operators;
|
---|
31 | using HeuristicLab.Parameters;
|
---|
32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
33 | using HeuristicLab.PluginInfrastructure;
|
---|
34 | using HeuristicLab.Random;
|
---|
35 |
|
---|
36 | namespace HeuristicLab.Algorithms.GeneticAlgorithm {
|
---|
37 | /// <summary>
|
---|
38 | /// An island genetic algorithm.
|
---|
39 | /// </summary>
|
---|
40 | [Item("Island Genetic Algorithm (Island-GA)", "An island genetic algorithm.")]
|
---|
41 | [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 110)]
|
---|
42 | [StorableClass]
|
---|
43 | public sealed class IslandGeneticAlgorithm : 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 ValueParameter<IntValue> SeedParameter {
|
---|
58 | get { return (ValueParameter<IntValue>)Parameters["Seed"]; }
|
---|
59 | }
|
---|
60 | private ValueParameter<BoolValue> SetSeedRandomlyParameter {
|
---|
61 | get { return (ValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
|
---|
62 | }
|
---|
63 | private ValueParameter<IntValue> NumberOfIslandsParameter {
|
---|
64 | get { return (ValueParameter<IntValue>)Parameters["NumberOfIslands"]; }
|
---|
65 | }
|
---|
66 | private ValueParameter<IntValue> MigrationIntervalParameter {
|
---|
67 | get { return (ValueParameter<IntValue>)Parameters["MigrationInterval"]; }
|
---|
68 | }
|
---|
69 | private ValueParameter<PercentValue> MigrationRateParameter {
|
---|
70 | get { return (ValueParameter<PercentValue>)Parameters["MigrationRate"]; }
|
---|
71 | }
|
---|
72 | public IConstrainedValueParameter<IMigrator> MigratorParameter {
|
---|
73 | get { return (IConstrainedValueParameter<IMigrator>)Parameters["Migrator"]; }
|
---|
74 | }
|
---|
75 | public IConstrainedValueParameter<ISelector> EmigrantsSelectorParameter {
|
---|
76 | get { return (IConstrainedValueParameter<ISelector>)Parameters["EmigrantsSelector"]; }
|
---|
77 | }
|
---|
78 | public IConstrainedValueParameter<IReplacer> ImmigrationReplacerParameter {
|
---|
79 | get { return (IConstrainedValueParameter<IReplacer>)Parameters["ImmigrationReplacer"]; }
|
---|
80 | }
|
---|
81 | private ValueParameter<IntValue> PopulationSizeParameter {
|
---|
82 | get { return (ValueParameter<IntValue>)Parameters["PopulationSize"]; }
|
---|
83 | }
|
---|
84 | private ValueParameter<IntValue> MaximumGenerationsParameter {
|
---|
85 | get { return (ValueParameter<IntValue>)Parameters["MaximumGenerations"]; }
|
---|
86 | }
|
---|
87 | public IConstrainedValueParameter<ISelector> SelectorParameter {
|
---|
88 | get { return (IConstrainedValueParameter<ISelector>)Parameters["Selector"]; }
|
---|
89 | }
|
---|
90 | public IConstrainedValueParameter<ICrossover> CrossoverParameter {
|
---|
91 | get { return (IConstrainedValueParameter<ICrossover>)Parameters["Crossover"]; }
|
---|
92 | }
|
---|
93 | private ValueParameter<PercentValue> MutationProbabilityParameter {
|
---|
94 | get { return (ValueParameter<PercentValue>)Parameters["MutationProbability"]; }
|
---|
95 | }
|
---|
96 | public IConstrainedValueParameter<IManipulator> MutatorParameter {
|
---|
97 | get { return (IConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; }
|
---|
98 | }
|
---|
99 | private ValueParameter<IntValue> ElitesParameter {
|
---|
100 | get { return (ValueParameter<IntValue>)Parameters["Elites"]; }
|
---|
101 | }
|
---|
102 | private IFixedValueParameter<BoolValue> ReevaluateElitesParameter {
|
---|
103 | get { return (IFixedValueParameter<BoolValue>)Parameters["ReevaluateElites"]; }
|
---|
104 | }
|
---|
105 | private ValueParameter<MultiAnalyzer> AnalyzerParameter {
|
---|
106 | get { return (ValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
|
---|
107 | }
|
---|
108 | private ValueParameter<MultiAnalyzer> IslandAnalyzerParameter {
|
---|
109 | get { return (ValueParameter<MultiAnalyzer>)Parameters["IslandAnalyzer"]; }
|
---|
110 | }
|
---|
111 | #endregion
|
---|
112 |
|
---|
113 | #region Properties
|
---|
114 | public IntValue Seed {
|
---|
115 | get { return SeedParameter.Value; }
|
---|
116 | set { SeedParameter.Value = value; }
|
---|
117 | }
|
---|
118 | public BoolValue SetSeedRandomly {
|
---|
119 | get { return SetSeedRandomlyParameter.Value; }
|
---|
120 | set { SetSeedRandomlyParameter.Value = value; }
|
---|
121 | }
|
---|
122 | public IntValue NumberOfIslands {
|
---|
123 | get { return NumberOfIslandsParameter.Value; }
|
---|
124 | set { NumberOfIslandsParameter.Value = value; }
|
---|
125 | }
|
---|
126 | public IntValue MigrationInterval {
|
---|
127 | get { return MigrationIntervalParameter.Value; }
|
---|
128 | set { MigrationIntervalParameter.Value = value; }
|
---|
129 | }
|
---|
130 | public PercentValue MigrationRate {
|
---|
131 | get { return MigrationRateParameter.Value; }
|
---|
132 | set { MigrationRateParameter.Value = value; }
|
---|
133 | }
|
---|
134 | public IMigrator Migrator {
|
---|
135 | get { return MigratorParameter.Value; }
|
---|
136 | set { MigratorParameter.Value = value; }
|
---|
137 | }
|
---|
138 | public ISelector EmigrantsSelector {
|
---|
139 | get { return EmigrantsSelectorParameter.Value; }
|
---|
140 | set { EmigrantsSelectorParameter.Value = value; }
|
---|
141 | }
|
---|
142 | public IReplacer ImmigrationReplacer {
|
---|
143 | get { return ImmigrationReplacerParameter.Value; }
|
---|
144 | set { ImmigrationReplacerParameter.Value = value; }
|
---|
145 | }
|
---|
146 | public IntValue PopulationSize {
|
---|
147 | get { return PopulationSizeParameter.Value; }
|
---|
148 | set { PopulationSizeParameter.Value = value; }
|
---|
149 | }
|
---|
150 | public IntValue MaximumGenerations {
|
---|
151 | get { return MaximumGenerationsParameter.Value; }
|
---|
152 | set { MaximumGenerationsParameter.Value = value; }
|
---|
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 PercentValue MutationProbability {
|
---|
163 | get { return MutationProbabilityParameter.Value; }
|
---|
164 | set { MutationProbabilityParameter.Value = value; }
|
---|
165 | }
|
---|
166 | public IManipulator Mutator {
|
---|
167 | get { return MutatorParameter.Value; }
|
---|
168 | set { MutatorParameter.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 MultiAnalyzer Analyzer {
|
---|
179 | get { return AnalyzerParameter.Value; }
|
---|
180 | set { AnalyzerParameter.Value = value; }
|
---|
181 | }
|
---|
182 | public MultiAnalyzer IslandAnalyzer {
|
---|
183 | get { return IslandAnalyzerParameter.Value; }
|
---|
184 | set { IslandAnalyzerParameter.Value = value; }
|
---|
185 | }
|
---|
186 | private RandomCreator RandomCreator {
|
---|
187 | get { return (RandomCreator)OperatorGraph.InitialOperator; }
|
---|
188 | }
|
---|
189 | private UniformSubScopesProcessor IslandProcessor {
|
---|
190 | get { return OperatorGraph.Iterate().OfType<UniformSubScopesProcessor>().First(x => x.Operator is SolutionsCreator); }
|
---|
191 | }
|
---|
192 | private SolutionsCreator SolutionsCreator {
|
---|
193 | get { return (SolutionsCreator)IslandProcessor.Operator; }
|
---|
194 | }
|
---|
195 | private IslandGeneticAlgorithmMainLoop MainLoop {
|
---|
196 | get { return FindMainLoop(IslandProcessor.Successor); }
|
---|
197 | }
|
---|
198 | [Storable]
|
---|
199 | private BestAverageWorstQualityAnalyzer islandQualityAnalyzer;
|
---|
200 | [Storable]
|
---|
201 | private BestAverageWorstQualityAnalyzer qualityAnalyzer;
|
---|
202 | #endregion
|
---|
203 |
|
---|
204 | [StorableConstructor]
|
---|
205 | private IslandGeneticAlgorithm(bool deserializing) : base(deserializing) { }
|
---|
206 | [StorableHook(HookType.AfterDeserialization)]
|
---|
207 | private void AfterDeserialization() {
|
---|
208 | // BackwardsCompatibility3.3
|
---|
209 | #region Backwards compatible code, remove with 3.4
|
---|
210 | if (!Parameters.ContainsKey("ReevaluateElites")) {
|
---|
211 | Parameters.Add(new FixedValueParameter<BoolValue>("ReevaluateElites", "Flag to determine if elite individuals should be reevaluated (i.e., if stochastic fitness functions are used.)", (BoolValue)new BoolValue(false).AsReadOnly()) { Hidden = true });
|
---|
212 | }
|
---|
213 | #endregion
|
---|
214 |
|
---|
215 | Initialize();
|
---|
216 | }
|
---|
217 | private IslandGeneticAlgorithm(IslandGeneticAlgorithm original, Cloner cloner)
|
---|
218 | : base(original, cloner) {
|
---|
219 | islandQualityAnalyzer = cloner.Clone(original.islandQualityAnalyzer);
|
---|
220 | qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
|
---|
221 | Initialize();
|
---|
222 | }
|
---|
223 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
224 | return new IslandGeneticAlgorithm(this, cloner);
|
---|
225 | }
|
---|
226 |
|
---|
227 | public IslandGeneticAlgorithm()
|
---|
228 | : base() {
|
---|
229 | Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
|
---|
230 | Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
231 | Parameters.Add(new ValueParameter<IntValue>("NumberOfIslands", "The number of islands.", new IntValue(5)));
|
---|
232 | Parameters.Add(new ValueParameter<IntValue>("MigrationInterval", "The number of generations that should pass between migration phases.", new IntValue(20)));
|
---|
233 | Parameters.Add(new ValueParameter<PercentValue>("MigrationRate", "The proportion of individuals that should migrate between the islands.", new PercentValue(0.15)));
|
---|
234 | Parameters.Add(new ConstrainedValueParameter<IMigrator>("Migrator", "The migration strategy."));
|
---|
235 | Parameters.Add(new ConstrainedValueParameter<ISelector>("EmigrantsSelector", "Selects the individuals that will be migrated."));
|
---|
236 | Parameters.Add(new ConstrainedValueParameter<IReplacer>("ImmigrationReplacer", "Selects the population from the unification of the original population and the immigrants."));
|
---|
237 | Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "The size of the population of solutions.", new IntValue(100)));
|
---|
238 | Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations that should be processed.", new IntValue(1000)));
|
---|
239 | Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
|
---|
240 | Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
|
---|
241 | Parameters.Add(new ValueParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new PercentValue(0.05)));
|
---|
242 | Parameters.Add(new OptionalConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
|
---|
243 | Parameters.Add(new ValueParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation.", new IntValue(1)));
|
---|
244 | 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 });
|
---|
245 | Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze the islands.", new MultiAnalyzer()));
|
---|
246 | Parameters.Add(new ValueParameter<MultiAnalyzer>("IslandAnalyzer", "The operator used to analyze each island.", new MultiAnalyzer()));
|
---|
247 |
|
---|
248 | RandomCreator randomCreator = new RandomCreator();
|
---|
249 | UniformSubScopesProcessor ussp0 = new UniformSubScopesProcessor();
|
---|
250 | LocalRandomCreator localRandomCreator = new LocalRandomCreator();
|
---|
251 | RandomCreator globalRandomResetter = new RandomCreator();
|
---|
252 | SubScopesCreator populationCreator = new SubScopesCreator();
|
---|
253 | UniformSubScopesProcessor ussp1 = new UniformSubScopesProcessor();
|
---|
254 | SolutionsCreator solutionsCreator = new SolutionsCreator();
|
---|
255 | VariableCreator variableCreator = new VariableCreator();
|
---|
256 | UniformSubScopesProcessor ussp2 = new UniformSubScopesProcessor();
|
---|
257 | SubScopesCounter subScopesCounter = new SubScopesCounter();
|
---|
258 | ResultsCollector resultsCollector = new ResultsCollector();
|
---|
259 | IslandGeneticAlgorithmMainLoop mainLoop = new IslandGeneticAlgorithmMainLoop();
|
---|
260 | OperatorGraph.InitialOperator = randomCreator;
|
---|
261 |
|
---|
262 | randomCreator.RandomParameter.ActualName = "GlobalRandom";
|
---|
263 | randomCreator.SeedParameter.ActualName = SeedParameter.Name;
|
---|
264 | randomCreator.SeedParameter.Value = null;
|
---|
265 | randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
|
---|
266 | randomCreator.SetSeedRandomlyParameter.Value = null;
|
---|
267 | randomCreator.Successor = populationCreator;
|
---|
268 |
|
---|
269 | populationCreator.NumberOfSubScopesParameter.ActualName = NumberOfIslandsParameter.Name;
|
---|
270 | populationCreator.Successor = ussp0;
|
---|
271 |
|
---|
272 | ussp0.Operator = localRandomCreator;
|
---|
273 | ussp0.Successor = globalRandomResetter;
|
---|
274 |
|
---|
275 | // BackwardsCompatibility3.3
|
---|
276 | // the global random is resetted to ensure the same algorithm results
|
---|
277 | #region Backwards compatible code, remove global random resetter with 3.4 and rewire the operator graph
|
---|
278 | globalRandomResetter.RandomParameter.ActualName = "GlobalRandom";
|
---|
279 | globalRandomResetter.SeedParameter.ActualName = SeedParameter.Name;
|
---|
280 | globalRandomResetter.SeedParameter.Value = null;
|
---|
281 | globalRandomResetter.SetSeedRandomlyParameter.Value = new BoolValue(false);
|
---|
282 | globalRandomResetter.Successor = ussp1;
|
---|
283 | #endregion
|
---|
284 |
|
---|
285 | ussp1.Operator = solutionsCreator;
|
---|
286 | ussp1.Successor = variableCreator;
|
---|
287 |
|
---|
288 | solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
|
---|
289 | //don't create solutions in parallel because the hive engine would distribute these tasks
|
---|
290 | solutionsCreator.ParallelParameter.Value = new BoolValue(false);
|
---|
291 | solutionsCreator.Successor = null;
|
---|
292 |
|
---|
293 | variableCreator.Name = "Initialize EvaluatedSolutions";
|
---|
294 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("EvaluatedSolutions", new IntValue()));
|
---|
295 | variableCreator.Successor = ussp2;
|
---|
296 |
|
---|
297 | ussp2.Operator = subScopesCounter;
|
---|
298 | ussp2.Successor = resultsCollector;
|
---|
299 |
|
---|
300 | subScopesCounter.Name = "Count EvaluatedSolutions";
|
---|
301 | subScopesCounter.ValueParameter.ActualName = "EvaluatedSolutions";
|
---|
302 | subScopesCounter.Successor = null;
|
---|
303 |
|
---|
304 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
|
---|
305 | resultsCollector.ResultsParameter.ActualName = "Results";
|
---|
306 | resultsCollector.Successor = mainLoop;
|
---|
307 |
|
---|
308 | mainLoop.EmigrantsSelectorParameter.ActualName = EmigrantsSelectorParameter.Name;
|
---|
309 | mainLoop.ImmigrationReplacerParameter.ActualName = ImmigrationReplacerParameter.Name;
|
---|
310 | mainLoop.MaximumGenerationsParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
311 | mainLoop.MigrationIntervalParameter.ActualName = MigrationIntervalParameter.Name;
|
---|
312 | mainLoop.MigrationRateParameter.ActualName = MigrationRateParameter.Name;
|
---|
313 | mainLoop.MigratorParameter.ActualName = MigratorParameter.Name;
|
---|
314 | mainLoop.NumberOfIslandsParameter.ActualName = NumberOfIslandsParameter.Name;
|
---|
315 | mainLoop.SelectorParameter.ActualName = SelectorParameter.Name;
|
---|
316 | mainLoop.CrossoverParameter.ActualName = CrossoverParameter.Name;
|
---|
317 | mainLoop.ElitesParameter.ActualName = ElitesParameter.Name;
|
---|
318 | mainLoop.ReevaluateElitesParameter.ActualName = ReevaluateElitesParameter.Name;
|
---|
319 | mainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
|
---|
320 | mainLoop.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
|
---|
321 | mainLoop.RandomParameter.ActualName = randomCreator.RandomParameter.ActualName;
|
---|
322 | mainLoop.ResultsParameter.ActualName = "Results";
|
---|
323 | mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
|
---|
324 | mainLoop.IslandAnalyzerParameter.ActualName = IslandAnalyzerParameter.Name;
|
---|
325 | mainLoop.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
|
---|
326 | mainLoop.Successor = null;
|
---|
327 |
|
---|
328 | foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name))
|
---|
329 | SelectorParameter.ValidValues.Add(selector);
|
---|
330 | ISelector proportionalSelector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("ProportionalSelector"));
|
---|
331 | if (proportionalSelector != null) SelectorParameter.Value = proportionalSelector;
|
---|
332 |
|
---|
333 | foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name))
|
---|
334 | EmigrantsSelectorParameter.ValidValues.Add(selector);
|
---|
335 |
|
---|
336 | foreach (IReplacer replacer in ApplicationManager.Manager.GetInstances<IReplacer>().OrderBy(x => x.Name))
|
---|
337 | ImmigrationReplacerParameter.ValidValues.Add(replacer);
|
---|
338 |
|
---|
339 | ParameterizeSelectors();
|
---|
340 |
|
---|
341 | foreach (IMigrator migrator in ApplicationManager.Manager.GetInstances<IMigrator>().OrderBy(x => x.Name)) {
|
---|
342 | // BackwardsCompatibility3.3
|
---|
343 | // Set the migration direction to counterclockwise
|
---|
344 | var unidirectionalRing = migrator as UnidirectionalRingMigrator;
|
---|
345 | if (unidirectionalRing != null) unidirectionalRing.ClockwiseMigrationParameter.Value = new BoolValue(false);
|
---|
346 | MigratorParameter.ValidValues.Add(migrator);
|
---|
347 | }
|
---|
348 |
|
---|
349 | qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
|
---|
350 | islandQualityAnalyzer = new BestAverageWorstQualityAnalyzer();
|
---|
351 | ParameterizeAnalyzers();
|
---|
352 | UpdateAnalyzers();
|
---|
353 |
|
---|
354 | Initialize();
|
---|
355 | }
|
---|
356 |
|
---|
357 | public override void Prepare() {
|
---|
358 | if (Problem != null) base.Prepare();
|
---|
359 | }
|
---|
360 |
|
---|
361 | #region Events
|
---|
362 | protected override void OnProblemChanged() {
|
---|
363 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
364 | ParameterizeStochasticOperatorForIsland(Problem.Evaluator);
|
---|
365 | foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
|
---|
366 | ParameterizeSolutionsCreator();
|
---|
367 | ParameterizeMainLoop();
|
---|
368 | ParameterizeSelectors();
|
---|
369 | ParameterizeAnalyzers();
|
---|
370 | ParameterizeIterationBasedOperators();
|
---|
371 | UpdateCrossovers();
|
---|
372 | UpdateMutators();
|
---|
373 | UpdateAnalyzers();
|
---|
374 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
375 | base.OnProblemChanged();
|
---|
376 | }
|
---|
377 |
|
---|
378 | protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
|
---|
379 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
380 | ParameterizeSolutionsCreator();
|
---|
381 | base.Problem_SolutionCreatorChanged(sender, e);
|
---|
382 | }
|
---|
383 | protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
|
---|
384 | ParameterizeStochasticOperatorForIsland(Problem.Evaluator);
|
---|
385 | ParameterizeSolutionsCreator();
|
---|
386 | ParameterizeMainLoop();
|
---|
387 | ParameterizeSelectors();
|
---|
388 | ParameterizeAnalyzers();
|
---|
389 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
390 | base.Problem_EvaluatorChanged(sender, e);
|
---|
391 | }
|
---|
392 | protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
|
---|
393 | foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
|
---|
394 | ParameterizeIterationBasedOperators();
|
---|
395 | UpdateCrossovers();
|
---|
396 | UpdateMutators();
|
---|
397 | UpdateAnalyzers();
|
---|
398 | base.Problem_OperatorsChanged(sender, e);
|
---|
399 | }
|
---|
400 | private void ElitesParameter_ValueChanged(object sender, EventArgs e) {
|
---|
401 | Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
|
---|
402 | ParameterizeSelectors();
|
---|
403 | }
|
---|
404 | private void Elites_ValueChanged(object sender, EventArgs e) {
|
---|
405 | ParameterizeSelectors();
|
---|
406 | }
|
---|
407 | private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
|
---|
408 | NumberOfIslands.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
|
---|
409 | ParameterizeSelectors();
|
---|
410 | }
|
---|
411 | private void PopulationSize_ValueChanged(object sender, EventArgs e) {
|
---|
412 | ParameterizeSelectors();
|
---|
413 | }
|
---|
414 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
415 | ParameterizeMainLoop();
|
---|
416 | ParameterizeSelectors();
|
---|
417 | ParameterizeAnalyzers();
|
---|
418 | }
|
---|
419 | private void MigrationRateParameter_ValueChanged(object sender, EventArgs e) {
|
---|
420 | MigrationRate.ValueChanged += new EventHandler(MigrationRate_ValueChanged);
|
---|
421 | ParameterizeSelectors();
|
---|
422 | }
|
---|
423 | private void MigrationRate_ValueChanged(object sender, EventArgs e) {
|
---|
424 | ParameterizeSelectors();
|
---|
425 | }
|
---|
426 | #endregion
|
---|
427 |
|
---|
428 | #region Helpers
|
---|
429 | private void Initialize() {
|
---|
430 | PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
|
---|
431 | PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
|
---|
432 | MigrationRateParameter.ValueChanged += new EventHandler(MigrationRateParameter_ValueChanged);
|
---|
433 | MigrationRate.ValueChanged += new EventHandler(MigrationRate_ValueChanged);
|
---|
434 | ElitesParameter.ValueChanged += new EventHandler(ElitesParameter_ValueChanged);
|
---|
435 | Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
|
---|
436 | if (Problem != null) {
|
---|
437 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
438 | }
|
---|
439 | }
|
---|
440 | private void ParameterizeSolutionsCreator() {
|
---|
441 | SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
442 | SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
|
---|
443 | }
|
---|
444 | private void ParameterizeMainLoop() {
|
---|
445 | MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
446 | MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
447 | MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
448 | MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
449 | }
|
---|
450 | private void ParameterizeStochasticOperator(IOperator op) {
|
---|
451 | IStochasticOperator stochasticOp = op as IStochasticOperator;
|
---|
452 | if (stochasticOp != null) {
|
---|
453 | stochasticOp.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
454 | stochasticOp.RandomParameter.Hidden = true;
|
---|
455 | }
|
---|
456 | }
|
---|
457 | private void ParameterizeStochasticOperatorForIsland(IOperator op) {
|
---|
458 | IStochasticOperator stochasticOp = op as IStochasticOperator;
|
---|
459 | if (stochasticOp != null) {
|
---|
460 | stochasticOp.RandomParameter.ActualName = "LocalRandom";
|
---|
461 | stochasticOp.RandomParameter.Hidden = true;
|
---|
462 | }
|
---|
463 | }
|
---|
464 | private void ParameterizeSelectors() {
|
---|
465 | foreach (ISelector selector in SelectorParameter.ValidValues) {
|
---|
466 | selector.CopySelected = new BoolValue(true);
|
---|
467 | selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * (PopulationSize.Value - Elites.Value));
|
---|
468 | selector.NumberOfSelectedSubScopesParameter.Hidden = true;
|
---|
469 | ParameterizeStochasticOperatorForIsland(selector);
|
---|
470 | }
|
---|
471 | foreach (ISelector selector in EmigrantsSelectorParameter.ValidValues) {
|
---|
472 | selector.CopySelected = new BoolValue(true);
|
---|
473 | selector.NumberOfSelectedSubScopesParameter.Value = new IntValue((int)Math.Ceiling(PopulationSize.Value * MigrationRate.Value));
|
---|
474 | selector.NumberOfSelectedSubScopesParameter.Hidden = true;
|
---|
475 | ParameterizeStochasticOperator(selector);
|
---|
476 | }
|
---|
477 | foreach (IReplacer replacer in ImmigrationReplacerParameter.ValidValues) {
|
---|
478 | ParameterizeStochasticOperator(replacer);
|
---|
479 | }
|
---|
480 | if (Problem != null) {
|
---|
481 | foreach (ISingleObjectiveSelector selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
|
---|
482 | selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
483 | selector.MaximizationParameter.Hidden = true;
|
---|
484 | selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
485 | selector.QualityParameter.Hidden = true;
|
---|
486 | }
|
---|
487 | foreach (ISingleObjectiveSelector selector in EmigrantsSelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
|
---|
488 | selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
489 | selector.MaximizationParameter.Hidden = true;
|
---|
490 | selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
491 | selector.QualityParameter.Hidden = true;
|
---|
492 | }
|
---|
493 | foreach (ISingleObjectiveReplacer selector in ImmigrationReplacerParameter.ValidValues.OfType<ISingleObjectiveReplacer>()) {
|
---|
494 | selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
495 | selector.MaximizationParameter.Hidden = true;
|
---|
496 | selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
497 | selector.QualityParameter.Hidden = true;
|
---|
498 | }
|
---|
499 | }
|
---|
500 | }
|
---|
501 | private void ParameterizeAnalyzers() {
|
---|
502 | islandQualityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
503 | islandQualityAnalyzer.ResultsParameter.Hidden = true;
|
---|
504 | islandQualityAnalyzer.QualityParameter.Depth = 1;
|
---|
505 | qualityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
506 | qualityAnalyzer.ResultsParameter.Hidden = true;
|
---|
507 | qualityAnalyzer.QualityParameter.Depth = 2;
|
---|
508 |
|
---|
509 | if (Problem != null) {
|
---|
510 | islandQualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
511 | islandQualityAnalyzer.MaximizationParameter.Hidden = true;
|
---|
512 | islandQualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
513 | islandQualityAnalyzer.QualityParameter.Hidden = true;
|
---|
514 | islandQualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
515 | islandQualityAnalyzer.BestKnownQualityParameter.Hidden = true;
|
---|
516 | qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
517 | qualityAnalyzer.MaximizationParameter.Hidden = true;
|
---|
518 | qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
519 | qualityAnalyzer.QualityParameter.Hidden = true;
|
---|
520 | qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
521 | qualityAnalyzer.BestKnownQualityParameter.Hidden = true;
|
---|
522 | }
|
---|
523 | }
|
---|
524 | private void ParameterizeIterationBasedOperators() {
|
---|
525 | if (Problem != null) {
|
---|
526 | foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {
|
---|
527 | op.IterationsParameter.ActualName = "Generations";
|
---|
528 | op.IterationsParameter.Hidden = true;
|
---|
529 | op.MaximumIterationsParameter.ActualName = "MaximumGenerations";
|
---|
530 | op.MaximumIterationsParameter.Hidden = true;
|
---|
531 | }
|
---|
532 | }
|
---|
533 | }
|
---|
534 | private void UpdateCrossovers() {
|
---|
535 | ICrossover oldCrossover = CrossoverParameter.Value;
|
---|
536 | ICrossover defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
|
---|
537 | CrossoverParameter.ValidValues.Clear();
|
---|
538 | foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name)) {
|
---|
539 | ParameterizeStochasticOperatorForIsland(crossover);
|
---|
540 | CrossoverParameter.ValidValues.Add(crossover);
|
---|
541 | }
|
---|
542 | if (oldCrossover != null) {
|
---|
543 | ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
|
---|
544 | if (crossover != null) CrossoverParameter.Value = crossover;
|
---|
545 | else oldCrossover = null;
|
---|
546 | }
|
---|
547 | if (oldCrossover == null && defaultCrossover != null)
|
---|
548 | CrossoverParameter.Value = defaultCrossover;
|
---|
549 | }
|
---|
550 | private void UpdateMutators() {
|
---|
551 | IManipulator oldMutator = MutatorParameter.Value;
|
---|
552 | MutatorParameter.ValidValues.Clear();
|
---|
553 | foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name)) {
|
---|
554 | ParameterizeStochasticOperatorForIsland(mutator);
|
---|
555 | MutatorParameter.ValidValues.Add(mutator);
|
---|
556 | }
|
---|
557 | if (oldMutator != null) {
|
---|
558 | IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
|
---|
559 | if (mutator != null) MutatorParameter.Value = mutator;
|
---|
560 | }
|
---|
561 | }
|
---|
562 | private void UpdateAnalyzers() {
|
---|
563 | IslandAnalyzer.Operators.Clear();
|
---|
564 | Analyzer.Operators.Clear();
|
---|
565 | IslandAnalyzer.Operators.Add(islandQualityAnalyzer, islandQualityAnalyzer.EnabledByDefault);
|
---|
566 | if (Problem != null) {
|
---|
567 | foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
|
---|
568 | foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
|
---|
569 | param.Depth = 2;
|
---|
570 | Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
|
---|
571 | }
|
---|
572 | }
|
---|
573 | Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
|
---|
574 | }
|
---|
575 | private IslandGeneticAlgorithmMainLoop FindMainLoop(IOperator start) {
|
---|
576 | IOperator mainLoop = start;
|
---|
577 | while (mainLoop != null && !(mainLoop is IslandGeneticAlgorithmMainLoop))
|
---|
578 | mainLoop = ((SingleSuccessorOperator)mainLoop).Successor;
|
---|
579 | if (mainLoop == null) return null;
|
---|
580 | else return (IslandGeneticAlgorithmMainLoop)mainLoop;
|
---|
581 | }
|
---|
582 | #endregion
|
---|
583 | }
|
---|
584 | }
|
---|