Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis.IslandAlgorithms/HeuristicLab.Algorithms.GeneticAlgorithm/3.3/IslandGeneticAlgorithm.cs @ 10155

Last change on this file since 10155 was 10155, checked in by mkommend, 11 years ago

#1997: Added possibility to reevaluate immigrants to the IslandGA and IslandOSGA.

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