Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.GeneticAlgorithm/3.3/IslandGeneticAlgorithm.cs @ 3671

Last change on this file since 3671 was 3671, checked in by abeham, 15 years ago

#999

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