Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2994-AutoDiffForIntervals/HeuristicLab.Algorithms.GeneticAlgorithm/3.3/IslandGeneticAlgorithm.cs @ 16911

Last change on this file since 16911 was 16911, checked in by gkronber, 5 years ago

#2994: merged r16839:16910 from trunk to branch

File size: 30.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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      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      }
223      #endregion
224
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
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."));
246      Parameters.Add(new ConstrainedValueParameter<IReplacer>("ImmigrationReplacer", "Selects the population from the unification of the original population and the immigrants."));
247      Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "The size of the population of solutions.", new IntValue(100)));
248      Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations that should be processed.", new IntValue(1000)));
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)));
252      Parameters.Add(new ConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
253      Parameters.Add(new ValueParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation.", new IntValue(1)));
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 });
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()));
257
258      RandomCreator randomCreator = new RandomCreator();
259      UniformSubScopesProcessor ussp0 = new UniformSubScopesProcessor();
260      LocalRandomCreator localRandomCreator = new LocalRandomCreator();
261      RandomCreator globalRandomResetter = new RandomCreator();
262      SubScopesCreator populationCreator = new SubScopesCreator();
263      UniformSubScopesProcessor ussp1 = new UniformSubScopesProcessor();
264      SolutionsCreator solutionsCreator = new SolutionsCreator();
265      VariableCreator variableCreator = new VariableCreator();
266      UniformSubScopesProcessor ussp2 = new UniformSubScopesProcessor();
267      SubScopesCounter subScopesCounter = new SubScopesCounter();
268      ResultsCollector resultsCollector = new ResultsCollector();
269      IslandGeneticAlgorithmMainLoop mainLoop = new IslandGeneticAlgorithmMainLoop();
270      OperatorGraph.InitialOperator = randomCreator;
271
272      randomCreator.RandomParameter.ActualName = "GlobalRandom";
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;
280      populationCreator.Successor = ussp0;
281
282      ussp0.Operator = localRandomCreator;
283      ussp0.Successor = globalRandomResetter;
284
285      // BackwardsCompatibility3.3
286      // the global random is resetted to ensure the same algorithm results
287      #region Backwards compatible code, remove global random resetter with 3.4 and rewire the operator graph
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
295      ussp1.Operator = solutionsCreator;
296      ussp1.Successor = variableCreator;
297
298      solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
299      //don't create solutions in parallel because the hive engine would distribute these tasks
300      solutionsCreator.ParallelParameter.Value = new BoolValue(false);
301      solutionsCreator.Successor = null;
302
303      variableCreator.Name = "Initialize EvaluatedSolutions";
304      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("EvaluatedSolutions", new IntValue()));
305      variableCreator.Successor = ussp2;
306
307      ussp2.Operator = subScopesCounter;
308      ussp2.Successor = resultsCollector;
309
310      subScopesCounter.Name = "Count EvaluatedSolutions";
311      subScopesCounter.ValueParameter.ActualName = "EvaluatedSolutions";
312      subScopesCounter.Successor = null;
313
314      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
315      resultsCollector.ResultsParameter.ActualName = "Results";
316      resultsCollector.Successor = mainLoop;
317
318      mainLoop.EmigrantsSelectorParameter.ActualName = EmigrantsSelectorParameter.Name;
319      mainLoop.ImmigrationReplacerParameter.ActualName = ImmigrationReplacerParameter.Name;
320      mainLoop.MaximumGenerationsParameter.ActualName = MaximumGenerationsParameter.Name;
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;
328      mainLoop.ReevaluateElitesParameter.ActualName = ReevaluateElitesParameter.Name;
329      mainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
330      mainLoop.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
331      mainLoop.RandomParameter.ActualName = randomCreator.RandomParameter.ActualName;
332      mainLoop.ResultsParameter.ActualName = "Results";
333      mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
334      mainLoop.IslandAnalyzerParameter.ActualName = IslandAnalyzerParameter.Name;
335      mainLoop.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
336      mainLoop.Successor = null;
337
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
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);
356        MigratorParameter.ValidValues.Add(migrator);
357      }
358
359      qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
360      islandQualityAnalyzer = new BestAverageWorstQualityAnalyzer();
361      ParameterizeAnalyzers();
362      UpdateAnalyzers();
363
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);
374      foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
375      ParameterizeStochasticOperatorForIsland(Problem.Evaluator);
376      ParameterizeSolutionsCreator();
377      ParameterizeMainLoop();
378      ParameterizeSelectors();
379      ParameterizeAnalyzers();
380      ParameterizeIterationBasedOperators();
381      UpdateCrossovers();
382      UpdateMutators();
383      UpdateAnalyzers();
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) {
394      ParameterizeStochasticOperatorForIsland(Problem.Evaluator);
395      ParameterizeSolutionsCreator();
396      ParameterizeMainLoop();
397      ParameterizeSelectors();
398      ParameterizeAnalyzers();
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) {
403      foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
404      ParameterizeStochasticOperatorForIsland(Problem.Evaluator);
405      ParameterizeIterationBasedOperators();
406      UpdateCrossovers();
407      UpdateMutators();
408      UpdateAnalyzers();
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();
428      ParameterizeAnalyzers();
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() {
452      SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
453      SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
454    }
455    private void ParameterizeMainLoop() {
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;
460    }
461    private void ParameterizeStochasticOperator(IOperator op) {
462      IStochasticOperator stochasticOp = op as IStochasticOperator;
463      if (stochasticOp != null) {
464        stochasticOp.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
465        stochasticOp.RandomParameter.Hidden = true;
466      }
467    }
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    }
475    private void ParameterizeSelectors() {
476      foreach (ISelector selector in SelectorParameter.ValidValues) {
477        selector.CopySelected = new BoolValue(true);
478        selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * (PopulationSize.Value - Elites.Value));
479        selector.NumberOfSelectedSubScopesParameter.Hidden = true;
480        ParameterizeStochasticOperatorForIsland(selector);
481      }
482      foreach (ISelector selector in EmigrantsSelectorParameter.ValidValues) {
483        selector.CopySelected = new BoolValue(true);
484        selector.NumberOfSelectedSubScopesParameter.Value = new IntValue((int)Math.Ceiling(PopulationSize.Value * MigrationRate.Value));
485        selector.NumberOfSelectedSubScopesParameter.Hidden = true;
486        ParameterizeStochasticOperator(selector);
487      }
488      foreach (IReplacer replacer in ImmigrationReplacerParameter.ValidValues) {
489        ParameterizeStochasticOperator(replacer);
490      }
491      if (Problem != null) {
492        foreach (ISingleObjectiveSelector selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
493          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
494          selector.MaximizationParameter.Hidden = true;
495          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
496          selector.QualityParameter.Hidden = true;
497        }
498        foreach (ISingleObjectiveSelector selector in EmigrantsSelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
499          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
500          selector.MaximizationParameter.Hidden = true;
501          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
502          selector.QualityParameter.Hidden = true;
503        }
504        foreach (ISingleObjectiveReplacer selector in ImmigrationReplacerParameter.ValidValues.OfType<ISingleObjectiveReplacer>()) {
505          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
506          selector.MaximizationParameter.Hidden = true;
507          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
508          selector.QualityParameter.Hidden = true;
509        }
510      }
511    }
512    private void ParameterizeAnalyzers() {
513      islandQualityAnalyzer.ResultsParameter.ActualName = "Results";
514      islandQualityAnalyzer.ResultsParameter.Hidden = true;
515      islandQualityAnalyzer.QualityParameter.Depth = 1;
516      qualityAnalyzer.ResultsParameter.ActualName = "Results";
517      qualityAnalyzer.ResultsParameter.Hidden = true;
518      qualityAnalyzer.QualityParameter.Depth = 2;
519
520      if (Problem != null) {
521        islandQualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
522        islandQualityAnalyzer.MaximizationParameter.Hidden = true;
523        islandQualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
524        islandQualityAnalyzer.QualityParameter.Hidden = true;
525        islandQualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
526        islandQualityAnalyzer.BestKnownQualityParameter.Hidden = true;
527        qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
528        qualityAnalyzer.MaximizationParameter.Hidden = true;
529        qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
530        qualityAnalyzer.QualityParameter.Hidden = true;
531        qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
532        qualityAnalyzer.BestKnownQualityParameter.Hidden = true;
533      }
534    }
535    private void ParameterizeIterationBasedOperators() {
536      if (Problem != null) {
537        foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {
538          op.IterationsParameter.ActualName = "Generations";
539          op.IterationsParameter.Hidden = true;
540          op.MaximumIterationsParameter.ActualName = "MaximumGenerations";
541          op.MaximumIterationsParameter.Hidden = true;
542        }
543      }
544    }
545    private void UpdateCrossovers() {
546      ICrossover oldCrossover = CrossoverParameter.Value;
547      ICrossover defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
548      CrossoverParameter.ValidValues.Clear();
549      foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name)) {
550        ParameterizeStochasticOperatorForIsland(crossover);
551        CrossoverParameter.ValidValues.Add(crossover);
552      }
553      if (oldCrossover != null) {
554        ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
555        if (crossover != null) CrossoverParameter.Value = crossover;
556        else oldCrossover = null;
557      }
558      if (oldCrossover == null && defaultCrossover != null)
559        CrossoverParameter.Value = defaultCrossover;
560    }
561    private void UpdateMutators() {
562      IManipulator oldMutator = MutatorParameter.Value;
563      MutatorParameter.ValidValues.Clear();
564      IManipulator defaultMutator = Problem.Operators.OfType<IManipulator>().FirstOrDefault();
565
566      foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name)) {
567        ParameterizeStochasticOperatorForIsland(mutator);
568        MutatorParameter.ValidValues.Add(mutator);
569      }
570      if (oldMutator != null) {
571        IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
572        if (mutator != null) MutatorParameter.Value = mutator;
573        else oldMutator = null;
574      }
575
576      if (oldMutator == null && defaultMutator != null)
577        MutatorParameter.Value = defaultMutator;
578    }
579    private void UpdateAnalyzers() {
580      IslandAnalyzer.Operators.Clear();
581      Analyzer.Operators.Clear();
582      IslandAnalyzer.Operators.Add(islandQualityAnalyzer, islandQualityAnalyzer.EnabledByDefault);
583      if (Problem != null) {
584        foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
585          foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
586            param.Depth = 2;
587          Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
588        }
589      }
590      Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
591    }
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    }
599    #endregion
600  }
601}
Note: See TracBrowser for help on using the repository browser.