Changeset 12162


Ignore:
Timestamp:
03/09/15 11:55:47 (3 years ago)
Author:
pfleck
Message:

#2350 Fixed SteadyState ALPS Mover and MainLoop.

Location:
branches/ALPS/HeuristicLab.Algorithms.ALPS.SteadyState/3.3
Files:
2 edited

Legend:

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

    r12159 r12162  
    9696      var clearMatingPool = new SubScopesRemover() { Name = "Clear WorkingScope" };
    9797      var tryMoveUp = new AlpsSsMover() { Name = "Try Move Up" };
    98       var removeWorkingScope = new LeftReducer() { Name = "Remove Working Scope" };
    9998      var incrementEvaluations = new IntCounter() { Name = "incr. EvaluatedSolutions" };
    10099      var incrIterations = new IntCounter() { Name = "Incr. Iterations" };
     
    207206      clearMatingPool.Successor = createRandomIndividual;
    208207
    209       tryMoveUp.Successor = removeWorkingScope;
    210 
    211       removeWorkingScope.Successor = incrementEvaluations;
     208      tryMoveUp.Successor = incrementEvaluations;
    212209
    213210      incrementEvaluations.Increment = new IntValue(1);
  • branches/ALPS/HeuristicLab.Algorithms.ALPS.SteadyState/3.3/AlpsSsMover.cs

    r12150 r12162  
    2020#endregion
    2121
    22 using System;
    2322using System.Linq;
    2423using HeuristicLab.Common;
     
    3534  [StorableClass]
    3635  public class AlpsSsMover : SingleSuccessorOperator, IStochasticOperator {
     36    private ILookupParameter<IntValue> LayerParameter {
     37      get { return (ILookupParameter<IntValue>)Parameters["Layer"]; }
     38    }
    3739    private ILookupParameter<IntValue> TargetIndexParameter {
    3840      get { return (ILookupParameter<IntValue>)Parameters["TargetIndex"]; }
     
    4345    private ILookupParameter<IntValue> NumberOfLayersParameter {
    4446      get { return (ILookupParameter<IntValue>)Parameters["NumberOfLayers"]; }
     47    }
     48    private ILookupParameter<IScope> WorkingScopeParameter {
     49      get { return (ILookupParameter<IScope>)Parameters["WorkingScope"]; }
     50    }
     51    private ILookupParameter<IScope> LayersParameter {
     52      get { return (ILookupParameter<IScope>)Parameters["Layers"]; }
    4553    }
    4654    public ILookupParameter<IRandom> RandomParameter {
     
    5967    public AlpsSsMover()
    6068      : base() {
     69      Parameters.Add(new LookupParameter<IntValue>("Layer"));
    6170      Parameters.Add(new LookupParameter<IntValue>("TargetIndex"));
    6271      Parameters.Add(new LookupParameter<IntValue>("PopulationSize"));
    6372      Parameters.Add(new LookupParameter<IntValue>("NumberOfLayers"));
     73      Parameters.Add(new LookupParameter<IScope>("WorkingScope"));
     74      Parameters.Add(new LookupParameter<IScope>("Layers"));
    6475      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
    6576    }
    6677
     78    private int n;
     79    private int m;
     80    private IScope layers;
     81    private IRandom rand;
    6782    public override IOperation Apply() {
    68       int i = ((IntValue)ExecutionContext.Scope.Variables["Layer"].Value).Value;
     83      int i = LayerParameter.ActualValue.Value;
    6984      int j = TargetIndexParameter.ActualValue.Value;
    70       int n = NumberOfLayersParameter.ActualValue.Value;
    71       int m = PopulationSizeParameter.ActualValue.Value;
    72       var random = RandomParameter.ActualValue;
     85      n = NumberOfLayersParameter.ActualValue.Value;
     86      m = PopulationSizeParameter.ActualValue.Value;
     87      rand = RandomParameter.ActualValue;
    7388
    74       var layer = ExecutionContext.Scope.SubScopes[0];
    75       var newIndividual = ExecutionContext.Scope.SubScopes[1];
    76       var gs = ExecutionContext.Scope.Parent;
     89      layers = LayersParameter.ActualValue;
     90      var newIndividual = (IScope)WorkingScopeParameter.ActualValue.Clone();
     91      newIndividual.Name = j.ToString();
    7792
    78       if (i < n) {
    79         TryMoveUp(gs, newIndividual, layer, i, j, n, m, random);
    80         layer.SubScopes[j] = newIndividual;
    81         newIndividual.Name = j.ToString();
    82       }
     93      TryMoveUp(i, j);
     94      var currentLayer = layers.SubScopes[i];
     95      currentLayer.SubScopes[j] = newIndividual;
    8396
    8497      return base.Apply();
    8598    }
     99    private void TryMoveUp(int i, int j) {
     100      var currentLayer = layers.SubScopes[i];
     101      var currentIndividual = currentLayer.SubScopes[j];
     102      if (i < n - 1) {
     103        var nextLayer = layers.SubScopes[i + 1];
     104        int? k = FindReplaceable(nextLayer, currentIndividual);
    86105
    87     private void TryMoveUp(IScope gs, IScope individual, IScope layer, int i, int j, int n, int m, IRandom random) {
    88       if (i < n) {
    89         if (gs.SubScopes.Count <= i + 1) {
    90           var newLayer = new Scope((i + 1).ToString());
    91           newLayer.Variables.Add(new Variable("Layer", new IntValue(i + 1)));
    92           newLayer.Variables.Add(new Variable("LayerPopulationSize", new IntValue(1)));
    93           gs.SubScopes.Add(newLayer);
    94         }
    95 
    96         var higherLayer = gs.SubScopes[i + 1];
    97         var replaceIndex = FindReplaceable(higherLayer, individual, random, m);
    98         if (replaceIndex.HasValue) {
    99           var replacedIndividual = layer.SubScopes[j];
    100           if (replaceIndex.Value >= higherLayer.SubScopes.Count) {
    101             higherLayer.SubScopes.Add(replacedIndividual);
    102           } else {
    103             TryMoveUp(gs, higherLayer.SubScopes[j], higherLayer, i + 1, replaceIndex.Value, n, m, random);
    104             // TODO higherlayer[j] does not exist
    105             higherLayer.SubScopes[replaceIndex.Value] = replacedIndividual;
    106           }
     106        if (k.HasValue) {
     107          TryMoveUp(i + 1, k.Value);
     108          nextLayer.SubScopes[k.Value] = currentIndividual;
    107109        }
    108110      }
    109111    }
    110 
    111     private int? FindReplaceable(IScope layer, IScope individual, IRandom random, int m) {
    112       if (layer.SubScopes.Count < m)
    113         return layer.SubScopes.Count;
    114       return Enumerable.Range(0, layer.SubScopes.Count).SampleRandom(random);
     112    private int? FindReplaceable(IScope layer, IScope individual) {
     113      return Enumerable.Range(0, layer.SubScopes.Count).SampleRandom(rand);
    115114    }
    116115  }
Note: See TracChangeset for help on using the changeset viewer.