[637] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using HeuristicLab.Core;
|
---|
| 6 | using HeuristicLab.Data;
|
---|
| 7 |
|
---|
| 8 | namespace 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 | }
|
---|