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