Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17564 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
RevLine 
[3356]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3356]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;
[16861]24using HEAL.Attic;
[4068]25using HeuristicLab.Analysis;
[3376]26using HeuristicLab.Common;
[3356]27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Operators;
30using HeuristicLab.Optimization;
31using HeuristicLab.Optimization.Operators;
32using HeuristicLab.Parameters;
33using HeuristicLab.PluginInfrastructure;
[3368]34using HeuristicLab.Random;
[3356]35
36namespace HeuristicLab.Algorithms.GeneticAlgorithm {
37  /// <summary>
38  /// An island genetic algorithm.
39  /// </summary>
[13173]40  [Item("Island Genetic Algorithm (Island-GA)", "An island genetic algorithm.")]
41  [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 110)]
[16565]42  [StorableType("C36FD509-4EF2-4BA7-9483-8CFCEF7EDA91")]
[5809]43  public sealed class IslandGeneticAlgorithm : HeuristicOptimizationEngineAlgorithm, IStorableContent {
[4437]44    public string Filename { get; set; }
[3356]45
46    #region Problem Properties
47    public override Type ProblemType {
[5809]48      get { return typeof(ISingleObjectiveHeuristicOptimizationProblem); }
[3356]49    }
[5809]50    public new ISingleObjectiveHeuristicOptimizationProblem Problem {
51      get { return (ISingleObjectiveHeuristicOptimizationProblem)base.Problem; }
[3356]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    }
[8121]72    public IConstrainedValueParameter<IMigrator> MigratorParameter {
73      get { return (IConstrainedValueParameter<IMigrator>)Parameters["Migrator"]; }
[3356]74    }
[8121]75    public IConstrainedValueParameter<ISelector> EmigrantsSelectorParameter {
76      get { return (IConstrainedValueParameter<ISelector>)Parameters["EmigrantsSelector"]; }
[3356]77    }
[8121]78    public IConstrainedValueParameter<IReplacer> ImmigrationReplacerParameter {
79      get { return (IConstrainedValueParameter<IReplacer>)Parameters["ImmigrationReplacer"]; }
[3356]80    }
81    private ValueParameter<IntValue> PopulationSizeParameter {
82      get { return (ValueParameter<IntValue>)Parameters["PopulationSize"]; }
83    }
[3609]84    private ValueParameter<IntValue> MaximumGenerationsParameter {
85      get { return (ValueParameter<IntValue>)Parameters["MaximumGenerations"]; }
[3356]86    }
[8121]87    public IConstrainedValueParameter<ISelector> SelectorParameter {
88      get { return (IConstrainedValueParameter<ISelector>)Parameters["Selector"]; }
[3356]89    }
[8121]90    public IConstrainedValueParameter<ICrossover> CrossoverParameter {
91      get { return (IConstrainedValueParameter<ICrossover>)Parameters["Crossover"]; }
[3356]92    }
93    private ValueParameter<PercentValue> MutationProbabilityParameter {
94      get { return (ValueParameter<PercentValue>)Parameters["MutationProbability"]; }
95    }
[8121]96    public IConstrainedValueParameter<IManipulator> MutatorParameter {
97      get { return (IConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; }
[3356]98    }
99    private ValueParameter<IntValue> ElitesParameter {
100      get { return (ValueParameter<IntValue>)Parameters["Elites"]; }
101    }
[9569]102    private IFixedValueParameter<BoolValue> ReevaluateElitesParameter {
103      get { return (IFixedValueParameter<BoolValue>)Parameters["ReevaluateElites"]; }
104    }
[3658]105    private ValueParameter<MultiAnalyzer> AnalyzerParameter {
106      get { return (ValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
[3650]107    }
[3658]108    private ValueParameter<MultiAnalyzer> IslandAnalyzerParameter {
109      get { return (ValueParameter<MultiAnalyzer>)Parameters["IslandAnalyzer"]; }
[3650]110    }
[3356]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    }
[3601]142    public IReplacer ImmigrationReplacer {
143      get { return ImmigrationReplacerParameter.Value; }
144      set { ImmigrationReplacerParameter.Value = value; }
[3356]145    }
146    public IntValue PopulationSize {
147      get { return PopulationSizeParameter.Value; }
148      set { PopulationSizeParameter.Value = value; }
149    }
[3609]150    public IntValue MaximumGenerations {
151      get { return MaximumGenerationsParameter.Value; }
152      set { MaximumGenerationsParameter.Value = value; }
[3356]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    }
[9569]174    public bool ReevaluteElites {
175      get { return ReevaluateElitesParameter.Value.Value; }
176      set { ReevaluateElitesParameter.Value.Value = value; }
177    }
[3658]178    public MultiAnalyzer Analyzer {
[3650]179      get { return AnalyzerParameter.Value; }
180      set { AnalyzerParameter.Value = value; }
181    }
[3658]182    public MultiAnalyzer IslandAnalyzer {
[3650]183      get { return IslandAnalyzerParameter.Value; }
184      set { IslandAnalyzerParameter.Value = value; }
185    }
[3359]186    private RandomCreator RandomCreator {
187      get { return (RandomCreator)OperatorGraph.InitialOperator; }
188    }
[3429]189    private UniformSubScopesProcessor IslandProcessor {
[9039]190      get { return OperatorGraph.Iterate().OfType<UniformSubScopesProcessor>().First(x => x.Operator is SolutionsCreator); }
[3429]191    }
[3359]192    private SolutionsCreator SolutionsCreator {
[3429]193      get { return (SolutionsCreator)IslandProcessor.Operator; }
[3359]194    }
195    private IslandGeneticAlgorithmMainLoop MainLoop {
[5366]196      get { return FindMainLoop(IslandProcessor.Successor); }
[3359]197    }
[3689]198    [Storable]
[3662]199    private BestAverageWorstQualityAnalyzer islandQualityAnalyzer;
[3689]200    [Storable]
[3671]201    private BestAverageWorstQualityAnalyzer qualityAnalyzer;
[3356]202    #endregion
203
204    [StorableConstructor]
[16565]205    private IslandGeneticAlgorithm(StorableConstructorFlag _) : base(_) { }
[4722]206    [StorableHook(HookType.AfterDeserialization)]
207    private void AfterDeserialization() {
[9592]208      // BackwardsCompatibility3.3
[9591]209      #region Backwards compatible code, remove with 3.4
[9569]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      }
[17198]213
[15049]214      var optionalMutatorParameter = MutatorParameter as OptionalConstrainedValueParameter<IManipulator>;
[17198]215      var mutatorParameter = MutatorParameter as ConstrainedValueParameter<IManipulator>;
216      if (mutatorParameter == null && optionalMutatorParameter != null) {
[15049]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      }
[9591]225      #endregion
226
[4722]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
[3356]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."));
[3601]248      Parameters.Add(new ConstrainedValueParameter<IReplacer>("ImmigrationReplacer", "Selects the population from the unification of the original population and the immigrants."));
[3356]249      Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "The size of the population of solutions.", new IntValue(100)));
[3609]250      Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations that should be processed.", new IntValue(1000)));
[3356]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)));
[15049]254      Parameters.Add(new ConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
[3356]255      Parameters.Add(new ValueParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation.", new IntValue(1)));
[9569]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 });
[3658]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()));
[4068]259
[3359]260      RandomCreator randomCreator = new RandomCreator();
[9039]261      UniformSubScopesProcessor ussp0 = new UniformSubScopesProcessor();
262      LocalRandomCreator localRandomCreator = new LocalRandomCreator();
263      RandomCreator globalRandomResetter = new RandomCreator();
[3356]264      SubScopesCreator populationCreator = new SubScopesCreator();
265      UniformSubScopesProcessor ussp1 = new UniformSubScopesProcessor();
[3359]266      SolutionsCreator solutionsCreator = new SolutionsCreator();
[5351]267      VariableCreator variableCreator = new VariableCreator();
268      UniformSubScopesProcessor ussp2 = new UniformSubScopesProcessor();
269      SubScopesCounter subScopesCounter = new SubScopesCounter();
[5356]270      ResultsCollector resultsCollector = new ResultsCollector();
[3359]271      IslandGeneticAlgorithmMainLoop mainLoop = new IslandGeneticAlgorithmMainLoop();
[3356]272      OperatorGraph.InitialOperator = randomCreator;
273
[7395]274      randomCreator.RandomParameter.ActualName = "GlobalRandom";
[3356]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;
[9039]282      populationCreator.Successor = ussp0;
[3356]283
[9039]284      ussp0.Operator = localRandomCreator;
285      ussp0.Successor = globalRandomResetter;
286
287      // BackwardsCompatibility3.3
288      // the global random is resetted to ensure the same algorithm results
[9076]289      #region Backwards compatible code, remove global random resetter with 3.4 and rewire the operator graph
[9039]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
[3356]297      ussp1.Operator = solutionsCreator;
[5351]298      ussp1.Successor = variableCreator;
[3356]299
300      solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
[7395]301      //don't create solutions in parallel because the hive engine would distribute these tasks
302      solutionsCreator.ParallelParameter.Value = new BoolValue(false);
[3356]303      solutionsCreator.Successor = null;
304
[5351]305      variableCreator.Name = "Initialize EvaluatedSolutions";
306      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("EvaluatedSolutions", new IntValue()));
307      variableCreator.Successor = ussp2;
308
309      ussp2.Operator = subScopesCounter;
[5356]310      ussp2.Successor = resultsCollector;
[5351]311
312      subScopesCounter.Name = "Count EvaluatedSolutions";
313      subScopesCounter.ValueParameter.ActualName = "EvaluatedSolutions";
314      subScopesCounter.Successor = null;
315
[5356]316      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
317      resultsCollector.ResultsParameter.ActualName = "Results";
318      resultsCollector.Successor = mainLoop;
319
[3356]320      mainLoop.EmigrantsSelectorParameter.ActualName = EmigrantsSelectorParameter.Name;
[3601]321      mainLoop.ImmigrationReplacerParameter.ActualName = ImmigrationReplacerParameter.Name;
[3609]322      mainLoop.MaximumGenerationsParameter.ActualName = MaximumGenerationsParameter.Name;
[3356]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;
[9569]330      mainLoop.ReevaluateElitesParameter.ActualName = ReevaluateElitesParameter.Name;
[3356]331      mainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
332      mainLoop.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
333      mainLoop.RandomParameter.ActualName = randomCreator.RandomParameter.ActualName;
334      mainLoop.ResultsParameter.ActualName = "Results";
[3650]335      mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
336      mainLoop.IslandAnalyzerParameter.ActualName = IslandAnalyzerParameter.Name;
[5351]337      mainLoop.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
[3356]338      mainLoop.Successor = null;
339
[3689]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
[13094]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;
[13109]357        if (unidirectionalRing != null) unidirectionalRing.ClockwiseMigrationParameter.Value = new BoolValue(false);
[3689]358        MigratorParameter.ValidValues.Add(migrator);
[13094]359      }
[3689]360
361      qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
362      islandQualityAnalyzer = new BestAverageWorstQualityAnalyzer();
363      ParameterizeAnalyzers();
364      UpdateAnalyzers();
365
[3356]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);
[16861]376      foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
[9039]377      ParameterizeStochasticOperatorForIsland(Problem.Evaluator);
[3356]378      ParameterizeSolutionsCreator();
379      ParameterizeMainLoop();
380      ParameterizeSelectors();
[3650]381      ParameterizeAnalyzers();
[3750]382      ParameterizeIterationBasedOperators();
[3356]383      UpdateCrossovers();
384      UpdateMutators();
[3650]385      UpdateAnalyzers();
[3356]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) {
[9039]396      ParameterizeStochasticOperatorForIsland(Problem.Evaluator);
[3356]397      ParameterizeSolutionsCreator();
398      ParameterizeMainLoop();
399      ParameterizeSelectors();
[3650]400      ParameterizeAnalyzers();
[3356]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) {
[7999]405      foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
[16861]406      ParameterizeStochasticOperatorForIsland(Problem.Evaluator);
[3750]407      ParameterizeIterationBasedOperators();
[3356]408      UpdateCrossovers();
409      UpdateMutators();
[3650]410      UpdateAnalyzers();
[3356]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();
[3650]430      ParameterizeAnalyzers();
[3356]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() {
[3359]454      SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
455      SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
[3356]456    }
457    private void ParameterizeMainLoop() {
[3359]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;
[3356]462    }
463    private void ParameterizeStochasticOperator(IOperator op) {
[6051]464      IStochasticOperator stochasticOp = op as IStochasticOperator;
465      if (stochasticOp != null) {
466        stochasticOp.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
467        stochasticOp.RandomParameter.Hidden = true;
468      }
[3356]469    }
[7395]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    }
[3356]477    private void ParameterizeSelectors() {
[3689]478      foreach (ISelector selector in SelectorParameter.ValidValues) {
[3356]479        selector.CopySelected = new BoolValue(true);
480        selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * (PopulationSize.Value - Elites.Value));
[6051]481        selector.NumberOfSelectedSubScopesParameter.Hidden = true;
[7395]482        ParameterizeStochasticOperatorForIsland(selector);
[3356]483      }
[3689]484      foreach (ISelector selector in EmigrantsSelectorParameter.ValidValues) {
[3356]485        selector.CopySelected = new BoolValue(true);
486        selector.NumberOfSelectedSubScopesParameter.Value = new IntValue((int)Math.Ceiling(PopulationSize.Value * MigrationRate.Value));
[6051]487        selector.NumberOfSelectedSubScopesParameter.Hidden = true;
[3356]488        ParameterizeStochasticOperator(selector);
489      }
[3689]490      foreach (IReplacer replacer in ImmigrationReplacerParameter.ValidValues) {
[3601]491        ParameterizeStochasticOperator(replacer);
[3356]492      }
493      if (Problem != null) {
[3689]494        foreach (ISingleObjectiveSelector selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
[3356]495          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
[6051]496          selector.MaximizationParameter.Hidden = true;
[3356]497          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
[6051]498          selector.QualityParameter.Hidden = true;
[3356]499        }
[3689]500        foreach (ISingleObjectiveSelector selector in EmigrantsSelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
[3356]501          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
[6051]502          selector.MaximizationParameter.Hidden = true;
[3356]503          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
[6051]504          selector.QualityParameter.Hidden = true;
[3356]505        }
[3689]506        foreach (ISingleObjectiveReplacer selector in ImmigrationReplacerParameter.ValidValues.OfType<ISingleObjectiveReplacer>()) {
[3356]507          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
[6051]508          selector.MaximizationParameter.Hidden = true;
[3356]509          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
[6051]510          selector.QualityParameter.Hidden = true;
[3356]511        }
512      }
513    }
[3650]514    private void ParameterizeAnalyzers() {
515      islandQualityAnalyzer.ResultsParameter.ActualName = "Results";
[6051]516      islandQualityAnalyzer.ResultsParameter.Hidden = true;
[3673]517      islandQualityAnalyzer.QualityParameter.Depth = 1;
[3671]518      qualityAnalyzer.ResultsParameter.ActualName = "Results";
[6051]519      qualityAnalyzer.ResultsParameter.Hidden = true;
[3673]520      qualityAnalyzer.QualityParameter.Depth = 2;
521
[3650]522      if (Problem != null) {
523        islandQualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
[6051]524        islandQualityAnalyzer.MaximizationParameter.Hidden = true;
[3650]525        islandQualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
[6051]526        islandQualityAnalyzer.QualityParameter.Hidden = true;
[3650]527        islandQualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
[6051]528        islandQualityAnalyzer.BestKnownQualityParameter.Hidden = true;
[3671]529        qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
[6051]530        qualityAnalyzer.MaximizationParameter.Hidden = true;
[3671]531        qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
[6051]532        qualityAnalyzer.QualityParameter.Hidden = true;
[3671]533        qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
[6051]534        qualityAnalyzer.BestKnownQualityParameter.Hidden = true;
[3650]535      }
536    }
[3750]537    private void ParameterizeIterationBasedOperators() {
538      if (Problem != null) {
539        foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {
540          op.IterationsParameter.ActualName = "Generations";
[6051]541          op.IterationsParameter.Hidden = true;
[3750]542          op.MaximumIterationsParameter.ActualName = "MaximumGenerations";
[6051]543          op.MaximumIterationsParameter.Hidden = true;
[3750]544        }
545      }
546    }
[3356]547    private void UpdateCrossovers() {
548      ICrossover oldCrossover = CrossoverParameter.Value;
[7511]549      ICrossover defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
[3356]550      CrossoverParameter.ValidValues.Clear();
[7524]551      foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name)) {
552        ParameterizeStochasticOperatorForIsland(crossover);
[3356]553        CrossoverParameter.ValidValues.Add(crossover);
[7524]554      }
[3356]555      if (oldCrossover != null) {
556        ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
557        if (crossover != null) CrossoverParameter.Value = crossover;
[7511]558        else oldCrossover = null;
[3356]559      }
[7511]560      if (oldCrossover == null && defaultCrossover != null)
561        CrossoverParameter.Value = defaultCrossover;
[3356]562    }
563    private void UpdateMutators() {
564      IManipulator oldMutator = MutatorParameter.Value;
565      MutatorParameter.ValidValues.Clear();
[15049]566      IManipulator defaultMutator = Problem.Operators.OfType<IManipulator>().FirstOrDefault();
567
[7395]568      foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name)) {
569        ParameterizeStochasticOperatorForIsland(mutator);
[3356]570        MutatorParameter.ValidValues.Add(mutator);
[7395]571      }
[3356]572      if (oldMutator != null) {
573        IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
574        if (mutator != null) MutatorParameter.Value = mutator;
[15049]575        else oldMutator = null;
[3356]576      }
[15049]577
578      if (oldMutator == null && defaultMutator != null)
579        MutatorParameter.Value = defaultMutator;
[3356]580    }
[3650]581    private void UpdateAnalyzers() {
582      IslandAnalyzer.Operators.Clear();
583      Analyzer.Operators.Clear();
[7172]584      IslandAnalyzer.Operators.Add(islandQualityAnalyzer, islandQualityAnalyzer.EnabledByDefault);
[3650]585      if (Problem != null) {
[3816]586        foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
[3671]587          foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
588            param.Depth = 2;
[7172]589          Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
[3650]590        }
591      }
[7172]592      Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
[3650]593    }
[5366]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    }
[3356]601    #endregion
602  }
603}
Note: See TracBrowser for help on using the repository browser.