Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7999 was 7999, checked in by ascheibe, 12 years ago

#1864

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