Free cookie consent management tool by TermsFeed Policy Generator

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

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

Sorted usings and removed unused usings in entire solution (#1094)

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