Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Algorithms.GeneticAlgorithm/3.3/IslandGeneticAlgorithm.cs @ 17114

Last change on this file since 17114 was 17114, checked in by abeham, 5 years ago

#3004: merged to stable (16861)

File size: 30.8 KB
RevLine 
[3356]1#region License Information
2/* HeuristicLab
[17097]3 * Copyright (C) 2002-2019 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;
[17114]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>
[13295]40  [Item("Island Genetic Algorithm (Island-GA)", "An island genetic algorithm.")]
41  [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 110)]
[17097]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    }
[9673]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    }
[9673]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]
[17097]205    private IslandGeneticAlgorithm(StorableConstructorFlag _) : base(_) { }
[4722]206    [StorableHook(HookType.AfterDeserialization)]
207    private void AfterDeserialization() {
[9673]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      }
[15238]213      var optionalMutatorParameter = MutatorParameter as OptionalConstrainedValueParameter<IManipulator>;
214      if (optionalMutatorParameter != null) {
215        Parameters.Remove(optionalMutatorParameter);
216        Parameters.Add(new ConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
217        foreach (var m in optionalMutatorParameter.ValidValues)
218          MutatorParameter.ValidValues.Add(m);
219        if (optionalMutatorParameter.Value == null) MutationProbability.Value = 0; // to guarantee that the old configuration results in the same behavior
220        else Mutator = optionalMutatorParameter.Value;
221        optionalMutatorParameter.ValidValues.Clear(); // to avoid dangling references to the old parameter its valid values are cleared
222      }
[9673]223      #endregion
224
[4722]225      Initialize();
226    }
227    private IslandGeneticAlgorithm(IslandGeneticAlgorithm original, Cloner cloner)
228      : base(original, cloner) {
229      islandQualityAnalyzer = cloner.Clone(original.islandQualityAnalyzer);
230      qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
231      Initialize();
232    }
233    public override IDeepCloneable Clone(Cloner cloner) {
234      return new IslandGeneticAlgorithm(this, cloner);
235    }
236
[3356]237    public IslandGeneticAlgorithm()
238      : base() {
239      Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
240      Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
241      Parameters.Add(new ValueParameter<IntValue>("NumberOfIslands", "The number of islands.", new IntValue(5)));
242      Parameters.Add(new ValueParameter<IntValue>("MigrationInterval", "The number of generations that should pass between migration phases.", new IntValue(20)));
243      Parameters.Add(new ValueParameter<PercentValue>("MigrationRate", "The proportion of individuals that should migrate between the islands.", new PercentValue(0.15)));
244      Parameters.Add(new ConstrainedValueParameter<IMigrator>("Migrator", "The migration strategy."));
245      Parameters.Add(new ConstrainedValueParameter<ISelector>("EmigrantsSelector", "Selects the individuals that will be migrated."));
[3601]246      Parameters.Add(new ConstrainedValueParameter<IReplacer>("ImmigrationReplacer", "Selects the population from the unification of the original population and the immigrants."));
[3356]247      Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "The size of the population of solutions.", new IntValue(100)));
[3609]248      Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations that should be processed.", new IntValue(1000)));
[3356]249      Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
250      Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
251      Parameters.Add(new ValueParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new PercentValue(0.05)));
[15238]252      Parameters.Add(new ConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
[3356]253      Parameters.Add(new ValueParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation.", new IntValue(1)));
[9673]254      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]255      Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze the islands.", new MultiAnalyzer()));
256      Parameters.Add(new ValueParameter<MultiAnalyzer>("IslandAnalyzer", "The operator used to analyze each island.", new MultiAnalyzer()));
[4068]257
[3359]258      RandomCreator randomCreator = new RandomCreator();
[9039]259      UniformSubScopesProcessor ussp0 = new UniformSubScopesProcessor();
260      LocalRandomCreator localRandomCreator = new LocalRandomCreator();
261      RandomCreator globalRandomResetter = new RandomCreator();
[3356]262      SubScopesCreator populationCreator = new SubScopesCreator();
263      UniformSubScopesProcessor ussp1 = new UniformSubScopesProcessor();
[3359]264      SolutionsCreator solutionsCreator = new SolutionsCreator();
[5351]265      VariableCreator variableCreator = new VariableCreator();
266      UniformSubScopesProcessor ussp2 = new UniformSubScopesProcessor();
267      SubScopesCounter subScopesCounter = new SubScopesCounter();
[5356]268      ResultsCollector resultsCollector = new ResultsCollector();
[3359]269      IslandGeneticAlgorithmMainLoop mainLoop = new IslandGeneticAlgorithmMainLoop();
[3356]270      OperatorGraph.InitialOperator = randomCreator;
271
[7395]272      randomCreator.RandomParameter.ActualName = "GlobalRandom";
[3356]273      randomCreator.SeedParameter.ActualName = SeedParameter.Name;
274      randomCreator.SeedParameter.Value = null;
275      randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
276      randomCreator.SetSeedRandomlyParameter.Value = null;
277      randomCreator.Successor = populationCreator;
278
279      populationCreator.NumberOfSubScopesParameter.ActualName = NumberOfIslandsParameter.Name;
[9039]280      populationCreator.Successor = ussp0;
[3356]281
[9039]282      ussp0.Operator = localRandomCreator;
283      ussp0.Successor = globalRandomResetter;
284
285      // BackwardsCompatibility3.3
286      // the global random is resetted to ensure the same algorithm results
[9076]287      #region Backwards compatible code, remove global random resetter with 3.4 and rewire the operator graph
[9039]288      globalRandomResetter.RandomParameter.ActualName = "GlobalRandom";
289      globalRandomResetter.SeedParameter.ActualName = SeedParameter.Name;
290      globalRandomResetter.SeedParameter.Value = null;
291      globalRandomResetter.SetSeedRandomlyParameter.Value = new BoolValue(false);
292      globalRandomResetter.Successor = ussp1;
293      #endregion
294
[3356]295      ussp1.Operator = solutionsCreator;
[5351]296      ussp1.Successor = variableCreator;
[3356]297
298      solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
[7395]299      //don't create solutions in parallel because the hive engine would distribute these tasks
300      solutionsCreator.ParallelParameter.Value = new BoolValue(false);
[3356]301      solutionsCreator.Successor = null;
302
[5351]303      variableCreator.Name = "Initialize EvaluatedSolutions";
304      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("EvaluatedSolutions", new IntValue()));
305      variableCreator.Successor = ussp2;
306
307      ussp2.Operator = subScopesCounter;
[5356]308      ussp2.Successor = resultsCollector;
[5351]309
310      subScopesCounter.Name = "Count EvaluatedSolutions";
311      subScopesCounter.ValueParameter.ActualName = "EvaluatedSolutions";
312      subScopesCounter.Successor = null;
313
[5356]314      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
315      resultsCollector.ResultsParameter.ActualName = "Results";
316      resultsCollector.Successor = mainLoop;
317
[3356]318      mainLoop.EmigrantsSelectorParameter.ActualName = EmigrantsSelectorParameter.Name;
[3601]319      mainLoop.ImmigrationReplacerParameter.ActualName = ImmigrationReplacerParameter.Name;
[3609]320      mainLoop.MaximumGenerationsParameter.ActualName = MaximumGenerationsParameter.Name;
[3356]321      mainLoop.MigrationIntervalParameter.ActualName = MigrationIntervalParameter.Name;
322      mainLoop.MigrationRateParameter.ActualName = MigrationRateParameter.Name;
323      mainLoop.MigratorParameter.ActualName = MigratorParameter.Name;
324      mainLoop.NumberOfIslandsParameter.ActualName = NumberOfIslandsParameter.Name;
325      mainLoop.SelectorParameter.ActualName = SelectorParameter.Name;
326      mainLoop.CrossoverParameter.ActualName = CrossoverParameter.Name;
327      mainLoop.ElitesParameter.ActualName = ElitesParameter.Name;
[9673]328      mainLoop.ReevaluateElitesParameter.ActualName = ReevaluateElitesParameter.Name;
[3356]329      mainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
330      mainLoop.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
331      mainLoop.RandomParameter.ActualName = randomCreator.RandomParameter.ActualName;
332      mainLoop.ResultsParameter.ActualName = "Results";
[3650]333      mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
334      mainLoop.IslandAnalyzerParameter.ActualName = IslandAnalyzerParameter.Name;
[5351]335      mainLoop.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
[3356]336      mainLoop.Successor = null;
337
[3689]338      foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name))
339        SelectorParameter.ValidValues.Add(selector);
340      ISelector proportionalSelector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("ProportionalSelector"));
341      if (proportionalSelector != null) SelectorParameter.Value = proportionalSelector;
342
343      foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name))
344        EmigrantsSelectorParameter.ValidValues.Add(selector);
345
346      foreach (IReplacer replacer in ApplicationManager.Manager.GetInstances<IReplacer>().OrderBy(x => x.Name))
347        ImmigrationReplacerParameter.ValidValues.Add(replacer);
348
349      ParameterizeSelectors();
350
[13134]351      foreach (IMigrator migrator in ApplicationManager.Manager.GetInstances<IMigrator>().OrderBy(x => x.Name)) {
352        // BackwardsCompatibility3.3
353        // Set the migration direction to counterclockwise
354        var unidirectionalRing = migrator as UnidirectionalRingMigrator;
355        if (unidirectionalRing != null) unidirectionalRing.ClockwiseMigrationParameter.Value = new BoolValue(false);
[3689]356        MigratorParameter.ValidValues.Add(migrator);
[13134]357      }
[3689]358
359      qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
360      islandQualityAnalyzer = new BestAverageWorstQualityAnalyzer();
361      ParameterizeAnalyzers();
362      UpdateAnalyzers();
363
[3356]364      Initialize();
365    }
366
367    public override void Prepare() {
368      if (Problem != null) base.Prepare();
369    }
370
371    #region Events
372    protected override void OnProblemChanged() {
373      ParameterizeStochasticOperator(Problem.SolutionCreator);
[17114]374      foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
[9039]375      ParameterizeStochasticOperatorForIsland(Problem.Evaluator);
[3356]376      ParameterizeSolutionsCreator();
377      ParameterizeMainLoop();
378      ParameterizeSelectors();
[3650]379      ParameterizeAnalyzers();
[3750]380      ParameterizeIterationBasedOperators();
[3356]381      UpdateCrossovers();
382      UpdateMutators();
[3650]383      UpdateAnalyzers();
[3356]384      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
385      base.OnProblemChanged();
386    }
387
388    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
389      ParameterizeStochasticOperator(Problem.SolutionCreator);
390      ParameterizeSolutionsCreator();
391      base.Problem_SolutionCreatorChanged(sender, e);
392    }
393    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
[9039]394      ParameterizeStochasticOperatorForIsland(Problem.Evaluator);
[3356]395      ParameterizeSolutionsCreator();
396      ParameterizeMainLoop();
397      ParameterizeSelectors();
[3650]398      ParameterizeAnalyzers();
[3356]399      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
400      base.Problem_EvaluatorChanged(sender, e);
401    }
402    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
[7999]403      foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
[17114]404      ParameterizeStochasticOperatorForIsland(Problem.Evaluator);
[3750]405      ParameterizeIterationBasedOperators();
[3356]406      UpdateCrossovers();
407      UpdateMutators();
[3650]408      UpdateAnalyzers();
[3356]409      base.Problem_OperatorsChanged(sender, e);
410    }
411    private void ElitesParameter_ValueChanged(object sender, EventArgs e) {
412      Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
413      ParameterizeSelectors();
414    }
415    private void Elites_ValueChanged(object sender, EventArgs e) {
416      ParameterizeSelectors();
417    }
418    private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
419      NumberOfIslands.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
420      ParameterizeSelectors();
421    }
422    private void PopulationSize_ValueChanged(object sender, EventArgs e) {
423      ParameterizeSelectors();
424    }
425    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
426      ParameterizeMainLoop();
427      ParameterizeSelectors();
[3650]428      ParameterizeAnalyzers();
[3356]429    }
430    private void MigrationRateParameter_ValueChanged(object sender, EventArgs e) {
431      MigrationRate.ValueChanged += new EventHandler(MigrationRate_ValueChanged);
432      ParameterizeSelectors();
433    }
434    private void MigrationRate_ValueChanged(object sender, EventArgs e) {
435      ParameterizeSelectors();
436    }
437    #endregion
438
439    #region Helpers
440    private void Initialize() {
441      PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
442      PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
443      MigrationRateParameter.ValueChanged += new EventHandler(MigrationRateParameter_ValueChanged);
444      MigrationRate.ValueChanged += new EventHandler(MigrationRate_ValueChanged);
445      ElitesParameter.ValueChanged += new EventHandler(ElitesParameter_ValueChanged);
446      Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
447      if (Problem != null) {
448        Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
449      }
450    }
451    private void ParameterizeSolutionsCreator() {
[3359]452      SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
453      SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
[3356]454    }
455    private void ParameterizeMainLoop() {
[3359]456      MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
457      MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
458      MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
459      MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
[3356]460    }
461    private void ParameterizeStochasticOperator(IOperator op) {
[6051]462      IStochasticOperator stochasticOp = op as IStochasticOperator;
463      if (stochasticOp != null) {
464        stochasticOp.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
465        stochasticOp.RandomParameter.Hidden = true;
466      }
[3356]467    }
[7395]468    private void ParameterizeStochasticOperatorForIsland(IOperator op) {
469      IStochasticOperator stochasticOp = op as IStochasticOperator;
470      if (stochasticOp != null) {
471        stochasticOp.RandomParameter.ActualName = "LocalRandom";
472        stochasticOp.RandomParameter.Hidden = true;
473      }
474    }
[3356]475    private void ParameterizeSelectors() {
[3689]476      foreach (ISelector selector in SelectorParameter.ValidValues) {
[3356]477        selector.CopySelected = new BoolValue(true);
478        selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * (PopulationSize.Value - Elites.Value));
[6051]479        selector.NumberOfSelectedSubScopesParameter.Hidden = true;
[7395]480        ParameterizeStochasticOperatorForIsland(selector);
[3356]481      }
[3689]482      foreach (ISelector selector in EmigrantsSelectorParameter.ValidValues) {
[3356]483        selector.CopySelected = new BoolValue(true);
484        selector.NumberOfSelectedSubScopesParameter.Value = new IntValue((int)Math.Ceiling(PopulationSize.Value * MigrationRate.Value));
[6051]485        selector.NumberOfSelectedSubScopesParameter.Hidden = true;
[3356]486        ParameterizeStochasticOperator(selector);
487      }
[3689]488      foreach (IReplacer replacer in ImmigrationReplacerParameter.ValidValues) {
[3601]489        ParameterizeStochasticOperator(replacer);
[3356]490      }
491      if (Problem != null) {
[3689]492        foreach (ISingleObjectiveSelector selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
[3356]493          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
[6051]494          selector.MaximizationParameter.Hidden = true;
[3356]495          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
[6051]496          selector.QualityParameter.Hidden = true;
[3356]497        }
[3689]498        foreach (ISingleObjectiveSelector selector in EmigrantsSelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
[3356]499          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
[6051]500          selector.MaximizationParameter.Hidden = true;
[3356]501          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
[6051]502          selector.QualityParameter.Hidden = true;
[3356]503        }
[3689]504        foreach (ISingleObjectiveReplacer selector in ImmigrationReplacerParameter.ValidValues.OfType<ISingleObjectiveReplacer>()) {
[3356]505          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
[6051]506          selector.MaximizationParameter.Hidden = true;
[3356]507          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
[6051]508          selector.QualityParameter.Hidden = true;
[3356]509        }
510      }
511    }
[3650]512    private void ParameterizeAnalyzers() {
513      islandQualityAnalyzer.ResultsParameter.ActualName = "Results";
[6051]514      islandQualityAnalyzer.ResultsParameter.Hidden = true;
[3673]515      islandQualityAnalyzer.QualityParameter.Depth = 1;
[3671]516      qualityAnalyzer.ResultsParameter.ActualName = "Results";
[6051]517      qualityAnalyzer.ResultsParameter.Hidden = true;
[3673]518      qualityAnalyzer.QualityParameter.Depth = 2;
519
[3650]520      if (Problem != null) {
521        islandQualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
[6051]522        islandQualityAnalyzer.MaximizationParameter.Hidden = true;
[3650]523        islandQualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
[6051]524        islandQualityAnalyzer.QualityParameter.Hidden = true;
[3650]525        islandQualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
[6051]526        islandQualityAnalyzer.BestKnownQualityParameter.Hidden = true;
[3671]527        qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
[6051]528        qualityAnalyzer.MaximizationParameter.Hidden = true;
[3671]529        qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
[6051]530        qualityAnalyzer.QualityParameter.Hidden = true;
[3671]531        qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
[6051]532        qualityAnalyzer.BestKnownQualityParameter.Hidden = true;
[3650]533      }
534    }
[3750]535    private void ParameterizeIterationBasedOperators() {
536      if (Problem != null) {
537        foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {
538          op.IterationsParameter.ActualName = "Generations";
[6051]539          op.IterationsParameter.Hidden = true;
[3750]540          op.MaximumIterationsParameter.ActualName = "MaximumGenerations";
[6051]541          op.MaximumIterationsParameter.Hidden = true;
[3750]542        }
543      }
544    }
[3356]545    private void UpdateCrossovers() {
546      ICrossover oldCrossover = CrossoverParameter.Value;
[7511]547      ICrossover defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
[3356]548      CrossoverParameter.ValidValues.Clear();
[7524]549      foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name)) {
550        ParameterizeStochasticOperatorForIsland(crossover);
[3356]551        CrossoverParameter.ValidValues.Add(crossover);
[7524]552      }
[3356]553      if (oldCrossover != null) {
554        ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
555        if (crossover != null) CrossoverParameter.Value = crossover;
[7511]556        else oldCrossover = null;
[3356]557      }
[7511]558      if (oldCrossover == null && defaultCrossover != null)
559        CrossoverParameter.Value = defaultCrossover;
[3356]560    }
561    private void UpdateMutators() {
562      IManipulator oldMutator = MutatorParameter.Value;
563      MutatorParameter.ValidValues.Clear();
[15238]564      IManipulator defaultMutator = Problem.Operators.OfType<IManipulator>().FirstOrDefault();
565
[7395]566      foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name)) {
567        ParameterizeStochasticOperatorForIsland(mutator);
[3356]568        MutatorParameter.ValidValues.Add(mutator);
[7395]569      }
[3356]570      if (oldMutator != null) {
571        IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
572        if (mutator != null) MutatorParameter.Value = mutator;
[15238]573        else oldMutator = null;
[3356]574      }
[15238]575
576      if (oldMutator == null && defaultMutator != null)
577        MutatorParameter.Value = defaultMutator;
[3356]578    }
[3650]579    private void UpdateAnalyzers() {
580      IslandAnalyzer.Operators.Clear();
581      Analyzer.Operators.Clear();
[7172]582      IslandAnalyzer.Operators.Add(islandQualityAnalyzer, islandQualityAnalyzer.EnabledByDefault);
[3650]583      if (Problem != null) {
[3816]584        foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
[3671]585          foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
586            param.Depth = 2;
[7172]587          Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
[3650]588        }
589      }
[7172]590      Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
[3650]591    }
[5366]592    private IslandGeneticAlgorithmMainLoop FindMainLoop(IOperator start) {
593      IOperator mainLoop = start;
594      while (mainLoop != null && !(mainLoop is IslandGeneticAlgorithmMainLoop))
595        mainLoop = ((SingleSuccessorOperator)mainLoop).Successor;
596      if (mainLoop == null) return null;
597      else return (IslandGeneticAlgorithmMainLoop)mainLoop;
598    }
[3356]599    #endregion
600  }
601}
Note: See TracBrowser for help on using the repository browser.