1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2013 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", "An island genetic algorithm.")]
|
---|
41 | [Creatable("Algorithms")]
|
---|
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 | if (!Parameters.ContainsKey("ReevaluateElites")) {
|
---|
209 | 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 });
|
---|
210 | }
|
---|
211 | Initialize();
|
---|
212 | }
|
---|
213 | private IslandGeneticAlgorithm(IslandGeneticAlgorithm original, Cloner cloner)
|
---|
214 | : base(original, cloner) {
|
---|
215 | islandQualityAnalyzer = cloner.Clone(original.islandQualityAnalyzer);
|
---|
216 | qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
|
---|
217 | Initialize();
|
---|
218 | }
|
---|
219 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
220 | return new IslandGeneticAlgorithm(this, cloner);
|
---|
221 | }
|
---|
222 |
|
---|
223 | public IslandGeneticAlgorithm()
|
---|
224 | : base() {
|
---|
225 | Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
|
---|
226 | Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
227 | Parameters.Add(new ValueParameter<IntValue>("NumberOfIslands", "The number of islands.", new IntValue(5)));
|
---|
228 | Parameters.Add(new ValueParameter<IntValue>("MigrationInterval", "The number of generations that should pass between migration phases.", new IntValue(20)));
|
---|
229 | Parameters.Add(new ValueParameter<PercentValue>("MigrationRate", "The proportion of individuals that should migrate between the islands.", new PercentValue(0.15)));
|
---|
230 | Parameters.Add(new ConstrainedValueParameter<IMigrator>("Migrator", "The migration strategy."));
|
---|
231 | Parameters.Add(new ConstrainedValueParameter<ISelector>("EmigrantsSelector", "Selects the individuals that will be migrated."));
|
---|
232 | Parameters.Add(new ConstrainedValueParameter<IReplacer>("ImmigrationReplacer", "Selects the population from the unification of the original population and the immigrants."));
|
---|
233 | Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "The size of the population of solutions.", new IntValue(100)));
|
---|
234 | Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations that should be processed.", new IntValue(1000)));
|
---|
235 | Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
|
---|
236 | Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
|
---|
237 | Parameters.Add(new ValueParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new PercentValue(0.05)));
|
---|
238 | Parameters.Add(new OptionalConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
|
---|
239 | Parameters.Add(new ValueParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation.", new IntValue(1)));
|
---|
240 | 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 });
|
---|
241 | Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze the islands.", new MultiAnalyzer()));
|
---|
242 | Parameters.Add(new ValueParameter<MultiAnalyzer>("IslandAnalyzer", "The operator used to analyze each island.", new MultiAnalyzer()));
|
---|
243 |
|
---|
244 | RandomCreator randomCreator = new RandomCreator();
|
---|
245 | UniformSubScopesProcessor ussp0 = new UniformSubScopesProcessor();
|
---|
246 | LocalRandomCreator localRandomCreator = new LocalRandomCreator();
|
---|
247 | RandomCreator globalRandomResetter = new RandomCreator();
|
---|
248 | SubScopesCreator populationCreator = new SubScopesCreator();
|
---|
249 | UniformSubScopesProcessor ussp1 = new UniformSubScopesProcessor();
|
---|
250 | SolutionsCreator solutionsCreator = new SolutionsCreator();
|
---|
251 | VariableCreator variableCreator = new VariableCreator();
|
---|
252 | UniformSubScopesProcessor ussp2 = new UniformSubScopesProcessor();
|
---|
253 | SubScopesCounter subScopesCounter = new SubScopesCounter();
|
---|
254 | ResultsCollector resultsCollector = new ResultsCollector();
|
---|
255 | IslandGeneticAlgorithmMainLoop mainLoop = new IslandGeneticAlgorithmMainLoop();
|
---|
256 | OperatorGraph.InitialOperator = randomCreator;
|
---|
257 |
|
---|
258 | randomCreator.RandomParameter.ActualName = "GlobalRandom";
|
---|
259 | randomCreator.SeedParameter.ActualName = SeedParameter.Name;
|
---|
260 | randomCreator.SeedParameter.Value = null;
|
---|
261 | randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
|
---|
262 | randomCreator.SetSeedRandomlyParameter.Value = null;
|
---|
263 | randomCreator.Successor = populationCreator;
|
---|
264 |
|
---|
265 | populationCreator.NumberOfSubScopesParameter.ActualName = NumberOfIslandsParameter.Name;
|
---|
266 | populationCreator.Successor = ussp0;
|
---|
267 |
|
---|
268 | ussp0.Operator = localRandomCreator;
|
---|
269 | ussp0.Successor = globalRandomResetter;
|
---|
270 |
|
---|
271 | // BackwardsCompatibility3.3
|
---|
272 | // the global random is resetted to ensure the same algorithm results
|
---|
273 | #region Backwards compatible code, remove global random resetter with 3.4 and rewire the operator graph
|
---|
274 | globalRandomResetter.RandomParameter.ActualName = "GlobalRandom";
|
---|
275 | globalRandomResetter.SeedParameter.ActualName = SeedParameter.Name;
|
---|
276 | globalRandomResetter.SeedParameter.Value = null;
|
---|
277 | globalRandomResetter.SetSeedRandomlyParameter.Value = new BoolValue(false);
|
---|
278 | globalRandomResetter.Successor = ussp1;
|
---|
279 | #endregion
|
---|
280 |
|
---|
281 | ussp1.Operator = solutionsCreator;
|
---|
282 | ussp1.Successor = variableCreator;
|
---|
283 |
|
---|
284 | solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
|
---|
285 | //don't create solutions in parallel because the hive engine would distribute these tasks
|
---|
286 | solutionsCreator.ParallelParameter.Value = new BoolValue(false);
|
---|
287 | solutionsCreator.Successor = null;
|
---|
288 |
|
---|
289 | variableCreator.Name = "Initialize EvaluatedSolutions";
|
---|
290 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("EvaluatedSolutions", new IntValue()));
|
---|
291 | variableCreator.Successor = ussp2;
|
---|
292 |
|
---|
293 | ussp2.Operator = subScopesCounter;
|
---|
294 | ussp2.Successor = resultsCollector;
|
---|
295 |
|
---|
296 | subScopesCounter.Name = "Count EvaluatedSolutions";
|
---|
297 | subScopesCounter.ValueParameter.ActualName = "EvaluatedSolutions";
|
---|
298 | subScopesCounter.Successor = null;
|
---|
299 |
|
---|
300 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
|
---|
301 | resultsCollector.ResultsParameter.ActualName = "Results";
|
---|
302 | resultsCollector.Successor = mainLoop;
|
---|
303 |
|
---|
304 | mainLoop.EmigrantsSelectorParameter.ActualName = EmigrantsSelectorParameter.Name;
|
---|
305 | mainLoop.ImmigrationReplacerParameter.ActualName = ImmigrationReplacerParameter.Name;
|
---|
306 | mainLoop.MaximumGenerationsParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
307 | mainLoop.MigrationIntervalParameter.ActualName = MigrationIntervalParameter.Name;
|
---|
308 | mainLoop.MigrationRateParameter.ActualName = MigrationRateParameter.Name;
|
---|
309 | mainLoop.MigratorParameter.ActualName = MigratorParameter.Name;
|
---|
310 | mainLoop.NumberOfIslandsParameter.ActualName = NumberOfIslandsParameter.Name;
|
---|
311 | mainLoop.SelectorParameter.ActualName = SelectorParameter.Name;
|
---|
312 | mainLoop.CrossoverParameter.ActualName = CrossoverParameter.Name;
|
---|
313 | mainLoop.ElitesParameter.ActualName = ElitesParameter.Name;
|
---|
314 | mainLoop.ReevaluateElitesParameter.ActualName = ReevaluateElitesParameter.Name;
|
---|
315 | mainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
|
---|
316 | mainLoop.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
|
---|
317 | mainLoop.RandomParameter.ActualName = randomCreator.RandomParameter.ActualName;
|
---|
318 | mainLoop.ResultsParameter.ActualName = "Results";
|
---|
319 | mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
|
---|
320 | mainLoop.IslandAnalyzerParameter.ActualName = IslandAnalyzerParameter.Name;
|
---|
321 | mainLoop.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
|
---|
322 | mainLoop.Successor = null;
|
---|
323 |
|
---|
324 | foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name))
|
---|
325 | SelectorParameter.ValidValues.Add(selector);
|
---|
326 | ISelector proportionalSelector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("ProportionalSelector"));
|
---|
327 | if (proportionalSelector != null) SelectorParameter.Value = proportionalSelector;
|
---|
328 |
|
---|
329 | foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name))
|
---|
330 | EmigrantsSelectorParameter.ValidValues.Add(selector);
|
---|
331 |
|
---|
332 | foreach (IReplacer replacer in ApplicationManager.Manager.GetInstances<IReplacer>().OrderBy(x => x.Name))
|
---|
333 | ImmigrationReplacerParameter.ValidValues.Add(replacer);
|
---|
334 |
|
---|
335 | ParameterizeSelectors();
|
---|
336 |
|
---|
337 | foreach (IMigrator migrator in ApplicationManager.Manager.GetInstances<IMigrator>().OrderBy(x => x.Name))
|
---|
338 | MigratorParameter.ValidValues.Add(migrator);
|
---|
339 |
|
---|
340 | qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
|
---|
341 | islandQualityAnalyzer = new BestAverageWorstQualityAnalyzer();
|
---|
342 | ParameterizeAnalyzers();
|
---|
343 | UpdateAnalyzers();
|
---|
344 |
|
---|
345 | Initialize();
|
---|
346 | }
|
---|
347 |
|
---|
348 | public override void Prepare() {
|
---|
349 | if (Problem != null) base.Prepare();
|
---|
350 | }
|
---|
351 |
|
---|
352 | #region Events
|
---|
353 | protected override void OnProblemChanged() {
|
---|
354 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
355 | ParameterizeStochasticOperatorForIsland(Problem.Evaluator);
|
---|
356 | foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
|
---|
357 | ParameterizeSolutionsCreator();
|
---|
358 | ParameterizeMainLoop();
|
---|
359 | ParameterizeSelectors();
|
---|
360 | ParameterizeAnalyzers();
|
---|
361 | ParameterizeIterationBasedOperators();
|
---|
362 | UpdateCrossovers();
|
---|
363 | UpdateMutators();
|
---|
364 | UpdateAnalyzers();
|
---|
365 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
366 | base.OnProblemChanged();
|
---|
367 | }
|
---|
368 |
|
---|
369 | protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
|
---|
370 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
371 | ParameterizeSolutionsCreator();
|
---|
372 | base.Problem_SolutionCreatorChanged(sender, e);
|
---|
373 | }
|
---|
374 | protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
|
---|
375 | ParameterizeStochasticOperatorForIsland(Problem.Evaluator);
|
---|
376 | ParameterizeSolutionsCreator();
|
---|
377 | ParameterizeMainLoop();
|
---|
378 | ParameterizeSelectors();
|
---|
379 | ParameterizeAnalyzers();
|
---|
380 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
381 | base.Problem_EvaluatorChanged(sender, e);
|
---|
382 | }
|
---|
383 | protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
|
---|
384 | foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
|
---|
385 | ParameterizeIterationBasedOperators();
|
---|
386 | UpdateCrossovers();
|
---|
387 | UpdateMutators();
|
---|
388 | UpdateAnalyzers();
|
---|
389 | base.Problem_OperatorsChanged(sender, e);
|
---|
390 | }
|
---|
391 | private void ElitesParameter_ValueChanged(object sender, EventArgs e) {
|
---|
392 | Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
|
---|
393 | ParameterizeSelectors();
|
---|
394 | }
|
---|
395 | private void Elites_ValueChanged(object sender, EventArgs e) {
|
---|
396 | ParameterizeSelectors();
|
---|
397 | }
|
---|
398 | private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
|
---|
399 | NumberOfIslands.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
|
---|
400 | ParameterizeSelectors();
|
---|
401 | }
|
---|
402 | private void PopulationSize_ValueChanged(object sender, EventArgs e) {
|
---|
403 | ParameterizeSelectors();
|
---|
404 | }
|
---|
405 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
406 | ParameterizeMainLoop();
|
---|
407 | ParameterizeSelectors();
|
---|
408 | ParameterizeAnalyzers();
|
---|
409 | }
|
---|
410 | private void MigrationRateParameter_ValueChanged(object sender, EventArgs e) {
|
---|
411 | MigrationRate.ValueChanged += new EventHandler(MigrationRate_ValueChanged);
|
---|
412 | ParameterizeSelectors();
|
---|
413 | }
|
---|
414 | private void MigrationRate_ValueChanged(object sender, EventArgs e) {
|
---|
415 | ParameterizeSelectors();
|
---|
416 | }
|
---|
417 | #endregion
|
---|
418 |
|
---|
419 | #region Helpers
|
---|
420 | private void Initialize() {
|
---|
421 | PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
|
---|
422 | PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
|
---|
423 | MigrationRateParameter.ValueChanged += new EventHandler(MigrationRateParameter_ValueChanged);
|
---|
424 | MigrationRate.ValueChanged += new EventHandler(MigrationRate_ValueChanged);
|
---|
425 | ElitesParameter.ValueChanged += new EventHandler(ElitesParameter_ValueChanged);
|
---|
426 | Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
|
---|
427 | if (Problem != null) {
|
---|
428 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
429 | }
|
---|
430 | }
|
---|
431 | private void ParameterizeSolutionsCreator() {
|
---|
432 | SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
433 | SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
|
---|
434 | }
|
---|
435 | private void ParameterizeMainLoop() {
|
---|
436 | MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
437 | MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
438 | MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
439 | MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
440 | }
|
---|
441 | private void ParameterizeStochasticOperator(IOperator op) {
|
---|
442 | IStochasticOperator stochasticOp = op as IStochasticOperator;
|
---|
443 | if (stochasticOp != null) {
|
---|
444 | stochasticOp.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
445 | stochasticOp.RandomParameter.Hidden = true;
|
---|
446 | }
|
---|
447 | }
|
---|
448 | private void ParameterizeStochasticOperatorForIsland(IOperator op) {
|
---|
449 | IStochasticOperator stochasticOp = op as IStochasticOperator;
|
---|
450 | if (stochasticOp != null) {
|
---|
451 | stochasticOp.RandomParameter.ActualName = "LocalRandom";
|
---|
452 | stochasticOp.RandomParameter.Hidden = true;
|
---|
453 | }
|
---|
454 | }
|
---|
455 | private void ParameterizeSelectors() {
|
---|
456 | foreach (ISelector selector in SelectorParameter.ValidValues) {
|
---|
457 | selector.CopySelected = new BoolValue(true);
|
---|
458 | selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * (PopulationSize.Value - Elites.Value));
|
---|
459 | selector.NumberOfSelectedSubScopesParameter.Hidden = true;
|
---|
460 | ParameterizeStochasticOperatorForIsland(selector);
|
---|
461 | }
|
---|
462 | foreach (ISelector selector in EmigrantsSelectorParameter.ValidValues) {
|
---|
463 | selector.CopySelected = new BoolValue(true);
|
---|
464 | selector.NumberOfSelectedSubScopesParameter.Value = new IntValue((int)Math.Ceiling(PopulationSize.Value * MigrationRate.Value));
|
---|
465 | selector.NumberOfSelectedSubScopesParameter.Hidden = true;
|
---|
466 | ParameterizeStochasticOperator(selector);
|
---|
467 | }
|
---|
468 | foreach (IReplacer replacer in ImmigrationReplacerParameter.ValidValues) {
|
---|
469 | ParameterizeStochasticOperator(replacer);
|
---|
470 | }
|
---|
471 | if (Problem != null) {
|
---|
472 | foreach (ISingleObjectiveSelector selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
|
---|
473 | selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
474 | selector.MaximizationParameter.Hidden = true;
|
---|
475 | selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
476 | selector.QualityParameter.Hidden = true;
|
---|
477 | }
|
---|
478 | foreach (ISingleObjectiveSelector selector in EmigrantsSelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
|
---|
479 | selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
480 | selector.MaximizationParameter.Hidden = true;
|
---|
481 | selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
482 | selector.QualityParameter.Hidden = true;
|
---|
483 | }
|
---|
484 | foreach (ISingleObjectiveReplacer selector in ImmigrationReplacerParameter.ValidValues.OfType<ISingleObjectiveReplacer>()) {
|
---|
485 | selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
486 | selector.MaximizationParameter.Hidden = true;
|
---|
487 | selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
488 | selector.QualityParameter.Hidden = true;
|
---|
489 | }
|
---|
490 | }
|
---|
491 | }
|
---|
492 | private void ParameterizeAnalyzers() {
|
---|
493 | islandQualityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
494 | islandQualityAnalyzer.ResultsParameter.Hidden = true;
|
---|
495 | islandQualityAnalyzer.QualityParameter.Depth = 1;
|
---|
496 | qualityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
497 | qualityAnalyzer.ResultsParameter.Hidden = true;
|
---|
498 | qualityAnalyzer.QualityParameter.Depth = 2;
|
---|
499 |
|
---|
500 | if (Problem != null) {
|
---|
501 | islandQualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
502 | islandQualityAnalyzer.MaximizationParameter.Hidden = true;
|
---|
503 | islandQualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
504 | islandQualityAnalyzer.QualityParameter.Hidden = true;
|
---|
505 | islandQualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
506 | islandQualityAnalyzer.BestKnownQualityParameter.Hidden = true;
|
---|
507 | qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
508 | qualityAnalyzer.MaximizationParameter.Hidden = true;
|
---|
509 | qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
510 | qualityAnalyzer.QualityParameter.Hidden = true;
|
---|
511 | qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
512 | qualityAnalyzer.BestKnownQualityParameter.Hidden = true;
|
---|
513 | }
|
---|
514 | }
|
---|
515 | private void ParameterizeIterationBasedOperators() {
|
---|
516 | if (Problem != null) {
|
---|
517 | foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {
|
---|
518 | op.IterationsParameter.ActualName = "Generations";
|
---|
519 | op.IterationsParameter.Hidden = true;
|
---|
520 | op.MaximumIterationsParameter.ActualName = "MaximumGenerations";
|
---|
521 | op.MaximumIterationsParameter.Hidden = true;
|
---|
522 | }
|
---|
523 | }
|
---|
524 | }
|
---|
525 | private void UpdateCrossovers() {
|
---|
526 | ICrossover oldCrossover = CrossoverParameter.Value;
|
---|
527 | ICrossover defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
|
---|
528 | CrossoverParameter.ValidValues.Clear();
|
---|
529 | foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name)) {
|
---|
530 | ParameterizeStochasticOperatorForIsland(crossover);
|
---|
531 | CrossoverParameter.ValidValues.Add(crossover);
|
---|
532 | }
|
---|
533 | if (oldCrossover != null) {
|
---|
534 | ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
|
---|
535 | if (crossover != null) CrossoverParameter.Value = crossover;
|
---|
536 | else oldCrossover = null;
|
---|
537 | }
|
---|
538 | if (oldCrossover == null && defaultCrossover != null)
|
---|
539 | CrossoverParameter.Value = defaultCrossover;
|
---|
540 | }
|
---|
541 | private void UpdateMutators() {
|
---|
542 | IManipulator oldMutator = MutatorParameter.Value;
|
---|
543 | MutatorParameter.ValidValues.Clear();
|
---|
544 | foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name)) {
|
---|
545 | ParameterizeStochasticOperatorForIsland(mutator);
|
---|
546 | MutatorParameter.ValidValues.Add(mutator);
|
---|
547 | }
|
---|
548 | if (oldMutator != null) {
|
---|
549 | IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
|
---|
550 | if (mutator != null) MutatorParameter.Value = mutator;
|
---|
551 | }
|
---|
552 | }
|
---|
553 | private void UpdateAnalyzers() {
|
---|
554 | IslandAnalyzer.Operators.Clear();
|
---|
555 | Analyzer.Operators.Clear();
|
---|
556 | IslandAnalyzer.Operators.Add(islandQualityAnalyzer, islandQualityAnalyzer.EnabledByDefault);
|
---|
557 | if (Problem != null) {
|
---|
558 | foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
|
---|
559 | foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
|
---|
560 | param.Depth = 2;
|
---|
561 | Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
|
---|
562 | }
|
---|
563 | }
|
---|
564 | Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
|
---|
565 | }
|
---|
566 | private IslandGeneticAlgorithmMainLoop FindMainLoop(IOperator start) {
|
---|
567 | IOperator mainLoop = start;
|
---|
568 | while (mainLoop != null && !(mainLoop is IslandGeneticAlgorithmMainLoop))
|
---|
569 | mainLoop = ((SingleSuccessorOperator)mainLoop).Successor;
|
---|
570 | if (mainLoop == null) return null;
|
---|
571 | else return (IslandGeneticAlgorithmMainLoop)mainLoop;
|
---|
572 | }
|
---|
573 | #endregion
|
---|
574 | }
|
---|
575 | }
|
---|