Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3662 was 3662, checked in by swagner, 15 years ago

Worked on refactoring of algorithm analysis and tracing (#999)

  • adapted analyzers
File size: 32.7 KB
RevLine 
[3429]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;
[3654]25using HeuristicLab.Analysis;
[3429]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>
[3479]41  [Item("Island Offspring Selection Genetic Algorithm", "An island offspring selection genetic algorithm.")]
[3429]42  [Creatable("Algorithms")]
43  [StorableClass]
[3479]44  public sealed class IslandOffspringSelectionGeneticAlgorithm : EngineAlgorithm {
[3429]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    }
[3611]78    private ConstrainedValueParameter<IReplacer> ImmigrationReplacerParameter {
79      get { return (ConstrainedValueParameter<IReplacer>)Parameters["ImmigrationReplacer"]; }
[3429]80    }
81    private ValueParameter<IntValue> PopulationSizeParameter {
82      get { return (ValueParameter<IntValue>)Parameters["PopulationSize"]; }
83    }
[3611]84    private ValueParameter<IntValue> MaximumGenerationsParameter {
85      get { return (ValueParameter<IntValue>)Parameters["MaximumGenerations"]; }
[3429]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    }
[3658]123    private ValueParameter<MultiAnalyzer> AnalyzerParameter {
124      get { return (ValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
[3654]125    }
[3658]126    private ValueParameter<MultiAnalyzer> IslandAnalyzerParameter {
127      get { return (ValueParameter<MultiAnalyzer>)Parameters["IslandAnalyzer"]; }
[3654]128    }
[3429]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    }
[3611]160    public IReplacer ImmigrationReplacer {
161      get { return ImmigrationReplacerParameter.Value; }
162      set { ImmigrationReplacerParameter.Value = value; }
[3429]163    }
164    public IntValue PopulationSize {
165      get { return PopulationSizeParameter.Value; }
166      set { PopulationSizeParameter.Value = value; }
167    }
[3611]168    public IntValue MaximumGenerations {
169      get { return MaximumGenerationsParameter.Value; }
170      set { MaximumGenerationsParameter.Value = value; }
[3429]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    }
[3658]220    public MultiAnalyzer Analyzer {
[3654]221      get { return AnalyzerParameter.Value; }
222      set { AnalyzerParameter.Value = value; }
223    }
[3658]224    public MultiAnalyzer IslandAnalyzer {
[3654]225      get { return IslandAnalyzerParameter.Value; }
226      set { IslandAnalyzerParameter.Value = value; }
227    }
[3429]228    private List<ISelector> selectors;
229    private IEnumerable<ISelector> Selectors {
230      get { return selectors; }
231    }
232    private List<IDiscreteDoubleValueModifier> comparisonFactorModifiers;
233    private List<ISelector> emigrantsSelectors;
[3611]234    private List<IReplacer> immigrationReplacers;
[3429]235    private List<IMigrator> migrators;
236    private RandomCreator RandomCreator {
237      get { return (RandomCreator)OperatorGraph.InitialOperator; }
238    }
239    private UniformSubScopesProcessor IslandProcessor {
240      get { return ((RandomCreator.Successor as SubScopesCreator).Successor as UniformSubScopesProcessor); }
241    }
242    private SolutionsCreator SolutionsCreator {
243      get { return (SolutionsCreator)IslandProcessor.Operator; }
244    }
[3479]245    private IslandOffspringSelectionGeneticAlgorithmMainLoop MainLoop {
246      get { return (IslandOffspringSelectionGeneticAlgorithmMainLoop)IslandProcessor.Successor; }
[3429]247    }
[3662]248    private BestAverageWorstQualityAnalyzer islandQualityAnalyzer;
[3654]249    //private MultipopulationBestAverageWorstQualityAnalyzer qualityAnalyzer;
[3429]250    #endregion
251
252    [StorableConstructor]
[3479]253    private IslandOffspringSelectionGeneticAlgorithm(bool deserializing) : base(deserializing) { }
254    public IslandOffspringSelectionGeneticAlgorithm()
[3429]255      : base() {
256      Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
257      Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
258      Parameters.Add(new ValueParameter<IntValue>("NumberOfIslands", "The number of islands.", new IntValue(5)));
259      Parameters.Add(new ValueParameter<IntValue>("MigrationInterval", "The number of generations that should pass between migration phases.", new IntValue(20)));
260      Parameters.Add(new ValueParameter<PercentValue>("MigrationRate", "The proportion of individuals that should migrate between the islands.", new PercentValue(0.15)));
261      Parameters.Add(new ConstrainedValueParameter<IMigrator>("Migrator", "The migration strategy."));
262      Parameters.Add(new ConstrainedValueParameter<ISelector>("EmigrantsSelector", "Selects the individuals that will be migrated."));
[3611]263      Parameters.Add(new ConstrainedValueParameter<IReplacer>("ImmigrationReplacer", "Selects the population from the unification of the original population and the immigrants."));
[3479]264      Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "The size of the population of solutions of each island.", new IntValue(100)));
[3611]265      Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations that should be processed.", new IntValue(100)));
[3429]266      Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
267      Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
268      Parameters.Add(new ValueParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new PercentValue(0.05)));
269      Parameters.Add(new OptionalConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
270      Parameters.Add(new ValueParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation.", new IntValue(1)));
271      Parameters.Add(new ValueParameter<BoolValue>("Parallel", "True if the islands should be run in parallel (also requires a parallel engine)", new BoolValue(false)));
272      Parameters.Add(new ValueLookupParameter<DoubleValue>("SuccessRatio", "The ratio of successful to total children that should be achieved.", new DoubleValue(1)));
273      Parameters.Add(new ValueLookupParameter<DoubleValue>("ComparisonFactorLowerBound", "The lower bound of the comparison factor (start).", new DoubleValue(0)));
274      Parameters.Add(new ValueLookupParameter<DoubleValue>("ComparisonFactorUpperBound", "The upper bound of the comparison factor (end).", new DoubleValue(1)));
275      Parameters.Add(new OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier>("ComparisonFactorModifier", "The operator used to modify the comparison factor.", new ItemSet<IDiscreteDoubleValueModifier>(new IDiscreteDoubleValueModifier[] { new LinearDiscreteDoubleValueModifier() }), new LinearDiscreteDoubleValueModifier()));
276      Parameters.Add(new ValueLookupParameter<DoubleValue>("MaximumSelectionPressure", "The maximum selection pressure that terminates the algorithm.", new DoubleValue(100)));
277      Parameters.Add(new ValueLookupParameter<BoolValue>("OffspringSelectionBeforeMutation", "True if the offspring selection step should be applied before mutation, false if it should be applied after mutation.", new BoolValue(false)));
[3658]278      Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze the islands.", new MultiAnalyzer()));
279      Parameters.Add(new ValueParameter<MultiAnalyzer>("IslandAnalyzer", "The operator used to analyze each island.", new MultiAnalyzer()));
[3654]280     
[3429]281      RandomCreator randomCreator = new RandomCreator();
282      SubScopesCreator populationCreator = new SubScopesCreator();
283      UniformSubScopesProcessor ussp1 = new UniformSubScopesProcessor();
284      SolutionsCreator solutionsCreator = new SolutionsCreator();
[3479]285      IslandOffspringSelectionGeneticAlgorithmMainLoop mainLoop = new IslandOffspringSelectionGeneticAlgorithmMainLoop();
[3429]286      OperatorGraph.InitialOperator = randomCreator;
287
288      randomCreator.RandomParameter.ActualName = "Random";
289      randomCreator.SeedParameter.ActualName = SeedParameter.Name;
290      randomCreator.SeedParameter.Value = null;
291      randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
292      randomCreator.SetSeedRandomlyParameter.Value = null;
293      randomCreator.Successor = populationCreator;
294
295      populationCreator.NumberOfSubScopesParameter.ActualName = NumberOfIslandsParameter.Name;
296      populationCreator.Successor = ussp1;
297
298      ussp1.Parallel = null;
299      ussp1.ParallelParameter.ActualName = ParallelParameter.Name;
300      ussp1.Operator = solutionsCreator;
301      ussp1.Successor = mainLoop;
302
303      solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
304      solutionsCreator.Successor = null;
305
306      mainLoop.EmigrantsSelectorParameter.ActualName = EmigrantsSelectorParameter.Name;
[3611]307      mainLoop.ImmigrationReplacerParameter.ActualName = ImmigrationReplacerParameter.Name;
308      mainLoop.MaximumGenerationsParameter.ActualName = MaximumGenerationsParameter.Name;
[3429]309      mainLoop.MigrationIntervalParameter.ActualName = MigrationIntervalParameter.Name;
310      mainLoop.MigrationRateParameter.ActualName = MigrationRateParameter.Name;
311      mainLoop.MigratorParameter.ActualName = MigratorParameter.Name;
312      mainLoop.NumberOfIslandsParameter.ActualName = NumberOfIslandsParameter.Name;
313      mainLoop.SelectorParameter.ActualName = SelectorParameter.Name;
314      mainLoop.CrossoverParameter.ActualName = CrossoverParameter.Name;
315      mainLoop.ElitesParameter.ActualName = ElitesParameter.Name;
316      mainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
317      mainLoop.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
318      mainLoop.RandomParameter.ActualName = randomCreator.RandomParameter.ActualName;
319      mainLoop.ResultsParameter.ActualName = "Results";
320      mainLoop.SuccessRatioParameter.ActualName = SuccessRatioParameter.Name;
[3611]321      mainLoop.ComparisonFactorParameter.ActualName = "ComparisonFactor";
[3429]322      mainLoop.ComparisonFactorLowerBoundParameter.ActualName = ComparisonFactorLowerBoundParameter.Name;
323      mainLoop.ComparisonFactorModifierParameter.ActualName = ComparisonFactorModifierParameter.Name;
324      mainLoop.ComparisonFactorUpperBoundParameter.ActualName = ComparisonFactorUpperBoundParameter.Name;
325      mainLoop.MaximumSelectionPressureParameter.ActualName = MaximumSelectionPressureParameter.Name;
326      mainLoop.OffspringSelectionBeforeMutationParameter.ActualName = OffspringSelectionBeforeMutationParameter.Name;
327
328      mainLoop.Successor = null;
329
330      Initialize();
331    }
332
333    public override IDeepCloneable Clone(Cloner cloner) {
[3479]334      IslandOffspringSelectionGeneticAlgorithm clone = (IslandOffspringSelectionGeneticAlgorithm)base.Clone(cloner);
[3429]335      clone.Initialize();
336      return clone;
337    }
338
339    public override void Prepare() {
340      if (Problem != null) base.Prepare();
341    }
342
343    #region Events
344    protected override void OnProblemChanged() {
345      ParameterizeStochasticOperator(Problem.SolutionCreator);
346      ParameterizeStochasticOperator(Problem.Evaluator);
347      foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
348      ParameterizeSolutionsCreator();
349      ParameterizeMainLoop();
350      ParameterizeSelectors();
[3654]351      ParameterizeAnalyzers();
[3429]352      UpdateCrossovers();
353      UpdateMutators();
[3654]354      UpdateAnalyzers();
[3429]355      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
356      base.OnProblemChanged();
357    }
358
359    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
360      ParameterizeStochasticOperator(Problem.SolutionCreator);
361      ParameterizeSolutionsCreator();
362      base.Problem_SolutionCreatorChanged(sender, e);
363    }
364    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
365      ParameterizeStochasticOperator(Problem.Evaluator);
366      ParameterizeSolutionsCreator();
367      ParameterizeMainLoop();
368      ParameterizeSelectors();
[3654]369      ParameterizeAnalyzers();
[3429]370      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
371      base.Problem_EvaluatorChanged(sender, e);
372    }
373    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
374      foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
375      UpdateCrossovers();
376      UpdateMutators();
[3654]377      UpdateAnalyzers();
[3429]378      base.Problem_OperatorsChanged(sender, e);
379    }
380    private void ElitesParameter_ValueChanged(object sender, EventArgs e) {
381      Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
382      ParameterizeSelectors();
383    }
384    private void Elites_ValueChanged(object sender, EventArgs e) {
385      ParameterizeSelectors();
386    }
387    private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
388      NumberOfIslands.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
389      ParameterizeSelectors();
390    }
391    private void PopulationSize_ValueChanged(object sender, EventArgs e) {
392      ParameterizeSelectors();
393    }
394    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
395      ParameterizeMainLoop();
396      ParameterizeSelectors();
[3654]397      ParameterizeAnalyzers();
[3429]398    }
399    private void MigrationRateParameter_ValueChanged(object sender, EventArgs e) {
400      MigrationRate.ValueChanged += new EventHandler(MigrationRate_ValueChanged);
401      ParameterizeSelectors();
402    }
403    private void MigrationRate_ValueChanged(object sender, EventArgs e) {
404      ParameterizeSelectors();
405    }
406    private void MaximumMigrationsParameter_ValueChanged(object sender, EventArgs e) {
[3611]407      MaximumGenerations.ValueChanged += new EventHandler(MaximumMigrations_ValueChanged);
[3429]408      ParameterizeComparisonFactorModifiers();
409    }
410    private void MaximumMigrations_ValueChanged(object sender, EventArgs e) {
411      ParameterizeComparisonFactorModifiers();
412    }
413    private void MigrationIntervalParameter_ValueChanged(object sender, EventArgs e) {
414      MigrationInterval.ValueChanged += new EventHandler(MigrationInterval_ValueChanged);
415      ParameterizeComparisonFactorModifiers();
416    }
417    private void MigrationInterval_ValueChanged(object sender, EventArgs e) {
418      ParameterizeComparisonFactorModifiers();
419    }
420    #endregion
421
422    #region Helpers
423    [StorableHook(HookType.AfterDeserialization)]
424    private void Initialize() {
425      InitializeSelectors();
[3654]426      InitializeAnalyzers();
[3429]427      UpdateSelectors();
[3654]428      UpdateAnalyzers();
[3429]429      InitializeComparisonFactorModifiers();
430      UpdateComparisonFactorModifiers();
431      InitializeMigrators();
432      UpdateMigrators();
433      PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
434      PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
435      MigrationRateParameter.ValueChanged += new EventHandler(MigrationRateParameter_ValueChanged);
436      MigrationRate.ValueChanged += new EventHandler(MigrationRate_ValueChanged);
437      ElitesParameter.ValueChanged += new EventHandler(ElitesParameter_ValueChanged);
438      Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
439      MigrationIntervalParameter.ValueChanged += new EventHandler(MigrationIntervalParameter_ValueChanged);
440      MigrationInterval.ValueChanged += new EventHandler(MigrationInterval_ValueChanged);
[3611]441      MaximumGenerationsParameter.ValueChanged += new EventHandler(MaximumMigrationsParameter_ValueChanged);
442      MaximumGenerations.ValueChanged += new EventHandler(MaximumMigrations_ValueChanged);
[3429]443      if (Problem != null) {
444        UpdateCrossovers();
445        UpdateMutators();
446        Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
447      }
448    }
449    private void ParameterizeSolutionsCreator() {
450      SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
451      SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
452    }
453    private void ParameterizeMainLoop() {
454      MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
455      MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
456      MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
457      MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
458    }
459    private void ParameterizeStochasticOperator(IOperator op) {
460      if (op is IStochasticOperator)
461        ((IStochasticOperator)op).RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
462    }
463    private void InitializeSelectors() {
464      selectors = new List<ISelector>();
465      selectors.AddRange(ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name));
466      emigrantsSelectors = new List<ISelector>();
467      emigrantsSelectors.AddRange(ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name));
[3611]468      immigrationReplacers = new List<IReplacer>();
469      immigrationReplacers.AddRange(ApplicationManager.Manager.GetInstances<IReplacer>().OrderBy(x => x.Name));
[3429]470      ParameterizeSelectors();
471    }
[3654]472    private void InitializeAnalyzers() {
[3662]473      islandQualityAnalyzer = new BestAverageWorstQualityAnalyzer();
[3654]474      //qualityAnalyzer = new MultipopulationBestAverageWorstQualityAnalyzer();
475      ParameterizeAnalyzers();
476    }
[3429]477    private void InitializeComparisonFactorModifiers() {
478      comparisonFactorModifiers = new List<IDiscreteDoubleValueModifier>();
479      comparisonFactorModifiers.AddRange(ApplicationManager.Manager.GetInstances<IDiscreteDoubleValueModifier>().OrderBy(x => x.Name));
480      ParameterizeComparisonFactorModifiers();
481    }
482    private void InitializeMigrators() {
483      migrators = new List<IMigrator>();
484      migrators.AddRange(ApplicationManager.Manager.GetInstances<IMigrator>().OrderBy(x => x.Name));
485      UpdateMigrators();
486    }
487    private void ParameterizeSelectors() {
488      foreach (ISelector selector in Selectors) {
489        selector.CopySelected = new BoolValue(true);
490        selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * (PopulationSize.Value - Elites.Value));
491        ParameterizeStochasticOperator(selector);
492      }
493      foreach (ISelector selector in emigrantsSelectors) {
494        selector.CopySelected = new BoolValue(true);
495        selector.NumberOfSelectedSubScopesParameter.Value = new IntValue((int)Math.Ceiling(PopulationSize.Value * MigrationRate.Value));
496        ParameterizeStochasticOperator(selector);
497      }
[3611]498      foreach (IReplacer selector in immigrationReplacers) {
[3429]499        ParameterizeStochasticOperator(selector);
500      }
501      if (Problem != null) {
502        foreach (ISingleObjectiveSelector selector in Selectors.OfType<ISingleObjectiveSelector>()) {
503          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
504          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
505        }
506        foreach (ISingleObjectiveSelector selector in emigrantsSelectors.OfType<ISingleObjectiveSelector>()) {
507          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
508          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
509        }
[3611]510        foreach (ISingleObjectiveReplacer replacer in immigrationReplacers.OfType<ISingleObjectiveReplacer>()) {
511          replacer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
512          replacer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
[3429]513        }
514      }
515    }
[3654]516    private void ParameterizeAnalyzers() {
517      islandQualityAnalyzer.ResultsParameter.ActualName = "Results";
518      //qualityAnalyzer.ResultsParameter.ActualName = "Results";
519      if (Problem != null) {
520        islandQualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
521        islandQualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
522        islandQualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
523      }
524    }
[3429]525    private void ParameterizeComparisonFactorModifiers() {
526      foreach (IDiscreteDoubleValueModifier modifier in comparisonFactorModifiers) {
527        modifier.IndexParameter.ActualName = "Generations";
[3611]528        modifier.EndIndexParameter.Value = new IntValue(MigrationInterval.Value * MaximumGenerations.Value);
[3429]529        modifier.EndValueParameter.ActualName = ComparisonFactorUpperBoundParameter.Name;
530        modifier.StartIndexParameter.Value = new IntValue(0);
531        modifier.StartValueParameter.ActualName = ComparisonFactorLowerBoundParameter.Name;
532        modifier.ValueParameter.ActualName = "ComparisonFactor";
533      }
534    }
535    private void UpdateSelectors() {
536      ISelector oldSelector = SelectorParameter.Value;
537      SelectorParameter.ValidValues.Clear();
538      foreach (ISelector selector in Selectors.OrderBy(x => x.Name))
539        SelectorParameter.ValidValues.Add(selector);
540
541      ISelector proportionalSelector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("ProportionalSelector"));
542      if (proportionalSelector != null) SelectorParameter.Value = proportionalSelector;
543
544      if (oldSelector != null) {
545        ISelector selector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldSelector.GetType());
546        if (selector != null) SelectorParameter.Value = selector;
547      }
548
549      oldSelector = EmigrantsSelector;
550      EmigrantsSelectorParameter.ValidValues.Clear();
551      foreach (ISelector selector in emigrantsSelectors)
552        EmigrantsSelectorParameter.ValidValues.Add(selector);
553      if (oldSelector != null) {
554        ISelector selector = EmigrantsSelectorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldSelector.GetType());
555        if (selector != null) EmigrantsSelectorParameter.Value = selector;
556      }
557
[3611]558      IReplacer oldReplacer = ImmigrationReplacerParameter.Value;
559      ImmigrationReplacerParameter.ValidValues.Clear();
560      foreach (IReplacer replacer in immigrationReplacers)
561        ImmigrationReplacerParameter.ValidValues.Add(replacer);
[3429]562      if (oldSelector != null) {
[3611]563        IReplacer replacer = ImmigrationReplacerParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldSelector.GetType());
564        if (replacer != null) ImmigrationReplacerParameter.Value = replacer;
[3429]565      }
566    }
567    private void UpdateComparisonFactorModifiers() {
568      IDiscreteDoubleValueModifier oldModifier = ComparisonFactorModifier;
569
570      ComparisonFactorModifierParameter.ValidValues.Clear();
571      foreach (IDiscreteDoubleValueModifier modifier in comparisonFactorModifiers)
572        ComparisonFactorModifierParameter.ValidValues.Add(modifier);
573
574      if (oldModifier != null) {
575        IDiscreteDoubleValueModifier mod = ComparisonFactorModifierParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldModifier.GetType());
576        if (mod != null) ComparisonFactorModifierParameter.Value = mod;
577      }
578    }
579    private void UpdateMigrators() {
580      IMigrator oldMigrator = Migrator;
581      MigratorParameter.ValidValues.Clear();
582      foreach (IMigrator migrator in migrators)
583        MigratorParameter.ValidValues.Add(migrator);
584      if (oldMigrator != null) {
585        IMigrator migrator = MigratorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMigrator.GetType());
586        if (migrator != null) MigratorParameter.Value = migrator;
587      } else if (MigratorParameter.ValidValues.Count > 0) MigratorParameter.Value = MigratorParameter.ValidValues.First();
588    }
589    private void UpdateCrossovers() {
590      ICrossover oldCrossover = CrossoverParameter.Value;
591      CrossoverParameter.ValidValues.Clear();
592      foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name))
593        CrossoverParameter.ValidValues.Add(crossover);
594      if (oldCrossover != null) {
595        ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
596        if (crossover != null) CrossoverParameter.Value = crossover;
597      }
598    }
599    private void UpdateMutators() {
600      IManipulator oldMutator = MutatorParameter.Value;
601      MutatorParameter.ValidValues.Clear();
602      foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
603        MutatorParameter.ValidValues.Add(mutator);
604      if (oldMutator != null) {
605        IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
606        if (mutator != null) MutatorParameter.Value = mutator;
607      }
608    }
[3654]609    private void UpdateAnalyzers() {
610      IslandAnalyzer.Operators.Clear();
611      Analyzer.Operators.Clear();
612      IslandAnalyzer.Operators.Add(islandQualityAnalyzer);
613      //Analyzer.Operators.Add(qualityAnalyzer);
614      if (Problem != null) {
[3658]615        foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>().OrderBy(x => x.Name)) {
[3654]616          IslandAnalyzer.Operators.Add(analyzer);
617        }
[3658]618        foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>().OrderBy(x => x.Name))
[3654]619          Analyzer.Operators.Add(analyzer);
620      }
621    }
[3429]622    #endregion
623  }
624}
Note: See TracBrowser for help on using the repository browser.