Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm/3.3/IslandOffspringSelectionGeneticAlgorithm.cs @ 17991

Last change on this file since 17991 was 17198, checked in by mkommend, 5 years ago

#3020: Adapated AfterDeserializationHook of genetic algorithms to check if the mutator parameter already has the correct type.
In detail the following algorithms have been adapted: ALPS-GA, ALPS-OSGA, GA, Island-GA, NSGA-2, Island-OSGA, OSGA, SASEGASA.

File size: 38.9 KB
RevLine 
[3429]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3429]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;
[17198]24using HEAL.Attic;
[3654]25using HeuristicLab.Analysis;
[3429]26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Operators;
30using HeuristicLab.Optimization;
31using HeuristicLab.Optimization.Operators;
32using HeuristicLab.Parameters;
33using HeuristicLab.PluginInfrastructure;
34using HeuristicLab.Random;
35
36namespace HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm {
37  /// <summary>
38  /// An offspring selection island genetic algorithm.
39  /// </summary>
[13173]40  [Item("Island Offspring Selection Genetic Algorithm (Island-OSGA)", "An island offspring selection genetic algorithm.")]
[12504]41  [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 130)]
[16565]42  [StorableType("1CE20A3D-8DC7-4504-AEEB-95A0E40B9274")]
[5809]43  public sealed class IslandOffspringSelectionGeneticAlgorithm : HeuristicOptimizationEngineAlgorithm, IStorableContent {
[4437]44    public string Filename { get; set; }
[3429]45
46    #region Problem Properties
47    public override Type ProblemType {
[5809]48      get { return typeof(ISingleObjectiveHeuristicOptimizationProblem); }
[3429]49    }
[5809]50    public new ISingleObjectiveHeuristicOptimizationProblem Problem {
51      get { return (ISingleObjectiveHeuristicOptimizationProblem)base.Problem; }
[3429]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"]; }
[3429]74    }
[8121]75    public IConstrainedValueParameter<ISelector> EmigrantsSelectorParameter {
76      get { return (IConstrainedValueParameter<ISelector>)Parameters["EmigrantsSelector"]; }
[3429]77    }
[8121]78    public IConstrainedValueParameter<IReplacer> ImmigrationReplacerParameter {
79      get { return (IConstrainedValueParameter<IReplacer>)Parameters["ImmigrationReplacer"]; }
[3429]80    }
81    private ValueParameter<IntValue> PopulationSizeParameter {
82      get { return (ValueParameter<IntValue>)Parameters["PopulationSize"]; }
83    }
[3611]84    private ValueParameter<IntValue> MaximumGenerationsParameter {
85      get { return (ValueParameter<IntValue>)Parameters["MaximumGenerations"]; }
[3429]86    }
[8121]87    public IConstrainedValueParameter<ISelector> SelectorParameter {
88      get { return (IConstrainedValueParameter<ISelector>)Parameters["Selector"]; }
[3429]89    }
[8121]90    public IConstrainedValueParameter<ICrossover> CrossoverParameter {
91      get { return (IConstrainedValueParameter<ICrossover>)Parameters["Crossover"]; }
[3429]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"]; }
[3429]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    }
[3429]105    private ValueLookupParameter<DoubleValue> SuccessRatioParameter {
106      get { return (ValueLookupParameter<DoubleValue>)Parameters["SuccessRatio"]; }
107    }
108    private ValueLookupParameter<DoubleValue> ComparisonFactorLowerBoundParameter {
109      get { return (ValueLookupParameter<DoubleValue>)Parameters["ComparisonFactorLowerBound"]; }
110    }
111    private ValueLookupParameter<DoubleValue> ComparisonFactorUpperBoundParameter {
112      get { return (ValueLookupParameter<DoubleValue>)Parameters["ComparisonFactorUpperBound"]; }
113    }
[8121]114    public IConstrainedValueParameter<IDiscreteDoubleValueModifier> ComparisonFactorModifierParameter {
115      get { return (IConstrainedValueParameter<IDiscreteDoubleValueModifier>)Parameters["ComparisonFactorModifier"]; }
[3429]116    }
117    private ValueLookupParameter<DoubleValue> MaximumSelectionPressureParameter {
118      get { return (ValueLookupParameter<DoubleValue>)Parameters["MaximumSelectionPressure"]; }
119    }
120    private ValueLookupParameter<BoolValue> OffspringSelectionBeforeMutationParameter {
121      get { return (ValueLookupParameter<BoolValue>)Parameters["OffspringSelectionBeforeMutation"]; }
122    }
[3700]123    private ValueLookupParameter<IntValue> SelectedParentsParameter {
124      get { return (ValueLookupParameter<IntValue>)Parameters["SelectedParents"]; }
125    }
[3658]126    private ValueParameter<MultiAnalyzer> AnalyzerParameter {
127      get { return (ValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
[3654]128    }
[3658]129    private ValueParameter<MultiAnalyzer> IslandAnalyzerParameter {
130      get { return (ValueParameter<MultiAnalyzer>)Parameters["IslandAnalyzer"]; }
[3654]131    }
[3891]132    private ValueParameter<IntValue> MaximumEvaluatedSolutionsParameter {
133      get { return (ValueParameter<IntValue>)Parameters["MaximumEvaluatedSolutions"]; }
134    }
[10643]135    private IFixedValueParameter<BoolValue> FillPopulationWithParentsParameter {
136      get { return (IFixedValueParameter<BoolValue>)Parameters["FillPopulationWithParents"]; }
137    }
[3429]138    #endregion
139
140    #region Properties
141    public IntValue Seed {
142      get { return SeedParameter.Value; }
143      set { SeedParameter.Value = value; }
144    }
145    public BoolValue SetSeedRandomly {
146      get { return SetSeedRandomlyParameter.Value; }
147      set { SetSeedRandomlyParameter.Value = value; }
148    }
149    public IntValue NumberOfIslands {
150      get { return NumberOfIslandsParameter.Value; }
151      set { NumberOfIslandsParameter.Value = value; }
152    }
153    public IntValue MigrationInterval {
154      get { return MigrationIntervalParameter.Value; }
155      set { MigrationIntervalParameter.Value = value; }
156    }
157    public PercentValue MigrationRate {
158      get { return MigrationRateParameter.Value; }
159      set { MigrationRateParameter.Value = value; }
160    }
161    public IMigrator Migrator {
162      get { return MigratorParameter.Value; }
163      set { MigratorParameter.Value = value; }
164    }
165    public ISelector EmigrantsSelector {
166      get { return EmigrantsSelectorParameter.Value; }
167      set { EmigrantsSelectorParameter.Value = value; }
168    }
[3611]169    public IReplacer ImmigrationReplacer {
170      get { return ImmigrationReplacerParameter.Value; }
171      set { ImmigrationReplacerParameter.Value = value; }
[3429]172    }
173    public IntValue PopulationSize {
174      get { return PopulationSizeParameter.Value; }
175      set { PopulationSizeParameter.Value = value; }
176    }
[3611]177    public IntValue MaximumGenerations {
178      get { return MaximumGenerationsParameter.Value; }
179      set { MaximumGenerationsParameter.Value = value; }
[3429]180    }
181    public ISelector Selector {
182      get { return SelectorParameter.Value; }
183      set { SelectorParameter.Value = value; }
184    }
185    public ICrossover Crossover {
186      get { return CrossoverParameter.Value; }
187      set { CrossoverParameter.Value = value; }
188    }
189    public PercentValue MutationProbability {
190      get { return MutationProbabilityParameter.Value; }
191      set { MutationProbabilityParameter.Value = value; }
192    }
193    public IManipulator Mutator {
194      get { return MutatorParameter.Value; }
195      set { MutatorParameter.Value = value; }
196    }
197    public IntValue Elites {
198      get { return ElitesParameter.Value; }
199      set { ElitesParameter.Value = value; }
200    }
[9569]201    public bool ReevaluteElites {
202      get { return ReevaluateElitesParameter.Value.Value; }
203      set { ReevaluateElitesParameter.Value.Value = value; }
204    }
[10643]205    public DoubleValue SuccessRatio {
[3429]206      get { return SuccessRatioParameter.Value; }
207      set { SuccessRatioParameter.Value = value; }
208    }
[10643]209    public DoubleValue ComparisonFactorLowerBound {
[3429]210      get { return ComparisonFactorLowerBoundParameter.Value; }
211      set { ComparisonFactorLowerBoundParameter.Value = value; }
212    }
[10643]213    public DoubleValue ComparisonFactorUpperBound {
[3429]214      get { return ComparisonFactorUpperBoundParameter.Value; }
215      set { ComparisonFactorUpperBoundParameter.Value = value; }
216    }
[8121]217    public IDiscreteDoubleValueModifier ComparisonFactorModifier {
[3429]218      get { return ComparisonFactorModifierParameter.Value; }
219      set { ComparisonFactorModifierParameter.Value = value; }
220    }
[10643]221    public DoubleValue MaximumSelectionPressure {
[3429]222      get { return MaximumSelectionPressureParameter.Value; }
223      set { MaximumSelectionPressureParameter.Value = value; }
224    }
[10643]225    public BoolValue OffspringSelectionBeforeMutation {
[3429]226      get { return OffspringSelectionBeforeMutationParameter.Value; }
227      set { OffspringSelectionBeforeMutationParameter.Value = value; }
228    }
[3658]229    public MultiAnalyzer Analyzer {
[3654]230      get { return AnalyzerParameter.Value; }
231      set { AnalyzerParameter.Value = value; }
232    }
[3658]233    public MultiAnalyzer IslandAnalyzer {
[3654]234      get { return IslandAnalyzerParameter.Value; }
235      set { IslandAnalyzerParameter.Value = value; }
236    }
[3891]237    public IntValue MaximumEvaluatedSolutions {
238      get { return MaximumEvaluatedSolutionsParameter.Value; }
239      set { MaximumEvaluatedSolutionsParameter.Value = value; }
240    }
[10643]241    public bool FillPopulationWithParents {
242      get { return FillPopulationWithParentsParameter.Value.Value; }
243      set { FillPopulationWithParentsParameter.Value.Value = value; }
244    }
[3429]245    private RandomCreator RandomCreator {
246      get { return (RandomCreator)OperatorGraph.InitialOperator; }
247    }
248    private UniformSubScopesProcessor IslandProcessor {
249      get { return ((RandomCreator.Successor as SubScopesCreator).Successor as UniformSubScopesProcessor); }
250    }
251    private SolutionsCreator SolutionsCreator {
252      get { return (SolutionsCreator)IslandProcessor.Operator; }
253    }
[3479]254    private IslandOffspringSelectionGeneticAlgorithmMainLoop MainLoop {
[5366]255      get { return FindMainLoop(IslandProcessor.Successor); }
[3429]256    }
[3689]257    [Storable]
[3662]258    private BestAverageWorstQualityAnalyzer islandQualityAnalyzer;
[3689]259    [Storable]
[3673]260    private BestAverageWorstQualityAnalyzer qualityAnalyzer;
[3689]261    [Storable]
[3673]262    private ValueAnalyzer islandSelectionPressureAnalyzer;
[3689]263    [Storable]
[3673]264    private ValueAnalyzer selectionPressureAnalyzer;
[5683]265    [Storable]
266    private SuccessfulOffspringAnalyzer successfulOffspringAnalyzer;
[3429]267    #endregion
268
269    [StorableConstructor]
[16565]270    private IslandOffspringSelectionGeneticAlgorithm(StorableConstructorFlag _) : base(_) { }
[4722]271    [StorableHook(HookType.AfterDeserialization)]
272    private void AfterDeserialization() {
[9592]273      // BackwardsCompatibility3.3
274      #region Backwards compatible code, remove with 3.4
[5683]275      if (successfulOffspringAnalyzer == null)
276        successfulOffspringAnalyzer = new SuccessfulOffspringAnalyzer();
[9569]277      if (!Parameters.ContainsKey("ReevaluateElites")) {
278        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 });
279      }
[10643]280      if (!Parameters.ContainsKey("FillPopulationWithParents"))
281        Parameters.Add(new FixedValueParameter<BoolValue>("FillPopulationWithParents", "True if the population should be filled with parent individual or false if worse children should be used when the maximum selection pressure is exceeded.", new BoolValue(false)) { Hidden = true });
[15049]282
283      var optionalMutatorParameter = MutatorParameter as OptionalConstrainedValueParameter<IManipulator>;
[17198]284      var mutatorParameter = MutatorParameter as ConstrainedValueParameter<IManipulator>;
285      if (mutatorParameter == null && optionalMutatorParameter != null) {
[15049]286        Parameters.Remove(optionalMutatorParameter);
287        Parameters.Add(new ConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
288        foreach (var m in optionalMutatorParameter.ValidValues)
289          MutatorParameter.ValidValues.Add(m);
290        if (optionalMutatorParameter.Value == null) MutationProbability.Value = 0; // to guarantee that the old configuration results in the same behavior
291        else Mutator = optionalMutatorParameter.Value;
292        optionalMutatorParameter.ValidValues.Clear(); // to avoid dangling references to the old parameter its valid values are cleared
293      }
[5683]294      #endregion
[7172]295
[4722]296      Initialize();
297    }
298    private IslandOffspringSelectionGeneticAlgorithm(IslandOffspringSelectionGeneticAlgorithm original, Cloner cloner)
299      : base(original, cloner) {
300      islandQualityAnalyzer = cloner.Clone(original.islandQualityAnalyzer);
301      qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
302      islandSelectionPressureAnalyzer = cloner.Clone(original.islandSelectionPressureAnalyzer);
303      selectionPressureAnalyzer = cloner.Clone(original.selectionPressureAnalyzer);
[5683]304      successfulOffspringAnalyzer = cloner.Clone(original.successfulOffspringAnalyzer);
[4722]305      Initialize();
306    }
307    public override IDeepCloneable Clone(Cloner cloner) {
308      return new IslandOffspringSelectionGeneticAlgorithm(this, cloner);
309    }
[3479]310    public IslandOffspringSelectionGeneticAlgorithm()
[3429]311      : base() {
312      Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
313      Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
314      Parameters.Add(new ValueParameter<IntValue>("NumberOfIslands", "The number of islands.", new IntValue(5)));
315      Parameters.Add(new ValueParameter<IntValue>("MigrationInterval", "The number of generations that should pass between migration phases.", new IntValue(20)));
316      Parameters.Add(new ValueParameter<PercentValue>("MigrationRate", "The proportion of individuals that should migrate between the islands.", new PercentValue(0.15)));
317      Parameters.Add(new ConstrainedValueParameter<IMigrator>("Migrator", "The migration strategy."));
318      Parameters.Add(new ConstrainedValueParameter<ISelector>("EmigrantsSelector", "Selects the individuals that will be migrated."));
[3698]319      Parameters.Add(new ConstrainedValueParameter<IReplacer>("ImmigrationReplacer", "Replaces part of the original population with the immigrants."));
[3479]320      Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "The size of the population of solutions of each island.", new IntValue(100)));
[3611]321      Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations that should be processed.", new IntValue(100)));
[3429]322      Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
323      Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
324      Parameters.Add(new ValueParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new PercentValue(0.05)));
[15049]325      Parameters.Add(new ConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
[3429]326      Parameters.Add(new ValueParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation.", new IntValue(1)));
[9569]327      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 });
[3429]328      Parameters.Add(new ValueLookupParameter<DoubleValue>("SuccessRatio", "The ratio of successful to total children that should be achieved.", new DoubleValue(1)));
329      Parameters.Add(new ValueLookupParameter<DoubleValue>("ComparisonFactorLowerBound", "The lower bound of the comparison factor (start).", new DoubleValue(0)));
330      Parameters.Add(new ValueLookupParameter<DoubleValue>("ComparisonFactorUpperBound", "The upper bound of the comparison factor (end).", new DoubleValue(1)));
331      Parameters.Add(new OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier>("ComparisonFactorModifier", "The operator used to modify the comparison factor.", new ItemSet<IDiscreteDoubleValueModifier>(new IDiscreteDoubleValueModifier[] { new LinearDiscreteDoubleValueModifier() }), new LinearDiscreteDoubleValueModifier()));
332      Parameters.Add(new ValueLookupParameter<DoubleValue>("MaximumSelectionPressure", "The maximum selection pressure that terminates the algorithm.", new DoubleValue(100)));
333      Parameters.Add(new ValueLookupParameter<BoolValue>("OffspringSelectionBeforeMutation", "True if the offspring selection step should be applied before mutation, false if it should be applied after mutation.", new BoolValue(false)));
[3735]334      Parameters.Add(new ValueLookupParameter<IntValue>("SelectedParents", "How much parents should be selected each time the offspring selection step is performed until the population is filled. This parameter should be about the same or twice the size of PopulationSize for smaller problems, and less for large problems.", new IntValue(200)));
[3658]335      Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze the islands.", new MultiAnalyzer()));
336      Parameters.Add(new ValueParameter<MultiAnalyzer>("IslandAnalyzer", "The operator used to analyze each island.", new MultiAnalyzer()));
[3891]337      Parameters.Add(new ValueParameter<IntValue>("MaximumEvaluatedSolutions", "The maximum number of evaluated solutions (approximately).", new IntValue(int.MaxValue)));
[10643]338      Parameters.Add(new FixedValueParameter<BoolValue>("FillPopulationWithParents", "True if the population should be filled with parent individual or false if worse children should be used when the maximum selection pressure is exceeded.", new BoolValue(true)) { Hidden = true });
[3891]339
[3429]340      RandomCreator randomCreator = new RandomCreator();
341      SubScopesCreator populationCreator = new SubScopesCreator();
342      UniformSubScopesProcessor ussp1 = new UniformSubScopesProcessor();
343      SolutionsCreator solutionsCreator = new SolutionsCreator();
[5356]344      VariableCreator variableCreator = new VariableCreator();
345      UniformSubScopesProcessor ussp2 = new UniformSubScopesProcessor();
346      SubScopesCounter subScopesCounter = new SubScopesCounter();
347      ResultsCollector resultsCollector = new ResultsCollector();
[3479]348      IslandOffspringSelectionGeneticAlgorithmMainLoop mainLoop = new IslandOffspringSelectionGeneticAlgorithmMainLoop();
[3429]349      OperatorGraph.InitialOperator = randomCreator;
350
351      randomCreator.RandomParameter.ActualName = "Random";
352      randomCreator.SeedParameter.ActualName = SeedParameter.Name;
353      randomCreator.SeedParameter.Value = null;
354      randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
355      randomCreator.SetSeedRandomlyParameter.Value = null;
356      randomCreator.Successor = populationCreator;
357
358      populationCreator.NumberOfSubScopesParameter.ActualName = NumberOfIslandsParameter.Name;
359      populationCreator.Successor = ussp1;
360
361      ussp1.Operator = solutionsCreator;
[5356]362      ussp1.Successor = variableCreator;
[3429]363
364      solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
365      solutionsCreator.Successor = null;
366
[5356]367      variableCreator.Name = "Initialize EvaluatedSolutions";
368      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("EvaluatedSolutions", new IntValue()));
369      variableCreator.Successor = ussp2;
370
371      ussp2.Operator = subScopesCounter;
372      ussp2.Successor = resultsCollector;
373
374      subScopesCounter.Name = "Increment EvaluatedSolutions";
375      subScopesCounter.ValueParameter.ActualName = "EvaluatedSolutions";
376
377      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", "", "EvaluatedSolutions"));
378      resultsCollector.ResultsParameter.ActualName = "Results";
379      resultsCollector.Successor = mainLoop;
380
[3429]381      mainLoop.EmigrantsSelectorParameter.ActualName = EmigrantsSelectorParameter.Name;
[3611]382      mainLoop.ImmigrationReplacerParameter.ActualName = ImmigrationReplacerParameter.Name;
383      mainLoop.MaximumGenerationsParameter.ActualName = MaximumGenerationsParameter.Name;
[3429]384      mainLoop.MigrationIntervalParameter.ActualName = MigrationIntervalParameter.Name;
385      mainLoop.MigrationRateParameter.ActualName = MigrationRateParameter.Name;
386      mainLoop.MigratorParameter.ActualName = MigratorParameter.Name;
387      mainLoop.NumberOfIslandsParameter.ActualName = NumberOfIslandsParameter.Name;
388      mainLoop.SelectorParameter.ActualName = SelectorParameter.Name;
389      mainLoop.CrossoverParameter.ActualName = CrossoverParameter.Name;
390      mainLoop.ElitesParameter.ActualName = ElitesParameter.Name;
[9569]391      mainLoop.ReevaluateElitesParameter.ActualName = ReevaluateElitesParameter.Name;
[3429]392      mainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
393      mainLoop.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
394      mainLoop.RandomParameter.ActualName = randomCreator.RandomParameter.ActualName;
395      mainLoop.ResultsParameter.ActualName = "Results";
396      mainLoop.SuccessRatioParameter.ActualName = SuccessRatioParameter.Name;
[3611]397      mainLoop.ComparisonFactorParameter.ActualName = "ComparisonFactor";
[3744]398      mainLoop.ComparisonFactorStartParameter.ActualName = ComparisonFactorLowerBoundParameter.Name;
[3429]399      mainLoop.ComparisonFactorModifierParameter.ActualName = ComparisonFactorModifierParameter.Name;
400      mainLoop.MaximumSelectionPressureParameter.ActualName = MaximumSelectionPressureParameter.Name;
401      mainLoop.OffspringSelectionBeforeMutationParameter.ActualName = OffspringSelectionBeforeMutationParameter.Name;
[5356]402      mainLoop.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
[10643]403      mainLoop.FillPopulationWithParentsParameter.ActualName = FillPopulationWithParentsParameter.Name;
[3429]404      mainLoop.Successor = null;
405
[3689]406      foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name))
407        SelectorParameter.ValidValues.Add(selector);
408      ISelector proportionalSelector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("ProportionalSelector"));
409      if (proportionalSelector != null) SelectorParameter.Value = proportionalSelector;
410
411      foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name))
412        EmigrantsSelectorParameter.ValidValues.Add(selector);
413
414      foreach (IReplacer replacer in ApplicationManager.Manager.GetInstances<IReplacer>().OrderBy(x => x.Name))
415        ImmigrationReplacerParameter.ValidValues.Add(replacer);
416
417      ParameterizeSelectors();
418
[13094]419      foreach (IMigrator migrator in ApplicationManager.Manager.GetInstances<IMigrator>().OrderBy(x => x.Name)) {
420        // BackwardsCompatibility3.3
421        // Set the migration direction to counterclockwise
422        var unidirectionalRing = migrator as UnidirectionalRingMigrator;
[13109]423        if (unidirectionalRing != null) unidirectionalRing.ClockwiseMigrationParameter.Value = new BoolValue(false);
[3689]424        MigratorParameter.ValidValues.Add(migrator);
[13094]425      }
[3689]426
427      foreach (IDiscreteDoubleValueModifier modifier in ApplicationManager.Manager.GetInstances<IDiscreteDoubleValueModifier>().OrderBy(x => x.Name))
428        ComparisonFactorModifierParameter.ValidValues.Add(modifier);
429      IDiscreteDoubleValueModifier linearModifier = ComparisonFactorModifierParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("LinearDiscreteDoubleValueModifier"));
430      if (linearModifier != null) ComparisonFactorModifierParameter.Value = linearModifier;
431      ParameterizeComparisonFactorModifiers();
432
433      qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
434      islandQualityAnalyzer = new BestAverageWorstQualityAnalyzer();
435      selectionPressureAnalyzer = new ValueAnalyzer();
436      islandSelectionPressureAnalyzer = new ValueAnalyzer();
[5683]437      successfulOffspringAnalyzer = new SuccessfulOffspringAnalyzer();
[3689]438      ParameterizeAnalyzers();
439      UpdateAnalyzers();
440
[3429]441      Initialize();
442    }
443    public override void Prepare() {
444      if (Problem != null) base.Prepare();
445    }
446
447    #region Events
448    protected override void OnProblemChanged() {
449      ParameterizeStochasticOperator(Problem.SolutionCreator);
450      ParameterizeStochasticOperator(Problem.Evaluator);
[7999]451      foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
[3429]452      ParameterizeSolutionsCreator();
453      ParameterizeMainLoop();
454      ParameterizeSelectors();
[3654]455      ParameterizeAnalyzers();
[3750]456      ParameterizeIterationBasedOperators();
[3429]457      UpdateCrossovers();
458      UpdateMutators();
[3654]459      UpdateAnalyzers();
[3429]460      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
461      base.OnProblemChanged();
462    }
463
464    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
465      ParameterizeStochasticOperator(Problem.SolutionCreator);
466      ParameterizeSolutionsCreator();
467      base.Problem_SolutionCreatorChanged(sender, e);
468    }
469    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
470      ParameterizeStochasticOperator(Problem.Evaluator);
471      ParameterizeSolutionsCreator();
472      ParameterizeMainLoop();
473      ParameterizeSelectors();
[3654]474      ParameterizeAnalyzers();
[3429]475      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
476      base.Problem_EvaluatorChanged(sender, e);
477    }
478    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
[7999]479      foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
[3750]480      ParameterizeIterationBasedOperators();
[3429]481      UpdateCrossovers();
482      UpdateMutators();
[3654]483      UpdateAnalyzers();
[3429]484      base.Problem_OperatorsChanged(sender, e);
485    }
486    private void ElitesParameter_ValueChanged(object sender, EventArgs e) {
487      Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
488      ParameterizeSelectors();
489    }
490    private void Elites_ValueChanged(object sender, EventArgs e) {
491      ParameterizeSelectors();
492    }
493    private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
494      NumberOfIslands.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
495      ParameterizeSelectors();
496    }
497    private void PopulationSize_ValueChanged(object sender, EventArgs e) {
498      ParameterizeSelectors();
499    }
500    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
501      ParameterizeMainLoop();
502      ParameterizeSelectors();
[3654]503      ParameterizeAnalyzers();
[3429]504    }
505    private void MigrationRateParameter_ValueChanged(object sender, EventArgs e) {
506      MigrationRate.ValueChanged += new EventHandler(MigrationRate_ValueChanged);
507      ParameterizeSelectors();
508    }
509    private void MigrationRate_ValueChanged(object sender, EventArgs e) {
510      ParameterizeSelectors();
511    }
512    private void MaximumMigrationsParameter_ValueChanged(object sender, EventArgs e) {
[3611]513      MaximumGenerations.ValueChanged += new EventHandler(MaximumMigrations_ValueChanged);
[3429]514      ParameterizeComparisonFactorModifiers();
515    }
516    private void MaximumMigrations_ValueChanged(object sender, EventArgs e) {
517      ParameterizeComparisonFactorModifiers();
518    }
519    private void MigrationIntervalParameter_ValueChanged(object sender, EventArgs e) {
520      MigrationInterval.ValueChanged += new EventHandler(MigrationInterval_ValueChanged);
521      ParameterizeComparisonFactorModifiers();
522    }
523    private void MigrationInterval_ValueChanged(object sender, EventArgs e) {
524      ParameterizeComparisonFactorModifiers();
525    }
526    #endregion
527
528    #region Helpers
529    private void Initialize() {
530      PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
531      PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
532      MigrationRateParameter.ValueChanged += new EventHandler(MigrationRateParameter_ValueChanged);
533      MigrationRate.ValueChanged += new EventHandler(MigrationRate_ValueChanged);
534      ElitesParameter.ValueChanged += new EventHandler(ElitesParameter_ValueChanged);
535      Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
536      MigrationIntervalParameter.ValueChanged += new EventHandler(MigrationIntervalParameter_ValueChanged);
537      MigrationInterval.ValueChanged += new EventHandler(MigrationInterval_ValueChanged);
[3611]538      MaximumGenerationsParameter.ValueChanged += new EventHandler(MaximumMigrationsParameter_ValueChanged);
539      MaximumGenerations.ValueChanged += new EventHandler(MaximumMigrations_ValueChanged);
[3429]540      if (Problem != null) {
541        Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
542      }
543    }
544    private void ParameterizeSolutionsCreator() {
545      SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
546      SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
547    }
548    private void ParameterizeMainLoop() {
549      MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
550      MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
551      MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
552      MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
553    }
554    private void ParameterizeStochasticOperator(IOperator op) {
555      if (op is IStochasticOperator)
556        ((IStochasticOperator)op).RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
557    }
558    private void ParameterizeSelectors() {
[3689]559      foreach (ISelector selector in SelectorParameter.ValidValues) {
[3429]560        selector.CopySelected = new BoolValue(true);
[3700]561        selector.NumberOfSelectedSubScopesParameter.Value = null;
562        selector.NumberOfSelectedSubScopesParameter.ActualName = SelectedParentsParameter.Name;
[3429]563        ParameterizeStochasticOperator(selector);
564      }
[3689]565      foreach (ISelector selector in EmigrantsSelectorParameter.ValidValues) {
[3429]566        selector.CopySelected = new BoolValue(true);
567        selector.NumberOfSelectedSubScopesParameter.Value = new IntValue((int)Math.Ceiling(PopulationSize.Value * MigrationRate.Value));
568        ParameterizeStochasticOperator(selector);
569      }
[3689]570      foreach (IReplacer selector in ImmigrationReplacerParameter.ValidValues) {
[3429]571        ParameterizeStochasticOperator(selector);
572      }
573      if (Problem != null) {
[3689]574        foreach (ISingleObjectiveSelector selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
[3429]575          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
576          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
577        }
[3689]578        foreach (ISingleObjectiveSelector selector in EmigrantsSelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
[3429]579          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
580          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
581        }
[3689]582        foreach (ISingleObjectiveReplacer replacer in ImmigrationReplacerParameter.ValidValues.OfType<ISingleObjectiveReplacer>()) {
[3611]583          replacer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
584          replacer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
[3429]585        }
586      }
587    }
[3654]588    private void ParameterizeAnalyzers() {
589      islandQualityAnalyzer.ResultsParameter.ActualName = "Results";
[3673]590      islandQualityAnalyzer.QualityParameter.Depth = 1;
591      qualityAnalyzer.ResultsParameter.ActualName = "Results";
592      qualityAnalyzer.QualityParameter.Depth = 2;
593
594      islandSelectionPressureAnalyzer.ResultsParameter.ActualName = "Results";
595      islandSelectionPressureAnalyzer.Name = "SelectionPressure Analyzer";
596      islandSelectionPressureAnalyzer.ValueParameter.Depth = 0;
597      islandSelectionPressureAnalyzer.ValueParameter.ActualName = "SelectionPressure";
598      islandSelectionPressureAnalyzer.ValuesParameter.ActualName = "Selection Pressure History";
599
600      selectionPressureAnalyzer.ResultsParameter.ActualName = "Results";
601      selectionPressureAnalyzer.Name = "SelectionPressure Analyzer";
602      selectionPressureAnalyzer.ValueParameter.Depth = 1;
603      selectionPressureAnalyzer.ValueParameter.ActualName = "SelectionPressure";
604      selectionPressureAnalyzer.ValuesParameter.ActualName = "Selection Pressure History";
605
[5683]606      successfulOffspringAnalyzer.ResultsParameter.ActualName = "Results";
607      successfulOffspringAnalyzer.GenerationsParameter.ActualName = "Generations";
[5725]608      successfulOffspringAnalyzer.SuccessfulOffspringFlagParameter.Value.Value = "SuccessfulOffspring";
[5683]609      successfulOffspringAnalyzer.DepthParameter.Value = new IntValue(2);
610
[3654]611      if (Problem != null) {
612        islandQualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
613        islandQualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
614        islandQualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
[3673]615
616        qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
617        qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
618        qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
[3654]619      }
620    }
[3429]621    private void ParameterizeComparisonFactorModifiers() {
[3689]622      foreach (IDiscreteDoubleValueModifier modifier in ComparisonFactorModifierParameter.ValidValues) {
[3429]623        modifier.IndexParameter.ActualName = "Generations";
[3611]624        modifier.EndIndexParameter.Value = new IntValue(MigrationInterval.Value * MaximumGenerations.Value);
[3429]625        modifier.EndValueParameter.ActualName = ComparisonFactorUpperBoundParameter.Name;
626        modifier.StartIndexParameter.Value = new IntValue(0);
627        modifier.StartValueParameter.ActualName = ComparisonFactorLowerBoundParameter.Name;
628        modifier.ValueParameter.ActualName = "ComparisonFactor";
629      }
630    }
[3750]631    private void ParameterizeIterationBasedOperators() {
632      if (Problem != null) {
633        foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {
634          op.IterationsParameter.ActualName = "Generations";
635          op.MaximumIterationsParameter.ActualName = MaximumGenerationsParameter.Name;
636        }
637      }
638    }
[3429]639    private void UpdateCrossovers() {
640      ICrossover oldCrossover = CrossoverParameter.Value;
[7511]641      ICrossover defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
[3429]642      CrossoverParameter.ValidValues.Clear();
643      foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name))
644        CrossoverParameter.ValidValues.Add(crossover);
645      if (oldCrossover != null) {
646        ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
647        if (crossover != null) CrossoverParameter.Value = crossover;
[7511]648        else oldCrossover = null;
[3429]649      }
[7511]650      if (oldCrossover == null && defaultCrossover != null)
651        CrossoverParameter.Value = defaultCrossover;
[3429]652    }
653    private void UpdateMutators() {
654      IManipulator oldMutator = MutatorParameter.Value;
655      MutatorParameter.ValidValues.Clear();
[15049]656      IManipulator defaultMutator = Problem.Operators.OfType<IManipulator>().FirstOrDefault();
657
[3429]658      foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
659        MutatorParameter.ValidValues.Add(mutator);
[15049]660
[3429]661      if (oldMutator != null) {
662        IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
663        if (mutator != null) MutatorParameter.Value = mutator;
[15049]664        else oldMutator = null;
[3429]665      }
[15049]666
667      if (oldMutator == null && defaultMutator != null)
668        MutatorParameter.Value = defaultMutator;
[3429]669    }
[3654]670    private void UpdateAnalyzers() {
671      IslandAnalyzer.Operators.Clear();
672      Analyzer.Operators.Clear();
[7172]673      IslandAnalyzer.Operators.Add(islandQualityAnalyzer, islandQualityAnalyzer.EnabledByDefault);
674      IslandAnalyzer.Operators.Add(islandSelectionPressureAnalyzer, islandSelectionPressureAnalyzer.EnabledByDefault);
[3654]675      if (Problem != null) {
[3816]676        foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
[3673]677          foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
678            param.Depth = 2;
[7172]679          Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
[3654]680        }
681      }
[7172]682      Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
683      Analyzer.Operators.Add(selectionPressureAnalyzer, selectionPressureAnalyzer.EnabledByDefault);
684      Analyzer.Operators.Add(successfulOffspringAnalyzer, successfulOffspringAnalyzer.EnabledByDefault);
[3654]685    }
[5366]686    private IslandOffspringSelectionGeneticAlgorithmMainLoop FindMainLoop(IOperator start) {
687      IOperator mainLoop = start;
688      while (mainLoop != null && !(mainLoop is IslandOffspringSelectionGeneticAlgorithmMainLoop))
689        mainLoop = ((SingleSuccessorOperator)mainLoop).Successor;
690      if (mainLoop == null) return null;
691      else return (IslandOffspringSelectionGeneticAlgorithmMainLoop)mainLoop;
692    }
[3429]693    #endregion
694  }
695}
Note: See TracBrowser for help on using the repository browser.