Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis.IslandAlgorithms/HeuristicLab.Algorithms.GeneticAlgorithm/3.3/IslandGeneticAlgorithm.cs @ 10142

Last change on this file since 10142 was 10142, checked in by mkommend, 10 years ago

#1997: Worked on symbolic data analysis island algorithms:

  • Refactored and removed unnecessary code in symbolic island GA and symbolic island GA evaluator.
  • Added EnumerableItem to ease the passing of rows to the actual evaluator.
  • Added prototype of island offspring selection GA.
  • Corrected build configuration (plugin classes, project settings, ...)
File size: 29.5 KB
RevLine 
[3356]1#region License Information
2/* HeuristicLab
[9756]3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3356]4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Linq;
[4068]24using HeuristicLab.Analysis;
[3376]25using HeuristicLab.Common;
[3356]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;
[3368]34using HeuristicLab.Random;
[3356]35
36namespace HeuristicLab.Algorithms.GeneticAlgorithm {
37  /// <summary>
38  /// An island genetic algorithm.
39  /// </summary>
40  [Item("Island Genetic Algorithm", "An island genetic algorithm.")]
41  [Creatable("Algorithms")]
42  [StorableClass]
[10142]43  public class IslandGeneticAlgorithm : HeuristicOptimizationEngineAlgorithm, IStorableContent {
[4437]44    public string Filename { get; set; }
[3356]45
46    #region Problem Properties
47    public override Type ProblemType {
[5809]48      get { return typeof(ISingleObjectiveHeuristicOptimizationProblem); }
[3356]49    }
[5809]50    public new ISingleObjectiveHeuristicOptimizationProblem Problem {
51      get { return (ISingleObjectiveHeuristicOptimizationProblem)base.Problem; }
[3356]52      set { base.Problem = value; }
53    }
54    #endregion
55
56    #region Parameter Properties
[10142]57    protected ValueParameter<IntValue> SeedParameter {
[3356]58      get { return (ValueParameter<IntValue>)Parameters["Seed"]; }
59    }
[10142]60    protected ValueParameter<BoolValue> SetSeedRandomlyParameter {
[3356]61      get { return (ValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
62    }
[10142]63    protected ValueParameter<IntValue> NumberOfIslandsParameter {
[3356]64      get { return (ValueParameter<IntValue>)Parameters["NumberOfIslands"]; }
65    }
[10142]66    protected ValueParameter<IntValue> MigrationIntervalParameter {
[3356]67      get { return (ValueParameter<IntValue>)Parameters["MigrationInterval"]; }
68    }
[10142]69    protected ValueParameter<PercentValue> MigrationRateParameter {
[3356]70      get { return (ValueParameter<PercentValue>)Parameters["MigrationRate"]; }
71    }
[8121]72    public IConstrainedValueParameter<IMigrator> MigratorParameter {
73      get { return (IConstrainedValueParameter<IMigrator>)Parameters["Migrator"]; }
[3356]74    }
[8121]75    public IConstrainedValueParameter<ISelector> EmigrantsSelectorParameter {
76      get { return (IConstrainedValueParameter<ISelector>)Parameters["EmigrantsSelector"]; }
[3356]77    }
[8121]78    public IConstrainedValueParameter<IReplacer> ImmigrationReplacerParameter {
79      get { return (IConstrainedValueParameter<IReplacer>)Parameters["ImmigrationReplacer"]; }
[3356]80    }
[10142]81    protected ValueParameter<IntValue> PopulationSizeParameter {
[3356]82      get { return (ValueParameter<IntValue>)Parameters["PopulationSize"]; }
83    }
[10142]84    protected ValueParameter<IntValue> MaximumGenerationsParameter {
[3609]85      get { return (ValueParameter<IntValue>)Parameters["MaximumGenerations"]; }
[3356]86    }
[8121]87    public IConstrainedValueParameter<ISelector> SelectorParameter {
88      get { return (IConstrainedValueParameter<ISelector>)Parameters["Selector"]; }
[3356]89    }
[8121]90    public IConstrainedValueParameter<ICrossover> CrossoverParameter {
91      get { return (IConstrainedValueParameter<ICrossover>)Parameters["Crossover"]; }
[3356]92    }
[10142]93    protected ValueParameter<PercentValue> MutationProbabilityParameter {
[3356]94      get { return (ValueParameter<PercentValue>)Parameters["MutationProbability"]; }
95    }
[8121]96    public IConstrainedValueParameter<IManipulator> MutatorParameter {
97      get { return (IConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; }
[3356]98    }
[10142]99    protected ValueParameter<IntValue> ElitesParameter {
[3356]100      get { return (ValueParameter<IntValue>)Parameters["Elites"]; }
101    }
[10142]102    protected IFixedValueParameter<BoolValue> ReevaluateElitesParameter {
[9756]103      get { return (IFixedValueParameter<BoolValue>)Parameters["ReevaluateElites"]; }
104    }
[10142]105    protected ValueParameter<MultiAnalyzer> AnalyzerParameter {
[3658]106      get { return (ValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
[3650]107    }
[10142]108    protected ValueParameter<MultiAnalyzer> IslandAnalyzerParameter {
[3658]109      get { return (ValueParameter<MultiAnalyzer>)Parameters["IslandAnalyzer"]; }
[3650]110    }
[3356]111    #endregion
112
113    #region Properties
114    public IntValue Seed {
115      get { return SeedParameter.Value; }
116      set { SeedParameter.Value = value; }
117    }
118    public BoolValue SetSeedRandomly {
119      get { return SetSeedRandomlyParameter.Value; }
120      set { SetSeedRandomlyParameter.Value = value; }
121    }
122    public IntValue NumberOfIslands {
123      get { return NumberOfIslandsParameter.Value; }
124      set { NumberOfIslandsParameter.Value = value; }
125    }
126    public IntValue MigrationInterval {
127      get { return MigrationIntervalParameter.Value; }
128      set { MigrationIntervalParameter.Value = value; }
129    }
130    public PercentValue MigrationRate {
131      get { return MigrationRateParameter.Value; }
132      set { MigrationRateParameter.Value = value; }
133    }
134    public IMigrator Migrator {
135      get { return MigratorParameter.Value; }
136      set { MigratorParameter.Value = value; }
137    }
138    public ISelector EmigrantsSelector {
139      get { return EmigrantsSelectorParameter.Value; }
140      set { EmigrantsSelectorParameter.Value = value; }
141    }
[3601]142    public IReplacer ImmigrationReplacer {
143      get { return ImmigrationReplacerParameter.Value; }
144      set { ImmigrationReplacerParameter.Value = value; }
[3356]145    }
146    public IntValue PopulationSize {
147      get { return PopulationSizeParameter.Value; }
148      set { PopulationSizeParameter.Value = value; }
149    }
[3609]150    public IntValue MaximumGenerations {
151      get { return MaximumGenerationsParameter.Value; }
152      set { MaximumGenerationsParameter.Value = value; }
[3356]153    }
154    public ISelector Selector {
155      get { return SelectorParameter.Value; }
156      set { SelectorParameter.Value = value; }
157    }
158    public ICrossover Crossover {
159      get { return CrossoverParameter.Value; }
160      set { CrossoverParameter.Value = value; }
161    }
162    public PercentValue MutationProbability {
163      get { return MutationProbabilityParameter.Value; }
164      set { MutationProbabilityParameter.Value = value; }
165    }
166    public IManipulator Mutator {
167      get { return MutatorParameter.Value; }
168      set { MutatorParameter.Value = value; }
169    }
170    public IntValue Elites {
171      get { return ElitesParameter.Value; }
172      set { ElitesParameter.Value = value; }
173    }
[9756]174    public bool ReevaluteElites {
175      get { return ReevaluateElitesParameter.Value.Value; }
176      set { ReevaluateElitesParameter.Value.Value = value; }
177    }
[3658]178    public MultiAnalyzer Analyzer {
[3650]179      get { return AnalyzerParameter.Value; }
180      set { AnalyzerParameter.Value = value; }
181    }
[3658]182    public MultiAnalyzer IslandAnalyzer {
[3650]183      get { return IslandAnalyzerParameter.Value; }
184      set { IslandAnalyzerParameter.Value = value; }
185    }
[10142]186    protected RandomCreator RandomCreator {
[3359]187      get { return (RandomCreator)OperatorGraph.InitialOperator; }
188    }
[10142]189    protected UniformSubScopesProcessor IslandProcessor {
[9039]190      get { return OperatorGraph.Iterate().OfType<UniformSubScopesProcessor>().First(x => x.Operator is SolutionsCreator); }
[3429]191    }
[10142]192    protected SolutionsCreator SolutionsCreator {
[3429]193      get { return (SolutionsCreator)IslandProcessor.Operator; }
[3359]194    }
[10142]195    protected IslandGeneticAlgorithmMainLoop MainLoop {
[5366]196      get { return FindMainLoop(IslandProcessor.Successor); }
[3359]197    }
[3689]198    [Storable]
[10142]199    protected BestAverageWorstQualityAnalyzer islandQualityAnalyzer;
[3689]200    [Storable]
[10142]201    protected BestAverageWorstQualityAnalyzer qualityAnalyzer;
[3356]202    #endregion
203
204    [StorableConstructor]
[10142]205    protected IslandGeneticAlgorithm(bool deserializing) : base(deserializing) { }
[4722]206    [StorableHook(HookType.AfterDeserialization)]
207    private void AfterDeserialization() {
[9756]208      // BackwardsCompatibility3.3
209      #region Backwards compatible code, remove with 3.4
210      if (!Parameters.ContainsKey("ReevaluateElites")) {
211        Parameters.Add(new FixedValueParameter<BoolValue>("ReevaluateElites", "Flag to determine if elite individuals should be reevaluated (i.e., if stochastic fitness functions are used.)", (BoolValue)new BoolValue(false).AsReadOnly()) { Hidden = true });
212      }
213      #endregion
214
[4722]215      Initialize();
216    }
[10142]217    protected IslandGeneticAlgorithm(IslandGeneticAlgorithm original, Cloner cloner)
[4722]218      : base(original, cloner) {
219      islandQualityAnalyzer = cloner.Clone(original.islandQualityAnalyzer);
220      qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
221      Initialize();
222    }
223    public override IDeepCloneable Clone(Cloner cloner) {
224      return new IslandGeneticAlgorithm(this, cloner);
225    }
226
[3356]227    public IslandGeneticAlgorithm()
228      : base() {
229      Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
230      Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
231      Parameters.Add(new ValueParameter<IntValue>("NumberOfIslands", "The number of islands.", new IntValue(5)));
232      Parameters.Add(new ValueParameter<IntValue>("MigrationInterval", "The number of generations that should pass between migration phases.", new IntValue(20)));
233      Parameters.Add(new ValueParameter<PercentValue>("MigrationRate", "The proportion of individuals that should migrate between the islands.", new PercentValue(0.15)));
234      Parameters.Add(new ConstrainedValueParameter<IMigrator>("Migrator", "The migration strategy."));
235      Parameters.Add(new ConstrainedValueParameter<ISelector>("EmigrantsSelector", "Selects the individuals that will be migrated."));
[3601]236      Parameters.Add(new ConstrainedValueParameter<IReplacer>("ImmigrationReplacer", "Selects the population from the unification of the original population and the immigrants."));
[3356]237      Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "The size of the population of solutions.", new IntValue(100)));
[3609]238      Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations that should be processed.", new IntValue(1000)));
[3356]239      Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
240      Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
241      Parameters.Add(new ValueParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new PercentValue(0.05)));
242      Parameters.Add(new OptionalConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
243      Parameters.Add(new ValueParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation.", new IntValue(1)));
[9756]244      Parameters.Add(new FixedValueParameter<BoolValue>("ReevaluateElites", "Flag to determine if elite individuals should be reevaluated (i.e., if stochastic fitness functions are used.)", new BoolValue(false)) { Hidden = true });
[3658]245      Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze the islands.", new MultiAnalyzer()));
246      Parameters.Add(new ValueParameter<MultiAnalyzer>("IslandAnalyzer", "The operator used to analyze each island.", new MultiAnalyzer()));
[4068]247
[3359]248      RandomCreator randomCreator = new RandomCreator();
[9039]249      UniformSubScopesProcessor ussp0 = new UniformSubScopesProcessor();
250      LocalRandomCreator localRandomCreator = new LocalRandomCreator();
251      RandomCreator globalRandomResetter = new RandomCreator();
[3356]252      SubScopesCreator populationCreator = new SubScopesCreator();
253      UniformSubScopesProcessor ussp1 = new UniformSubScopesProcessor();
[3359]254      SolutionsCreator solutionsCreator = new SolutionsCreator();
[5351]255      VariableCreator variableCreator = new VariableCreator();
256      UniformSubScopesProcessor ussp2 = new UniformSubScopesProcessor();
257      SubScopesCounter subScopesCounter = new SubScopesCounter();
[5356]258      ResultsCollector resultsCollector = new ResultsCollector();
[3359]259      IslandGeneticAlgorithmMainLoop mainLoop = new IslandGeneticAlgorithmMainLoop();
[3356]260      OperatorGraph.InitialOperator = randomCreator;
261
[7395]262      randomCreator.RandomParameter.ActualName = "GlobalRandom";
[3356]263      randomCreator.SeedParameter.ActualName = SeedParameter.Name;
264      randomCreator.SeedParameter.Value = null;
265      randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
266      randomCreator.SetSeedRandomlyParameter.Value = null;
267      randomCreator.Successor = populationCreator;
268
269      populationCreator.NumberOfSubScopesParameter.ActualName = NumberOfIslandsParameter.Name;
[9039]270      populationCreator.Successor = ussp0;
[3356]271
[9039]272      ussp0.Operator = localRandomCreator;
273      ussp0.Successor = globalRandomResetter;
274
275      // BackwardsCompatibility3.3
276      // the global random is resetted to ensure the same algorithm results
[9756]277      #region Backwards compatible code, remove global random resetter with 3.4 and rewire the operator graph
[9039]278      globalRandomResetter.RandomParameter.ActualName = "GlobalRandom";
279      globalRandomResetter.SeedParameter.ActualName = SeedParameter.Name;
280      globalRandomResetter.SeedParameter.Value = null;
281      globalRandomResetter.SetSeedRandomlyParameter.Value = new BoolValue(false);
282      globalRandomResetter.Successor = ussp1;
283      #endregion
284
[3356]285      ussp1.Operator = solutionsCreator;
[5351]286      ussp1.Successor = variableCreator;
[3356]287
288      solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
[7395]289      //don't create solutions in parallel because the hive engine would distribute these tasks
290      solutionsCreator.ParallelParameter.Value = new BoolValue(false);
[3356]291      solutionsCreator.Successor = null;
292
[5351]293      variableCreator.Name = "Initialize EvaluatedSolutions";
294      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("EvaluatedSolutions", new IntValue()));
295      variableCreator.Successor = ussp2;
296
297      ussp2.Operator = subScopesCounter;
[5356]298      ussp2.Successor = resultsCollector;
[5351]299
300      subScopesCounter.Name = "Count EvaluatedSolutions";
301      subScopesCounter.ValueParameter.ActualName = "EvaluatedSolutions";
302      subScopesCounter.Successor = null;
303
[5356]304      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
305      resultsCollector.ResultsParameter.ActualName = "Results";
306      resultsCollector.Successor = mainLoop;
307
[3356]308      mainLoop.EmigrantsSelectorParameter.ActualName = EmigrantsSelectorParameter.Name;
[3601]309      mainLoop.ImmigrationReplacerParameter.ActualName = ImmigrationReplacerParameter.Name;
[3609]310      mainLoop.MaximumGenerationsParameter.ActualName = MaximumGenerationsParameter.Name;
[3356]311      mainLoop.MigrationIntervalParameter.ActualName = MigrationIntervalParameter.Name;
312      mainLoop.MigrationRateParameter.ActualName = MigrationRateParameter.Name;
313      mainLoop.MigratorParameter.ActualName = MigratorParameter.Name;
314      mainLoop.NumberOfIslandsParameter.ActualName = NumberOfIslandsParameter.Name;
315      mainLoop.SelectorParameter.ActualName = SelectorParameter.Name;
316      mainLoop.CrossoverParameter.ActualName = CrossoverParameter.Name;
317      mainLoop.ElitesParameter.ActualName = ElitesParameter.Name;
[9756]318      mainLoop.ReevaluateElitesParameter.ActualName = ReevaluateElitesParameter.Name;
[3356]319      mainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
320      mainLoop.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
321      mainLoop.RandomParameter.ActualName = randomCreator.RandomParameter.ActualName;
322      mainLoop.ResultsParameter.ActualName = "Results";
[3650]323      mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
324      mainLoop.IslandAnalyzerParameter.ActualName = IslandAnalyzerParameter.Name;
[5351]325      mainLoop.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
[3356]326      mainLoop.Successor = null;
327
[3689]328      foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name))
329        SelectorParameter.ValidValues.Add(selector);
330      ISelector proportionalSelector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("ProportionalSelector"));
331      if (proportionalSelector != null) SelectorParameter.Value = proportionalSelector;
332
333      foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name))
334        EmigrantsSelectorParameter.ValidValues.Add(selector);
335
336      foreach (IReplacer replacer in ApplicationManager.Manager.GetInstances<IReplacer>().OrderBy(x => x.Name))
337        ImmigrationReplacerParameter.ValidValues.Add(replacer);
338
339      ParameterizeSelectors();
340
341      foreach (IMigrator migrator in ApplicationManager.Manager.GetInstances<IMigrator>().OrderBy(x => x.Name))
342        MigratorParameter.ValidValues.Add(migrator);
343
344      qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
345      islandQualityAnalyzer = new BestAverageWorstQualityAnalyzer();
346      ParameterizeAnalyzers();
347      UpdateAnalyzers();
348
[3356]349      Initialize();
350    }
351
352    public override void Prepare() {
353      if (Problem != null) base.Prepare();
354    }
355
356    #region Events
357    protected override void OnProblemChanged() {
358      ParameterizeStochasticOperator(Problem.SolutionCreator);
[9039]359      ParameterizeStochasticOperatorForIsland(Problem.Evaluator);
[7999]360      foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
[3356]361      ParameterizeSolutionsCreator();
362      ParameterizeMainLoop();
363      ParameterizeSelectors();
[3650]364      ParameterizeAnalyzers();
[3750]365      ParameterizeIterationBasedOperators();
[3356]366      UpdateCrossovers();
367      UpdateMutators();
[3650]368      UpdateAnalyzers();
[3356]369      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
370      base.OnProblemChanged();
371    }
372
373    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
374      ParameterizeStochasticOperator(Problem.SolutionCreator);
375      ParameterizeSolutionsCreator();
376      base.Problem_SolutionCreatorChanged(sender, e);
377    }
378    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
[9039]379      ParameterizeStochasticOperatorForIsland(Problem.Evaluator);
[3356]380      ParameterizeSolutionsCreator();
381      ParameterizeMainLoop();
382      ParameterizeSelectors();
[3650]383      ParameterizeAnalyzers();
[3356]384      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
385      base.Problem_EvaluatorChanged(sender, e);
386    }
387    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
[7999]388      foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
[3750]389      ParameterizeIterationBasedOperators();
[3356]390      UpdateCrossovers();
391      UpdateMutators();
[3650]392      UpdateAnalyzers();
[3356]393      base.Problem_OperatorsChanged(sender, e);
394    }
395    private void ElitesParameter_ValueChanged(object sender, EventArgs e) {
396      Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
397      ParameterizeSelectors();
398    }
399    private void Elites_ValueChanged(object sender, EventArgs e) {
400      ParameterizeSelectors();
401    }
402    private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
403      NumberOfIslands.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
404      ParameterizeSelectors();
405    }
406    private void PopulationSize_ValueChanged(object sender, EventArgs e) {
407      ParameterizeSelectors();
408    }
409    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
410      ParameterizeMainLoop();
411      ParameterizeSelectors();
[3650]412      ParameterizeAnalyzers();
[3356]413    }
414    private void MigrationRateParameter_ValueChanged(object sender, EventArgs e) {
415      MigrationRate.ValueChanged += new EventHandler(MigrationRate_ValueChanged);
416      ParameterizeSelectors();
417    }
418    private void MigrationRate_ValueChanged(object sender, EventArgs e) {
419      ParameterizeSelectors();
420    }
421    #endregion
422
423    #region Helpers
[10142]424    protected virtual void Initialize() {
[3356]425      PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
426      PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
427      MigrationRateParameter.ValueChanged += new EventHandler(MigrationRateParameter_ValueChanged);
428      MigrationRate.ValueChanged += new EventHandler(MigrationRate_ValueChanged);
429      ElitesParameter.ValueChanged += new EventHandler(ElitesParameter_ValueChanged);
430      Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
431      if (Problem != null) {
432        Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
433      }
434    }
[10142]435    protected virtual void ParameterizeSolutionsCreator() {
[3359]436      SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
437      SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
[3356]438    }
[10142]439    protected virtual void ParameterizeMainLoop() {
[3359]440      MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
441      MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
442      MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
443      MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
[3356]444    }
[10142]445    protected virtual void ParameterizeStochasticOperator(IOperator op) {
[6051]446      IStochasticOperator stochasticOp = op as IStochasticOperator;
447      if (stochasticOp != null) {
448        stochasticOp.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
449        stochasticOp.RandomParameter.Hidden = true;
450      }
[3356]451    }
[10142]452    protected virtual void ParameterizeStochasticOperatorForIsland(IOperator op) {
[7395]453      IStochasticOperator stochasticOp = op as IStochasticOperator;
454      if (stochasticOp != null) {
455        stochasticOp.RandomParameter.ActualName = "LocalRandom";
456        stochasticOp.RandomParameter.Hidden = true;
457      }
458    }
[10142]459    protected virtual void ParameterizeSelectors() {
[3689]460      foreach (ISelector selector in SelectorParameter.ValidValues) {
[3356]461        selector.CopySelected = new BoolValue(true);
462        selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * (PopulationSize.Value - Elites.Value));
[6051]463        selector.NumberOfSelectedSubScopesParameter.Hidden = true;
[7395]464        ParameterizeStochasticOperatorForIsland(selector);
[3356]465      }
[3689]466      foreach (ISelector selector in EmigrantsSelectorParameter.ValidValues) {
[3356]467        selector.CopySelected = new BoolValue(true);
468        selector.NumberOfSelectedSubScopesParameter.Value = new IntValue((int)Math.Ceiling(PopulationSize.Value * MigrationRate.Value));
[6051]469        selector.NumberOfSelectedSubScopesParameter.Hidden = true;
[3356]470        ParameterizeStochasticOperator(selector);
471      }
[3689]472      foreach (IReplacer replacer in ImmigrationReplacerParameter.ValidValues) {
[3601]473        ParameterizeStochasticOperator(replacer);
[3356]474      }
475      if (Problem != null) {
[3689]476        foreach (ISingleObjectiveSelector selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
[3356]477          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
[6051]478          selector.MaximizationParameter.Hidden = true;
[3356]479          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
[6051]480          selector.QualityParameter.Hidden = true;
[3356]481        }
[3689]482        foreach (ISingleObjectiveSelector selector in EmigrantsSelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
[3356]483          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
[6051]484          selector.MaximizationParameter.Hidden = true;
[3356]485          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
[6051]486          selector.QualityParameter.Hidden = true;
[3356]487        }
[3689]488        foreach (ISingleObjectiveReplacer selector in ImmigrationReplacerParameter.ValidValues.OfType<ISingleObjectiveReplacer>()) {
[3356]489          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
[6051]490          selector.MaximizationParameter.Hidden = true;
[3356]491          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
[6051]492          selector.QualityParameter.Hidden = true;
[3356]493        }
494      }
495    }
[10142]496    protected virtual void ParameterizeAnalyzers() {
[3650]497      islandQualityAnalyzer.ResultsParameter.ActualName = "Results";
[6051]498      islandQualityAnalyzer.ResultsParameter.Hidden = true;
[3673]499      islandQualityAnalyzer.QualityParameter.Depth = 1;
[3671]500      qualityAnalyzer.ResultsParameter.ActualName = "Results";
[6051]501      qualityAnalyzer.ResultsParameter.Hidden = true;
[3673]502      qualityAnalyzer.QualityParameter.Depth = 2;
503
[3650]504      if (Problem != null) {
505        islandQualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
[6051]506        islandQualityAnalyzer.MaximizationParameter.Hidden = true;
[3650]507        islandQualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
[6051]508        islandQualityAnalyzer.QualityParameter.Hidden = true;
[3650]509        islandQualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
[6051]510        islandQualityAnalyzer.BestKnownQualityParameter.Hidden = true;
[3671]511        qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
[6051]512        qualityAnalyzer.MaximizationParameter.Hidden = true;
[3671]513        qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
[6051]514        qualityAnalyzer.QualityParameter.Hidden = true;
[3671]515        qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
[6051]516        qualityAnalyzer.BestKnownQualityParameter.Hidden = true;
[3650]517      }
518    }
[10142]519    protected virtual void ParameterizeIterationBasedOperators() {
[3750]520      if (Problem != null) {
521        foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {
522          op.IterationsParameter.ActualName = "Generations";
[6051]523          op.IterationsParameter.Hidden = true;
[3750]524          op.MaximumIterationsParameter.ActualName = "MaximumGenerations";
[6051]525          op.MaximumIterationsParameter.Hidden = true;
[3750]526        }
527      }
528    }
[10142]529    protected virtual void UpdateCrossovers() {
[3356]530      ICrossover oldCrossover = CrossoverParameter.Value;
[7511]531      ICrossover defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
[3356]532      CrossoverParameter.ValidValues.Clear();
[7524]533      foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name)) {
534        ParameterizeStochasticOperatorForIsland(crossover);
[3356]535        CrossoverParameter.ValidValues.Add(crossover);
[7524]536      }
[3356]537      if (oldCrossover != null) {
538        ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
539        if (crossover != null) CrossoverParameter.Value = crossover;
[7511]540        else oldCrossover = null;
[3356]541      }
[7511]542      if (oldCrossover == null && defaultCrossover != null)
543        CrossoverParameter.Value = defaultCrossover;
[3356]544    }
[10142]545    protected virtual void UpdateMutators() {
[3356]546      IManipulator oldMutator = MutatorParameter.Value;
547      MutatorParameter.ValidValues.Clear();
[7395]548      foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name)) {
549        ParameterizeStochasticOperatorForIsland(mutator);
[3356]550        MutatorParameter.ValidValues.Add(mutator);
[7395]551      }
[3356]552      if (oldMutator != null) {
553        IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
554        if (mutator != null) MutatorParameter.Value = mutator;
555      }
556    }
[10142]557    protected virtual void UpdateAnalyzers() {
[3650]558      IslandAnalyzer.Operators.Clear();
559      Analyzer.Operators.Clear();
[7172]560      IslandAnalyzer.Operators.Add(islandQualityAnalyzer, islandQualityAnalyzer.EnabledByDefault);
[3650]561      if (Problem != null) {
[3816]562        foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
[3671]563          foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
564            param.Depth = 2;
[7172]565          Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
[3650]566        }
567      }
[7172]568      Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
[3650]569    }
[10142]570    protected virtual IslandGeneticAlgorithmMainLoop FindMainLoop(IOperator start) {
[5366]571      IOperator mainLoop = start;
572      while (mainLoop != null && !(mainLoop is IslandGeneticAlgorithmMainLoop))
573        mainLoop = ((SingleSuccessorOperator)mainLoop).Successor;
574      if (mainLoop == null) return null;
575      else return (IslandGeneticAlgorithmMainLoop)mainLoop;
576    }
[3356]577    #endregion
578  }
579}
Note: See TracBrowser for help on using the repository browser.