Changeset 12531


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

#2269 Added Termination Criteria to standard ALPS-GA.

Location:
branches/ALPS/HeuristicLab.Algorithms.ALPS/3.3
Files:
3 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  }
  • branches/ALPS/HeuristicLab.Algorithms.ALPS/3.3/AlpsGeneticAlgorithm.cs

    r12271 r12531  
    2121
    2222using System;
     23using System.Collections.Generic;
    2324using System.Linq;
    2425using HeuristicLab.Common;
     
    4344      get { return (IValueParameter<IntArray>)Parameters["PopulationSize"]; }
    4445    }
    45     private IValueParameter<IntValue> MaximumGenerationsParameter {
    46       get { return (IValueParameter<IntValue>)Parameters["MaximumGenerations"]; }
    47     }
    4846    public IConstrainedValueParameter<ISelector> SelectorParameter {
    4947      get { return (IConstrainedValueParameter<ISelector>)Parameters["Selector"]; }
     
    7472      set { PopulationSizeParameter.Value = value; }
    7573    }
    76     public IntValue MaximumGenerations {
    77       get { return MaximumGenerationsParameter.Value; }
    78       set { MaximumGenerationsParameter.Value = value; }
    79     }
    80 
    8174    public ISelector Selector {
    8275      get { return SelectorParameter.Value; }
     
    112105    }
    113106    #endregion
     107
     108    [Storable]
     109    private ComparisonTerminator<IntValue> generationsTerminator;
    114110
    115111    [StorableConstructor]
     
    118114    private AlpsGeneticAlgorithm(AlpsGeneticAlgorithm original, Cloner cloner)
    119115      : base(original, cloner) {
     116      generationsTerminator = cloner.Clone(original.generationsTerminator);
    120117      Initialize();
    121118    }
     
    126123      : base() {
    127124      Parameters.Add(new ValueParameter<IntArray>("PopulationSize", "The size of the population of solutions each layer.", new IntArray(new[] { 100 })));
    128       Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations that should be processed.", new IntValue(1000)));
    129125      Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
    130126      Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
     
    274270          @operator.IterationsParameter.ActualName = "Generations";
    275271          @operator.IterationsParameter.Hidden = true;
    276           @operator.MaximumIterationsParameter.ActualName = MaximumGenerationsParameter.Name;
     272          @operator.MaximumIterationsParameter.ActualName = generationsTerminator.ThresholdParameter.Name;
    277273          @operator.MaximumIterationsParameter.Hidden = true;
    278274        }
     
    291287
    292288    #region Updates
     289    protected override void UpdateTerminators() {
     290      var newTerminators = new Dictionary<ITerminator, bool> {
     291        {generationsTerminator, !Terminators.Operators.Contains(generationsTerminator) || Terminators.Operators.ItemChecked(generationsTerminator)},
     292      };
     293
     294      base.UpdateTerminators();
     295
     296      foreach (var newTerminator in newTerminators)
     297        Terminators.Operators.Insert(0, newTerminator.Key, newTerminator.Value);
     298    }
     299    protected override void CreateTerminators() {
     300      generationsTerminator = new ComparisonTerminator<IntValue>("Generations", ComparisonType.Less, new IntValue(1000)) { Name = "Generations" };
     301      base.CreateTerminators();
     302    }
    293303    private void UpdateCrossovers() {
    294304      var oldCrossover = CrossoverParameter.Value;
  • branches/ALPS/HeuristicLab.Algorithms.ALPS/3.3/AlpsGeneticAlgorithmMainLoop.cs

    r12271 r12531  
    100100      var layerAnalyzerPlaceholder = new Placeholder() { Name = "LayerAnalyzer (Placeholder)" };
    101101      var analyzerPlaceholder = new Placeholder() { Name = "Analyzer (Placeholder)" };
    102       var generationsComparator = new Comparator() { Name = "Generations >= MaximumGenerations" };
    103       var terminateBranch = new ConditionalBranch() { Name = "Terminate?" };
     102      var termination = new TerminationOperator();
    104103
    105104      OperatorGraph.InitialOperator = variableCreator;
     
    165164
    166165      analyzerPlaceholder.OperatorParameter.ActualName = AnalyzerParameter.Name;
    167       analyzerPlaceholder.Successor = generationsComparator;
    168 
    169       generationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
    170       generationsComparator.LeftSideParameter.ActualName = "Generations";
    171       generationsComparator.RightSideParameter.ActualName = MaximumGenerationsParameter.Name;
    172       generationsComparator.ResultParameter.ActualName = "TerminateGenerations";
    173       generationsComparator.Successor = terminateBranch;
    174 
    175       terminateBranch.ConditionParameter.ActualName = "TerminateGenerations";
    176       terminateBranch.FalseBranch = matingPoolPreProcessor;
     166      analyzerPlaceholder.Successor = termination;
     167
     168      termination.ContinueBranch = matingPoolPreProcessor;
    177169    }
    178170
Note: See TracChangeset for help on using the changeset viewer.