Changeset 3479 for trunk/sources/HeuristicLab.Optimization.Operators
- Timestamp:
- 04/21/10 22:05:40 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified trunk/sources/HeuristicLab.Optimization.Operators/3.3/SASEGASAReunificator.cs ¶
r3451 r3479 26 26 using HeuristicLab.Operators; 27 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 using HeuristicLab.Data; 29 using HeuristicLab.Parameters; 28 30 29 31 namespace HeuristicLab.Optimization.Operators { … … 32 34 /// scopes by 1 and uniformly partitions the sub sub scopes again, maintaining the order. 33 35 /// </summary> 34 /*[Item("SASEGASAReunificator", "This operator merges the villages in a migration phase and redistributes the individuals. It is implemented as described in Affenzeller, M. et al. 2009. Genetic Algorithms and Genetic Programming - Modern Concepts and Practical Applications, CRC Press.")]36 [Item("SASEGASAReunificator", "This operator merges the villages in a migration phase and redistributes the individuals. It is implemented as described in Affenzeller, M. et al. 2009. Genetic Algorithms and Genetic Programming - Modern Concepts and Practical Applications, CRC Press.")] 35 37 [StorableClass] 36 38 public class SASEGASAReunificator : SingleSuccessorOperator, IMigrator { 37 39 40 public LookupParameter<IntValue> VillageCountParameter { 41 get { return (LookupParameter<IntValue>)Parameters["VillageCount"]; } 42 } 43 44 public SASEGASAReunificator() 45 : base() { 46 Parameters.Add(new LookupParameter<IntValue>("VillageCount", "The number of villages left after the reunification.")); 47 } 38 48 /// <summary> 39 49 /// Joins all sub sub scopes of the given <paramref name="scope"/>, reduces the number of sub 40 50 /// scopes by 1 and uniformly partitions the sub sub scopes again, maintaining the order. 41 51 /// </summary> 42 /// <exception cref="InvalidOperationException">Thrown when only 0 or 1 sub scope is available.</exception>52 /// <exception cref="InvalidOperationException">Thrown when there are less than 2 sub-scopes available or when VillageCount does not equal the number of sub-scopes.</exception> 43 53 /// <param name="scope">The current scope whose sub scopes to reduce.</param> 44 54 /// <returns><c>null</c>.</returns> 45 55 public override IOperation Apply() { 46 56 IScope scope = ExecutionContext.Scope; 47 int villageCount = scope.SubScopes.Count; 57 if (VillageCountParameter.ActualValue == null) VillageCountParameter.ActualValue = new IntValue(scope.SubScopes.Count); 58 int villageCount = VillageCountParameter.ActualValue.Value; 48 59 if (villageCount <= 1) 49 throw new InvalidOperationException("SASEGASA reunification requires 2 or more sub-scopes"); 60 throw new InvalidOperationException(Name + ": Reunification requires 2 or more sub-scopes"); 61 if (villageCount != scope.SubScopes.Count) 62 throw new InvalidOperationException(Name + ": VillageCount does not equal the number of sub-scopes"); 50 63 51 64 // get all villages 52 IList<IScope> population = new List<IScope>();65 List<IScope> population = new List<IScope>(); 53 66 for (int i = 0; i < villageCount; i++) { 54 while (scope.SubScopes[i].SubScopes[0].SubScopes.Count > 0) { 55 population.Add(scope.SubScopes[i].SubScopes[0].SubScopes[0]); 56 scope.SubScopes[i].SubScopes.Remove(scope.SubScopes[i].SubScopes[0].SubScopes[0]); 57 } 67 population.AddRange(scope.SubScopes[i].SubScopes); 58 68 scope.SubScopes[i].SubScopes.Clear(); 59 69 } … … 64 74 int populationPerVillage = population.Count / villageCount; 65 75 for (int i = 0; i < villageCount; i++) { 66 scope.SubScopes[i].SubScopes.Add(new Scope()); 67 scope.SubScopes[i].SubScopes.Add(new Scope()); 68 for (int j = 0; j < populationPerVillage; j++) { 69 scope.SubScopes[i].SubScopes[1].SubScopes.Add(population[0]); 70 population.RemoveAt(0); 71 } 76 scope.SubScopes[i].SubScopes.AddRange(population.GetRange(0, populationPerVillage)); 77 population.RemoveRange(0, populationPerVillage); 72 78 } 73 79 74 80 // add remaining sub-sub-scopes to last sub-scope 75 while (population.Count > 0) {76 scope.SubScopes[scope.SubScopes.Count - 1].SubScopes[1].SubScopes.Add(population[0]);77 population.RemoveAt(0); 78 }81 scope.SubScopes[scope.SubScopes.Count - 1].SubScopes.AddRange(population); 82 population.Clear(); 83 84 VillageCountParameter.ActualValue.Value = villageCount; 79 85 80 86 return base.Apply(); 81 87 } 82 } */88 } 83 89 }
Note: See TracChangeset
for help on using the changeset viewer.