1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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 | private ConstrainedValueParameter<IMigrator> MigratorParameter {
|
---|
73 | get { return (ConstrainedValueParameter<IMigrator>)Parameters["Migrator"]; }
|
---|
74 | }
|
---|
75 | private ConstrainedValueParameter<ISelector> EmigrantsSelectorParameter {
|
---|
76 | get { return (ConstrainedValueParameter<ISelector>)Parameters["EmigrantsSelector"]; }
|
---|
77 | }
|
---|
78 | private ConstrainedValueParameter<IReplacer> ImmigrationReplacerParameter {
|
---|
79 | get { return (ConstrainedValueParameter<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 | private ConstrainedValueParameter<ISelector> SelectorParameter {
|
---|
88 | get { return (ConstrainedValueParameter<ISelector>)Parameters["Selector"]; }
|
---|
89 | }
|
---|
90 | private ConstrainedValueParameter<ICrossover> CrossoverParameter {
|
---|
91 | get { return (ConstrainedValueParameter<ICrossover>)Parameters["Crossover"]; }
|
---|
92 | }
|
---|
93 | private ValueParameter<PercentValue> MutationProbabilityParameter {
|
---|
94 | get { return (ValueParameter<PercentValue>)Parameters["MutationProbability"]; }
|
---|
95 | }
|
---|
96 | private OptionalConstrainedValueParameter<IManipulator> MutatorParameter {
|
---|
97 | get { return (OptionalConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; }
|
---|
98 | }
|
---|
99 | private ValueParameter<IntValue> ElitesParameter {
|
---|
100 | get { return (ValueParameter<IntValue>)Parameters["Elites"]; }
|
---|
101 | }
|
---|
102 | private ValueParameter<MultiAnalyzer> AnalyzerParameter {
|
---|
103 | get { return (ValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
|
---|
104 | }
|
---|
105 | private ValueParameter<MultiAnalyzer> IslandAnalyzerParameter {
|
---|
106 | get { return (ValueParameter<MultiAnalyzer>)Parameters["IslandAnalyzer"]; }
|
---|
107 | }
|
---|
108 | #endregion
|
---|
109 |
|
---|
110 | #region Properties
|
---|
111 | public IntValue Seed {
|
---|
112 | get { return SeedParameter.Value; }
|
---|
113 | set { SeedParameter.Value = value; }
|
---|
114 | }
|
---|
115 | public BoolValue SetSeedRandomly {
|
---|
116 | get { return SetSeedRandomlyParameter.Value; }
|
---|
117 | set { SetSeedRandomlyParameter.Value = value; }
|
---|
118 | }
|
---|
119 | public IntValue NumberOfIslands {
|
---|
120 | get { return NumberOfIslandsParameter.Value; }
|
---|
121 | set { NumberOfIslandsParameter.Value = value; }
|
---|
122 | }
|
---|
123 | public IntValue MigrationInterval {
|
---|
124 | get { return MigrationIntervalParameter.Value; }
|
---|
125 | set { MigrationIntervalParameter.Value = value; }
|
---|
126 | }
|
---|
127 | public PercentValue MigrationRate {
|
---|
128 | get { return MigrationRateParameter.Value; }
|
---|
129 | set { MigrationRateParameter.Value = value; }
|
---|
130 | }
|
---|
131 | public IMigrator Migrator {
|
---|
132 | get { return MigratorParameter.Value; }
|
---|
133 | set { MigratorParameter.Value = value; }
|
---|
134 | }
|
---|
135 | public ISelector EmigrantsSelector {
|
---|
136 | get { return EmigrantsSelectorParameter.Value; }
|
---|
137 | set { EmigrantsSelectorParameter.Value = value; }
|
---|
138 | }
|
---|
139 | public IReplacer ImmigrationReplacer {
|
---|
140 | get { return ImmigrationReplacerParameter.Value; }
|
---|
141 | set { ImmigrationReplacerParameter.Value = value; }
|
---|
142 | }
|
---|
143 | public IntValue PopulationSize {
|
---|
144 | get { return PopulationSizeParameter.Value; }
|
---|
145 | set { PopulationSizeParameter.Value = value; }
|
---|
146 | }
|
---|
147 | public IntValue MaximumGenerations {
|
---|
148 | get { return MaximumGenerationsParameter.Value; }
|
---|
149 | set { MaximumGenerationsParameter.Value = value; }
|
---|
150 | }
|
---|
151 | public ISelector Selector {
|
---|
152 | get { return SelectorParameter.Value; }
|
---|
153 | set { SelectorParameter.Value = value; }
|
---|
154 | }
|
---|
155 | public ICrossover Crossover {
|
---|
156 | get { return CrossoverParameter.Value; }
|
---|
157 | set { CrossoverParameter.Value = value; }
|
---|
158 | }
|
---|
159 | public PercentValue MutationProbability {
|
---|
160 | get { return MutationProbabilityParameter.Value; }
|
---|
161 | set { MutationProbabilityParameter.Value = value; }
|
---|
162 | }
|
---|
163 | public IManipulator Mutator {
|
---|
164 | get { return MutatorParameter.Value; }
|
---|
165 | set { MutatorParameter.Value = value; }
|
---|
166 | }
|
---|
167 | public IntValue Elites {
|
---|
168 | get { return ElitesParameter.Value; }
|
---|
169 | set { ElitesParameter.Value = value; }
|
---|
170 | }
|
---|
171 | public MultiAnalyzer Analyzer {
|
---|
172 | get { return AnalyzerParameter.Value; }
|
---|
173 | set { AnalyzerParameter.Value = value; }
|
---|
174 | }
|
---|
175 | public MultiAnalyzer IslandAnalyzer {
|
---|
176 | get { return IslandAnalyzerParameter.Value; }
|
---|
177 | set { IslandAnalyzerParameter.Value = value; }
|
---|
178 | }
|
---|
179 | private RandomCreator RandomCreator {
|
---|
180 | get { return (RandomCreator)OperatorGraph.InitialOperator; }
|
---|
181 | }
|
---|
182 | private UniformSubScopesProcessor IslandProcessor {
|
---|
183 | get { return ((RandomCreator.Successor as SubScopesCreator).Successor as UniformSubScopesProcessor); }
|
---|
184 | }
|
---|
185 | private SolutionsCreator SolutionsCreator {
|
---|
186 | get { return (SolutionsCreator)IslandProcessor.Operator; }
|
---|
187 | }
|
---|
188 | private IslandGeneticAlgorithmMainLoop MainLoop {
|
---|
189 | get { return FindMainLoop(IslandProcessor.Successor); }
|
---|
190 | }
|
---|
191 | [Storable]
|
---|
192 | private BestAverageWorstQualityAnalyzer islandQualityAnalyzer;
|
---|
193 | [Storable]
|
---|
194 | private BestAverageWorstQualityAnalyzer qualityAnalyzer;
|
---|
195 | #endregion
|
---|
196 |
|
---|
197 | [StorableConstructor]
|
---|
198 | private IslandGeneticAlgorithm(bool deserializing) : base(deserializing) { }
|
---|
199 | [StorableHook(HookType.AfterDeserialization)]
|
---|
200 | private void AfterDeserialization() {
|
---|
201 | Initialize();
|
---|
202 | }
|
---|
203 | private IslandGeneticAlgorithm(IslandGeneticAlgorithm original, Cloner cloner)
|
---|
204 | : base(original, cloner) {
|
---|
205 | islandQualityAnalyzer = cloner.Clone(original.islandQualityAnalyzer);
|
---|
206 | qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
|
---|
207 | Initialize();
|
---|
208 | }
|
---|
209 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
210 | return new IslandGeneticAlgorithm(this, cloner);
|
---|
211 | }
|
---|
212 |
|
---|
213 | public IslandGeneticAlgorithm()
|
---|
214 | : base() {
|
---|
215 | Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
|
---|
216 | Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
217 | Parameters.Add(new ValueParameter<IntValue>("NumberOfIslands", "The number of islands.", new IntValue(5)));
|
---|
218 | Parameters.Add(new ValueParameter<IntValue>("MigrationInterval", "The number of generations that should pass between migration phases.", new IntValue(20)));
|
---|
219 | Parameters.Add(new ValueParameter<PercentValue>("MigrationRate", "The proportion of individuals that should migrate between the islands.", new PercentValue(0.15)));
|
---|
220 | Parameters.Add(new ConstrainedValueParameter<IMigrator>("Migrator", "The migration strategy."));
|
---|
221 | Parameters.Add(new ConstrainedValueParameter<ISelector>("EmigrantsSelector", "Selects the individuals that will be migrated."));
|
---|
222 | Parameters.Add(new ConstrainedValueParameter<IReplacer>("ImmigrationReplacer", "Selects the population from the unification of the original population and the immigrants."));
|
---|
223 | Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "The size of the population of solutions.", new IntValue(100)));
|
---|
224 | Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations that should be processed.", new IntValue(1000)));
|
---|
225 | Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
|
---|
226 | Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
|
---|
227 | Parameters.Add(new ValueParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new PercentValue(0.05)));
|
---|
228 | Parameters.Add(new OptionalConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
|
---|
229 | Parameters.Add(new ValueParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation.", new IntValue(1)));
|
---|
230 | Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze the islands.", new MultiAnalyzer()));
|
---|
231 | Parameters.Add(new ValueParameter<MultiAnalyzer>("IslandAnalyzer", "The operator used to analyze each island.", new MultiAnalyzer()));
|
---|
232 |
|
---|
233 | RandomCreator randomCreator = new RandomCreator();
|
---|
234 | SubScopesCreator populationCreator = new SubScopesCreator();
|
---|
235 | UniformSubScopesProcessor ussp1 = new UniformSubScopesProcessor();
|
---|
236 | SolutionsCreator solutionsCreator = new SolutionsCreator();
|
---|
237 | VariableCreator variableCreator = new VariableCreator();
|
---|
238 | UniformSubScopesProcessor ussp2 = new UniformSubScopesProcessor();
|
---|
239 | SubScopesCounter subScopesCounter = new SubScopesCounter();
|
---|
240 | ResultsCollector resultsCollector = new ResultsCollector();
|
---|
241 | IslandGeneticAlgorithmMainLoop mainLoop = new IslandGeneticAlgorithmMainLoop();
|
---|
242 | OperatorGraph.InitialOperator = randomCreator;
|
---|
243 |
|
---|
244 | randomCreator.RandomParameter.ActualName = "Random";
|
---|
245 | randomCreator.SeedParameter.ActualName = SeedParameter.Name;
|
---|
246 | randomCreator.SeedParameter.Value = null;
|
---|
247 | randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
|
---|
248 | randomCreator.SetSeedRandomlyParameter.Value = null;
|
---|
249 | randomCreator.Successor = populationCreator;
|
---|
250 |
|
---|
251 | populationCreator.NumberOfSubScopesParameter.ActualName = NumberOfIslandsParameter.Name;
|
---|
252 | populationCreator.Successor = ussp1;
|
---|
253 |
|
---|
254 | ussp1.Operator = solutionsCreator;
|
---|
255 | ussp1.Successor = variableCreator;
|
---|
256 |
|
---|
257 | solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
|
---|
258 | solutionsCreator.Successor = null;
|
---|
259 |
|
---|
260 | variableCreator.Name = "Initialize EvaluatedSolutions";
|
---|
261 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("EvaluatedSolutions", new IntValue()));
|
---|
262 | variableCreator.Successor = ussp2;
|
---|
263 |
|
---|
264 | ussp2.Operator = subScopesCounter;
|
---|
265 | ussp2.Successor = resultsCollector;
|
---|
266 |
|
---|
267 | subScopesCounter.Name = "Count EvaluatedSolutions";
|
---|
268 | subScopesCounter.ValueParameter.ActualName = "EvaluatedSolutions";
|
---|
269 | subScopesCounter.Successor = null;
|
---|
270 |
|
---|
271 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
|
---|
272 | resultsCollector.ResultsParameter.ActualName = "Results";
|
---|
273 | resultsCollector.Successor = mainLoop;
|
---|
274 |
|
---|
275 | mainLoop.EmigrantsSelectorParameter.ActualName = EmigrantsSelectorParameter.Name;
|
---|
276 | mainLoop.ImmigrationReplacerParameter.ActualName = ImmigrationReplacerParameter.Name;
|
---|
277 | mainLoop.MaximumGenerationsParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
278 | mainLoop.MigrationIntervalParameter.ActualName = MigrationIntervalParameter.Name;
|
---|
279 | mainLoop.MigrationRateParameter.ActualName = MigrationRateParameter.Name;
|
---|
280 | mainLoop.MigratorParameter.ActualName = MigratorParameter.Name;
|
---|
281 | mainLoop.NumberOfIslandsParameter.ActualName = NumberOfIslandsParameter.Name;
|
---|
282 | mainLoop.SelectorParameter.ActualName = SelectorParameter.Name;
|
---|
283 | mainLoop.CrossoverParameter.ActualName = CrossoverParameter.Name;
|
---|
284 | mainLoop.ElitesParameter.ActualName = ElitesParameter.Name;
|
---|
285 | mainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
|
---|
286 | mainLoop.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
|
---|
287 | mainLoop.RandomParameter.ActualName = randomCreator.RandomParameter.ActualName;
|
---|
288 | mainLoop.ResultsParameter.ActualName = "Results";
|
---|
289 | mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
|
---|
290 | mainLoop.IslandAnalyzerParameter.ActualName = IslandAnalyzerParameter.Name;
|
---|
291 | mainLoop.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
|
---|
292 | mainLoop.Successor = null;
|
---|
293 |
|
---|
294 | foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name))
|
---|
295 | SelectorParameter.ValidValues.Add(selector);
|
---|
296 | ISelector proportionalSelector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("ProportionalSelector"));
|
---|
297 | if (proportionalSelector != null) SelectorParameter.Value = proportionalSelector;
|
---|
298 |
|
---|
299 | foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name))
|
---|
300 | EmigrantsSelectorParameter.ValidValues.Add(selector);
|
---|
301 |
|
---|
302 | foreach (IReplacer replacer in ApplicationManager.Manager.GetInstances<IReplacer>().OrderBy(x => x.Name))
|
---|
303 | ImmigrationReplacerParameter.ValidValues.Add(replacer);
|
---|
304 |
|
---|
305 | ParameterizeSelectors();
|
---|
306 |
|
---|
307 | foreach (IMigrator migrator in ApplicationManager.Manager.GetInstances<IMigrator>().OrderBy(x => x.Name))
|
---|
308 | MigratorParameter.ValidValues.Add(migrator);
|
---|
309 |
|
---|
310 | qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
|
---|
311 | islandQualityAnalyzer = new BestAverageWorstQualityAnalyzer();
|
---|
312 | ParameterizeAnalyzers();
|
---|
313 | UpdateAnalyzers();
|
---|
314 |
|
---|
315 | Initialize();
|
---|
316 | }
|
---|
317 |
|
---|
318 | public override void Prepare() {
|
---|
319 | if (Problem != null) base.Prepare();
|
---|
320 | }
|
---|
321 |
|
---|
322 | #region Events
|
---|
323 | protected override void OnProblemChanged() {
|
---|
324 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
325 | ParameterizeStochasticOperator(Problem.Evaluator);
|
---|
326 | foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
|
---|
327 | ParameterizeSolutionsCreator();
|
---|
328 | ParameterizeMainLoop();
|
---|
329 | ParameterizeSelectors();
|
---|
330 | ParameterizeAnalyzers();
|
---|
331 | ParameterizeIterationBasedOperators();
|
---|
332 | UpdateCrossovers();
|
---|
333 | UpdateMutators();
|
---|
334 | UpdateAnalyzers();
|
---|
335 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
336 | base.OnProblemChanged();
|
---|
337 | }
|
---|
338 |
|
---|
339 | protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
|
---|
340 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
341 | ParameterizeSolutionsCreator();
|
---|
342 | base.Problem_SolutionCreatorChanged(sender, e);
|
---|
343 | }
|
---|
344 | protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
|
---|
345 | ParameterizeStochasticOperator(Problem.Evaluator);
|
---|
346 | ParameterizeSolutionsCreator();
|
---|
347 | ParameterizeMainLoop();
|
---|
348 | ParameterizeSelectors();
|
---|
349 | ParameterizeAnalyzers();
|
---|
350 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
351 | base.Problem_EvaluatorChanged(sender, e);
|
---|
352 | }
|
---|
353 | protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
|
---|
354 | foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
|
---|
355 | ParameterizeIterationBasedOperators();
|
---|
356 | UpdateCrossovers();
|
---|
357 | UpdateMutators();
|
---|
358 | UpdateAnalyzers();
|
---|
359 | base.Problem_OperatorsChanged(sender, e);
|
---|
360 | }
|
---|
361 | private void ElitesParameter_ValueChanged(object sender, EventArgs e) {
|
---|
362 | Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
|
---|
363 | ParameterizeSelectors();
|
---|
364 | }
|
---|
365 | private void Elites_ValueChanged(object sender, EventArgs e) {
|
---|
366 | ParameterizeSelectors();
|
---|
367 | }
|
---|
368 | private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
|
---|
369 | NumberOfIslands.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
|
---|
370 | ParameterizeSelectors();
|
---|
371 | }
|
---|
372 | private void PopulationSize_ValueChanged(object sender, EventArgs e) {
|
---|
373 | ParameterizeSelectors();
|
---|
374 | }
|
---|
375 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
376 | ParameterizeMainLoop();
|
---|
377 | ParameterizeSelectors();
|
---|
378 | ParameterizeAnalyzers();
|
---|
379 | }
|
---|
380 | private void MigrationRateParameter_ValueChanged(object sender, EventArgs e) {
|
---|
381 | MigrationRate.ValueChanged += new EventHandler(MigrationRate_ValueChanged);
|
---|
382 | ParameterizeSelectors();
|
---|
383 | }
|
---|
384 | private void MigrationRate_ValueChanged(object sender, EventArgs e) {
|
---|
385 | ParameterizeSelectors();
|
---|
386 | }
|
---|
387 | #endregion
|
---|
388 |
|
---|
389 | #region Helpers
|
---|
390 | private void Initialize() {
|
---|
391 | PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
|
---|
392 | PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
|
---|
393 | MigrationRateParameter.ValueChanged += new EventHandler(MigrationRateParameter_ValueChanged);
|
---|
394 | MigrationRate.ValueChanged += new EventHandler(MigrationRate_ValueChanged);
|
---|
395 | ElitesParameter.ValueChanged += new EventHandler(ElitesParameter_ValueChanged);
|
---|
396 | Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
|
---|
397 | if (Problem != null) {
|
---|
398 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
399 | }
|
---|
400 | }
|
---|
401 | private void ParameterizeSolutionsCreator() {
|
---|
402 | SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
403 | SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
|
---|
404 | }
|
---|
405 | private void ParameterizeMainLoop() {
|
---|
406 | MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
407 | MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
408 | MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
409 | MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
410 | }
|
---|
411 | private void ParameterizeStochasticOperator(IOperator op) {
|
---|
412 | if (op is IStochasticOperator)
|
---|
413 | ((IStochasticOperator)op).RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
414 | }
|
---|
415 | private void ParameterizeSelectors() {
|
---|
416 | foreach (ISelector selector in SelectorParameter.ValidValues) {
|
---|
417 | selector.CopySelected = new BoolValue(true);
|
---|
418 | selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * (PopulationSize.Value - Elites.Value));
|
---|
419 | ParameterizeStochasticOperator(selector);
|
---|
420 | }
|
---|
421 | foreach (ISelector selector in EmigrantsSelectorParameter.ValidValues) {
|
---|
422 | selector.CopySelected = new BoolValue(true);
|
---|
423 | selector.NumberOfSelectedSubScopesParameter.Value = new IntValue((int)Math.Ceiling(PopulationSize.Value * MigrationRate.Value));
|
---|
424 | ParameterizeStochasticOperator(selector);
|
---|
425 | }
|
---|
426 | foreach (IReplacer replacer in ImmigrationReplacerParameter.ValidValues) {
|
---|
427 | ParameterizeStochasticOperator(replacer);
|
---|
428 | }
|
---|
429 | if (Problem != null) {
|
---|
430 | foreach (ISingleObjectiveSelector selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
|
---|
431 | selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
432 | selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
433 | }
|
---|
434 | foreach (ISingleObjectiveSelector selector in EmigrantsSelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
|
---|
435 | selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
436 | selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
437 | }
|
---|
438 | foreach (ISingleObjectiveReplacer selector in ImmigrationReplacerParameter.ValidValues.OfType<ISingleObjectiveReplacer>()) {
|
---|
439 | selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
440 | selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
441 | }
|
---|
442 | }
|
---|
443 | }
|
---|
444 | private void ParameterizeAnalyzers() {
|
---|
445 | islandQualityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
446 | islandQualityAnalyzer.QualityParameter.Depth = 1;
|
---|
447 | qualityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
448 | qualityAnalyzer.QualityParameter.Depth = 2;
|
---|
449 |
|
---|
450 | if (Problem != null) {
|
---|
451 | islandQualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
452 | islandQualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
453 | islandQualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
454 | qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
455 | qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
456 | qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
457 | }
|
---|
458 | }
|
---|
459 | private void ParameterizeIterationBasedOperators() {
|
---|
460 | if (Problem != null) {
|
---|
461 | foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {
|
---|
462 | op.IterationsParameter.ActualName = "Generations";
|
---|
463 | op.MaximumIterationsParameter.ActualName = "MaximumGenerations";
|
---|
464 | }
|
---|
465 | }
|
---|
466 | }
|
---|
467 | private void UpdateCrossovers() {
|
---|
468 | ICrossover oldCrossover = CrossoverParameter.Value;
|
---|
469 | CrossoverParameter.ValidValues.Clear();
|
---|
470 | foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name))
|
---|
471 | CrossoverParameter.ValidValues.Add(crossover);
|
---|
472 | if (oldCrossover != null) {
|
---|
473 | ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
|
---|
474 | if (crossover != null) CrossoverParameter.Value = crossover;
|
---|
475 | }
|
---|
476 | }
|
---|
477 | private void UpdateMutators() {
|
---|
478 | IManipulator oldMutator = MutatorParameter.Value;
|
---|
479 | MutatorParameter.ValidValues.Clear();
|
---|
480 | foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
|
---|
481 | MutatorParameter.ValidValues.Add(mutator);
|
---|
482 | if (oldMutator != null) {
|
---|
483 | IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
|
---|
484 | if (mutator != null) MutatorParameter.Value = mutator;
|
---|
485 | }
|
---|
486 | }
|
---|
487 | private void UpdateAnalyzers() {
|
---|
488 | IslandAnalyzer.Operators.Clear();
|
---|
489 | Analyzer.Operators.Clear();
|
---|
490 | IslandAnalyzer.Operators.Add(islandQualityAnalyzer);
|
---|
491 | if (Problem != null) {
|
---|
492 | foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
|
---|
493 | foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
|
---|
494 | param.Depth = 2;
|
---|
495 | Analyzer.Operators.Add(analyzer);
|
---|
496 | }
|
---|
497 | }
|
---|
498 | Analyzer.Operators.Add(qualityAnalyzer);
|
---|
499 | }
|
---|
500 | private IslandGeneticAlgorithmMainLoop FindMainLoop(IOperator start) {
|
---|
501 | IOperator mainLoop = start;
|
---|
502 | while (mainLoop != null && !(mainLoop is IslandGeneticAlgorithmMainLoop))
|
---|
503 | mainLoop = ((SingleSuccessorOperator)mainLoop).Successor;
|
---|
504 | if (mainLoop == null) return null;
|
---|
505 | else return (IslandGeneticAlgorithmMainLoop)mainLoop;
|
---|
506 | }
|
---|
507 | #endregion
|
---|
508 | }
|
---|
509 | }
|
---|