Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm/3.3/OffspringSelectionGeneticAlgorithm.cs @ 18242

Last change on this file since 18242 was 17198, checked in by mkommend, 5 years ago

#3020: Adapated AfterDeserializationHook of genetic algorithms to check if the mutator parameter already has the correct type.
In detail the following algorithms have been adapted: ALPS-GA, ALPS-OSGA, GA, Island-GA, NSGA-2, Island-OSGA, OSGA, SASEGASA.

File size: 29.3 KB
RevLine 
[3378]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3378]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;
[17198]24using HEAL.Attic;
[3650]25using HeuristicLab.Analysis;
[4068]26using HeuristicLab.Common;
[3378]27using HeuristicLab.Core;
28using HeuristicLab.Data;
[5356]29using HeuristicLab.Operators;
[3378]30using HeuristicLab.Optimization;
31using HeuristicLab.Optimization.Operators;
32using HeuristicLab.Parameters;
33using HeuristicLab.PluginInfrastructure;
34using HeuristicLab.Random;
35
36namespace HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm {
37  /// <summary>
38  /// An offspring selection genetic algorithm.
39  /// </summary>
[13173]40  [Item("Offspring Selection Genetic Algorithm (OSGA)", "An offspring selection genetic algorithm (Affenzeller, M. et al. 2009. Genetic Algorithms and Genetic Programming - Modern Concepts and Practical Applications. CRC Press).")]
[12504]41  [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 120)]
[16565]42  [StorableType("7D0CF428-70B3-407F-A2AF-D544F115C998")]
[5809]43  public sealed class OffspringSelectionGeneticAlgorithm : HeuristicOptimizationEngineAlgorithm, IStorableContent {
[4437]44    public string Filename { get; set; }
45
[3378]46    #region Problem Properties
47    public override Type ProblemType {
[5809]48      get { return typeof(ISingleObjectiveHeuristicOptimizationProblem); }
[3378]49    }
[5809]50    public new ISingleObjectiveHeuristicOptimizationProblem Problem {
51      get { return (ISingleObjectiveHeuristicOptimizationProblem)base.Problem; }
[3378]52      set { base.Problem = value; }
53    }
54    #endregion
55
56    #region Parameter Properties
[15235]57    public IValueParameter<IntValue> SeedParameter {
[3378]58      get { return (ValueParameter<IntValue>)Parameters["Seed"]; }
59    }
[15235]60    public IValueParameter<BoolValue> SetSeedRandomlyParameter {
[3378]61      get { return (ValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
62    }
[15235]63    public IValueParameter<IntValue> PopulationSizeParameter {
[3378]64      get { return (ValueParameter<IntValue>)Parameters["PopulationSize"]; }
65    }
[8121]66    public IConstrainedValueParameter<ISelector> SelectorParameter {
67      get { return (IConstrainedValueParameter<ISelector>)Parameters["Selector"]; }
[3378]68    }
[8121]69    public IConstrainedValueParameter<ICrossover> CrossoverParameter {
70      get { return (IConstrainedValueParameter<ICrossover>)Parameters["Crossover"]; }
[3378]71    }
[15235]72    public IValueParameter<PercentValue> MutationProbabilityParameter {
[3378]73      get { return (ValueParameter<PercentValue>)Parameters["MutationProbability"]; }
74    }
[8121]75    public IConstrainedValueParameter<IManipulator> MutatorParameter {
76      get { return (IConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; }
[3378]77    }
[15235]78    public IValueParameter<IntValue> ElitesParameter {
[3378]79      get { return (ValueParameter<IntValue>)Parameters["Elites"]; }
80    }
[15235]81    public IFixedValueParameter<BoolValue> ReevaluateElitesParameter {
[9569]82      get { return (IFixedValueParameter<BoolValue>)Parameters["ReevaluateElites"]; }
83    }
[15235]84    public IValueParameter<IntValue> MaximumGenerationsParameter {
[3378]85      get { return (ValueParameter<IntValue>)Parameters["MaximumGenerations"]; }
86    }
[15235]87    public IValueLookupParameter<DoubleValue> SuccessRatioParameter {
[3379]88      get { return (ValueLookupParameter<DoubleValue>)Parameters["SuccessRatio"]; }
89    }
[15235]90    public IValueLookupParameter<DoubleValue> ComparisonFactorLowerBoundParameter {
[3379]91      get { return (ValueLookupParameter<DoubleValue>)Parameters["ComparisonFactorLowerBound"]; }
92    }
[15235]93    public IValueLookupParameter<DoubleValue> ComparisonFactorUpperBoundParameter {
[3379]94      get { return (ValueLookupParameter<DoubleValue>)Parameters["ComparisonFactorUpperBound"]; }
95    }
[8121]96    public IConstrainedValueParameter<IDiscreteDoubleValueModifier> ComparisonFactorModifierParameter {
97      get { return (IConstrainedValueParameter<IDiscreteDoubleValueModifier>)Parameters["ComparisonFactorModifier"]; }
[3379]98    }
[15235]99    public IValueLookupParameter<DoubleValue> MaximumSelectionPressureParameter {
[3379]100      get { return (ValueLookupParameter<DoubleValue>)Parameters["MaximumSelectionPressure"]; }
101    }
[15235]102    public IValueLookupParameter<BoolValue> OffspringSelectionBeforeMutationParameter {
[3379]103      get { return (ValueLookupParameter<BoolValue>)Parameters["OffspringSelectionBeforeMutation"]; }
104    }
[15235]105    public IValueLookupParameter<IntValue> SelectedParentsParameter {
[3510]106      get { return (ValueLookupParameter<IntValue>)Parameters["SelectedParents"]; }
107    }
[15235]108    public IValueParameter<MultiAnalyzer> AnalyzerParameter {
[3658]109      get { return (ValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
[3650]110    }
[15235]111    public IValueParameter<IntValue> MaximumEvaluatedSolutionsParameter {
[3891]112      get { return (ValueParameter<IntValue>)Parameters["MaximumEvaluatedSolutions"]; }
113    }
[15235]114    public IFixedValueParameter<BoolValue> FillPopulationWithParentsParameter {
[10643]115      get { return (IFixedValueParameter<BoolValue>)Parameters["FillPopulationWithParents"]; }
116    }
[3378]117    #endregion
118
119    #region Properties
120    public IntValue Seed {
121      get { return SeedParameter.Value; }
122      set { SeedParameter.Value = value; }
123    }
124    public BoolValue SetSeedRandomly {
125      get { return SetSeedRandomlyParameter.Value; }
126      set { SetSeedRandomlyParameter.Value = value; }
127    }
128    public IntValue PopulationSize {
129      get { return PopulationSizeParameter.Value; }
130      set { PopulationSizeParameter.Value = value; }
131    }
132    public ISelector Selector {
133      get { return SelectorParameter.Value; }
134      set { SelectorParameter.Value = value; }
135    }
136    public ICrossover Crossover {
137      get { return CrossoverParameter.Value; }
138      set { CrossoverParameter.Value = value; }
139    }
140    public PercentValue MutationProbability {
141      get { return MutationProbabilityParameter.Value; }
142      set { MutationProbabilityParameter.Value = value; }
143    }
144    public IManipulator Mutator {
145      get { return MutatorParameter.Value; }
146      set { MutatorParameter.Value = value; }
147    }
148    public IntValue Elites {
149      get { return ElitesParameter.Value; }
150      set { ElitesParameter.Value = value; }
151    }
[9569]152    public bool ReevaluteElites {
153      get { return ReevaluateElitesParameter.Value.Value; }
154      set { ReevaluateElitesParameter.Value.Value = value; }
155    }
[3378]156    public IntValue MaximumGenerations {
157      get { return MaximumGenerationsParameter.Value; }
158      set { MaximumGenerationsParameter.Value = value; }
159    }
[3510]160    public DoubleValue SuccessRatio {
[3379]161      get { return SuccessRatioParameter.Value; }
162      set { SuccessRatioParameter.Value = value; }
163    }
[3510]164    public DoubleValue ComparisonFactorLowerBound {
[3379]165      get { return ComparisonFactorLowerBoundParameter.Value; }
166      set { ComparisonFactorLowerBoundParameter.Value = value; }
167    }
[3510]168    public DoubleValue ComparisonFactorUpperBound {
[3379]169      get { return ComparisonFactorUpperBoundParameter.Value; }
170      set { ComparisonFactorUpperBoundParameter.Value = value; }
171    }
[3510]172    public IDiscreteDoubleValueModifier ComparisonFactorModifier {
[3379]173      get { return ComparisonFactorModifierParameter.Value; }
174      set { ComparisonFactorModifierParameter.Value = value; }
175    }
[3510]176    public DoubleValue MaximumSelectionPressure {
[3379]177      get { return MaximumSelectionPressureParameter.Value; }
178      set { MaximumSelectionPressureParameter.Value = value; }
179    }
[3510]180    public BoolValue OffspringSelectionBeforeMutation {
[3379]181      get { return OffspringSelectionBeforeMutationParameter.Value; }
182      set { OffspringSelectionBeforeMutationParameter.Value = value; }
183    }
[3510]184    public IntValue SelectedParents {
185      get { return SelectedParentsParameter.Value; }
186      set { SelectedParentsParameter.Value = value; }
187    }
[3658]188    public MultiAnalyzer Analyzer {
[3650]189      get { return AnalyzerParameter.Value; }
190      set { AnalyzerParameter.Value = value; }
191    }
[3891]192    public IntValue MaximumEvaluatedSolutions {
193      get { return MaximumEvaluatedSolutionsParameter.Value; }
194      set { MaximumEvaluatedSolutionsParameter.Value = value; }
195    }
[10643]196    public bool FillPopulationWithParents {
197      get { return FillPopulationWithParentsParameter.Value.Value; }
198      set { FillPopulationWithParentsParameter.Value.Value = value; }
199    }
[3378]200    private RandomCreator RandomCreator {
201      get { return (RandomCreator)OperatorGraph.InitialOperator; }
202    }
203    private SolutionsCreator SolutionsCreator {
204      get { return (SolutionsCreator)RandomCreator.Successor; }
205    }
206    private OffspringSelectionGeneticAlgorithmMainLoop MainLoop {
[5366]207      get { return FindMainLoop(SolutionsCreator.Successor); }
[3378]208    }
[3689]209    [Storable]
[3672]210    private BestAverageWorstQualityAnalyzer qualityAnalyzer;
[3689]211    [Storable]
[3672]212    private ValueAnalyzer selectionPressureAnalyzer;
[5683]213    [Storable]
214    private SuccessfulOffspringAnalyzer successfulOffspringAnalyzer;
[3378]215    #endregion
216
[3379]217    [StorableConstructor]
[16565]218    private OffspringSelectionGeneticAlgorithm(StorableConstructorFlag _) : base(_) { }
[4722]219    [StorableHook(HookType.AfterDeserialization)]
220    private void AfterDeserialization() {
[9592]221      // BackwardsCompatibility3.3
222      #region Backwards compatible code, remove with 3.4
[5683]223      if (successfulOffspringAnalyzer == null)
224        successfulOffspringAnalyzer = new SuccessfulOffspringAnalyzer();
[9569]225      if (!Parameters.ContainsKey("ReevaluateElites")) {
226        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 });
227      }
[10643]228      if (!Parameters.ContainsKey("FillPopulationWithParents"))
229        Parameters.Add(new FixedValueParameter<BoolValue>("FillPopulationWithParents", "True if the population should be filled with parent individual or false if worse children should be used when the maximum selection pressure is exceeded.", new BoolValue(false)) { Hidden = true });
[15049]230
231      var optionalMutatorParameter = MutatorParameter as OptionalConstrainedValueParameter<IManipulator>;
[17198]232      var mutatorParameter = MutatorParameter as ConstrainedValueParameter<IManipulator>;
233      if (mutatorParameter == null && optionalMutatorParameter != null) {
[15049]234        Parameters.Remove(optionalMutatorParameter);
235        Parameters.Add(new ConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
236        foreach (var m in optionalMutatorParameter.ValidValues)
237          MutatorParameter.ValidValues.Add(m);
238        if (optionalMutatorParameter.Value == null) MutationProbability.Value = 0; // to guarantee that the old configuration results in the same behavior
239        else Mutator = optionalMutatorParameter.Value;
240        optionalMutatorParameter.ValidValues.Clear(); // to avoid dangling references to the old parameter its valid values are cleared
241      }
[5683]242      #endregion
[7172]243
[4722]244      Initialize();
245    }
246    private OffspringSelectionGeneticAlgorithm(OffspringSelectionGeneticAlgorithm original, Cloner cloner)
247      : base(original, cloner) {
248      qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
249      selectionPressureAnalyzer = cloner.Clone(original.selectionPressureAnalyzer);
[5683]250      successfulOffspringAnalyzer = cloner.Clone(original.successfulOffspringAnalyzer);
[4722]251      Initialize();
252    }
253    public override IDeepCloneable Clone(Cloner cloner) {
254      return new OffspringSelectionGeneticAlgorithm(this, cloner);
255    }
[3378]256    public OffspringSelectionGeneticAlgorithm()
257      : base() {
258      Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
259      Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
260      Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "The size of the population of solutions.", new IntValue(100)));
261      Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
262      Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
263      Parameters.Add(new ValueParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new PercentValue(0.05)));
[15049]264      Parameters.Add(new ConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
[3378]265      Parameters.Add(new ValueParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation.", new IntValue(1)));
[9569]266      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 });
[3378]267      Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed.", new IntValue(1000)));
[3379]268      Parameters.Add(new ValueLookupParameter<DoubleValue>("SuccessRatio", "The ratio of successful to total children that should be achieved.", new DoubleValue(1)));
269      Parameters.Add(new ValueLookupParameter<DoubleValue>("ComparisonFactorLowerBound", "The lower bound of the comparison factor (start).", new DoubleValue(0)));
270      Parameters.Add(new ValueLookupParameter<DoubleValue>("ComparisonFactorUpperBound", "The upper bound of the comparison factor (end).", new DoubleValue(1)));
[3426]271      Parameters.Add(new OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier>("ComparisonFactorModifier", "The operator used to modify the comparison factor.", new ItemSet<IDiscreteDoubleValueModifier>(new IDiscreteDoubleValueModifier[] { new LinearDiscreteDoubleValueModifier() }), new LinearDiscreteDoubleValueModifier()));
[3379]272      Parameters.Add(new ValueLookupParameter<DoubleValue>("MaximumSelectionPressure", "The maximum selection pressure that terminates the algorithm.", new DoubleValue(100)));
273      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)));
[3735]274      Parameters.Add(new ValueLookupParameter<IntValue>("SelectedParents", "How much parents should be selected each time the offspring selection step is performed until the population is filled. This parameter should be about the same or twice the size of PopulationSize for smaller problems, and less for large problems.", new IntValue(200)));
[3658]275      Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze each generation.", new MultiAnalyzer()));
[3891]276      Parameters.Add(new ValueParameter<IntValue>("MaximumEvaluatedSolutions", "The maximum number of evaluated solutions (approximately).", new IntValue(int.MaxValue)));
[10643]277      Parameters.Add(new FixedValueParameter<BoolValue>("FillPopulationWithParents", "True if the population should be filled with parent individual or false if worse children should be used when the maximum selection pressure is exceeded.", new BoolValue(false)) { Hidden = true });
[3891]278
[3378]279      RandomCreator randomCreator = new RandomCreator();
280      SolutionsCreator solutionsCreator = new SolutionsCreator();
[5356]281      SubScopesCounter subScopesCounter = new SubScopesCounter();
282      ResultsCollector resultsCollector = new ResultsCollector();
[3378]283      OffspringSelectionGeneticAlgorithmMainLoop mainLoop = new OffspringSelectionGeneticAlgorithmMainLoop();
284      OperatorGraph.InitialOperator = randomCreator;
285
286      randomCreator.RandomParameter.ActualName = "Random";
287      randomCreator.SeedParameter.ActualName = SeedParameter.Name;
288      randomCreator.SeedParameter.Value = null;
289      randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
290      randomCreator.SetSeedRandomlyParameter.Value = null;
291      randomCreator.Successor = solutionsCreator;
292
293      solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
[5356]294      solutionsCreator.Successor = subScopesCounter;
[3378]295
[5356]296      subScopesCounter.Name = "Initialize EvaluatedSolutions";
297      subScopesCounter.ValueParameter.ActualName = "EvaluatedSolutions";
298      subScopesCounter.Successor = resultsCollector;
299
300      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", "", "EvaluatedSolutions"));
301      resultsCollector.ResultsParameter.ActualName = "Results";
302      resultsCollector.Successor = mainLoop;
303
[3744]304      mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
305      mainLoop.ComparisonFactorModifierParameter.ActualName = ComparisonFactorModifierParameter.Name;
306      mainLoop.ComparisonFactorParameter.ActualName = "ComparisonFactor";
307      mainLoop.ComparisonFactorStartParameter.ActualName = ComparisonFactorLowerBoundParameter.Name;
[3378]308      mainLoop.CrossoverParameter.ActualName = CrossoverParameter.Name;
309      mainLoop.ElitesParameter.ActualName = ElitesParameter.Name;
[9569]310      mainLoop.ReevaluateElitesParameter.ActualName = ReevaluateElitesParameter.Name;
[5356]311      mainLoop.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
[3378]312      mainLoop.MaximumGenerationsParameter.ActualName = MaximumGenerationsParameter.Name;
[3744]313      mainLoop.MaximumSelectionPressureParameter.ActualName = MaximumSelectionPressureParameter.Name;
314      mainLoop.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
[3378]315      mainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
[3744]316      mainLoop.OffspringSelectionBeforeMutationParameter.ActualName = OffspringSelectionBeforeMutationParameter.Name;
[3378]317      mainLoop.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
318      mainLoop.ResultsParameter.ActualName = "Results";
[3744]319      mainLoop.SelectorParameter.ActualName = SelectorParameter.Name;
320      mainLoop.SuccessRatioParameter.ActualName = SuccessRatioParameter.Name;
[10643]321      mainLoop.FillPopulationWithParentsParameter.ActualName = FillPopulationWithParentsParameter.Name;
[3378]322
[3689]323      foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name))
324        SelectorParameter.ValidValues.Add(selector);
325      ISelector proportionalSelector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("ProportionalSelector"));
326      if (proportionalSelector != null) SelectorParameter.Value = proportionalSelector;
327      ParameterizeSelectors();
328
329      foreach (IDiscreteDoubleValueModifier modifier in ApplicationManager.Manager.GetInstances<IDiscreteDoubleValueModifier>().OrderBy(x => x.Name))
330        ComparisonFactorModifierParameter.ValidValues.Add(modifier);
331      IDiscreteDoubleValueModifier linearModifier = ComparisonFactorModifierParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("LinearDiscreteDoubleValueModifier"));
332      if (linearModifier != null) ComparisonFactorModifierParameter.Value = linearModifier;
333      ParameterizeComparisonFactorModifiers();
334
335      qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
336      selectionPressureAnalyzer = new ValueAnalyzer();
[5683]337      successfulOffspringAnalyzer = new SuccessfulOffspringAnalyzer();
[3689]338      ParameterizeAnalyzers();
339      UpdateAnalyzers();
340
[3378]341      Initialize();
342    }
343
344
[4722]345
[3378]346    public override void Prepare() {
347      if (Problem != null) base.Prepare();
348    }
349
350    #region Events
351    protected override void OnProblemChanged() {
352      ParameterizeStochasticOperator(Problem.SolutionCreator);
353      ParameterizeStochasticOperator(Problem.Evaluator);
[7999]354      foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
[3378]355      ParameterizeSolutionsCreator();
[3379]356      ParameterizMainLoop();
[3378]357      ParameterizeSelectors();
[3650]358      ParameterizeAnalyzers();
[3750]359      ParameterizeIterationBasedOperators();
[3378]360      UpdateCrossovers();
361      UpdateMutators();
[3650]362      UpdateAnalyzers();
[3378]363      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
364      base.OnProblemChanged();
365    }
366
367    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
368      ParameterizeStochasticOperator(Problem.SolutionCreator);
369      ParameterizeSolutionsCreator();
370      base.Problem_SolutionCreatorChanged(sender, e);
371    }
372    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
373      ParameterizeStochasticOperator(Problem.Evaluator);
374      ParameterizeSolutionsCreator();
[3379]375      ParameterizMainLoop();
[3378]376      ParameterizeSelectors();
[3650]377      ParameterizeAnalyzers();
[3378]378      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
379      base.Problem_EvaluatorChanged(sender, e);
380    }
381    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
[7999]382      foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
[3750]383      ParameterizeIterationBasedOperators();
[3378]384      UpdateCrossovers();
385      UpdateMutators();
[3650]386      UpdateAnalyzers();
[3378]387      base.Problem_OperatorsChanged(sender, e);
388    }
389    private void ElitesParameter_ValueChanged(object sender, EventArgs e) {
390      Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
391      ParameterizeSelectors();
392    }
393    private void Elites_ValueChanged(object sender, EventArgs e) {
394      ParameterizeSelectors();
395    }
396    private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
397      PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
398      ParameterizeSelectors();
399    }
400    private void PopulationSize_ValueChanged(object sender, EventArgs e) {
401      ParameterizeSelectors();
402    }
403    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
[3379]404      ParameterizMainLoop();
[3378]405      ParameterizeSelectors();
[3650]406      ParameterizeAnalyzers();
[3378]407    }
408    #endregion
409
410    #region Helpers
411    private void Initialize() {
412      PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
413      PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
414      ElitesParameter.ValueChanged += new EventHandler(ElitesParameter_ValueChanged);
415      Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
416      if (Problem != null) {
417        Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
418      }
419    }
420    private void ParameterizeSolutionsCreator() {
421      SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
422      SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
423    }
[3379]424    private void ParameterizMainLoop() {
[3378]425      MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
426      MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
427      MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
428    }
429    private void ParameterizeStochasticOperator(IOperator op) {
430      if (op is IStochasticOperator)
431        ((IStochasticOperator)op).RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
432    }
433    private void ParameterizeSelectors() {
[3689]434      foreach (ISelector selector in SelectorParameter.ValidValues) {
[3378]435        selector.CopySelected = new BoolValue(true);
[3510]436        selector.NumberOfSelectedSubScopesParameter.Value = null;
437        selector.NumberOfSelectedSubScopesParameter.ActualName = SelectedParentsParameter.Name;
[3378]438        ParameterizeStochasticOperator(selector);
439      }
440      if (Problem != null) {
[3689]441        foreach (ISingleObjectiveSelector selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
[3378]442          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
443          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
444        }
445      }
446    }
[3650]447    private void ParameterizeAnalyzers() {
[3672]448      qualityAnalyzer.ResultsParameter.ActualName = "Results";
449      selectionPressureAnalyzer.Name = "SelectionPressure Analyzer";
450      selectionPressureAnalyzer.ResultsParameter.ActualName = "Results";
451      selectionPressureAnalyzer.ValueParameter.ActualName = "SelectionPressure";
452      selectionPressureAnalyzer.ValueParameter.Depth = 0;
453      selectionPressureAnalyzer.ValuesParameter.ActualName = "Selection Pressure History";
[5683]454      successfulOffspringAnalyzer.ResultsParameter.ActualName = "Results";
455      successfulOffspringAnalyzer.GenerationsParameter.ActualName = "Generations";
[5725]456      successfulOffspringAnalyzer.SuccessfulOffspringFlagParameter.Value.Value = "SuccessfulOffspring";
[5683]457      successfulOffspringAnalyzer.DepthParameter.Value = new IntValue(1);
[3650]458      if (Problem != null) {
[3672]459        qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
460        qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
461        qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
[3650]462      }
463    }
[3379]464    private void ParameterizeComparisonFactorModifiers() {
[3689]465      foreach (IDiscreteDoubleValueModifier modifier in ComparisonFactorModifierParameter.ValidValues) {
[3413]466        modifier.IndexParameter.ActualName = "Generations";
[3379]467        modifier.EndIndexParameter.ActualName = MaximumGenerationsParameter.Name;
468        modifier.EndValueParameter.ActualName = ComparisonFactorUpperBoundParameter.Name;
469        modifier.StartIndexParameter.Value = new IntValue(0);
470        modifier.StartValueParameter.ActualName = ComparisonFactorLowerBoundParameter.Name;
[3413]471        modifier.ValueParameter.ActualName = "ComparisonFactor";
[3379]472      }
473    }
[3750]474    private void ParameterizeIterationBasedOperators() {
475      if (Problem != null) {
476        foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {
477          op.IterationsParameter.ActualName = "Generations";
478          op.MaximumIterationsParameter.ActualName = MaximumGenerationsParameter.Name;
479        }
480      }
481    }
[3378]482    private void UpdateCrossovers() {
483      ICrossover oldCrossover = CrossoverParameter.Value;
484      CrossoverParameter.ValidValues.Clear();
[7493]485      ICrossover defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
486
[3378]487      foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name))
488        CrossoverParameter.ValidValues.Add(crossover);
[7493]489
[3378]490      if (oldCrossover != null) {
491        ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
492        if (crossover != null) CrossoverParameter.Value = crossover;
[7509]493        else oldCrossover = null;
494      }
495      if (oldCrossover == null && defaultCrossover != null)
[7493]496        CrossoverParameter.Value = defaultCrossover;
[3378]497    }
498    private void UpdateMutators() {
499      IManipulator oldMutator = MutatorParameter.Value;
500      MutatorParameter.ValidValues.Clear();
[15049]501      IManipulator defaultMutator = Problem.Operators.OfType<IManipulator>().FirstOrDefault();
502
[3378]503      foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
504        MutatorParameter.ValidValues.Add(mutator);
[15049]505
[3378]506      if (oldMutator != null) {
507        IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
508        if (mutator != null) MutatorParameter.Value = mutator;
[15049]509        else oldMutator = null;
[3378]510      }
[15049]511
512      if (oldMutator == null && defaultMutator != null)
513        MutatorParameter.Value = defaultMutator;
[3378]514    }
[3650]515    private void UpdateAnalyzers() {
516      Analyzer.Operators.Clear();
517      if (Problem != null) {
[3816]518        foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
[3672]519          foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
520            param.Depth = 1;
[7172]521          Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
[3672]522        }
[3650]523      }
[7172]524      Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
525      Analyzer.Operators.Add(selectionPressureAnalyzer, selectionPressureAnalyzer.EnabledByDefault);
526      Analyzer.Operators.Add(successfulOffspringAnalyzer, successfulOffspringAnalyzer.EnabledByDefault);
[3650]527    }
[5366]528    private OffspringSelectionGeneticAlgorithmMainLoop FindMainLoop(IOperator start) {
529      IOperator mainLoop = start;
530      while (mainLoop != null && !(mainLoop is OffspringSelectionGeneticAlgorithmMainLoop))
531        mainLoop = ((SingleSuccessorOperator)mainLoop).Successor;
532      if (mainLoop == null) return null;
533      else return (OffspringSelectionGeneticAlgorithmMainLoop)mainLoop;
534    }
[3378]535    #endregion
536  }
537}
Note: See TracBrowser for help on using the repository browser.