Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17867 was 17198, checked in by mkommend, 5 years ago

#3020: Adapated AfterDeserializationHook of genetic algorithms to check if the mutator parameter already has the correct type.
In detail the following algorithms have been adapted: ALPS-GA, ALPS-OSGA, GA, Island-GA, NSGA-2, Island-OSGA, OSGA, SASEGASA.

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