Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4453 was 4437, checked in by swagner, 14 years ago

Implemented !IStorableContent separately for each algorithm (#1193)

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