- Timestamp:
- 03/09/15 11:55:47 (10 years ago)
- 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 96 96 var clearMatingPool = new SubScopesRemover() { Name = "Clear WorkingScope" }; 97 97 var tryMoveUp = new AlpsSsMover() { Name = "Try Move Up" }; 98 var removeWorkingScope = new LeftReducer() { Name = "Remove Working Scope" };99 98 var incrementEvaluations = new IntCounter() { Name = "incr. EvaluatedSolutions" }; 100 99 var incrIterations = new IntCounter() { Name = "Incr. Iterations" }; … … 207 206 clearMatingPool.Successor = createRandomIndividual; 208 207 209 tryMoveUp.Successor = removeWorkingScope; 210 211 removeWorkingScope.Successor = incrementEvaluations; 208 tryMoveUp.Successor = incrementEvaluations; 212 209 213 210 incrementEvaluations.Increment = new IntValue(1); -
branches/ALPS/HeuristicLab.Algorithms.ALPS.SteadyState/3.3/AlpsSsMover.cs
r12150 r12162 20 20 #endregion 21 21 22 using System;23 22 using System.Linq; 24 23 using HeuristicLab.Common; … … 35 34 [StorableClass] 36 35 public class AlpsSsMover : SingleSuccessorOperator, IStochasticOperator { 36 private ILookupParameter<IntValue> LayerParameter { 37 get { return (ILookupParameter<IntValue>)Parameters["Layer"]; } 38 } 37 39 private ILookupParameter<IntValue> TargetIndexParameter { 38 40 get { return (ILookupParameter<IntValue>)Parameters["TargetIndex"]; } … … 43 45 private ILookupParameter<IntValue> NumberOfLayersParameter { 44 46 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"]; } 45 53 } 46 54 public ILookupParameter<IRandom> RandomParameter { … … 59 67 public AlpsSsMover() 60 68 : base() { 69 Parameters.Add(new LookupParameter<IntValue>("Layer")); 61 70 Parameters.Add(new LookupParameter<IntValue>("TargetIndex")); 62 71 Parameters.Add(new LookupParameter<IntValue>("PopulationSize")); 63 72 Parameters.Add(new LookupParameter<IntValue>("NumberOfLayers")); 73 Parameters.Add(new LookupParameter<IScope>("WorkingScope")); 74 Parameters.Add(new LookupParameter<IScope>("Layers")); 64 75 Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use.")); 65 76 } 66 77 78 private int n; 79 private int m; 80 private IScope layers; 81 private IRandom rand; 67 82 public override IOperation Apply() { 68 int i = ((IntValue)ExecutionContext.Scope.Variables["Layer"].Value).Value;83 int i = LayerParameter.ActualValue.Value; 69 84 int j = TargetIndexParameter.ActualValue.Value; 70 intn = NumberOfLayersParameter.ActualValue.Value;71 intm = PopulationSizeParameter.ActualValue.Value;72 var random= RandomParameter.ActualValue;85 n = NumberOfLayersParameter.ActualValue.Value; 86 m = PopulationSizeParameter.ActualValue.Value; 87 rand = RandomParameter.ActualValue; 73 88 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(); 77 92 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; 83 96 84 97 return base.Apply(); 85 98 } 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); 86 105 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; 107 109 } 108 110 } 109 111 } 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); 115 114 } 116 115 }
Note: See TracChangeset
for help on using the changeset viewer.