Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/10/15 11:54:17 (10 years ago)
Author:
pfleck
Message:

#2350 The AlpsSsMover now prioritize moved individuals based on their age and quality.

File:
1 edited

Legend:

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

    r12162 r12174  
    3434  [StorableClass]
    3535  public class AlpsSsMover : SingleSuccessorOperator, IStochasticOperator {
     36    #region Parameter Properties
    3637    private ILookupParameter<IntValue> LayerParameter {
    3738      get { return (ILookupParameter<IntValue>)Parameters["Layer"]; }
     
    4041      get { return (ILookupParameter<IntValue>)Parameters["TargetIndex"]; }
    4142    }
    42     private ILookupParameter<IntValue> PopulationSizeParameter {
    43       get { return (ILookupParameter<IntValue>)Parameters["PopulationSize"]; }
     43    private ILookupParameter<IntValue> LayerSizeParameter {
     44      get { return (ILookupParameter<IntValue>)Parameters["LayerSize"]; }
    4445    }
    4546    private ILookupParameter<IntValue> NumberOfLayersParameter {
     
    5556      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
    5657    }
     58    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
     59      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
     60    }
     61    public ValueLookupParameter<BoolValue> MaximizationParameter {
     62      get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
     63    }
     64    public IScopeTreeLookupParameter<IntValue> EvalsCreatedParameter {
     65      get { return (IScopeTreeLookupParameter<IntValue>)Parameters["EvalsCreated"]; }
     66    }
     67    public IScopeTreeLookupParameter<IntValue> EvalsMovedQualityParameter {
     68      get { return (IScopeTreeLookupParameter<IntValue>)Parameters["EvalsMoved"]; }
     69    }
     70    private ILookupParameter<IntValue> EvaluatedSolutionsParameter {
     71      get { return (ILookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
     72    }
     73    private ILookupParameter<IntArray> AgeLimitsParameter {
     74      get { return (ILookupParameter<IntArray>)Parameters["AgeLimits"]; }
     75    }
     76    #endregion
    5777
    5878    [StorableConstructor]
     
    6989      Parameters.Add(new LookupParameter<IntValue>("Layer"));
    7090      Parameters.Add(new LookupParameter<IntValue>("TargetIndex"));
    71       Parameters.Add(new LookupParameter<IntValue>("PopulationSize"));
     91      Parameters.Add(new LookupParameter<IntValue>("LayerSize"));
    7292      Parameters.Add(new LookupParameter<IntValue>("NumberOfLayers"));
    7393      Parameters.Add(new LookupParameter<IScope>("WorkingScope"));
    7494      Parameters.Add(new LookupParameter<IScope>("Layers"));
    7595      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
     96      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality"));
     97      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
     98      Parameters.Add(new ScopeTreeLookupParameter<IntValue>("EvalsCreated"));
     99      Parameters.Add(new ScopeTreeLookupParameter<IntValue>("EvalsMoved"));
     100      Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions"));
     101      Parameters.Add(new LookupParameter<IntArray>("AgeLimits"));
    76102    }
    77103
    78104    private int n;
    79105    private int m;
     106    private int evals;
     107    private int popSize;
    80108    private IScope layers;
    81109    private IRandom rand;
     110    private string qualityVariableName;
     111    private IntArray ageLimits;
     112    private bool maximization;
    82113    public override IOperation Apply() {
    83114      int i = LayerParameter.ActualValue.Value;
    84115      int j = TargetIndexParameter.ActualValue.Value;
    85116      n = NumberOfLayersParameter.ActualValue.Value;
    86       m = PopulationSizeParameter.ActualValue.Value;
     117      m = LayerSizeParameter.ActualValue.Value;
     118      evals = EvaluatedSolutionsParameter.ActualValue.Value;
     119      popSize = n * m;
    87120      rand = RandomParameter.ActualValue;
     121      qualityVariableName = QualityParameter.TranslatedName;
     122      ageLimits = AgeLimitsParameter.ActualValue;
     123      maximization = MaximizationParameter.ActualValue.Value;
    88124
    89125      layers = LayersParameter.ActualValue;
     
    110146      }
    111147    }
    112     private int? FindReplaceable(IScope layer, IScope individual) {
     148    private int? FindReplaceable(IScope layer, IScope individualToReplace) {
     149      int layerNumber = ((IntValue)layer.Variables["Layer"].Value).Value;
     150
     151      var individuals = (
     152        from individual in layer.SubScopes
     153        let quality = ((DoubleValue)individual.Variables[qualityVariableName].Value).Value
     154        let evalsCreated = ((IntValue)individual.Variables["EvalsCreated"].Value).Value
     155        let age = (evals - evalsCreated) / popSize
     156        select new { individual, quality, age }
     157        ).Select((x, index) => new { index, x.individual, x.quality, x.age })
     158        .ToList();
     159
     160      var ageLimit = layerNumber < n - 1 ? ageLimits[layerNumber] : int.MaxValue;
     161
     162      var toOldIndividual = individuals.FirstOrDefault(x => x.age >= ageLimit);
     163      if (toOldIndividual != null)
     164        return toOldIndividual.index;
     165
     166      double replaceQuality = ((DoubleValue)individualToReplace.Variables[qualityVariableName].Value).Value;
     167      var sortedIndividuals = maximization ? individuals.OrderBy(x => x.quality) : individuals.OrderByDescending(x => x.quality);
     168      var worstIndividual = sortedIndividuals.FirstOrDefault(x => maximization ? x.quality < replaceQuality : x.quality > replaceQuality);
     169      if (worstIndividual != null)
     170        return worstIndividual.index;
     171
    113172      return Enumerable.Range(0, layer.SubScopes.Count).SampleRandom(rand);
    114173    }
Note: See TracChangeset for help on using the changeset viewer.