Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HLScript/HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm/3.3/IslandOffspringSelectionGeneticAlgorithm.cs @ 11813

Last change on this file since 11813 was 9592, checked in by abeham, 12 years ago

#2038: Added tagging comment

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