Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/21/10 22:05:40 (14 years ago)
Author:
abeham
Message:

added first draft of SASEGASA #839

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Optimization.Operators/3.3/SASEGASAReunificator.cs

    r3451 r3479  
    2626using HeuristicLab.Operators;
    2727using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     28using HeuristicLab.Data;
     29using HeuristicLab.Parameters;
    2830
    2931namespace HeuristicLab.Optimization.Operators {
     
    3234  /// scopes by 1 and uniformly partitions the sub sub scopes again, maintaining the order.
    3335  /// </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.")]
    3537  [StorableClass]
    3638  public class SASEGASAReunificator : SingleSuccessorOperator, IMigrator {
    3739
     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    }
    3848    /// <summary>
    3949    /// Joins all sub sub scopes of the given <paramref name="scope"/>, reduces the number of sub
    4050    /// scopes by 1 and uniformly partitions the sub sub scopes again, maintaining the order.
    4151    /// </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>
    4353    /// <param name="scope">The current scope whose sub scopes to reduce.</param>
    4454    /// <returns><c>null</c>.</returns>
    4555    public override IOperation Apply() {
    4656      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;
    4859      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");
    5063
    5164      // get all villages
    52       IList<IScope> population = new List<IScope>();
     65      List<IScope> population = new List<IScope>();
    5366      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);
    5868        scope.SubScopes[i].SubScopes.Clear();
    5969      }
     
    6474      int populationPerVillage = population.Count / villageCount;
    6575      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);
    7278      }
    7379
    7480      // 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;
    7985
    8086      return base.Apply();
    8187    }
    82   }*/
     88  }
    8389}
Note: See TracChangeset for help on using the changeset viewer.