Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14113 was 13173, checked in by abeham, 9 years ago

#2454: added some acronyms

File size: 29.7 KB
RevLine 
[3356]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3356]4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Linq;
[4068]24using HeuristicLab.Analysis;
[3376]25using HeuristicLab.Common;
[3356]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;
[3368]34using HeuristicLab.Random;
[3356]35
36namespace HeuristicLab.Algorithms.GeneticAlgorithm {
37  /// <summary>
38  /// An island genetic algorithm.
39  /// </summary>
[13173]40  [Item("Island Genetic Algorithm (Island-GA)", "An island genetic algorithm.")]
41  [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 110)]
[3356]42  [StorableClass]
[5809]43  public sealed class IslandGeneticAlgorithm : HeuristicOptimizationEngineAlgorithm, IStorableContent {
[4437]44    public string Filename { get; set; }
[3356]45
46    #region Problem Properties
47    public override Type ProblemType {
[5809]48      get { return typeof(ISingleObjectiveHeuristicOptimizationProblem); }
[3356]49    }
[5809]50    public new ISingleObjectiveHeuristicOptimizationProblem Problem {
51      get { return (ISingleObjectiveHeuristicOptimizationProblem)base.Problem; }
[3356]52      set { base.Problem = value; }
53    }
54    #endregion
55
56    #region Parameter Properties
57    private ValueParameter<IntValue> SeedParameter {
58      get { return (ValueParameter<IntValue>)Parameters["Seed"]; }
59    }
60    private ValueParameter<BoolValue> SetSeedRandomlyParameter {
61      get { return (ValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
62    }
63    private ValueParameter<IntValue> NumberOfIslandsParameter {
64      get { return (ValueParameter<IntValue>)Parameters["NumberOfIslands"]; }
65    }
66    private ValueParameter<IntValue> MigrationIntervalParameter {
67      get { return (ValueParameter<IntValue>)Parameters["MigrationInterval"]; }
68    }
69    private ValueParameter<PercentValue> MigrationRateParameter {
70      get { return (ValueParameter<PercentValue>)Parameters["MigrationRate"]; }
71    }
[8121]72    public IConstrainedValueParameter<IMigrator> MigratorParameter {
73      get { return (IConstrainedValueParameter<IMigrator>)Parameters["Migrator"]; }
[3356]74    }
[8121]75    public IConstrainedValueParameter<ISelector> EmigrantsSelectorParameter {
76      get { return (IConstrainedValueParameter<ISelector>)Parameters["EmigrantsSelector"]; }
[3356]77    }
[8121]78    public IConstrainedValueParameter<IReplacer> ImmigrationReplacerParameter {
79      get { return (IConstrainedValueParameter<IReplacer>)Parameters["ImmigrationReplacer"]; }
[3356]80    }
81    private ValueParameter<IntValue> PopulationSizeParameter {
82      get { return (ValueParameter<IntValue>)Parameters["PopulationSize"]; }
83    }
[3609]84    private ValueParameter<IntValue> MaximumGenerationsParameter {
85      get { return (ValueParameter<IntValue>)Parameters["MaximumGenerations"]; }
[3356]86    }
[8121]87    public IConstrainedValueParameter<ISelector> SelectorParameter {
88      get { return (IConstrainedValueParameter<ISelector>)Parameters["Selector"]; }
[3356]89    }
[8121]90    public IConstrainedValueParameter<ICrossover> CrossoverParameter {
91      get { return (IConstrainedValueParameter<ICrossover>)Parameters["Crossover"]; }
[3356]92    }
93    private ValueParameter<PercentValue> MutationProbabilityParameter {
94      get { return (ValueParameter<PercentValue>)Parameters["MutationProbability"]; }
95    }
[8121]96    public IConstrainedValueParameter<IManipulator> MutatorParameter {
97      get { return (IConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; }
[3356]98    }
99    private ValueParameter<IntValue> ElitesParameter {
100      get { return (ValueParameter<IntValue>)Parameters["Elites"]; }
101    }
[9569]102    private IFixedValueParameter<BoolValue> ReevaluateElitesParameter {
103      get { return (IFixedValueParameter<BoolValue>)Parameters["ReevaluateElites"]; }
104    }
[3658]105    private ValueParameter<MultiAnalyzer> AnalyzerParameter {
106      get { return (ValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
[3650]107    }
[3658]108    private ValueParameter<MultiAnalyzer> IslandAnalyzerParameter {
109      get { return (ValueParameter<MultiAnalyzer>)Parameters["IslandAnalyzer"]; }
[3650]110    }
[3356]111    #endregion
112
113    #region Properties
114    public IntValue Seed {
115      get { return SeedParameter.Value; }
116      set { SeedParameter.Value = value; }
117    }
118    public BoolValue SetSeedRandomly {
119      get { return SetSeedRandomlyParameter.Value; }
120      set { SetSeedRandomlyParameter.Value = value; }
121    }
122    public IntValue NumberOfIslands {
123      get { return NumberOfIslandsParameter.Value; }
124      set { NumberOfIslandsParameter.Value = value; }
125    }
126    public IntValue MigrationInterval {
127      get { return MigrationIntervalParameter.Value; }
128      set { MigrationIntervalParameter.Value = value; }
129    }
130    public PercentValue MigrationRate {
131      get { return MigrationRateParameter.Value; }
132      set { MigrationRateParameter.Value = value; }
133    }
134    public IMigrator Migrator {
135      get { return MigratorParameter.Value; }
136      set { MigratorParameter.Value = value; }
137    }
138    public ISelector EmigrantsSelector {
139      get { return EmigrantsSelectorParameter.Value; }
140      set { EmigrantsSelectorParameter.Value = value; }
141    }
[3601]142    public IReplacer ImmigrationReplacer {
143      get { return ImmigrationReplacerParameter.Value; }
144      set { ImmigrationReplacerParameter.Value = value; }
[3356]145    }
146    public IntValue PopulationSize {
147      get { return PopulationSizeParameter.Value; }
148      set { PopulationSizeParameter.Value = value; }
149    }
[3609]150    public IntValue MaximumGenerations {
151      get { return MaximumGenerationsParameter.Value; }
152      set { MaximumGenerationsParameter.Value = value; }
[3356]153    }
154    public ISelector Selector {
155      get { return SelectorParameter.Value; }
156      set { SelectorParameter.Value = value; }
157    }
158    public ICrossover Crossover {
159      get { return CrossoverParameter.Value; }
160      set { CrossoverParameter.Value = value; }
161    }
162    public PercentValue MutationProbability {
163      get { return MutationProbabilityParameter.Value; }
164      set { MutationProbabilityParameter.Value = value; }
165    }
166    public IManipulator Mutator {
167      get { return MutatorParameter.Value; }
168      set { MutatorParameter.Value = value; }
169    }
170    public IntValue Elites {
171      get { return ElitesParameter.Value; }
172      set { ElitesParameter.Value = value; }
173    }
[9569]174    public bool ReevaluteElites {
175      get { return ReevaluateElitesParameter.Value.Value; }
176      set { ReevaluateElitesParameter.Value.Value = value; }
177    }
[3658]178    public MultiAnalyzer Analyzer {
[3650]179      get { return AnalyzerParameter.Value; }
180      set { AnalyzerParameter.Value = value; }
181    }
[3658]182    public MultiAnalyzer IslandAnalyzer {
[3650]183      get { return IslandAnalyzerParameter.Value; }
184      set { IslandAnalyzerParameter.Value = value; }
185    }
[3359]186    private RandomCreator RandomCreator {
187      get { return (RandomCreator)OperatorGraph.InitialOperator; }
188    }
[3429]189    private UniformSubScopesProcessor IslandProcessor {
[9039]190      get { return OperatorGraph.Iterate().OfType<UniformSubScopesProcessor>().First(x => x.Operator is SolutionsCreator); }
[3429]191    }
[3359]192    private SolutionsCreator SolutionsCreator {
[3429]193      get { return (SolutionsCreator)IslandProcessor.Operator; }
[3359]194    }
195    private IslandGeneticAlgorithmMainLoop MainLoop {
[5366]196      get { return FindMainLoop(IslandProcessor.Successor); }
[3359]197    }
[3689]198    [Storable]
[3662]199    private BestAverageWorstQualityAnalyzer islandQualityAnalyzer;
[3689]200    [Storable]
[3671]201    private BestAverageWorstQualityAnalyzer qualityAnalyzer;
[3356]202    #endregion
203
204    [StorableConstructor]
205    private IslandGeneticAlgorithm(bool deserializing) : base(deserializing) { }
[4722]206    [StorableHook(HookType.AfterDeserialization)]
207    private void AfterDeserialization() {
[9592]208      // BackwardsCompatibility3.3
[9591]209      #region Backwards compatible code, remove with 3.4
[9569]210      if (!Parameters.ContainsKey("ReevaluateElites")) {
211        Parameters.Add(new FixedValueParameter<BoolValue>("ReevaluateElites", "Flag to determine if elite individuals should be reevaluated (i.e., if stochastic fitness functions are used.)", (BoolValue)new BoolValue(false).AsReadOnly()) { Hidden = true });
212      }
[9591]213      #endregion
214
[4722]215      Initialize();
216    }
217    private IslandGeneticAlgorithm(IslandGeneticAlgorithm original, Cloner cloner)
218      : base(original, cloner) {
219      islandQualityAnalyzer = cloner.Clone(original.islandQualityAnalyzer);
220      qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
221      Initialize();
222    }
223    public override IDeepCloneable Clone(Cloner cloner) {
224      return new IslandGeneticAlgorithm(this, cloner);
225    }
226
[3356]227    public IslandGeneticAlgorithm()
228      : base() {
229      Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
230      Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
231      Parameters.Add(new ValueParameter<IntValue>("NumberOfIslands", "The number of islands.", new IntValue(5)));
232      Parameters.Add(new ValueParameter<IntValue>("MigrationInterval", "The number of generations that should pass between migration phases.", new IntValue(20)));
233      Parameters.Add(new ValueParameter<PercentValue>("MigrationRate", "The proportion of individuals that should migrate between the islands.", new PercentValue(0.15)));
234      Parameters.Add(new ConstrainedValueParameter<IMigrator>("Migrator", "The migration strategy."));
235      Parameters.Add(new ConstrainedValueParameter<ISelector>("EmigrantsSelector", "Selects the individuals that will be migrated."));
[3601]236      Parameters.Add(new ConstrainedValueParameter<IReplacer>("ImmigrationReplacer", "Selects the population from the unification of the original population and the immigrants."));
[3356]237      Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "The size of the population of solutions.", new IntValue(100)));
[3609]238      Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations that should be processed.", new IntValue(1000)));
[3356]239      Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
240      Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
241      Parameters.Add(new ValueParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new PercentValue(0.05)));
242      Parameters.Add(new OptionalConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
243      Parameters.Add(new ValueParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation.", new IntValue(1)));
[9569]244      Parameters.Add(new FixedValueParameter<BoolValue>("ReevaluateElites", "Flag to determine if elite individuals should be reevaluated (i.e., if stochastic fitness functions are used.)", new BoolValue(false)) { Hidden = true });
[3658]245      Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze the islands.", new MultiAnalyzer()));
246      Parameters.Add(new ValueParameter<MultiAnalyzer>("IslandAnalyzer", "The operator used to analyze each island.", new MultiAnalyzer()));
[4068]247
[3359]248      RandomCreator randomCreator = new RandomCreator();
[9039]249      UniformSubScopesProcessor ussp0 = new UniformSubScopesProcessor();
250      LocalRandomCreator localRandomCreator = new LocalRandomCreator();
251      RandomCreator globalRandomResetter = new RandomCreator();
[3356]252      SubScopesCreator populationCreator = new SubScopesCreator();
253      UniformSubScopesProcessor ussp1 = new UniformSubScopesProcessor();
[3359]254      SolutionsCreator solutionsCreator = new SolutionsCreator();
[5351]255      VariableCreator variableCreator = new VariableCreator();
256      UniformSubScopesProcessor ussp2 = new UniformSubScopesProcessor();
257      SubScopesCounter subScopesCounter = new SubScopesCounter();
[5356]258      ResultsCollector resultsCollector = new ResultsCollector();
[3359]259      IslandGeneticAlgorithmMainLoop mainLoop = new IslandGeneticAlgorithmMainLoop();
[3356]260      OperatorGraph.InitialOperator = randomCreator;
261
[7395]262      randomCreator.RandomParameter.ActualName = "GlobalRandom";
[3356]263      randomCreator.SeedParameter.ActualName = SeedParameter.Name;
264      randomCreator.SeedParameter.Value = null;
265      randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
266      randomCreator.SetSeedRandomlyParameter.Value = null;
267      randomCreator.Successor = populationCreator;
268
269      populationCreator.NumberOfSubScopesParameter.ActualName = NumberOfIslandsParameter.Name;
[9039]270      populationCreator.Successor = ussp0;
[3356]271
[9039]272      ussp0.Operator = localRandomCreator;
273      ussp0.Successor = globalRandomResetter;
274
275      // BackwardsCompatibility3.3
276      // the global random is resetted to ensure the same algorithm results
[9076]277      #region Backwards compatible code, remove global random resetter with 3.4 and rewire the operator graph
[9039]278      globalRandomResetter.RandomParameter.ActualName = "GlobalRandom";
279      globalRandomResetter.SeedParameter.ActualName = SeedParameter.Name;
280      globalRandomResetter.SeedParameter.Value = null;
281      globalRandomResetter.SetSeedRandomlyParameter.Value = new BoolValue(false);
282      globalRandomResetter.Successor = ussp1;
283      #endregion
284
[3356]285      ussp1.Operator = solutionsCreator;
[5351]286      ussp1.Successor = variableCreator;
[3356]287
288      solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
[7395]289      //don't create solutions in parallel because the hive engine would distribute these tasks
290      solutionsCreator.ParallelParameter.Value = new BoolValue(false);
[3356]291      solutionsCreator.Successor = null;
292
[5351]293      variableCreator.Name = "Initialize EvaluatedSolutions";
294      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("EvaluatedSolutions", new IntValue()));
295      variableCreator.Successor = ussp2;
296
297      ussp2.Operator = subScopesCounter;
[5356]298      ussp2.Successor = resultsCollector;
[5351]299
300      subScopesCounter.Name = "Count EvaluatedSolutions";
301      subScopesCounter.ValueParameter.ActualName = "EvaluatedSolutions";
302      subScopesCounter.Successor = null;
303
[5356]304      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
305      resultsCollector.ResultsParameter.ActualName = "Results";
306      resultsCollector.Successor = mainLoop;
307
[3356]308      mainLoop.EmigrantsSelectorParameter.ActualName = EmigrantsSelectorParameter.Name;
[3601]309      mainLoop.ImmigrationReplacerParameter.ActualName = ImmigrationReplacerParameter.Name;
[3609]310      mainLoop.MaximumGenerationsParameter.ActualName = MaximumGenerationsParameter.Name;
[3356]311      mainLoop.MigrationIntervalParameter.ActualName = MigrationIntervalParameter.Name;
312      mainLoop.MigrationRateParameter.ActualName = MigrationRateParameter.Name;
313      mainLoop.MigratorParameter.ActualName = MigratorParameter.Name;
314      mainLoop.NumberOfIslandsParameter.ActualName = NumberOfIslandsParameter.Name;
315      mainLoop.SelectorParameter.ActualName = SelectorParameter.Name;
316      mainLoop.CrossoverParameter.ActualName = CrossoverParameter.Name;
317      mainLoop.ElitesParameter.ActualName = ElitesParameter.Name;
[9569]318      mainLoop.ReevaluateElitesParameter.ActualName = ReevaluateElitesParameter.Name;
[3356]319      mainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
320      mainLoop.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
321      mainLoop.RandomParameter.ActualName = randomCreator.RandomParameter.ActualName;
322      mainLoop.ResultsParameter.ActualName = "Results";
[3650]323      mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
324      mainLoop.IslandAnalyzerParameter.ActualName = IslandAnalyzerParameter.Name;
[5351]325      mainLoop.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
[3356]326      mainLoop.Successor = null;
327
[3689]328      foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name))
329        SelectorParameter.ValidValues.Add(selector);
330      ISelector proportionalSelector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("ProportionalSelector"));
331      if (proportionalSelector != null) SelectorParameter.Value = proportionalSelector;
332
333      foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name))
334        EmigrantsSelectorParameter.ValidValues.Add(selector);
335
336      foreach (IReplacer replacer in ApplicationManager.Manager.GetInstances<IReplacer>().OrderBy(x => x.Name))
337        ImmigrationReplacerParameter.ValidValues.Add(replacer);
338
339      ParameterizeSelectors();
340
[13094]341      foreach (IMigrator migrator in ApplicationManager.Manager.GetInstances<IMigrator>().OrderBy(x => x.Name)) {
342        // BackwardsCompatibility3.3
343        // Set the migration direction to counterclockwise
344        var unidirectionalRing = migrator as UnidirectionalRingMigrator;
[13109]345        if (unidirectionalRing != null) unidirectionalRing.ClockwiseMigrationParameter.Value = new BoolValue(false);
[3689]346        MigratorParameter.ValidValues.Add(migrator);
[13094]347      }
[3689]348
349      qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
350      islandQualityAnalyzer = new BestAverageWorstQualityAnalyzer();
351      ParameterizeAnalyzers();
352      UpdateAnalyzers();
353
[3356]354      Initialize();
355    }
356
357    public override void Prepare() {
358      if (Problem != null) base.Prepare();
359    }
360
361    #region Events
362    protected override void OnProblemChanged() {
363      ParameterizeStochasticOperator(Problem.SolutionCreator);
[9039]364      ParameterizeStochasticOperatorForIsland(Problem.Evaluator);
[7999]365      foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
[3356]366      ParameterizeSolutionsCreator();
367      ParameterizeMainLoop();
368      ParameterizeSelectors();
[3650]369      ParameterizeAnalyzers();
[3750]370      ParameterizeIterationBasedOperators();
[3356]371      UpdateCrossovers();
372      UpdateMutators();
[3650]373      UpdateAnalyzers();
[3356]374      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
375      base.OnProblemChanged();
376    }
377
378    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
379      ParameterizeStochasticOperator(Problem.SolutionCreator);
380      ParameterizeSolutionsCreator();
381      base.Problem_SolutionCreatorChanged(sender, e);
382    }
383    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
[9039]384      ParameterizeStochasticOperatorForIsland(Problem.Evaluator);
[3356]385      ParameterizeSolutionsCreator();
386      ParameterizeMainLoop();
387      ParameterizeSelectors();
[3650]388      ParameterizeAnalyzers();
[3356]389      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
390      base.Problem_EvaluatorChanged(sender, e);
391    }
392    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
[7999]393      foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
[3750]394      ParameterizeIterationBasedOperators();
[3356]395      UpdateCrossovers();
396      UpdateMutators();
[3650]397      UpdateAnalyzers();
[3356]398      base.Problem_OperatorsChanged(sender, e);
399    }
400    private void ElitesParameter_ValueChanged(object sender, EventArgs e) {
401      Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
402      ParameterizeSelectors();
403    }
404    private void Elites_ValueChanged(object sender, EventArgs e) {
405      ParameterizeSelectors();
406    }
407    private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
408      NumberOfIslands.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
409      ParameterizeSelectors();
410    }
411    private void PopulationSize_ValueChanged(object sender, EventArgs e) {
412      ParameterizeSelectors();
413    }
414    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
415      ParameterizeMainLoop();
416      ParameterizeSelectors();
[3650]417      ParameterizeAnalyzers();
[3356]418    }
419    private void MigrationRateParameter_ValueChanged(object sender, EventArgs e) {
420      MigrationRate.ValueChanged += new EventHandler(MigrationRate_ValueChanged);
421      ParameterizeSelectors();
422    }
423    private void MigrationRate_ValueChanged(object sender, EventArgs e) {
424      ParameterizeSelectors();
425    }
426    #endregion
427
428    #region Helpers
429    private void Initialize() {
430      PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
431      PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
432      MigrationRateParameter.ValueChanged += new EventHandler(MigrationRateParameter_ValueChanged);
433      MigrationRate.ValueChanged += new EventHandler(MigrationRate_ValueChanged);
434      ElitesParameter.ValueChanged += new EventHandler(ElitesParameter_ValueChanged);
435      Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
436      if (Problem != null) {
437        Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
438      }
439    }
440    private void ParameterizeSolutionsCreator() {
[3359]441      SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
442      SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
[3356]443    }
444    private void ParameterizeMainLoop() {
[3359]445      MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
446      MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
447      MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
448      MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
[3356]449    }
450    private void ParameterizeStochasticOperator(IOperator op) {
[6051]451      IStochasticOperator stochasticOp = op as IStochasticOperator;
452      if (stochasticOp != null) {
453        stochasticOp.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
454        stochasticOp.RandomParameter.Hidden = true;
455      }
[3356]456    }
[7395]457    private void ParameterizeStochasticOperatorForIsland(IOperator op) {
458      IStochasticOperator stochasticOp = op as IStochasticOperator;
459      if (stochasticOp != null) {
460        stochasticOp.RandomParameter.ActualName = "LocalRandom";
461        stochasticOp.RandomParameter.Hidden = true;
462      }
463    }
[3356]464    private void ParameterizeSelectors() {
[3689]465      foreach (ISelector selector in SelectorParameter.ValidValues) {
[3356]466        selector.CopySelected = new BoolValue(true);
467        selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * (PopulationSize.Value - Elites.Value));
[6051]468        selector.NumberOfSelectedSubScopesParameter.Hidden = true;
[7395]469        ParameterizeStochasticOperatorForIsland(selector);
[3356]470      }
[3689]471      foreach (ISelector selector in EmigrantsSelectorParameter.ValidValues) {
[3356]472        selector.CopySelected = new BoolValue(true);
473        selector.NumberOfSelectedSubScopesParameter.Value = new IntValue((int)Math.Ceiling(PopulationSize.Value * MigrationRate.Value));
[6051]474        selector.NumberOfSelectedSubScopesParameter.Hidden = true;
[3356]475        ParameterizeStochasticOperator(selector);
476      }
[3689]477      foreach (IReplacer replacer in ImmigrationReplacerParameter.ValidValues) {
[3601]478        ParameterizeStochasticOperator(replacer);
[3356]479      }
480      if (Problem != null) {
[3689]481        foreach (ISingleObjectiveSelector selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
[3356]482          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
[6051]483          selector.MaximizationParameter.Hidden = true;
[3356]484          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
[6051]485          selector.QualityParameter.Hidden = true;
[3356]486        }
[3689]487        foreach (ISingleObjectiveSelector selector in EmigrantsSelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
[3356]488          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
[6051]489          selector.MaximizationParameter.Hidden = true;
[3356]490          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
[6051]491          selector.QualityParameter.Hidden = true;
[3356]492        }
[3689]493        foreach (ISingleObjectiveReplacer selector in ImmigrationReplacerParameter.ValidValues.OfType<ISingleObjectiveReplacer>()) {
[3356]494          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
[6051]495          selector.MaximizationParameter.Hidden = true;
[3356]496          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
[6051]497          selector.QualityParameter.Hidden = true;
[3356]498        }
499      }
500    }
[3650]501    private void ParameterizeAnalyzers() {
502      islandQualityAnalyzer.ResultsParameter.ActualName = "Results";
[6051]503      islandQualityAnalyzer.ResultsParameter.Hidden = true;
[3673]504      islandQualityAnalyzer.QualityParameter.Depth = 1;
[3671]505      qualityAnalyzer.ResultsParameter.ActualName = "Results";
[6051]506      qualityAnalyzer.ResultsParameter.Hidden = true;
[3673]507      qualityAnalyzer.QualityParameter.Depth = 2;
508
[3650]509      if (Problem != null) {
510        islandQualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
[6051]511        islandQualityAnalyzer.MaximizationParameter.Hidden = true;
[3650]512        islandQualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
[6051]513        islandQualityAnalyzer.QualityParameter.Hidden = true;
[3650]514        islandQualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
[6051]515        islandQualityAnalyzer.BestKnownQualityParameter.Hidden = true;
[3671]516        qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
[6051]517        qualityAnalyzer.MaximizationParameter.Hidden = true;
[3671]518        qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
[6051]519        qualityAnalyzer.QualityParameter.Hidden = true;
[3671]520        qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
[6051]521        qualityAnalyzer.BestKnownQualityParameter.Hidden = true;
[3650]522      }
523    }
[3750]524    private void ParameterizeIterationBasedOperators() {
525      if (Problem != null) {
526        foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {
527          op.IterationsParameter.ActualName = "Generations";
[6051]528          op.IterationsParameter.Hidden = true;
[3750]529          op.MaximumIterationsParameter.ActualName = "MaximumGenerations";
[6051]530          op.MaximumIterationsParameter.Hidden = true;
[3750]531        }
532      }
533    }
[3356]534    private void UpdateCrossovers() {
535      ICrossover oldCrossover = CrossoverParameter.Value;
[7511]536      ICrossover defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
[3356]537      CrossoverParameter.ValidValues.Clear();
[7524]538      foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name)) {
539        ParameterizeStochasticOperatorForIsland(crossover);
[3356]540        CrossoverParameter.ValidValues.Add(crossover);
[7524]541      }
[3356]542      if (oldCrossover != null) {
543        ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
544        if (crossover != null) CrossoverParameter.Value = crossover;
[7511]545        else oldCrossover = null;
[3356]546      }
[7511]547      if (oldCrossover == null && defaultCrossover != null)
548        CrossoverParameter.Value = defaultCrossover;
[3356]549    }
550    private void UpdateMutators() {
551      IManipulator oldMutator = MutatorParameter.Value;
552      MutatorParameter.ValidValues.Clear();
[7395]553      foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name)) {
554        ParameterizeStochasticOperatorForIsland(mutator);
[3356]555        MutatorParameter.ValidValues.Add(mutator);
[7395]556      }
[3356]557      if (oldMutator != null) {
558        IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
559        if (mutator != null) MutatorParameter.Value = mutator;
560      }
561    }
[3650]562    private void UpdateAnalyzers() {
563      IslandAnalyzer.Operators.Clear();
564      Analyzer.Operators.Clear();
[7172]565      IslandAnalyzer.Operators.Add(islandQualityAnalyzer, islandQualityAnalyzer.EnabledByDefault);
[3650]566      if (Problem != null) {
[3816]567        foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
[3671]568          foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
569            param.Depth = 2;
[7172]570          Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
[3650]571        }
572      }
[7172]573      Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
[3650]574    }
[5366]575    private IslandGeneticAlgorithmMainLoop FindMainLoop(IOperator start) {
576      IOperator mainLoop = start;
577      while (mainLoop != null && !(mainLoop is IslandGeneticAlgorithmMainLoop))
578        mainLoop = ((SingleSuccessorOperator)mainLoop).Successor;
579      if (mainLoop == null) return null;
580      else return (IslandGeneticAlgorithmMainLoop)mainLoop;
581    }
[3356]582    #endregion
583  }
584}
Note: See TracBrowser for help on using the repository browser.