Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3690 was 3689, checked in by abeham, 15 years ago

#893

  • fixed wiring in the algorithms
File size: 31.7 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.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Analysis;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Operators;
30using HeuristicLab.Optimization;
31using HeuristicLab.Optimization.Operators;
32using HeuristicLab.Parameters;
33using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
34using HeuristicLab.PluginInfrastructure;
35using HeuristicLab.Random;
36
37namespace HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm {
38  /// <summary>
39  /// An offspring selection island genetic algorithm.
40  /// </summary>
41  [Item("Island Offspring Selection Genetic Algorithm", "An island offspring selection genetic algorithm.")]
42  [Creatable("Algorithms")]
43  [StorableClass]
44  public sealed class IslandOffspringSelectionGeneticAlgorithm : EngineAlgorithm {
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 ValueParameter<BoolValue> ParallelParameter {
103      get { return (ValueParameter<BoolValue>)Parameters["Parallel"]; }
104    }
105    private ValueLookupParameter<DoubleValue> SuccessRatioParameter {
106      get { return (ValueLookupParameter<DoubleValue>)Parameters["SuccessRatio"]; }
107    }
108    private ValueLookupParameter<DoubleValue> ComparisonFactorLowerBoundParameter {
109      get { return (ValueLookupParameter<DoubleValue>)Parameters["ComparisonFactorLowerBound"]; }
110    }
111    private ValueLookupParameter<DoubleValue> ComparisonFactorUpperBoundParameter {
112      get { return (ValueLookupParameter<DoubleValue>)Parameters["ComparisonFactorUpperBound"]; }
113    }
114    private OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier> ComparisonFactorModifierParameter {
115      get { return (OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier>)Parameters["ComparisonFactorModifier"]; }
116    }
117    private ValueLookupParameter<DoubleValue> MaximumSelectionPressureParameter {
118      get { return (ValueLookupParameter<DoubleValue>)Parameters["MaximumSelectionPressure"]; }
119    }
120    private ValueLookupParameter<BoolValue> OffspringSelectionBeforeMutationParameter {
121      get { return (ValueLookupParameter<BoolValue>)Parameters["OffspringSelectionBeforeMutation"]; }
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    #endregion
130
131    #region Properties
132    public IntValue Seed {
133      get { return SeedParameter.Value; }
134      set { SeedParameter.Value = value; }
135    }
136    public BoolValue SetSeedRandomly {
137      get { return SetSeedRandomlyParameter.Value; }
138      set { SetSeedRandomlyParameter.Value = value; }
139    }
140    public IntValue NumberOfIslands {
141      get { return NumberOfIslandsParameter.Value; }
142      set { NumberOfIslandsParameter.Value = value; }
143    }
144    public IntValue MigrationInterval {
145      get { return MigrationIntervalParameter.Value; }
146      set { MigrationIntervalParameter.Value = value; }
147    }
148    public PercentValue MigrationRate {
149      get { return MigrationRateParameter.Value; }
150      set { MigrationRateParameter.Value = value; }
151    }
152    public IMigrator Migrator {
153      get { return MigratorParameter.Value; }
154      set { MigratorParameter.Value = value; }
155    }
156    public ISelector EmigrantsSelector {
157      get { return EmigrantsSelectorParameter.Value; }
158      set { EmigrantsSelectorParameter.Value = value; }
159    }
160    public IReplacer ImmigrationReplacer {
161      get { return ImmigrationReplacerParameter.Value; }
162      set { ImmigrationReplacerParameter.Value = value; }
163    }
164    public IntValue PopulationSize {
165      get { return PopulationSizeParameter.Value; }
166      set { PopulationSizeParameter.Value = value; }
167    }
168    public IntValue MaximumGenerations {
169      get { return MaximumGenerationsParameter.Value; }
170      set { MaximumGenerationsParameter.Value = value; }
171    }
172    public ISelector Selector {
173      get { return SelectorParameter.Value; }
174      set { SelectorParameter.Value = value; }
175    }
176    public ICrossover Crossover {
177      get { return CrossoverParameter.Value; }
178      set { CrossoverParameter.Value = value; }
179    }
180    public PercentValue MutationProbability {
181      get { return MutationProbabilityParameter.Value; }
182      set { MutationProbabilityParameter.Value = value; }
183    }
184    public IManipulator Mutator {
185      get { return MutatorParameter.Value; }
186      set { MutatorParameter.Value = value; }
187    }
188    public IntValue Elites {
189      get { return ElitesParameter.Value; }
190      set { ElitesParameter.Value = value; }
191    }
192    public BoolValue Parallel {
193      get { return ParallelParameter.Value; }
194      set { ParallelParameter.Value = value; }
195    }
196    private DoubleValue SuccessRatio {
197      get { return SuccessRatioParameter.Value; }
198      set { SuccessRatioParameter.Value = value; }
199    }
200    private DoubleValue ComparisonFactorLowerBound {
201      get { return ComparisonFactorLowerBoundParameter.Value; }
202      set { ComparisonFactorLowerBoundParameter.Value = value; }
203    }
204    private DoubleValue ComparisonFactorUpperBound {
205      get { return ComparisonFactorUpperBoundParameter.Value; }
206      set { ComparisonFactorUpperBoundParameter.Value = value; }
207    }
208    private IDiscreteDoubleValueModifier ComparisonFactorModifier {
209      get { return ComparisonFactorModifierParameter.Value; }
210      set { ComparisonFactorModifierParameter.Value = value; }
211    }
212    private DoubleValue MaximumSelectionPressure {
213      get { return MaximumSelectionPressureParameter.Value; }
214      set { MaximumSelectionPressureParameter.Value = value; }
215    }
216    private BoolValue OffspringSelectionBeforeMutation {
217      get { return OffspringSelectionBeforeMutationParameter.Value; }
218      set { OffspringSelectionBeforeMutationParameter.Value = value; }
219    }
220    public MultiAnalyzer Analyzer {
221      get { return AnalyzerParameter.Value; }
222      set { AnalyzerParameter.Value = value; }
223    }
224    public MultiAnalyzer IslandAnalyzer {
225      get { return IslandAnalyzerParameter.Value; }
226      set { IslandAnalyzerParameter.Value = value; }
227    }
228    private RandomCreator RandomCreator {
229      get { return (RandomCreator)OperatorGraph.InitialOperator; }
230    }
231    private UniformSubScopesProcessor IslandProcessor {
232      get { return ((RandomCreator.Successor as SubScopesCreator).Successor as UniformSubScopesProcessor); }
233    }
234    private SolutionsCreator SolutionsCreator {
235      get { return (SolutionsCreator)IslandProcessor.Operator; }
236    }
237    private IslandOffspringSelectionGeneticAlgorithmMainLoop MainLoop {
238      get { return (IslandOffspringSelectionGeneticAlgorithmMainLoop)IslandProcessor.Successor; }
239    }
240    [Storable]
241    private BestAverageWorstQualityAnalyzer islandQualityAnalyzer;
242    [Storable]
243    private BestAverageWorstQualityAnalyzer qualityAnalyzer;
244    [Storable]
245    private ValueAnalyzer islandSelectionPressureAnalyzer;
246    [Storable]
247    private ValueAnalyzer selectionPressureAnalyzer;
248    #endregion
249
250    [StorableConstructor]
251    private IslandOffspringSelectionGeneticAlgorithm(bool deserializing) : base(deserializing) { }
252    public IslandOffspringSelectionGeneticAlgorithm()
253      : base() {
254      Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
255      Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
256      Parameters.Add(new ValueParameter<IntValue>("NumberOfIslands", "The number of islands.", new IntValue(5)));
257      Parameters.Add(new ValueParameter<IntValue>("MigrationInterval", "The number of generations that should pass between migration phases.", new IntValue(20)));
258      Parameters.Add(new ValueParameter<PercentValue>("MigrationRate", "The proportion of individuals that should migrate between the islands.", new PercentValue(0.15)));
259      Parameters.Add(new ConstrainedValueParameter<IMigrator>("Migrator", "The migration strategy."));
260      Parameters.Add(new ConstrainedValueParameter<ISelector>("EmigrantsSelector", "Selects the individuals that will be migrated."));
261      Parameters.Add(new ConstrainedValueParameter<IReplacer>("ImmigrationReplacer", "Selects the population from the unification of the original population and the immigrants."));
262      Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "The size of the population of solutions of each island.", new IntValue(100)));
263      Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations that should be processed.", new IntValue(100)));
264      Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
265      Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
266      Parameters.Add(new ValueParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new PercentValue(0.05)));
267      Parameters.Add(new OptionalConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
268      Parameters.Add(new ValueParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation.", new IntValue(1)));
269      Parameters.Add(new ValueParameter<BoolValue>("Parallel", "True if the islands should be run in parallel (also requires a parallel engine)", new BoolValue(false)));
270      Parameters.Add(new ValueLookupParameter<DoubleValue>("SuccessRatio", "The ratio of successful to total children that should be achieved.", new DoubleValue(1)));
271      Parameters.Add(new ValueLookupParameter<DoubleValue>("ComparisonFactorLowerBound", "The lower bound of the comparison factor (start).", new DoubleValue(0)));
272      Parameters.Add(new ValueLookupParameter<DoubleValue>("ComparisonFactorUpperBound", "The upper bound of the comparison factor (end).", new DoubleValue(1)));
273      Parameters.Add(new OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier>("ComparisonFactorModifier", "The operator used to modify the comparison factor.", new ItemSet<IDiscreteDoubleValueModifier>(new IDiscreteDoubleValueModifier[] { new LinearDiscreteDoubleValueModifier() }), new LinearDiscreteDoubleValueModifier()));
274      Parameters.Add(new ValueLookupParameter<DoubleValue>("MaximumSelectionPressure", "The maximum selection pressure that terminates the algorithm.", new DoubleValue(100)));
275      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)));
276      Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze the islands.", new MultiAnalyzer()));
277      Parameters.Add(new ValueParameter<MultiAnalyzer>("IslandAnalyzer", "The operator used to analyze each island.", new MultiAnalyzer()));
278     
279      RandomCreator randomCreator = new RandomCreator();
280      SubScopesCreator populationCreator = new SubScopesCreator();
281      UniformSubScopesProcessor ussp1 = new UniformSubScopesProcessor();
282      SolutionsCreator solutionsCreator = new SolutionsCreator();
283      IslandOffspringSelectionGeneticAlgorithmMainLoop mainLoop = new IslandOffspringSelectionGeneticAlgorithmMainLoop();
284      OperatorGraph.InitialOperator = randomCreator;
285
286      randomCreator.RandomParameter.ActualName = "Random";
287      randomCreator.SeedParameter.ActualName = SeedParameter.Name;
288      randomCreator.SeedParameter.Value = null;
289      randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
290      randomCreator.SetSeedRandomlyParameter.Value = null;
291      randomCreator.Successor = populationCreator;
292
293      populationCreator.NumberOfSubScopesParameter.ActualName = NumberOfIslandsParameter.Name;
294      populationCreator.Successor = ussp1;
295
296      ussp1.Parallel = null;
297      ussp1.ParallelParameter.ActualName = ParallelParameter.Name;
298      ussp1.Operator = solutionsCreator;
299      ussp1.Successor = mainLoop;
300
301      solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
302      solutionsCreator.Successor = null;
303
304      mainLoop.EmigrantsSelectorParameter.ActualName = EmigrantsSelectorParameter.Name;
305      mainLoop.ImmigrationReplacerParameter.ActualName = ImmigrationReplacerParameter.Name;
306      mainLoop.MaximumGenerationsParameter.ActualName = MaximumGenerationsParameter.Name;
307      mainLoop.MigrationIntervalParameter.ActualName = MigrationIntervalParameter.Name;
308      mainLoop.MigrationRateParameter.ActualName = MigrationRateParameter.Name;
309      mainLoop.MigratorParameter.ActualName = MigratorParameter.Name;
310      mainLoop.NumberOfIslandsParameter.ActualName = NumberOfIslandsParameter.Name;
311      mainLoop.SelectorParameter.ActualName = SelectorParameter.Name;
312      mainLoop.CrossoverParameter.ActualName = CrossoverParameter.Name;
313      mainLoop.ElitesParameter.ActualName = ElitesParameter.Name;
314      mainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
315      mainLoop.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
316      mainLoop.RandomParameter.ActualName = randomCreator.RandomParameter.ActualName;
317      mainLoop.ResultsParameter.ActualName = "Results";
318      mainLoop.SuccessRatioParameter.ActualName = SuccessRatioParameter.Name;
319      mainLoop.ComparisonFactorParameter.ActualName = "ComparisonFactor";
320      mainLoop.ComparisonFactorLowerBoundParameter.ActualName = ComparisonFactorLowerBoundParameter.Name;
321      mainLoop.ComparisonFactorModifierParameter.ActualName = ComparisonFactorModifierParameter.Name;
322      mainLoop.ComparisonFactorUpperBoundParameter.ActualName = ComparisonFactorUpperBoundParameter.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      UpdateCrossovers();
383      UpdateMutators();
384      UpdateAnalyzers();
385      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
386      base.OnProblemChanged();
387    }
388
389    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
390      ParameterizeStochasticOperator(Problem.SolutionCreator);
391      ParameterizeSolutionsCreator();
392      base.Problem_SolutionCreatorChanged(sender, e);
393    }
394    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
395      ParameterizeStochasticOperator(Problem.Evaluator);
396      ParameterizeSolutionsCreator();
397      ParameterizeMainLoop();
398      ParameterizeSelectors();
399      ParameterizeAnalyzers();
400      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
401      base.Problem_EvaluatorChanged(sender, e);
402    }
403    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
404      foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
405      UpdateCrossovers();
406      UpdateMutators();
407      UpdateAnalyzers();
408      base.Problem_OperatorsChanged(sender, e);
409    }
410    private void ElitesParameter_ValueChanged(object sender, EventArgs e) {
411      Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
412      ParameterizeSelectors();
413    }
414    private void Elites_ValueChanged(object sender, EventArgs e) {
415      ParameterizeSelectors();
416    }
417    private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
418      NumberOfIslands.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
419      ParameterizeSelectors();
420    }
421    private void PopulationSize_ValueChanged(object sender, EventArgs e) {
422      ParameterizeSelectors();
423    }
424    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
425      ParameterizeMainLoop();
426      ParameterizeSelectors();
427      ParameterizeAnalyzers();
428    }
429    private void MigrationRateParameter_ValueChanged(object sender, EventArgs e) {
430      MigrationRate.ValueChanged += new EventHandler(MigrationRate_ValueChanged);
431      ParameterizeSelectors();
432    }
433    private void MigrationRate_ValueChanged(object sender, EventArgs e) {
434      ParameterizeSelectors();
435    }
436    private void MaximumMigrationsParameter_ValueChanged(object sender, EventArgs e) {
437      MaximumGenerations.ValueChanged += new EventHandler(MaximumMigrations_ValueChanged);
438      ParameterizeComparisonFactorModifiers();
439    }
440    private void MaximumMigrations_ValueChanged(object sender, EventArgs e) {
441      ParameterizeComparisonFactorModifiers();
442    }
443    private void MigrationIntervalParameter_ValueChanged(object sender, EventArgs e) {
444      MigrationInterval.ValueChanged += new EventHandler(MigrationInterval_ValueChanged);
445      ParameterizeComparisonFactorModifiers();
446    }
447    private void MigrationInterval_ValueChanged(object sender, EventArgs e) {
448      ParameterizeComparisonFactorModifiers();
449    }
450    #endregion
451
452    #region Helpers
453    [StorableHook(HookType.AfterDeserialization)]
454    private void Initialize() {
455      PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
456      PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
457      MigrationRateParameter.ValueChanged += new EventHandler(MigrationRateParameter_ValueChanged);
458      MigrationRate.ValueChanged += new EventHandler(MigrationRate_ValueChanged);
459      ElitesParameter.ValueChanged += new EventHandler(ElitesParameter_ValueChanged);
460      Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
461      MigrationIntervalParameter.ValueChanged += new EventHandler(MigrationIntervalParameter_ValueChanged);
462      MigrationInterval.ValueChanged += new EventHandler(MigrationInterval_ValueChanged);
463      MaximumGenerationsParameter.ValueChanged += new EventHandler(MaximumMigrationsParameter_ValueChanged);
464      MaximumGenerations.ValueChanged += new EventHandler(MaximumMigrations_ValueChanged);
465      if (Problem != null) {
466        Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
467      }
468    }
469    private void ParameterizeSolutionsCreator() {
470      SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
471      SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
472    }
473    private void ParameterizeMainLoop() {
474      MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
475      MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
476      MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
477      MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
478    }
479    private void ParameterizeStochasticOperator(IOperator op) {
480      if (op is IStochasticOperator)
481        ((IStochasticOperator)op).RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
482    }
483    private void ParameterizeSelectors() {
484      foreach (ISelector selector in SelectorParameter.ValidValues) {
485        selector.CopySelected = new BoolValue(true);
486        selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * (PopulationSize.Value - Elites.Value));
487        ParameterizeStochasticOperator(selector);
488      }
489      foreach (ISelector selector in EmigrantsSelectorParameter.ValidValues) {
490        selector.CopySelected = new BoolValue(true);
491        selector.NumberOfSelectedSubScopesParameter.Value = new IntValue((int)Math.Ceiling(PopulationSize.Value * MigrationRate.Value));
492        ParameterizeStochasticOperator(selector);
493      }
494      foreach (IReplacer selector in ImmigrationReplacerParameter.ValidValues) {
495        ParameterizeStochasticOperator(selector);
496      }
497      if (Problem != null) {
498        foreach (ISingleObjectiveSelector selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
499          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
500          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
501        }
502        foreach (ISingleObjectiveSelector selector in EmigrantsSelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
503          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
504          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
505        }
506        foreach (ISingleObjectiveReplacer replacer in ImmigrationReplacerParameter.ValidValues.OfType<ISingleObjectiveReplacer>()) {
507          replacer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
508          replacer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
509        }
510      }
511    }
512    private void ParameterizeAnalyzers() {
513      islandQualityAnalyzer.ResultsParameter.ActualName = "Results";
514      islandQualityAnalyzer.QualityParameter.Depth = 1;
515      qualityAnalyzer.ResultsParameter.ActualName = "Results";
516      qualityAnalyzer.QualityParameter.Depth = 2;
517
518      islandSelectionPressureAnalyzer.ResultsParameter.ActualName = "Results";
519      islandSelectionPressureAnalyzer.Name = "SelectionPressure Analyzer";
520      islandSelectionPressureAnalyzer.ValueParameter.Depth = 0;
521      islandSelectionPressureAnalyzer.ValueParameter.ActualName = "SelectionPressure";
522      islandSelectionPressureAnalyzer.ValuesParameter.ActualName = "Selection Pressure History";
523
524      selectionPressureAnalyzer.ResultsParameter.ActualName = "Results";
525      selectionPressureAnalyzer.Name = "SelectionPressure Analyzer";
526      selectionPressureAnalyzer.ValueParameter.Depth = 1;
527      selectionPressureAnalyzer.ValueParameter.ActualName = "SelectionPressure";
528      selectionPressureAnalyzer.ValuesParameter.ActualName = "Selection Pressure History";
529
530      if (Problem != null) {
531        islandQualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
532        islandQualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
533        islandQualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
534
535        qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
536        qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
537        qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
538      }
539    }
540    private void ParameterizeComparisonFactorModifiers() {
541      foreach (IDiscreteDoubleValueModifier modifier in ComparisonFactorModifierParameter.ValidValues) {
542        modifier.IndexParameter.ActualName = "Generations";
543        modifier.EndIndexParameter.Value = new IntValue(MigrationInterval.Value * MaximumGenerations.Value);
544        modifier.EndValueParameter.ActualName = ComparisonFactorUpperBoundParameter.Name;
545        modifier.StartIndexParameter.Value = new IntValue(0);
546        modifier.StartValueParameter.ActualName = ComparisonFactorLowerBoundParameter.Name;
547        modifier.ValueParameter.ActualName = "ComparisonFactor";
548      }
549    }
550    private void UpdateCrossovers() {
551      ICrossover oldCrossover = CrossoverParameter.Value;
552      CrossoverParameter.ValidValues.Clear();
553      foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name))
554        CrossoverParameter.ValidValues.Add(crossover);
555      if (oldCrossover != null) {
556        ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
557        if (crossover != null) CrossoverParameter.Value = crossover;
558      }
559    }
560    private void UpdateMutators() {
561      IManipulator oldMutator = MutatorParameter.Value;
562      MutatorParameter.ValidValues.Clear();
563      foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
564        MutatorParameter.ValidValues.Add(mutator);
565      if (oldMutator != null) {
566        IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
567        if (mutator != null) MutatorParameter.Value = mutator;
568      }
569    }
570    private void UpdateAnalyzers() {
571      IslandAnalyzer.Operators.Clear();
572      Analyzer.Operators.Clear();
573      IslandAnalyzer.Operators.Add(islandQualityAnalyzer);
574      IslandAnalyzer.Operators.Add(islandSelectionPressureAnalyzer);
575      Analyzer.Operators.Add(qualityAnalyzer);
576      Analyzer.Operators.Add(selectionPressureAnalyzer);
577      if (Problem != null) {
578        foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>().OrderBy(x => x.Name)) {
579          foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
580            param.Depth = 2;
581          Analyzer.Operators.Add(analyzer);
582        }
583      }
584    }
585    #endregion
586  }
587}
Note: See TracBrowser for help on using the repository browser.