Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5468 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 34.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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 : EngineAlgorithm, IStorableContent {
44    public string Filename { get; set; }
45
46    #region Problem Properties
47    public override Type ProblemType {
48      get { return typeof(ISingleObjectiveProblem); }
49    }
50    public new ISingleObjectiveProblem Problem {
51      get { return (ISingleObjectiveProblem)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    #endregion
252
253    [StorableConstructor]
254    private IslandOffspringSelectionGeneticAlgorithm(bool deserializing) : base(deserializing) { }
255    [StorableHook(HookType.AfterDeserialization)]
256    private void AfterDeserialization() {
257      Initialize();
258    }
259    private IslandOffspringSelectionGeneticAlgorithm(IslandOffspringSelectionGeneticAlgorithm original, Cloner cloner)
260      : base(original, cloner) {
261      islandQualityAnalyzer = cloner.Clone(original.islandQualityAnalyzer);
262      qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
263      islandSelectionPressureAnalyzer = cloner.Clone(original.islandSelectionPressureAnalyzer);
264      selectionPressureAnalyzer = cloner.Clone(original.selectionPressureAnalyzer);
265      Initialize();
266    }
267    public override IDeepCloneable Clone(Cloner cloner) {
268      return new IslandOffspringSelectionGeneticAlgorithm(this, cloner);
269    }
270    public IslandOffspringSelectionGeneticAlgorithm()
271      : base() {
272      Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
273      Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
274      Parameters.Add(new ValueParameter<IntValue>("NumberOfIslands", "The number of islands.", new IntValue(5)));
275      Parameters.Add(new ValueParameter<IntValue>("MigrationInterval", "The number of generations that should pass between migration phases.", new IntValue(20)));
276      Parameters.Add(new ValueParameter<PercentValue>("MigrationRate", "The proportion of individuals that should migrate between the islands.", new PercentValue(0.15)));
277      Parameters.Add(new ConstrainedValueParameter<IMigrator>("Migrator", "The migration strategy."));
278      Parameters.Add(new ConstrainedValueParameter<ISelector>("EmigrantsSelector", "Selects the individuals that will be migrated."));
279      Parameters.Add(new ConstrainedValueParameter<IReplacer>("ImmigrationReplacer", "Replaces part of the original population with the immigrants."));
280      Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "The size of the population of solutions of each island.", new IntValue(100)));
281      Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations that should be processed.", new IntValue(100)));
282      Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
283      Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
284      Parameters.Add(new ValueParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new PercentValue(0.05)));
285      Parameters.Add(new OptionalConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
286      Parameters.Add(new ValueParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation.", new IntValue(1)));
287      Parameters.Add(new ValueLookupParameter<DoubleValue>("SuccessRatio", "The ratio of successful to total children that should be achieved.", new DoubleValue(1)));
288      Parameters.Add(new ValueLookupParameter<DoubleValue>("ComparisonFactorLowerBound", "The lower bound of the comparison factor (start).", new DoubleValue(0)));
289      Parameters.Add(new ValueLookupParameter<DoubleValue>("ComparisonFactorUpperBound", "The upper bound of the comparison factor (end).", new DoubleValue(1)));
290      Parameters.Add(new OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier>("ComparisonFactorModifier", "The operator used to modify the comparison factor.", new ItemSet<IDiscreteDoubleValueModifier>(new IDiscreteDoubleValueModifier[] { new LinearDiscreteDoubleValueModifier() }), new LinearDiscreteDoubleValueModifier()));
291      Parameters.Add(new ValueLookupParameter<DoubleValue>("MaximumSelectionPressure", "The maximum selection pressure that terminates the algorithm.", new DoubleValue(100)));
292      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)));
293      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)));
294      Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze the islands.", new MultiAnalyzer()));
295      Parameters.Add(new ValueParameter<MultiAnalyzer>("IslandAnalyzer", "The operator used to analyze each island.", new MultiAnalyzer()));
296      Parameters.Add(new ValueParameter<IntValue>("MaximumEvaluatedSolutions", "The maximum number of evaluated solutions (approximately).", new IntValue(int.MaxValue)));
297
298      RandomCreator randomCreator = new RandomCreator();
299      SubScopesCreator populationCreator = new SubScopesCreator();
300      UniformSubScopesProcessor ussp1 = new UniformSubScopesProcessor();
301      SolutionsCreator solutionsCreator = new SolutionsCreator();
302      VariableCreator variableCreator = new VariableCreator();
303      UniformSubScopesProcessor ussp2 = new UniformSubScopesProcessor();
304      SubScopesCounter subScopesCounter = new SubScopesCounter();
305      ResultsCollector resultsCollector = new ResultsCollector();
306      IslandOffspringSelectionGeneticAlgorithmMainLoop mainLoop = new IslandOffspringSelectionGeneticAlgorithmMainLoop();
307      OperatorGraph.InitialOperator = randomCreator;
308
309      randomCreator.RandomParameter.ActualName = "Random";
310      randomCreator.SeedParameter.ActualName = SeedParameter.Name;
311      randomCreator.SeedParameter.Value = null;
312      randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
313      randomCreator.SetSeedRandomlyParameter.Value = null;
314      randomCreator.Successor = populationCreator;
315
316      populationCreator.NumberOfSubScopesParameter.ActualName = NumberOfIslandsParameter.Name;
317      populationCreator.Successor = ussp1;
318
319      ussp1.Operator = solutionsCreator;
320      ussp1.Successor = variableCreator;
321
322      solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
323      solutionsCreator.Successor = null;
324
325      variableCreator.Name = "Initialize EvaluatedSolutions";
326      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("EvaluatedSolutions", new IntValue()));
327      variableCreator.Successor = ussp2;
328
329      ussp2.Operator = subScopesCounter;
330      ussp2.Successor = resultsCollector;
331
332      subScopesCounter.Name = "Increment EvaluatedSolutions";
333      subScopesCounter.ValueParameter.ActualName = "EvaluatedSolutions";
334
335      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", "", "EvaluatedSolutions"));
336      resultsCollector.ResultsParameter.ActualName = "Results";
337      resultsCollector.Successor = mainLoop;
338
339      mainLoop.EmigrantsSelectorParameter.ActualName = EmigrantsSelectorParameter.Name;
340      mainLoop.ImmigrationReplacerParameter.ActualName = ImmigrationReplacerParameter.Name;
341      mainLoop.MaximumGenerationsParameter.ActualName = MaximumGenerationsParameter.Name;
342      mainLoop.MigrationIntervalParameter.ActualName = MigrationIntervalParameter.Name;
343      mainLoop.MigrationRateParameter.ActualName = MigrationRateParameter.Name;
344      mainLoop.MigratorParameter.ActualName = MigratorParameter.Name;
345      mainLoop.NumberOfIslandsParameter.ActualName = NumberOfIslandsParameter.Name;
346      mainLoop.SelectorParameter.ActualName = SelectorParameter.Name;
347      mainLoop.CrossoverParameter.ActualName = CrossoverParameter.Name;
348      mainLoop.ElitesParameter.ActualName = ElitesParameter.Name;
349      mainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
350      mainLoop.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
351      mainLoop.RandomParameter.ActualName = randomCreator.RandomParameter.ActualName;
352      mainLoop.ResultsParameter.ActualName = "Results";
353      mainLoop.SuccessRatioParameter.ActualName = SuccessRatioParameter.Name;
354      mainLoop.ComparisonFactorParameter.ActualName = "ComparisonFactor";
355      mainLoop.ComparisonFactorStartParameter.ActualName = ComparisonFactorLowerBoundParameter.Name;
356      mainLoop.ComparisonFactorModifierParameter.ActualName = ComparisonFactorModifierParameter.Name;
357      mainLoop.MaximumSelectionPressureParameter.ActualName = MaximumSelectionPressureParameter.Name;
358      mainLoop.OffspringSelectionBeforeMutationParameter.ActualName = OffspringSelectionBeforeMutationParameter.Name;
359      mainLoop.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
360      mainLoop.Successor = null;
361
362      foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name))
363        SelectorParameter.ValidValues.Add(selector);
364      ISelector proportionalSelector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("ProportionalSelector"));
365      if (proportionalSelector != null) SelectorParameter.Value = proportionalSelector;
366
367      foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name))
368        EmigrantsSelectorParameter.ValidValues.Add(selector);
369
370      foreach (IReplacer replacer in ApplicationManager.Manager.GetInstances<IReplacer>().OrderBy(x => x.Name))
371        ImmigrationReplacerParameter.ValidValues.Add(replacer);
372
373      ParameterizeSelectors();
374
375      foreach (IMigrator migrator in ApplicationManager.Manager.GetInstances<IMigrator>().OrderBy(x => x.Name))
376        MigratorParameter.ValidValues.Add(migrator);
377
378      foreach (IDiscreteDoubleValueModifier modifier in ApplicationManager.Manager.GetInstances<IDiscreteDoubleValueModifier>().OrderBy(x => x.Name))
379        ComparisonFactorModifierParameter.ValidValues.Add(modifier);
380      IDiscreteDoubleValueModifier linearModifier = ComparisonFactorModifierParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("LinearDiscreteDoubleValueModifier"));
381      if (linearModifier != null) ComparisonFactorModifierParameter.Value = linearModifier;
382      ParameterizeComparisonFactorModifiers();
383
384      qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
385      islandQualityAnalyzer = new BestAverageWorstQualityAnalyzer();
386      selectionPressureAnalyzer = new ValueAnalyzer();
387      islandSelectionPressureAnalyzer = new ValueAnalyzer();
388      ParameterizeAnalyzers();
389      UpdateAnalyzers();
390
391      Initialize();
392    }
393    public override void Prepare() {
394      if (Problem != null) base.Prepare();
395    }
396
397    #region Events
398    protected override void OnProblemChanged() {
399      ParameterizeStochasticOperator(Problem.SolutionCreator);
400      ParameterizeStochasticOperator(Problem.Evaluator);
401      foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
402      ParameterizeSolutionsCreator();
403      ParameterizeMainLoop();
404      ParameterizeSelectors();
405      ParameterizeAnalyzers();
406      ParameterizeIterationBasedOperators();
407      UpdateCrossovers();
408      UpdateMutators();
409      UpdateAnalyzers();
410      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
411      base.OnProblemChanged();
412    }
413
414    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
415      ParameterizeStochasticOperator(Problem.SolutionCreator);
416      ParameterizeSolutionsCreator();
417      base.Problem_SolutionCreatorChanged(sender, e);
418    }
419    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
420      ParameterizeStochasticOperator(Problem.Evaluator);
421      ParameterizeSolutionsCreator();
422      ParameterizeMainLoop();
423      ParameterizeSelectors();
424      ParameterizeAnalyzers();
425      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
426      base.Problem_EvaluatorChanged(sender, e);
427    }
428    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
429      foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
430      ParameterizeIterationBasedOperators();
431      UpdateCrossovers();
432      UpdateMutators();
433      UpdateAnalyzers();
434      base.Problem_OperatorsChanged(sender, e);
435    }
436    private void ElitesParameter_ValueChanged(object sender, EventArgs e) {
437      Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
438      ParameterizeSelectors();
439    }
440    private void Elites_ValueChanged(object sender, EventArgs e) {
441      ParameterizeSelectors();
442    }
443    private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
444      NumberOfIslands.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
445      ParameterizeSelectors();
446    }
447    private void PopulationSize_ValueChanged(object sender, EventArgs e) {
448      ParameterizeSelectors();
449    }
450    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
451      ParameterizeMainLoop();
452      ParameterizeSelectors();
453      ParameterizeAnalyzers();
454    }
455    private void MigrationRateParameter_ValueChanged(object sender, EventArgs e) {
456      MigrationRate.ValueChanged += new EventHandler(MigrationRate_ValueChanged);
457      ParameterizeSelectors();
458    }
459    private void MigrationRate_ValueChanged(object sender, EventArgs e) {
460      ParameterizeSelectors();
461    }
462    private void MaximumMigrationsParameter_ValueChanged(object sender, EventArgs e) {
463      MaximumGenerations.ValueChanged += new EventHandler(MaximumMigrations_ValueChanged);
464      ParameterizeComparisonFactorModifiers();
465    }
466    private void MaximumMigrations_ValueChanged(object sender, EventArgs e) {
467      ParameterizeComparisonFactorModifiers();
468    }
469    private void MigrationIntervalParameter_ValueChanged(object sender, EventArgs e) {
470      MigrationInterval.ValueChanged += new EventHandler(MigrationInterval_ValueChanged);
471      ParameterizeComparisonFactorModifiers();
472    }
473    private void MigrationInterval_ValueChanged(object sender, EventArgs e) {
474      ParameterizeComparisonFactorModifiers();
475    }
476    #endregion
477
478    #region Helpers
479    private void Initialize() {
480      PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
481      PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
482      MigrationRateParameter.ValueChanged += new EventHandler(MigrationRateParameter_ValueChanged);
483      MigrationRate.ValueChanged += new EventHandler(MigrationRate_ValueChanged);
484      ElitesParameter.ValueChanged += new EventHandler(ElitesParameter_ValueChanged);
485      Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
486      MigrationIntervalParameter.ValueChanged += new EventHandler(MigrationIntervalParameter_ValueChanged);
487      MigrationInterval.ValueChanged += new EventHandler(MigrationInterval_ValueChanged);
488      MaximumGenerationsParameter.ValueChanged += new EventHandler(MaximumMigrationsParameter_ValueChanged);
489      MaximumGenerations.ValueChanged += new EventHandler(MaximumMigrations_ValueChanged);
490      if (Problem != null) {
491        Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
492      }
493    }
494    private void ParameterizeSolutionsCreator() {
495      SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
496      SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
497    }
498    private void ParameterizeMainLoop() {
499      MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
500      MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
501      MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
502      MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
503    }
504    private void ParameterizeStochasticOperator(IOperator op) {
505      if (op is IStochasticOperator)
506        ((IStochasticOperator)op).RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
507    }
508    private void ParameterizeSelectors() {
509      foreach (ISelector selector in SelectorParameter.ValidValues) {
510        selector.CopySelected = new BoolValue(true);
511        selector.NumberOfSelectedSubScopesParameter.Value = null;
512        selector.NumberOfSelectedSubScopesParameter.ActualName = SelectedParentsParameter.Name;
513        ParameterizeStochasticOperator(selector);
514      }
515      foreach (ISelector selector in EmigrantsSelectorParameter.ValidValues) {
516        selector.CopySelected = new BoolValue(true);
517        selector.NumberOfSelectedSubScopesParameter.Value = new IntValue((int)Math.Ceiling(PopulationSize.Value * MigrationRate.Value));
518        ParameterizeStochasticOperator(selector);
519      }
520      foreach (IReplacer selector in ImmigrationReplacerParameter.ValidValues) {
521        ParameterizeStochasticOperator(selector);
522      }
523      if (Problem != null) {
524        foreach (ISingleObjectiveSelector selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
525          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
526          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
527        }
528        foreach (ISingleObjectiveSelector selector in EmigrantsSelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
529          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
530          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
531        }
532        foreach (ISingleObjectiveReplacer replacer in ImmigrationReplacerParameter.ValidValues.OfType<ISingleObjectiveReplacer>()) {
533          replacer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
534          replacer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
535        }
536      }
537    }
538    private void ParameterizeAnalyzers() {
539      islandQualityAnalyzer.ResultsParameter.ActualName = "Results";
540      islandQualityAnalyzer.QualityParameter.Depth = 1;
541      qualityAnalyzer.ResultsParameter.ActualName = "Results";
542      qualityAnalyzer.QualityParameter.Depth = 2;
543
544      islandSelectionPressureAnalyzer.ResultsParameter.ActualName = "Results";
545      islandSelectionPressureAnalyzer.Name = "SelectionPressure Analyzer";
546      islandSelectionPressureAnalyzer.ValueParameter.Depth = 0;
547      islandSelectionPressureAnalyzer.ValueParameter.ActualName = "SelectionPressure";
548      islandSelectionPressureAnalyzer.ValuesParameter.ActualName = "Selection Pressure History";
549
550      selectionPressureAnalyzer.ResultsParameter.ActualName = "Results";
551      selectionPressureAnalyzer.Name = "SelectionPressure Analyzer";
552      selectionPressureAnalyzer.ValueParameter.Depth = 1;
553      selectionPressureAnalyzer.ValueParameter.ActualName = "SelectionPressure";
554      selectionPressureAnalyzer.ValuesParameter.ActualName = "Selection Pressure History";
555
556      if (Problem != null) {
557        islandQualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
558        islandQualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
559        islandQualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
560
561        qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
562        qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
563        qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
564      }
565    }
566    private void ParameterizeComparisonFactorModifiers() {
567      foreach (IDiscreteDoubleValueModifier modifier in ComparisonFactorModifierParameter.ValidValues) {
568        modifier.IndexParameter.ActualName = "Generations";
569        modifier.EndIndexParameter.Value = new IntValue(MigrationInterval.Value * MaximumGenerations.Value);
570        modifier.EndValueParameter.ActualName = ComparisonFactorUpperBoundParameter.Name;
571        modifier.StartIndexParameter.Value = new IntValue(0);
572        modifier.StartValueParameter.ActualName = ComparisonFactorLowerBoundParameter.Name;
573        modifier.ValueParameter.ActualName = "ComparisonFactor";
574      }
575    }
576    private void ParameterizeIterationBasedOperators() {
577      if (Problem != null) {
578        foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {
579          op.IterationsParameter.ActualName = "Generations";
580          op.MaximumIterationsParameter.ActualName = MaximumGenerationsParameter.Name;
581        }
582      }
583    }
584    private void UpdateCrossovers() {
585      ICrossover oldCrossover = CrossoverParameter.Value;
586      CrossoverParameter.ValidValues.Clear();
587      foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name))
588        CrossoverParameter.ValidValues.Add(crossover);
589      if (oldCrossover != null) {
590        ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
591        if (crossover != null) CrossoverParameter.Value = crossover;
592      }
593    }
594    private void UpdateMutators() {
595      IManipulator oldMutator = MutatorParameter.Value;
596      MutatorParameter.ValidValues.Clear();
597      foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
598        MutatorParameter.ValidValues.Add(mutator);
599      if (oldMutator != null) {
600        IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
601        if (mutator != null) MutatorParameter.Value = mutator;
602      }
603    }
604    private void UpdateAnalyzers() {
605      IslandAnalyzer.Operators.Clear();
606      Analyzer.Operators.Clear();
607      IslandAnalyzer.Operators.Add(islandQualityAnalyzer);
608      IslandAnalyzer.Operators.Add(islandSelectionPressureAnalyzer);
609      if (Problem != null) {
610        foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
611          foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
612            param.Depth = 2;
613          Analyzer.Operators.Add(analyzer);
614        }
615      }
616      Analyzer.Operators.Add(qualityAnalyzer);
617      Analyzer.Operators.Add(selectionPressureAnalyzer);
618    }
619    private IslandOffspringSelectionGeneticAlgorithmMainLoop FindMainLoop(IOperator start) {
620      IOperator mainLoop = start;
621      while (mainLoop != null && !(mainLoop is IslandOffspringSelectionGeneticAlgorithmMainLoop))
622        mainLoop = ((SingleSuccessorOperator)mainLoop).Successor;
623      if (mainLoop == null) return null;
624      else return (IslandOffspringSelectionGeneticAlgorithmMainLoop)mainLoop;
625    }
626    #endregion
627  }
628}
Note: See TracBrowser for help on using the repository browser.