Changeset 1415


Ignore:
Timestamp:
03/25/09 14:25:36 (11 years ago)
Author:
abeham
Message:

fixed crossover preparator (#541)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.SimOpt/SimOptCrossoverPreparator.cs

    r637 r1415  
    1616    public SimOptCrossoverPreparator()
    1717      : base() {
    18       AddVariableInfo(new VariableInfo("Parents", "Number of parents per child", typeof(IntData), VariableKind.In));
    1918    }
    2019
    2120    public override IOperation Apply(IScope scope) {
    22       int parents = GetVariableValue<IntData>("Parents", scope, true).Data;
    23       int populationSize = scope.SubScopes.Count;
    24       int childrenSize = populationSize / parents;
    25       if (populationSize % parents > 0) throw new ArgumentException("ERROR in SimOptCrossoverPreparator: The number of subscopes is not a multiple of the number of parents per child");
    26       for (int i = 0; i < childrenSize; i++) {
    27         IScope child = new Scope(i.ToString());
    28         int parameters = scope.SubScopes[0].SubScopes.Count;
    29         for (int k = 0; k < parameters; k++) {
    30           child.AddSubScope(new Scope("Parameter_" + (k+1).ToString()));
    31           for (int j = 0; j < parents; j++) {
    32             IScope param = scope.SubScopes[j].SubScopes[0]; // take scope containing the parameter from the parent
    33             child.SubScopes[k].AddSubScope(param); // add it to the child
    34             scope.SubScopes[j].RemoveSubScope(param);
    35           }
     21      int parents = scope.SubScopes.Count;
     22      int parameters = scope.SubScopes[0].SubScopes.Count;
     23      // remove the parents and add them to a temporary list
     24      IList<IScope> parentsScopes = new List<IScope>();
     25      while (scope.SubScopes.Count > 0) {
     26        IScope tmp = scope.SubScopes[0];
     27        scope.RemoveSubScope(tmp);
     28        parentsScopes.Add(tmp);
     29      }
     30
     31      for (int i = 0; i < parameters; i++) {
     32        scope.AddSubScope(new Scope("Parameters_" + (i + 1).ToString()));
     33        for (int k = 0; k < parents; k++) {
     34          IScope param = parentsScopes[k].SubScopes[i];
     35          parentsScopes[k].RemoveSubScope(param);
     36          scope.SubScopes[i].AddSubScope(param);
    3637        }
    37         for (int j = 0; j < parents; j++)
    38           scope.RemoveSubScope(scope.SubScopes[0]); // remove the parent
    39         scope.SubScopes.Add(child); // add the child to the end of the scope list
    4038      }
    4139      return null;
Note: See TracChangeset for help on using the changeset viewer.