Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm/3.3/IslandOffspringSelectionGeneticAlgorithm.cs @ 10355

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

Merged cloning refactoring branch back into trunk (#922)

File size: 32.6 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    [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      IslandOffspringSelectionGeneticAlgorithmMainLoop mainLoop = new IslandOffspringSelectionGeneticAlgorithmMainLoop();
303      OperatorGraph.InitialOperator = randomCreator;
304
305      randomCreator.RandomParameter.ActualName = "Random";
306      randomCreator.SeedParameter.ActualName = SeedParameter.Name;
307      randomCreator.SeedParameter.Value = null;
308      randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
309      randomCreator.SetSeedRandomlyParameter.Value = null;
310      randomCreator.Successor = populationCreator;
311
312      populationCreator.NumberOfSubScopesParameter.ActualName = NumberOfIslandsParameter.Name;
313      populationCreator.Successor = ussp1;
314
315      ussp1.Operator = solutionsCreator;
316      ussp1.Successor = mainLoop;
317
318      solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
319      solutionsCreator.Successor = null;
320
321      mainLoop.EmigrantsSelectorParameter.ActualName = EmigrantsSelectorParameter.Name;
322      mainLoop.ImmigrationReplacerParameter.ActualName = ImmigrationReplacerParameter.Name;
323      mainLoop.MaximumGenerationsParameter.ActualName = MaximumGenerationsParameter.Name;
324      mainLoop.MigrationIntervalParameter.ActualName = MigrationIntervalParameter.Name;
325      mainLoop.MigrationRateParameter.ActualName = MigrationRateParameter.Name;
326      mainLoop.MigratorParameter.ActualName = MigratorParameter.Name;
327      mainLoop.NumberOfIslandsParameter.ActualName = NumberOfIslandsParameter.Name;
328      mainLoop.SelectorParameter.ActualName = SelectorParameter.Name;
329      mainLoop.CrossoverParameter.ActualName = CrossoverParameter.Name;
330      mainLoop.ElitesParameter.ActualName = ElitesParameter.Name;
331      mainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
332      mainLoop.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
333      mainLoop.RandomParameter.ActualName = randomCreator.RandomParameter.ActualName;
334      mainLoop.ResultsParameter.ActualName = "Results";
335      mainLoop.SuccessRatioParameter.ActualName = SuccessRatioParameter.Name;
336      mainLoop.ComparisonFactorParameter.ActualName = "ComparisonFactor";
337      mainLoop.ComparisonFactorStartParameter.ActualName = ComparisonFactorLowerBoundParameter.Name;
338      mainLoop.ComparisonFactorModifierParameter.ActualName = ComparisonFactorModifierParameter.Name;
339      mainLoop.MaximumSelectionPressureParameter.ActualName = MaximumSelectionPressureParameter.Name;
340      mainLoop.OffspringSelectionBeforeMutationParameter.ActualName = OffspringSelectionBeforeMutationParameter.Name;
341      mainLoop.Successor = null;
342
343      foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name))
344        SelectorParameter.ValidValues.Add(selector);
345      ISelector proportionalSelector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("ProportionalSelector"));
346      if (proportionalSelector != null) SelectorParameter.Value = proportionalSelector;
347
348      foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name))
349        EmigrantsSelectorParameter.ValidValues.Add(selector);
350
351      foreach (IReplacer replacer in ApplicationManager.Manager.GetInstances<IReplacer>().OrderBy(x => x.Name))
352        ImmigrationReplacerParameter.ValidValues.Add(replacer);
353
354      ParameterizeSelectors();
355
356      foreach (IMigrator migrator in ApplicationManager.Manager.GetInstances<IMigrator>().OrderBy(x => x.Name))
357        MigratorParameter.ValidValues.Add(migrator);
358
359      foreach (IDiscreteDoubleValueModifier modifier in ApplicationManager.Manager.GetInstances<IDiscreteDoubleValueModifier>().OrderBy(x => x.Name))
360        ComparisonFactorModifierParameter.ValidValues.Add(modifier);
361      IDiscreteDoubleValueModifier linearModifier = ComparisonFactorModifierParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("LinearDiscreteDoubleValueModifier"));
362      if (linearModifier != null) ComparisonFactorModifierParameter.Value = linearModifier;
363      ParameterizeComparisonFactorModifiers();
364
365      qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
366      islandQualityAnalyzer = new BestAverageWorstQualityAnalyzer();
367      selectionPressureAnalyzer = new ValueAnalyzer();
368      islandSelectionPressureAnalyzer = new ValueAnalyzer();
369      ParameterizeAnalyzers();
370      UpdateAnalyzers();
371
372      Initialize();
373    }
374    public override void Prepare() {
375      if (Problem != null) base.Prepare();
376    }
377
378    #region Events
379    protected override void OnProblemChanged() {
380      ParameterizeStochasticOperator(Problem.SolutionCreator);
381      ParameterizeStochasticOperator(Problem.Evaluator);
382      foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
383      ParameterizeSolutionsCreator();
384      ParameterizeMainLoop();
385      ParameterizeSelectors();
386      ParameterizeAnalyzers();
387      ParameterizeIterationBasedOperators();
388      UpdateCrossovers();
389      UpdateMutators();
390      UpdateAnalyzers();
391      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
392      base.OnProblemChanged();
393    }
394
395    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
396      ParameterizeStochasticOperator(Problem.SolutionCreator);
397      ParameterizeSolutionsCreator();
398      base.Problem_SolutionCreatorChanged(sender, e);
399    }
400    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
401      ParameterizeStochasticOperator(Problem.Evaluator);
402      ParameterizeSolutionsCreator();
403      ParameterizeMainLoop();
404      ParameterizeSelectors();
405      ParameterizeAnalyzers();
406      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
407      base.Problem_EvaluatorChanged(sender, e);
408    }
409    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
410      foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
411      ParameterizeIterationBasedOperators();
412      UpdateCrossovers();
413      UpdateMutators();
414      UpdateAnalyzers();
415      base.Problem_OperatorsChanged(sender, e);
416    }
417    private void ElitesParameter_ValueChanged(object sender, EventArgs e) {
418      Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
419      ParameterizeSelectors();
420    }
421    private void Elites_ValueChanged(object sender, EventArgs e) {
422      ParameterizeSelectors();
423    }
424    private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
425      NumberOfIslands.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
426      ParameterizeSelectors();
427    }
428    private void PopulationSize_ValueChanged(object sender, EventArgs e) {
429      ParameterizeSelectors();
430    }
431    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
432      ParameterizeMainLoop();
433      ParameterizeSelectors();
434      ParameterizeAnalyzers();
435    }
436    private void MigrationRateParameter_ValueChanged(object sender, EventArgs e) {
437      MigrationRate.ValueChanged += new EventHandler(MigrationRate_ValueChanged);
438      ParameterizeSelectors();
439    }
440    private void MigrationRate_ValueChanged(object sender, EventArgs e) {
441      ParameterizeSelectors();
442    }
443    private void MaximumMigrationsParameter_ValueChanged(object sender, EventArgs e) {
444      MaximumGenerations.ValueChanged += new EventHandler(MaximumMigrations_ValueChanged);
445      ParameterizeComparisonFactorModifiers();
446    }
447    private void MaximumMigrations_ValueChanged(object sender, EventArgs e) {
448      ParameterizeComparisonFactorModifiers();
449    }
450    private void MigrationIntervalParameter_ValueChanged(object sender, EventArgs e) {
451      MigrationInterval.ValueChanged += new EventHandler(MigrationInterval_ValueChanged);
452      ParameterizeComparisonFactorModifiers();
453    }
454    private void MigrationInterval_ValueChanged(object sender, EventArgs e) {
455      ParameterizeComparisonFactorModifiers();
456    }
457    #endregion
458
459    #region Helpers
460    private void Initialize() {
461      PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
462      PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
463      MigrationRateParameter.ValueChanged += new EventHandler(MigrationRateParameter_ValueChanged);
464      MigrationRate.ValueChanged += new EventHandler(MigrationRate_ValueChanged);
465      ElitesParameter.ValueChanged += new EventHandler(ElitesParameter_ValueChanged);
466      Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
467      MigrationIntervalParameter.ValueChanged += new EventHandler(MigrationIntervalParameter_ValueChanged);
468      MigrationInterval.ValueChanged += new EventHandler(MigrationInterval_ValueChanged);
469      MaximumGenerationsParameter.ValueChanged += new EventHandler(MaximumMigrationsParameter_ValueChanged);
470      MaximumGenerations.ValueChanged += new EventHandler(MaximumMigrations_ValueChanged);
471      if (Problem != null) {
472        Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
473      }
474    }
475    private void ParameterizeSolutionsCreator() {
476      SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
477      SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
478    }
479    private void ParameterizeMainLoop() {
480      MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
481      MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
482      MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
483      MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
484    }
485    private void ParameterizeStochasticOperator(IOperator op) {
486      if (op is IStochasticOperator)
487        ((IStochasticOperator)op).RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
488    }
489    private void ParameterizeSelectors() {
490      foreach (ISelector selector in SelectorParameter.ValidValues) {
491        selector.CopySelected = new BoolValue(true);
492        selector.NumberOfSelectedSubScopesParameter.Value = null;
493        selector.NumberOfSelectedSubScopesParameter.ActualName = SelectedParentsParameter.Name;
494        ParameterizeStochasticOperator(selector);
495      }
496      foreach (ISelector selector in EmigrantsSelectorParameter.ValidValues) {
497        selector.CopySelected = new BoolValue(true);
498        selector.NumberOfSelectedSubScopesParameter.Value = new IntValue((int)Math.Ceiling(PopulationSize.Value * MigrationRate.Value));
499        ParameterizeStochasticOperator(selector);
500      }
501      foreach (IReplacer selector in ImmigrationReplacerParameter.ValidValues) {
502        ParameterizeStochasticOperator(selector);
503      }
504      if (Problem != null) {
505        foreach (ISingleObjectiveSelector selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
506          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
507          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
508        }
509        foreach (ISingleObjectiveSelector selector in EmigrantsSelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
510          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
511          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
512        }
513        foreach (ISingleObjectiveReplacer replacer in ImmigrationReplacerParameter.ValidValues.OfType<ISingleObjectiveReplacer>()) {
514          replacer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
515          replacer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
516        }
517      }
518    }
519    private void ParameterizeAnalyzers() {
520      islandQualityAnalyzer.ResultsParameter.ActualName = "Results";
521      islandQualityAnalyzer.QualityParameter.Depth = 1;
522      qualityAnalyzer.ResultsParameter.ActualName = "Results";
523      qualityAnalyzer.QualityParameter.Depth = 2;
524
525      islandSelectionPressureAnalyzer.ResultsParameter.ActualName = "Results";
526      islandSelectionPressureAnalyzer.Name = "SelectionPressure Analyzer";
527      islandSelectionPressureAnalyzer.ValueParameter.Depth = 0;
528      islandSelectionPressureAnalyzer.ValueParameter.ActualName = "SelectionPressure";
529      islandSelectionPressureAnalyzer.ValuesParameter.ActualName = "Selection Pressure History";
530
531      selectionPressureAnalyzer.ResultsParameter.ActualName = "Results";
532      selectionPressureAnalyzer.Name = "SelectionPressure Analyzer";
533      selectionPressureAnalyzer.ValueParameter.Depth = 1;
534      selectionPressureAnalyzer.ValueParameter.ActualName = "SelectionPressure";
535      selectionPressureAnalyzer.ValuesParameter.ActualName = "Selection Pressure History";
536
537      if (Problem != null) {
538        islandQualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
539        islandQualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
540        islandQualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
541
542        qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
543        qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
544        qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
545      }
546    }
547    private void ParameterizeComparisonFactorModifiers() {
548      foreach (IDiscreteDoubleValueModifier modifier in ComparisonFactorModifierParameter.ValidValues) {
549        modifier.IndexParameter.ActualName = "Generations";
550        modifier.EndIndexParameter.Value = new IntValue(MigrationInterval.Value * MaximumGenerations.Value);
551        modifier.EndValueParameter.ActualName = ComparisonFactorUpperBoundParameter.Name;
552        modifier.StartIndexParameter.Value = new IntValue(0);
553        modifier.StartValueParameter.ActualName = ComparisonFactorLowerBoundParameter.Name;
554        modifier.ValueParameter.ActualName = "ComparisonFactor";
555      }
556    }
557    private void ParameterizeIterationBasedOperators() {
558      if (Problem != null) {
559        foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {
560          op.IterationsParameter.ActualName = "Generations";
561          op.MaximumIterationsParameter.ActualName = MaximumGenerationsParameter.Name;
562        }
563      }
564    }
565    private void UpdateCrossovers() {
566      ICrossover oldCrossover = CrossoverParameter.Value;
567      CrossoverParameter.ValidValues.Clear();
568      foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name))
569        CrossoverParameter.ValidValues.Add(crossover);
570      if (oldCrossover != null) {
571        ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
572        if (crossover != null) CrossoverParameter.Value = crossover;
573      }
574    }
575    private void UpdateMutators() {
576      IManipulator oldMutator = MutatorParameter.Value;
577      MutatorParameter.ValidValues.Clear();
578      foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
579        MutatorParameter.ValidValues.Add(mutator);
580      if (oldMutator != null) {
581        IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
582        if (mutator != null) MutatorParameter.Value = mutator;
583      }
584    }
585    private void UpdateAnalyzers() {
586      IslandAnalyzer.Operators.Clear();
587      Analyzer.Operators.Clear();
588      IslandAnalyzer.Operators.Add(islandQualityAnalyzer);
589      IslandAnalyzer.Operators.Add(islandSelectionPressureAnalyzer);
590      if (Problem != null) {
591        foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
592          foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
593            param.Depth = 2;
594          Analyzer.Operators.Add(analyzer);
595        }
596      }
597      Analyzer.Operators.Add(qualityAnalyzer);
598      Analyzer.Operators.Add(selectionPressureAnalyzer);
599    }
600    #endregion
601  }
602}
Note: See TracBrowser for help on using the repository browser.