Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2817-BinPackingSpeedup/HeuristicLab.Algorithms.GeneticAlgorithm/3.3/IslandGeneticAlgorithm.cs @ 16140

Last change on this file since 16140 was 16140, checked in by abeham, 6 years ago

#2817: updated to trunk r15680

File size: 30.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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 HeuristicLab.Analysis;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Optimization.Operators;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.PluginInfrastructure;
34using HeuristicLab.Random;
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  [StorableClass]
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(bool deserializing) : base(deserializing) { }
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      ParameterizeStochasticOperatorForIsland(Problem.Evaluator);
375      foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
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      ParameterizeIterationBasedOperators();
405      UpdateCrossovers();
406      UpdateMutators();
407      UpdateAnalyzers();
408      base.Problem_OperatorsChanged(sender, e);
409    }
410    private void ElitesParameter_ValueChanged(object sender, EventArgs e) {
411      Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
412      ParameterizeSelectors();
413    }
414    private void Elites_ValueChanged(object sender, EventArgs e) {
415      ParameterizeSelectors();
416    }
417    private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
418      NumberOfIslands.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
419      ParameterizeSelectors();
420    }
421    private void PopulationSize_ValueChanged(object sender, EventArgs e) {
422      ParameterizeSelectors();
423    }
424    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
425      ParameterizeMainLoop();
426      ParameterizeSelectors();
427      ParameterizeAnalyzers();
428    }
429    private void MigrationRateParameter_ValueChanged(object sender, EventArgs e) {
430      MigrationRate.ValueChanged += new EventHandler(MigrationRate_ValueChanged);
431      ParameterizeSelectors();
432    }
433    private void MigrationRate_ValueChanged(object sender, EventArgs e) {
434      ParameterizeSelectors();
435    }
436    #endregion
437
438    #region Helpers
439    private void Initialize() {
440      PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
441      PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
442      MigrationRateParameter.ValueChanged += new EventHandler(MigrationRateParameter_ValueChanged);
443      MigrationRate.ValueChanged += new EventHandler(MigrationRate_ValueChanged);
444      ElitesParameter.ValueChanged += new EventHandler(ElitesParameter_ValueChanged);
445      Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
446      if (Problem != null) {
447        Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
448      }
449    }
450    private void ParameterizeSolutionsCreator() {
451      SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
452      SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
453    }
454    private void ParameterizeMainLoop() {
455      MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
456      MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
457      MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
458      MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
459    }
460    private void ParameterizeStochasticOperator(IOperator op) {
461      IStochasticOperator stochasticOp = op as IStochasticOperator;
462      if (stochasticOp != null) {
463        stochasticOp.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
464        stochasticOp.RandomParameter.Hidden = true;
465      }
466    }
467    private void ParameterizeStochasticOperatorForIsland(IOperator op) {
468      IStochasticOperator stochasticOp = op as IStochasticOperator;
469      if (stochasticOp != null) {
470        stochasticOp.RandomParameter.ActualName = "LocalRandom";
471        stochasticOp.RandomParameter.Hidden = true;
472      }
473    }
474    private void ParameterizeSelectors() {
475      foreach (ISelector selector in SelectorParameter.ValidValues) {
476        selector.CopySelected = new BoolValue(true);
477        selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * (PopulationSize.Value - Elites.Value));
478        selector.NumberOfSelectedSubScopesParameter.Hidden = true;
479        ParameterizeStochasticOperatorForIsland(selector);
480      }
481      foreach (ISelector selector in EmigrantsSelectorParameter.ValidValues) {
482        selector.CopySelected = new BoolValue(true);
483        selector.NumberOfSelectedSubScopesParameter.Value = new IntValue((int)Math.Ceiling(PopulationSize.Value * MigrationRate.Value));
484        selector.NumberOfSelectedSubScopesParameter.Hidden = true;
485        ParameterizeStochasticOperator(selector);
486      }
487      foreach (IReplacer replacer in ImmigrationReplacerParameter.ValidValues) {
488        ParameterizeStochasticOperator(replacer);
489      }
490      if (Problem != null) {
491        foreach (ISingleObjectiveSelector selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
492          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
493          selector.MaximizationParameter.Hidden = true;
494          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
495          selector.QualityParameter.Hidden = true;
496        }
497        foreach (ISingleObjectiveSelector selector in EmigrantsSelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
498          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
499          selector.MaximizationParameter.Hidden = true;
500          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
501          selector.QualityParameter.Hidden = true;
502        }
503        foreach (ISingleObjectiveReplacer selector in ImmigrationReplacerParameter.ValidValues.OfType<ISingleObjectiveReplacer>()) {
504          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
505          selector.MaximizationParameter.Hidden = true;
506          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
507          selector.QualityParameter.Hidden = true;
508        }
509      }
510    }
511    private void ParameterizeAnalyzers() {
512      islandQualityAnalyzer.ResultsParameter.ActualName = "Results";
513      islandQualityAnalyzer.ResultsParameter.Hidden = true;
514      islandQualityAnalyzer.QualityParameter.Depth = 1;
515      qualityAnalyzer.ResultsParameter.ActualName = "Results";
516      qualityAnalyzer.ResultsParameter.Hidden = true;
517      qualityAnalyzer.QualityParameter.Depth = 2;
518
519      if (Problem != null) {
520        islandQualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
521        islandQualityAnalyzer.MaximizationParameter.Hidden = true;
522        islandQualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
523        islandQualityAnalyzer.QualityParameter.Hidden = true;
524        islandQualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
525        islandQualityAnalyzer.BestKnownQualityParameter.Hidden = true;
526        qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
527        qualityAnalyzer.MaximizationParameter.Hidden = true;
528        qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
529        qualityAnalyzer.QualityParameter.Hidden = true;
530        qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
531        qualityAnalyzer.BestKnownQualityParameter.Hidden = true;
532      }
533    }
534    private void ParameterizeIterationBasedOperators() {
535      if (Problem != null) {
536        foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {
537          op.IterationsParameter.ActualName = "Generations";
538          op.IterationsParameter.Hidden = true;
539          op.MaximumIterationsParameter.ActualName = "MaximumGenerations";
540          op.MaximumIterationsParameter.Hidden = true;
541        }
542      }
543    }
544    private void UpdateCrossovers() {
545      ICrossover oldCrossover = CrossoverParameter.Value;
546      ICrossover defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
547      CrossoverParameter.ValidValues.Clear();
548      foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name)) {
549        ParameterizeStochasticOperatorForIsland(crossover);
550        CrossoverParameter.ValidValues.Add(crossover);
551      }
552      if (oldCrossover != null) {
553        ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
554        if (crossover != null) CrossoverParameter.Value = crossover;
555        else oldCrossover = null;
556      }
557      if (oldCrossover == null && defaultCrossover != null)
558        CrossoverParameter.Value = defaultCrossover;
559    }
560    private void UpdateMutators() {
561      IManipulator oldMutator = MutatorParameter.Value;
562      MutatorParameter.ValidValues.Clear();
563      IManipulator defaultMutator = Problem.Operators.OfType<IManipulator>().FirstOrDefault();
564
565      foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name)) {
566        ParameterizeStochasticOperatorForIsland(mutator);
567        MutatorParameter.ValidValues.Add(mutator);
568      }
569      if (oldMutator != null) {
570        IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
571        if (mutator != null) MutatorParameter.Value = mutator;
572        else oldMutator = null;
573      }
574
575      if (oldMutator == null && defaultMutator != null)
576        MutatorParameter.Value = defaultMutator;
577    }
578    private void UpdateAnalyzers() {
579      IslandAnalyzer.Operators.Clear();
580      Analyzer.Operators.Clear();
581      IslandAnalyzer.Operators.Add(islandQualityAnalyzer, islandQualityAnalyzer.EnabledByDefault);
582      if (Problem != null) {
583        foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
584          foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
585            param.Depth = 2;
586          Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
587        }
588      }
589      Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
590    }
591    private IslandGeneticAlgorithmMainLoop FindMainLoop(IOperator start) {
592      IOperator mainLoop = start;
593      while (mainLoop != null && !(mainLoop is IslandGeneticAlgorithmMainLoop))
594        mainLoop = ((SingleSuccessorOperator)mainLoop).Successor;
595      if (mainLoop == null) return null;
596      else return (IslandGeneticAlgorithmMainLoop)mainLoop;
597    }
598    #endregion
599  }
600}
Note: See TracBrowser for help on using the repository browser.