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

Last change on this file since 3611 was 3611, checked in by abeham, 11 years ago

Changed Island OSGA #976

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