Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Algorithms.GeneticAlgorithm/3.3/IslandGeneticAlgorithm.cs @ 13419

Last change on this file since 13419 was 9363, checked in by spimming, 12 years ago

#1888:

  • Merged revisions from trunk
File size: 28.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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
22using System;
23using System.Linq;
24using HeuristicLab.Analysis;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Optimization.Operators;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.PluginInfrastructure;
34using HeuristicLab.Random;
35
36namespace 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 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 OperatorGraph.Iterate().OfType<UniformSubScopesProcessor>().First(x => x.Operator is SolutionsCreator); }
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      UniformSubScopesProcessor ussp0 = new UniformSubScopesProcessor();
235      LocalRandomCreator localRandomCreator = new LocalRandomCreator();
236      RandomCreator globalRandomResetter = new RandomCreator();
237      SubScopesCreator populationCreator = new SubScopesCreator();
238      UniformSubScopesProcessor ussp1 = new UniformSubScopesProcessor();
239      SolutionsCreator solutionsCreator = new SolutionsCreator();
240      VariableCreator variableCreator = new VariableCreator();
241      UniformSubScopesProcessor ussp2 = new UniformSubScopesProcessor();
242      SubScopesCounter subScopesCounter = new SubScopesCounter();
243      ResultsCollector resultsCollector = new ResultsCollector();
244      IslandGeneticAlgorithmMainLoop mainLoop = new IslandGeneticAlgorithmMainLoop();
245      OperatorGraph.InitialOperator = randomCreator;
246
247      randomCreator.RandomParameter.ActualName = "GlobalRandom";
248      randomCreator.SeedParameter.ActualName = SeedParameter.Name;
249      randomCreator.SeedParameter.Value = null;
250      randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
251      randomCreator.SetSeedRandomlyParameter.Value = null;
252      randomCreator.Successor = populationCreator;
253
254      populationCreator.NumberOfSubScopesParameter.ActualName = NumberOfIslandsParameter.Name;
255      populationCreator.Successor = ussp0;
256
257      ussp0.Operator = localRandomCreator;
258      ussp0.Successor = globalRandomResetter;
259
260      // BackwardsCompatibility3.3
261      // the global random is resetted to ensure the same algorithm results
262      #region Backwards compatible code, remove global random resetter with 3.4 and rewire the operator graph
263      globalRandomResetter.RandomParameter.ActualName = "GlobalRandom";
264      globalRandomResetter.SeedParameter.ActualName = SeedParameter.Name;
265      globalRandomResetter.SeedParameter.Value = null;
266      globalRandomResetter.SetSeedRandomlyParameter.Value = new BoolValue(false);
267      globalRandomResetter.Successor = ussp1;
268      #endregion
269
270      ussp1.Operator = solutionsCreator;
271      ussp1.Successor = variableCreator;
272
273      solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
274      //don't create solutions in parallel because the hive engine would distribute these tasks
275      solutionsCreator.ParallelParameter.Value = new BoolValue(false);
276      solutionsCreator.Successor = null;
277
278      variableCreator.Name = "Initialize EvaluatedSolutions";
279      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("EvaluatedSolutions", new IntValue()));
280      variableCreator.Successor = ussp2;
281
282      ussp2.Operator = subScopesCounter;
283      ussp2.Successor = resultsCollector;
284
285      subScopesCounter.Name = "Count EvaluatedSolutions";
286      subScopesCounter.ValueParameter.ActualName = "EvaluatedSolutions";
287      subScopesCounter.Successor = null;
288
289      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
290      resultsCollector.ResultsParameter.ActualName = "Results";
291      resultsCollector.Successor = mainLoop;
292
293      mainLoop.EmigrantsSelectorParameter.ActualName = EmigrantsSelectorParameter.Name;
294      mainLoop.ImmigrationReplacerParameter.ActualName = ImmigrationReplacerParameter.Name;
295      mainLoop.MaximumGenerationsParameter.ActualName = MaximumGenerationsParameter.Name;
296      mainLoop.MigrationIntervalParameter.ActualName = MigrationIntervalParameter.Name;
297      mainLoop.MigrationRateParameter.ActualName = MigrationRateParameter.Name;
298      mainLoop.MigratorParameter.ActualName = MigratorParameter.Name;
299      mainLoop.NumberOfIslandsParameter.ActualName = NumberOfIslandsParameter.Name;
300      mainLoop.SelectorParameter.ActualName = SelectorParameter.Name;
301      mainLoop.CrossoverParameter.ActualName = CrossoverParameter.Name;
302      mainLoop.ElitesParameter.ActualName = ElitesParameter.Name;
303      mainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
304      mainLoop.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
305      mainLoop.RandomParameter.ActualName = randomCreator.RandomParameter.ActualName;
306      mainLoop.ResultsParameter.ActualName = "Results";
307      mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
308      mainLoop.IslandAnalyzerParameter.ActualName = IslandAnalyzerParameter.Name;
309      mainLoop.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
310      mainLoop.Successor = null;
311
312      foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name))
313        SelectorParameter.ValidValues.Add(selector);
314      ISelector proportionalSelector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("ProportionalSelector"));
315      if (proportionalSelector != null) SelectorParameter.Value = proportionalSelector;
316
317      foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name))
318        EmigrantsSelectorParameter.ValidValues.Add(selector);
319
320      foreach (IReplacer replacer in ApplicationManager.Manager.GetInstances<IReplacer>().OrderBy(x => x.Name))
321        ImmigrationReplacerParameter.ValidValues.Add(replacer);
322
323      ParameterizeSelectors();
324
325      foreach (IMigrator migrator in ApplicationManager.Manager.GetInstances<IMigrator>().OrderBy(x => x.Name))
326        MigratorParameter.ValidValues.Add(migrator);
327
328      qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
329      islandQualityAnalyzer = new BestAverageWorstQualityAnalyzer();
330      ParameterizeAnalyzers();
331      UpdateAnalyzers();
332
333      Initialize();
334    }
335
336    public override void Prepare() {
337      if (Problem != null) base.Prepare();
338    }
339
340    #region Events
341    protected override void OnProblemChanged() {
342      ParameterizeStochasticOperator(Problem.SolutionCreator);
343      ParameterizeStochasticOperatorForIsland(Problem.Evaluator);
344      foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
345      ParameterizeSolutionsCreator();
346      ParameterizeMainLoop();
347      ParameterizeSelectors();
348      ParameterizeAnalyzers();
349      ParameterizeIterationBasedOperators();
350      UpdateCrossovers();
351      UpdateMutators();
352      UpdateAnalyzers();
353      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
354      base.OnProblemChanged();
355    }
356
357    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
358      ParameterizeStochasticOperator(Problem.SolutionCreator);
359      ParameterizeSolutionsCreator();
360      base.Problem_SolutionCreatorChanged(sender, e);
361    }
362    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
363      ParameterizeStochasticOperatorForIsland(Problem.Evaluator);
364      ParameterizeSolutionsCreator();
365      ParameterizeMainLoop();
366      ParameterizeSelectors();
367      ParameterizeAnalyzers();
368      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
369      base.Problem_EvaluatorChanged(sender, e);
370    }
371    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
372      foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
373      ParameterizeIterationBasedOperators();
374      UpdateCrossovers();
375      UpdateMutators();
376      UpdateAnalyzers();
377      base.Problem_OperatorsChanged(sender, e);
378    }
379    private void ElitesParameter_ValueChanged(object sender, EventArgs e) {
380      Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
381      ParameterizeSelectors();
382    }
383    private void Elites_ValueChanged(object sender, EventArgs e) {
384      ParameterizeSelectors();
385    }
386    private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
387      NumberOfIslands.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
388      ParameterizeSelectors();
389    }
390    private void PopulationSize_ValueChanged(object sender, EventArgs e) {
391      ParameterizeSelectors();
392    }
393    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
394      ParameterizeMainLoop();
395      ParameterizeSelectors();
396      ParameterizeAnalyzers();
397    }
398    private void MigrationRateParameter_ValueChanged(object sender, EventArgs e) {
399      MigrationRate.ValueChanged += new EventHandler(MigrationRate_ValueChanged);
400      ParameterizeSelectors();
401    }
402    private void MigrationRate_ValueChanged(object sender, EventArgs e) {
403      ParameterizeSelectors();
404    }
405    #endregion
406
407    #region Helpers
408    private void Initialize() {
409      PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
410      PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
411      MigrationRateParameter.ValueChanged += new EventHandler(MigrationRateParameter_ValueChanged);
412      MigrationRate.ValueChanged += new EventHandler(MigrationRate_ValueChanged);
413      ElitesParameter.ValueChanged += new EventHandler(ElitesParameter_ValueChanged);
414      Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
415      if (Problem != null) {
416        Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
417      }
418    }
419    private void ParameterizeSolutionsCreator() {
420      SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
421      SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
422    }
423    private void ParameterizeMainLoop() {
424      MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
425      MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
426      MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
427      MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
428    }
429    private void ParameterizeStochasticOperator(IOperator op) {
430      IStochasticOperator stochasticOp = op as IStochasticOperator;
431      if (stochasticOp != null) {
432        stochasticOp.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
433        stochasticOp.RandomParameter.Hidden = true;
434      }
435    }
436    private void ParameterizeStochasticOperatorForIsland(IOperator op) {
437      IStochasticOperator stochasticOp = op as IStochasticOperator;
438      if (stochasticOp != null) {
439        stochasticOp.RandomParameter.ActualName = "LocalRandom";
440        stochasticOp.RandomParameter.Hidden = true;
441      }
442    }
443    private void ParameterizeSelectors() {
444      foreach (ISelector selector in SelectorParameter.ValidValues) {
445        selector.CopySelected = new BoolValue(true);
446        selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * (PopulationSize.Value - Elites.Value));
447        selector.NumberOfSelectedSubScopesParameter.Hidden = true;
448        ParameterizeStochasticOperatorForIsland(selector);
449      }
450      foreach (ISelector selector in EmigrantsSelectorParameter.ValidValues) {
451        selector.CopySelected = new BoolValue(true);
452        selector.NumberOfSelectedSubScopesParameter.Value = new IntValue((int)Math.Ceiling(PopulationSize.Value * MigrationRate.Value));
453        selector.NumberOfSelectedSubScopesParameter.Hidden = true;
454        ParameterizeStochasticOperator(selector);
455      }
456      foreach (IReplacer replacer in ImmigrationReplacerParameter.ValidValues) {
457        ParameterizeStochasticOperator(replacer);
458      }
459      if (Problem != null) {
460        foreach (ISingleObjectiveSelector selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
461          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
462          selector.MaximizationParameter.Hidden = true;
463          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
464          selector.QualityParameter.Hidden = true;
465        }
466        foreach (ISingleObjectiveSelector selector in EmigrantsSelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
467          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
468          selector.MaximizationParameter.Hidden = true;
469          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
470          selector.QualityParameter.Hidden = true;
471        }
472        foreach (ISingleObjectiveReplacer selector in ImmigrationReplacerParameter.ValidValues.OfType<ISingleObjectiveReplacer>()) {
473          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
474          selector.MaximizationParameter.Hidden = true;
475          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
476          selector.QualityParameter.Hidden = true;
477        }
478      }
479    }
480    private void ParameterizeAnalyzers() {
481      islandQualityAnalyzer.ResultsParameter.ActualName = "Results";
482      islandQualityAnalyzer.ResultsParameter.Hidden = true;
483      islandQualityAnalyzer.QualityParameter.Depth = 1;
484      qualityAnalyzer.ResultsParameter.ActualName = "Results";
485      qualityAnalyzer.ResultsParameter.Hidden = true;
486      qualityAnalyzer.QualityParameter.Depth = 2;
487
488      if (Problem != null) {
489        islandQualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
490        islandQualityAnalyzer.MaximizationParameter.Hidden = true;
491        islandQualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
492        islandQualityAnalyzer.QualityParameter.Hidden = true;
493        islandQualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
494        islandQualityAnalyzer.BestKnownQualityParameter.Hidden = true;
495        qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
496        qualityAnalyzer.MaximizationParameter.Hidden = true;
497        qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
498        qualityAnalyzer.QualityParameter.Hidden = true;
499        qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
500        qualityAnalyzer.BestKnownQualityParameter.Hidden = true;
501      }
502    }
503    private void ParameterizeIterationBasedOperators() {
504      if (Problem != null) {
505        foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {
506          op.IterationsParameter.ActualName = "Generations";
507          op.IterationsParameter.Hidden = true;
508          op.MaximumIterationsParameter.ActualName = "MaximumGenerations";
509          op.MaximumIterationsParameter.Hidden = true;
510        }
511      }
512    }
513    private void UpdateCrossovers() {
514      ICrossover oldCrossover = CrossoverParameter.Value;
515      ICrossover defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
516      CrossoverParameter.ValidValues.Clear();
517      foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name)) {
518        ParameterizeStochasticOperatorForIsland(crossover);
519        CrossoverParameter.ValidValues.Add(crossover);
520      }
521      if (oldCrossover != null) {
522        ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
523        if (crossover != null) CrossoverParameter.Value = crossover;
524        else oldCrossover = null;
525      }
526      if (oldCrossover == null && defaultCrossover != null)
527        CrossoverParameter.Value = defaultCrossover;
528    }
529    private void UpdateMutators() {
530      IManipulator oldMutator = MutatorParameter.Value;
531      MutatorParameter.ValidValues.Clear();
532      foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name)) {
533        ParameterizeStochasticOperatorForIsland(mutator);
534        MutatorParameter.ValidValues.Add(mutator);
535      }
536      if (oldMutator != null) {
537        IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
538        if (mutator != null) MutatorParameter.Value = mutator;
539      }
540    }
541    private void UpdateAnalyzers() {
542      IslandAnalyzer.Operators.Clear();
543      Analyzer.Operators.Clear();
544      IslandAnalyzer.Operators.Add(islandQualityAnalyzer, islandQualityAnalyzer.EnabledByDefault);
545      if (Problem != null) {
546        foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
547          foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
548            param.Depth = 2;
549          Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
550        }
551      }
552      Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
553    }
554    private IslandGeneticAlgorithmMainLoop FindMainLoop(IOperator start) {
555      IOperator mainLoop = start;
556      while (mainLoop != null && !(mainLoop is IslandGeneticAlgorithmMainLoop))
557        mainLoop = ((SingleSuccessorOperator)mainLoop).Successor;
558      if (mainLoop == null) return null;
559      else return (IslandGeneticAlgorithmMainLoop)mainLoop;
560    }
561    #endregion
562  }
563}
Note: See TracBrowser for help on using the repository browser.