Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3744 was 3744, checked in by abeham, 14 years ago

#893

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