Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HL-3.2-MonoMigration/HeuristicLab.SimOpt/SimOptCrossoverPreparator.cs @ 4075

Last change on this file since 4075 was 637, checked in by abeham, 16 years ago

[trunk] Redesigned and simplified SimOpt according to ticket #291

File size: 1.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Core;
6using HeuristicLab.Data;
7
8namespace HeuristicLab.SimOpt {
9  public class SimOptCrossoverPreparator : OperatorBase {
10    public override string Description {
11      get {
12        return @"Prepares the parent parameters for crossing";
13      }
14    }
15
16    public SimOptCrossoverPreparator()
17      : base() {
18      AddVariableInfo(new VariableInfo("Parents", "Number of parents per child", typeof(IntData), VariableKind.In));
19    }
20
21    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          }
36        }
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
40      }
41      return null;
42    }
43  }
44}
Note: See TracBrowser for help on using the repository browser.