Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/29/15 16:27:22 (9 years ago)
Author:
pfleck
Message:

#2269 Added Termination Criteria to standard ALPS-GA.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/ALPS/HeuristicLab.Algorithms.ALPS/3.3/Alps.cs

    r12271 r12531  
    2121
    2222using System;
     23using System.Collections.Generic;
    2324using System.Linq;
    2425using HeuristicLab.Analysis;
     
    8788      get { return (IValueLookupParameter<PercentValue>)Parameters["MatingPoolSelectionPercentage"]; }
    8889    }
     90    private IValueParameter<MultiTerminator> TerminatorParameter {
     91      get { return (IValueParameter<MultiTerminator>)Parameters["Terminator"]; }
     92    }
    8993    #endregion
    9094
     
    135139      get { return MatingPoolSelectionPercentageParameter.Value; }
    136140      set { MatingPoolSelectionPercentageParameter.Value = value; }
     141    }
     142    public MultiTerminator Terminators {
     143      get { return TerminatorParameter.Value; }
    137144    }
    138145    #endregion
     
    152159    [Storable]
    153160    private BestAverageWorstQualityAnalyzer layerQualityAnalyzer;
     161    #endregion
     162    #region Preconfigured Terminators
     163    [Storable]
     164    private ComparisonTerminator<IntValue> evaluationsTerminator;
     165    [Storable]
     166    private SingleObjectiveQualityTerminator qualityTerminator;
     167    [Storable]
     168    private ExecutionTimeTerminator executionTimeTerminator;
    154169    #endregion
    155170
     
    161176      qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
    162177      layerQualityAnalyzer = cloner.Clone(original.layerQualityAnalyzer);
     178      evaluationsTerminator = cloner.Clone(original.evaluationsTerminator);
     179      qualityTerminator = cloner.Clone(original.qualityTerminator);
     180      executionTimeTerminator = cloner.Clone(original.executionTimeTerminator);
    163181      Initialize();
    164182    }
     
    178196      Parameters.Add(new ValueParameter<IntValue>("MatingPoolRange", "The range of layers used for creating a mating pool. (1 = current + previous layer)", new IntValue(1)) { Hidden = true });
    179197      Parameters.Add(new ValueParameter<PercentValue>("MatingPoolSelectionPercentage", "Percentage of the previous layers used for creating a mating pool.", new PercentValue(1.0, restrictToUnitInterval: true)) { Hidden = true });
     198      Parameters.Add(new ValueParameter<MultiTerminator>("Terminator", "The termination criteria which sould be checked.", new MultiTerminator()));
    180199
    181200      qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
     
    187206      ParameterizeAnalyzers();
    188207      UpdateAnalyzers();
     208
     209      CreateTerminators();
     210      ParameterizeTerminators();
     211      UpdateTerminators();
    189212
    190213      Initialize();
     
    206229      ParameterizeSolutionsCreator();
    207230      ParameterizeAnalyzers();
     231      ParameterizeTerminators();
    208232      UpdateAnalyzers();
     233      UpdateTerminators();
    209234      Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
    210235    }
     236    protected override void RegisterProblemEvents() {
     237      base.RegisterProblemEvents();
     238      var maximizationParameter = (IValueParameter<BoolValue>)Problem.MaximizationParameter;
     239      if (maximizationParameter != null) maximizationParameter.ValueChanged += new EventHandler(MaximizationParameter_ValueChanged);
     240    }
     241    protected override void DeregisterProblemEvents() {
     242      var maximizationParameter = (IValueParameter<BoolValue>)Problem.MaximizationParameter;
     243      if (maximizationParameter != null) maximizationParameter.ValueChanged -= new EventHandler(MaximizationParameter_ValueChanged);
     244      base.DeregisterProblemEvents();
     245    }
     246
    211247    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
    212248      base.Problem_SolutionCreatorChanged(sender, e);
     
    226262        ParameterizeStochasticOperator(@operator);
    227263      UpdateAnalyzers();
     264      UpdateTerminators();
    228265      base.Problem_OperatorsChanged(sender, e);
    229266    }
    230267    protected virtual void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
    231268      ParameterizeAnalyzers();
     269    }
     270    private void MaximizationParameter_ValueChanged(object sender, EventArgs e) {
     271      ParameterizeTerminators();
     272    }
     273    private void QualityAnalyzer_CurrentBestQualityParameter_NameChanged(object sender, EventArgs e) {
     274      ParameterizeTerminators();
    232275    }
    233276
     
    296339      Analyzer.Operators.ItemsAdded += AnalyzerOperators_ItemsAdded;
    297340      LayerAnalyzer.Operators.ItemsAdded += LayerAnalyzerOperators_ItemsAdded;
     341      qualityAnalyzer.CurrentBestQualityParameter.NameChanged += new EventHandler(QualityAnalyzer_CurrentBestQualityParameter_NameChanged);
    298342    }
    299343    private void ParameterizeSolutionsCreator() {
     
    323367      }
    324368    }
    325 
     369    private void ParameterizeTerminators() {
     370      qualityTerminator.Parameterize(qualityAnalyzer.CurrentBestQualityParameter, Problem);
     371    }
    326372    private void ParameterizeAgeInheritanceReduction() {
    327373      AgeInheritanceReduction.Value = GetAgeInheritanceReduction(AgeInheritance.Value);
     
    364410      }
    365411    }
     412    protected virtual void UpdateTerminators() {
     413      var newTerminators = new Dictionary<ITerminator, bool> {
     414        {evaluationsTerminator, Terminators.Operators.Contains(evaluationsTerminator) && Terminators.Operators.ItemChecked(evaluationsTerminator)},
     415        {qualityTerminator, Terminators.Operators.Contains(qualityTerminator) && Terminators.Operators.ItemChecked(qualityTerminator) },
     416        {executionTimeTerminator, Terminators.Operators.Contains(executionTimeTerminator) && Terminators.Operators.ItemChecked(executionTimeTerminator)}
     417      };
     418      if (Problem != null) {
     419        foreach (var terminator in Problem.Operators.OfType<ITerminator>())
     420          newTerminators.Add(terminator, !Terminators.Operators.Contains(terminator) || Terminators.Operators.ItemChecked(terminator));
     421      }
     422
     423      Terminators.Operators.Clear();
     424
     425      foreach (var newTerminator in newTerminators)
     426        Terminators.Operators.Add(newTerminator.Key, newTerminator.Value);
     427    }
     428    protected virtual void CreateTerminators() {
     429      evaluationsTerminator = new ComparisonTerminator<IntValue>("EvaluatedSolutions", ComparisonType.Less, new IntValue(int.MaxValue)) { Name = "Evaluations" };
     430      qualityTerminator = new SingleObjectiveQualityTerminator() { Name = "Quality" };
     431      executionTimeTerminator = new ExecutionTimeTerminator(this, new TimeSpanValue(TimeSpan.FromMinutes(5)));
     432    }
    366433    #endregion
    367434  }
Note: See TracChangeset for help on using the changeset viewer.