Changeset 12271


Ignore:
Timestamp:
03/31/15 13:18:59 (4 years ago)
Author:
pfleck
Message:

#2269 Added AgeInheritance as own type and value for consistent configuration.

Location:
branches/ALPS
Files:
1 added
1 deleted
7 edited

Legend:

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

    r12270 r12271  
    131131      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 });
    132132      Parameters.Add(new ValueParameter<IntValue>("BatchSize", "Number of inner iterations before updates and analyzers are fired.", new IntValue(100)) { Hidden = true });
    133       AgeInheritance = new ReductionOperation(ReductionOperations.Min);
    134133
    135134      var randomCreator = new RandomCreator();
     
    282281    }
    283282
     283    protected override ReductionOperations GetAgeInheritanceReduction(AgeInheritance ageInheritance) {
     284      switch (ageInheritance) {
     285        case ALPS.AgeInheritance.Older: return ReductionOperations.Min;
     286        case ALPS.AgeInheritance.Agerage: return ReductionOperations.Avg;
     287        case ALPS.AgeInheritance.Younger: return ReductionOperations.Max;
     288        default: throw new NotSupportedException("AgeInheritance " + ageInheritance + " is not supported.");
     289      }
     290    }
     291
    284292    protected override void ParameterizeStochasticOperator(IOperator @operator) {
    285293      var stochasticOperator = @operator as IStochasticOperator;
  • branches/ALPS/HeuristicLab.Algorithms.ALPS.SteadyState/3.3/AlpsSsGeneticAlgorithmMainOperator.cs

    r12259 r12271  
    4848      var mutator = new Placeholder() { Name = "Mutator (Placeholder)" };
    4949      var ageReducer = new DataReducer() { Name = "Calculate EvalsCreated" };
    50       var lastMoveReducer = new DataReducer() { Name = "Calculate LastMove" };
     50      var lastMoveAssigner = new Assigner() { Name = "Set LastMove" };
    5151      var subScopesRemover = new SubScopesRemover();
    5252      var evaluator = new Placeholder() { Name = "Evaluator (Placeholder)" };
     
    7171
    7272      ageReducer.ParameterToReduce.ActualName = "EvalsCreated";
    73       ageReducer.ReductionOperation.ActualName = "AgeInheritance";
     73      ageReducer.ReductionOperation.ActualName = "AgeInheritanceReduction";
    7474      ageReducer.ReductionOperation.Value = null;
    7575      ageReducer.TargetParameter.ActualName = "EvalsCreated";
    7676      ageReducer.TargetOperation.Value = new ReductionOperation(ReductionOperations.Assign);
    77       ageReducer.Successor = lastMoveReducer;
     77      ageReducer.Successor = lastMoveAssigner;
    7878
    79       lastMoveReducer.ParameterToReduce.ActualName = "LastMove";
    80       lastMoveReducer.ReductionOperation.ActualName = "AgeInheritance";
    81       lastMoveReducer.ReductionOperation.Value = null;
    82       lastMoveReducer.TargetParameter.ActualName = "LastMove";
    83       lastMoveReducer.TargetOperation.Value = new ReductionOperation(ReductionOperations.Assign);
    84       lastMoveReducer.Successor = subScopesRemover;
     79      lastMoveAssigner.LeftSideParameter.ActualName = "LastMove";
     80      lastMoveAssigner.RightSideParameter.ActualName = "EvaluatedSolutions";
     81      lastMoveAssigner.Successor = subScopesRemover;
    8582
    8683      subScopesRemover.Successor = evaluator;
  • branches/ALPS/HeuristicLab.Algorithms.ALPS/3.3/AgingScheme.cs

    r12149 r12271  
    2020#endregion
    2121
     22using System;
     23using System.Collections.Generic;
     24using System.Linq;
     25using HeuristicLab.Data;
     26
    2227namespace HeuristicLab.Algorithms.ALPS {
     28  /// <summary>
     29  /// Defines the growth of age limits for the layers.
     30  /// </summary>
    2331  public enum AgingScheme {
    2432    Linear,
     
    2735    Exponential
    2836  }
     37
     38  /// <summary>
     39  /// Helper for calculating the age limits for a AgingScheme and a given AgeGap.
     40  /// </summary>
     41  public static class AgingSchemeCalculator {
     42    public static IntArray CalculateAgeLimits(this AgingScheme scheme, int ageGap, int numberOfLayers) {
     43      IEnumerable<int> schemeGenerator;
     44      switch (scheme) {
     45        case AgingScheme.Linear: schemeGenerator = LinearAgingScheme(); break;
     46        case AgingScheme.Fibonacci: schemeGenerator = FibonacciAgingScheme(); break;
     47        case AgingScheme.Polynomial: schemeGenerator = PolynomialAgingScheme(2); break;
     48        case AgingScheme.Exponential: schemeGenerator = ExponentialAgingScheme(2); break;
     49        default: throw new NotSupportedException("Aging Scheme " + scheme + " is not supported.");
     50      }
     51
     52      return new IntArray(schemeGenerator.Select(a => a * ageGap).Take(numberOfLayers).ToArray());
     53    }
     54
     55    #region Scheme definitions
     56    // 1 2 3 4 5 6 7 ...
     57    private static IEnumerable<int> LinearAgingScheme() {
     58      for (int i = 0; ; i++)
     59        yield return i + 1;
     60    }
     61    // 1 2 3 5 8 13 21 ...
     62    private static IEnumerable<int> FibonacciAgingScheme() {
     63      for (int i = 1, next = 2, temp; ; temp = next, next = i + next, i = temp)
     64        yield return i;
     65    }
     66    // (n^2): 1 2 4 9 16 25 36 ...
     67    private static IEnumerable<int> PolynomialAgingScheme(double exp) {
     68      yield return 1;
     69      yield return 2;
     70      for (int i = 2; ; i++)
     71        yield return (int)Math.Pow(i, exp);
     72    }
     73    // 1 2 4 8 16 32 64 ...
     74    private static IEnumerable<int> ExponentialAgingScheme(double @base) {
     75      for (int i = 0; ; i++)
     76        yield return (int)Math.Pow(@base, i);
     77    }
     78    #endregion
     79  }
    2980}
  • branches/ALPS/HeuristicLab.Algorithms.ALPS/3.3/Alps.cs

    r12260 r12271  
    2121
    2222using System;
    23 using System.Collections.Generic;
    2423using System.Linq;
    2524using HeuristicLab.Analysis;
     
    3635
    3736namespace HeuristicLab.Algorithms.ALPS {
    38   [Item("ALPS", "A base class for all ALPS-based algorithms.")]
     37  [Item("ALPS", "Base class for all ALPS-based algorithms.")]
    3938  [StorableClass]
    4039  public abstract class Alps : HeuristicOptimizationEngineAlgorithm, IStorableContent {
     
    7675      get { return (IValueParameter<IntArray>)Parameters["AgeLimits"]; }
    7776    }
    78     private IValueParameter<ReductionOperation> AgeInheritanceParameter {
    79       get { return (IValueParameter<ReductionOperation>)Parameters["AgeInheritance"]; }
     77    private IValueParameter<EnumValue<AgeInheritance>> AgeInheritanceParameter {
     78      get { return (IValueParameter<EnumValue<AgeInheritance>>)Parameters["AgeInheritance"]; }
     79    }
     80    private IValueParameter<ReductionOperation> AgeInheritanceReductionParameter {
     81      get { return (IValueParameter<ReductionOperation>)Parameters["AgeInheritanceReduction"]; }
    8082    }
    8183    private IValueParameter<IntValue> MatingPoolRangeParameter {
     
    118120      set { AgeLimitsParameter.Value = value; }
    119121    }
    120     public ReductionOperation AgeInheritance {
     122    public EnumValue<AgeInheritance> AgeInheritance {
    121123      get { return AgeInheritanceParameter.Value; }
    122124      set { AgeInheritanceParameter.Value = value; }
     125    }
     126    private ReductionOperation AgeInheritanceReduction {
     127      get { return AgeInheritanceReductionParameter.Value; }
     128      set { AgeInheritanceReductionParameter.Value = value; }
    123129    }
    124130    public IntValue MatingPoolRange {
     
    168174      Parameters.Add(new ValueParameter<IntValue>("AgeGap", "The frequency of reseeding the lowest layer and scaling factor for the age-limits for the layers", new IntValue(20)));
    169175      Parameters.Add(new ValueParameter<IntArray>("AgeLimits", new IntArray(new int[0])) { Hidden = true });
    170       Parameters.Add(new ValueParameter<ReductionOperation>("AgeInheritance", "The operator for determining the age of an offspring based the parents' age.", new ReductionOperation(ReductionOperations.Max)) { Hidden = true });
     176      Parameters.Add(new ValueParameter<EnumValue<AgeInheritance>>("AgeInheritance", "The operator for determining the age of an offspring based the parents' age.", new EnumValue<AgeInheritance>(ALPS.AgeInheritance.Older)));
     177      Parameters.Add(new ValueParameter<ReductionOperation>("AgeInheritanceReduction") { Hidden = true });
    171178      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 });
    172179      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 });
     
    175182      layerQualityAnalyzer = new BestAverageWorstQualityAnalyzer();
    176183
    177       // Create Operator Graph
    178 
    179       RecalculateAgeLimits();
     184      ParameterizeAgeLimits();
     185      ParameterizeAgeInheritanceReduction();
    180186
    181187      ParameterizeAnalyzers();
     
    185191    }
    186192
     193
     194    #region Events
    187195    public override void Prepare() {
    188196      if (Problem != null)
     
    190198    }
    191199
    192     #region Events
    193200    protected override void OnProblemChanged() {
    194201      base.OnProblemChanged();
     
    227234    private void AgeGapParameter_ValueChanged(object sender, EventArgs e) {
    228235      AgeGap.ValueChanged += AgeGap_ValueChanged;
    229       RecalculateAgeLimits();
     236      ParameterizeAgeLimits();
    230237    }
    231238    private void AgeGap_ValueChanged(object sender, EventArgs e) {
    232       RecalculateAgeLimits();
     239      ParameterizeAgeLimits();
    233240    }
    234241    private void AgingSchemeParameter_ValueChanged(object sender, EventArgs e) {
    235242      AgingScheme.ValueChanged += AgingScheme_ValueChanged;
    236       RecalculateAgeLimits();
     243      ParameterizeAgeLimits();
    237244    }
    238245    private void AgingScheme_ValueChanged(object sender, EventArgs e) {
    239       RecalculateAgeLimits();
     246      ParameterizeAgeLimits();
    240247    }
    241248    private void NumberOfLayersParameter_ValueChanged(object sender, EventArgs e) {
    242249      NumberOfLayers.ValueChanged += NumberOfLayers_ValueChanged;
    243       RecalculateAgeLimits();
     250      ParameterizeAgeLimits();
    244251    }
    245252    private void NumberOfLayers_ValueChanged(object sender, EventArgs e) {
    246       RecalculateAgeLimits();
     253      ParameterizeAgeLimits();
     254    }
     255    private void AgeInheritanceParameter_ValueChanged(object sender, EventArgs e) {
     256      AgeInheritance.ValueChanged += AgeInheritance_ValueChanged;
     257      ParameterizeAgeInheritanceReduction();
     258    }
     259    private void AgeInheritance_ValueChanged(object sender, EventArgs e) {
     260      ParameterizeAgeInheritanceReduction();
    247261    }
    248262    private void AnalyzerOperators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IAnalyzer>> e) {
     
    278292      NumberOfLayersParameter.ValueChanged += NumberOfLayersParameter_ValueChanged;
    279293      NumberOfLayers.ValueChanged += NumberOfLayers_ValueChanged;
     294      AgeInheritanceParameter.ValueChanged += AgeInheritanceParameter_ValueChanged;
     295      AgeInheritance.ValueChanged += AgeInheritance_ValueChanged;
    280296      Analyzer.Operators.ItemsAdded += AnalyzerOperators_ItemsAdded;
    281297      LayerAnalyzer.Operators.ItemsAdded += LayerAnalyzerOperators_ItemsAdded;
     
    307323      }
    308324    }
     325
     326    private void ParameterizeAgeInheritanceReduction() {
     327      AgeInheritanceReduction.Value = GetAgeInheritanceReduction(AgeInheritance.Value);
     328    }
     329    protected abstract ReductionOperations GetAgeInheritanceReduction(AgeInheritance ageInheritance);
     330    private void ParameterizeAgeLimits() {
     331      var scheme = AgingScheme.Value;
     332      int ageGap = AgeGap.Value;
     333      int numberOfLayers = NumberOfLayers.Value;
     334      AgeLimits = scheme.CalculateAgeLimits(ageGap, numberOfLayers);
     335    }
    309336    protected virtual void ParameterizeStochasticOperator(IOperator @operator) {
    310337      var stochasticOperator = @operator as IStochasticOperator;
     
    338365    }
    339366    #endregion
    340 
    341     private void RecalculateAgeLimits() {
    342       var scheme = AgingScheme.Value;
    343       int ageGap = AgeGap.Value;
    344       int numberOfLayers = NumberOfLayers.Value;
    345       AgeLimits = AgingSchemeCalculator.CalculateAgeLimits(scheme, ageGap, numberOfLayers);
    346     }
    347367  }
    348368}
  • branches/ALPS/HeuristicLab.Algorithms.ALPS/3.3/AlpsGeneticAlgorithm.cs

    r12260 r12271  
    279279      }
    280280    }
     281
     282    protected override ReductionOperations GetAgeInheritanceReduction(AgeInheritance ageInheritance) {
     283      switch (ageInheritance) {
     284        case ALPS.AgeInheritance.Older: return ReductionOperations.Max;
     285        case ALPS.AgeInheritance.Agerage: return ReductionOperations.Avg;
     286        case ALPS.AgeInheritance.Younger: return ReductionOperations.Min;
     287        default: throw new NotSupportedException("AgeInheritance " + ageInheritance + " is not supported.");
     288      }
     289    }
    281290    #endregion
    282291
  • branches/ALPS/HeuristicLab.Algorithms.ALPS/3.3/AlpsGeneticAlgorithmMainLoop.cs

    r12197 r12271  
    246246      ageCalculator.TargetParameter.ActualName = "Age";
    247247      ageCalculator.ReductionOperation.Value = null;
    248       ageCalculator.ReductionOperation.ActualName = "AgeInheritance";
     248      ageCalculator.ReductionOperation.ActualName = "AgeInheritanceReduction";
    249249      ageCalculator.TargetOperation.Value = new ReductionOperation(ReductionOperations.Assign);
    250250      ageCalculator.Successor = crossoverSuccessor;
  • branches/ALPS/HeuristicLab.Algorithms.ALPS/3.3/HeuristicLab.Algorithms.ALPS-3.3.csproj

    r12119 r12271  
    132132  </ItemGroup>
    133133  <ItemGroup>
    134     <Compile Include="AgingSchemeCalculator.cs" />
     134    <Compile Include="AgeInheritance.cs" />
    135135    <Compile Include="Alps.cs" />
    136136    <Compile Include="Analyzers\OldestAverageYoungestAgeAnalyzer.cs" />
Note: See TracChangeset for help on using the changeset viewer.