1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2014 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 | // 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 | MigratorParameter.ValidValues.Add(migrator);
|
---|
343 |
|
---|
344 | qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
|
---|
345 | islandQualityAnalyzer = new BestAverageWorstQualityAnalyzer();
|
---|
346 | ParameterizeAnalyzers();
|
---|
347 | UpdateAnalyzers();
|
---|
348 |
|
---|
349 | Initialize();
|
---|
350 | }
|
---|
351 |
|
---|
352 | public override void Prepare() {
|
---|
353 | if (Problem != null) base.Prepare();
|
---|
354 | }
|
---|
355 |
|
---|
356 | #region Events
|
---|
357 | protected override void OnProblemChanged() {
|
---|
358 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
359 | ParameterizeStochasticOperatorForIsland(Problem.Evaluator);
|
---|
360 | foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
|
---|
361 | ParameterizeSolutionsCreator();
|
---|
362 | ParameterizeMainLoop();
|
---|
363 | ParameterizeSelectors();
|
---|
364 | ParameterizeAnalyzers();
|
---|
365 | ParameterizeIterationBasedOperators();
|
---|
366 | UpdateCrossovers();
|
---|
367 | UpdateMutators();
|
---|
368 | UpdateAnalyzers();
|
---|
369 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
370 | base.OnProblemChanged();
|
---|
371 | }
|
---|
372 |
|
---|
373 | protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
|
---|
374 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
375 | ParameterizeSolutionsCreator();
|
---|
376 | base.Problem_SolutionCreatorChanged(sender, e);
|
---|
377 | }
|
---|
378 | protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
|
---|
379 | ParameterizeStochasticOperatorForIsland(Problem.Evaluator);
|
---|
380 | ParameterizeSolutionsCreator();
|
---|
381 | ParameterizeMainLoop();
|
---|
382 | ParameterizeSelectors();
|
---|
383 | ParameterizeAnalyzers();
|
---|
384 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
385 | base.Problem_EvaluatorChanged(sender, e);
|
---|
386 | }
|
---|
387 | protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
|
---|
388 | foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
|
---|
389 | ParameterizeIterationBasedOperators();
|
---|
390 | UpdateCrossovers();
|
---|
391 | UpdateMutators();
|
---|
392 | UpdateAnalyzers();
|
---|
393 | base.Problem_OperatorsChanged(sender, e);
|
---|
394 | }
|
---|
395 | private void ElitesParameter_ValueChanged(object sender, EventArgs e) {
|
---|
396 | Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
|
---|
397 | ParameterizeSelectors();
|
---|
398 | }
|
---|
399 | private void Elites_ValueChanged(object sender, EventArgs e) {
|
---|
400 | ParameterizeSelectors();
|
---|
401 | }
|
---|
402 | private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
|
---|
403 | NumberOfIslands.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
|
---|
404 | ParameterizeSelectors();
|
---|
405 | }
|
---|
406 | private void PopulationSize_ValueChanged(object sender, EventArgs e) {
|
---|
407 | ParameterizeSelectors();
|
---|
408 | }
|
---|
409 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
410 | ParameterizeMainLoop();
|
---|
411 | ParameterizeSelectors();
|
---|
412 | ParameterizeAnalyzers();
|
---|
413 | }
|
---|
414 | private void MigrationRateParameter_ValueChanged(object sender, EventArgs e) {
|
---|
415 | MigrationRate.ValueChanged += new EventHandler(MigrationRate_ValueChanged);
|
---|
416 | ParameterizeSelectors();
|
---|
417 | }
|
---|
418 | private void MigrationRate_ValueChanged(object sender, EventArgs e) {
|
---|
419 | ParameterizeSelectors();
|
---|
420 | }
|
---|
421 | #endregion
|
---|
422 |
|
---|
423 | #region Helpers
|
---|
424 | private void Initialize() {
|
---|
425 | PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
|
---|
426 | PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
|
---|
427 | MigrationRateParameter.ValueChanged += new EventHandler(MigrationRateParameter_ValueChanged);
|
---|
428 | MigrationRate.ValueChanged += new EventHandler(MigrationRate_ValueChanged);
|
---|
429 | ElitesParameter.ValueChanged += new EventHandler(ElitesParameter_ValueChanged);
|
---|
430 | Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
|
---|
431 | if (Problem != null) {
|
---|
432 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
433 | }
|
---|
434 | }
|
---|
435 | private void ParameterizeSolutionsCreator() {
|
---|
436 | SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
437 | SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
|
---|
438 | }
|
---|
439 | private void ParameterizeMainLoop() {
|
---|
440 | MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
441 | MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
442 | MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
443 | MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
444 | }
|
---|
445 | private void ParameterizeStochasticOperator(IOperator op) {
|
---|
446 | IStochasticOperator stochasticOp = op as IStochasticOperator;
|
---|
447 | if (stochasticOp != null) {
|
---|
448 | stochasticOp.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
449 | stochasticOp.RandomParameter.Hidden = true;
|
---|
450 | }
|
---|
451 | }
|
---|
452 | private void ParameterizeStochasticOperatorForIsland(IOperator op) {
|
---|
453 | IStochasticOperator stochasticOp = op as IStochasticOperator;
|
---|
454 | if (stochasticOp != null) {
|
---|
455 | stochasticOp.RandomParameter.ActualName = "LocalRandom";
|
---|
456 | stochasticOp.RandomParameter.Hidden = true;
|
---|
457 | }
|
---|
458 | }
|
---|
459 | private void ParameterizeSelectors() {
|
---|
460 | foreach (ISelector selector in SelectorParameter.ValidValues) {
|
---|
461 | selector.CopySelected = new BoolValue(true);
|
---|
462 | selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * (PopulationSize.Value - Elites.Value));
|
---|
463 | selector.NumberOfSelectedSubScopesParameter.Hidden = true;
|
---|
464 | ParameterizeStochasticOperatorForIsland(selector);
|
---|
465 | }
|
---|
466 | foreach (ISelector selector in EmigrantsSelectorParameter.ValidValues) {
|
---|
467 | selector.CopySelected = new BoolValue(true);
|
---|
468 | selector.NumberOfSelectedSubScopesParameter.Value = new IntValue((int)Math.Ceiling(PopulationSize.Value * MigrationRate.Value));
|
---|
469 | selector.NumberOfSelectedSubScopesParameter.Hidden = true;
|
---|
470 | ParameterizeStochasticOperator(selector);
|
---|
471 | }
|
---|
472 | foreach (IReplacer replacer in ImmigrationReplacerParameter.ValidValues) {
|
---|
473 | ParameterizeStochasticOperator(replacer);
|
---|
474 | }
|
---|
475 | if (Problem != null) {
|
---|
476 | foreach (ISingleObjectiveSelector selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
|
---|
477 | selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
478 | selector.MaximizationParameter.Hidden = true;
|
---|
479 | selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
480 | selector.QualityParameter.Hidden = true;
|
---|
481 | }
|
---|
482 | foreach (ISingleObjectiveSelector selector in EmigrantsSelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
|
---|
483 | selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
484 | selector.MaximizationParameter.Hidden = true;
|
---|
485 | selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
486 | selector.QualityParameter.Hidden = true;
|
---|
487 | }
|
---|
488 | foreach (ISingleObjectiveReplacer selector in ImmigrationReplacerParameter.ValidValues.OfType<ISingleObjectiveReplacer>()) {
|
---|
489 | selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
490 | selector.MaximizationParameter.Hidden = true;
|
---|
491 | selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
492 | selector.QualityParameter.Hidden = true;
|
---|
493 | }
|
---|
494 | }
|
---|
495 | }
|
---|
496 | private void ParameterizeAnalyzers() {
|
---|
497 | islandQualityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
498 | islandQualityAnalyzer.ResultsParameter.Hidden = true;
|
---|
499 | islandQualityAnalyzer.QualityParameter.Depth = 1;
|
---|
500 | qualityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
501 | qualityAnalyzer.ResultsParameter.Hidden = true;
|
---|
502 | qualityAnalyzer.QualityParameter.Depth = 2;
|
---|
503 |
|
---|
504 | if (Problem != null) {
|
---|
505 | islandQualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
506 | islandQualityAnalyzer.MaximizationParameter.Hidden = true;
|
---|
507 | islandQualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
508 | islandQualityAnalyzer.QualityParameter.Hidden = true;
|
---|
509 | islandQualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
510 | islandQualityAnalyzer.BestKnownQualityParameter.Hidden = true;
|
---|
511 | qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
512 | qualityAnalyzer.MaximizationParameter.Hidden = true;
|
---|
513 | qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
514 | qualityAnalyzer.QualityParameter.Hidden = true;
|
---|
515 | qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
516 | qualityAnalyzer.BestKnownQualityParameter.Hidden = true;
|
---|
517 | }
|
---|
518 | }
|
---|
519 | private void ParameterizeIterationBasedOperators() {
|
---|
520 | if (Problem != null) {
|
---|
521 | foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {
|
---|
522 | op.IterationsParameter.ActualName = "Generations";
|
---|
523 | op.IterationsParameter.Hidden = true;
|
---|
524 | op.MaximumIterationsParameter.ActualName = "MaximumGenerations";
|
---|
525 | op.MaximumIterationsParameter.Hidden = true;
|
---|
526 | }
|
---|
527 | }
|
---|
528 | }
|
---|
529 | private void UpdateCrossovers() {
|
---|
530 | ICrossover oldCrossover = CrossoverParameter.Value;
|
---|
531 | ICrossover defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
|
---|
532 | CrossoverParameter.ValidValues.Clear();
|
---|
533 | foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name)) {
|
---|
534 | ParameterizeStochasticOperatorForIsland(crossover);
|
---|
535 | CrossoverParameter.ValidValues.Add(crossover);
|
---|
536 | }
|
---|
537 | if (oldCrossover != null) {
|
---|
538 | ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
|
---|
539 | if (crossover != null) CrossoverParameter.Value = crossover;
|
---|
540 | else oldCrossover = null;
|
---|
541 | }
|
---|
542 | if (oldCrossover == null && defaultCrossover != null)
|
---|
543 | CrossoverParameter.Value = defaultCrossover;
|
---|
544 | }
|
---|
545 | private void UpdateMutators() {
|
---|
546 | IManipulator oldMutator = MutatorParameter.Value;
|
---|
547 | MutatorParameter.ValidValues.Clear();
|
---|
548 | foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name)) {
|
---|
549 | ParameterizeStochasticOperatorForIsland(mutator);
|
---|
550 | MutatorParameter.ValidValues.Add(mutator);
|
---|
551 | }
|
---|
552 | if (oldMutator != null) {
|
---|
553 | IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
|
---|
554 | if (mutator != null) MutatorParameter.Value = mutator;
|
---|
555 | }
|
---|
556 | }
|
---|
557 | private void UpdateAnalyzers() {
|
---|
558 | IslandAnalyzer.Operators.Clear();
|
---|
559 | Analyzer.Operators.Clear();
|
---|
560 | IslandAnalyzer.Operators.Add(islandQualityAnalyzer, islandQualityAnalyzer.EnabledByDefault);
|
---|
561 | if (Problem != null) {
|
---|
562 | foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
|
---|
563 | foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
|
---|
564 | param.Depth = 2;
|
---|
565 | Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
|
---|
566 | }
|
---|
567 | }
|
---|
568 | Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
|
---|
569 | }
|
---|
570 | private IslandGeneticAlgorithmMainLoop FindMainLoop(IOperator start) {
|
---|
571 | IOperator mainLoop = start;
|
---|
572 | while (mainLoop != null && !(mainLoop is IslandGeneticAlgorithmMainLoop))
|
---|
573 | mainLoop = ((SingleSuccessorOperator)mainLoop).Successor;
|
---|
574 | if (mainLoop == null) return null;
|
---|
575 | else return (IslandGeneticAlgorithmMainLoop)mainLoop;
|
---|
576 | }
|
---|
577 | #endregion
|
---|
578 | }
|
---|
579 | }
|
---|