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 | using HeuristicLab.Evolutionary;
|
---|
8 |
|
---|
9 | namespace HeuristicLab.SimOpt {
|
---|
10 | public class SimOptSquentialSubOperatorCrossover : OperatorBase {
|
---|
11 | public override string Description {
|
---|
12 | get { return @"Takes the parameter vector of two items and on each index applies the respectively indexed suboperator"; }
|
---|
13 | }
|
---|
14 |
|
---|
15 | public SimOptSquentialSubOperatorCrossover()
|
---|
16 | : base() {
|
---|
17 | AddVariableInfo(new VariableInfo("Items", "The parameter vector", typeof(ConstrainedItemList), VariableKind.In | VariableKind.New));
|
---|
18 | AddVariableInfo(new VariableInfo("Parents", "The 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 |
|
---|
24 | int subScopesCount = scope.SubScopes.Count;
|
---|
25 | if (subScopesCount < parents || (subScopesCount % parents) != 0)
|
---|
26 | throw new InvalidOperationException("Size of mating pool is not a multiple (>1) of the number of parents per child");
|
---|
27 | int children = subScopesCount / parents;
|
---|
28 |
|
---|
29 | CompositeOperation co = new CompositeOperation();
|
---|
30 | for (int i = 0; i < SubOperators.Count; i++) {
|
---|
31 | if (SubOperators[i].GetVariable("Index") != null) {
|
---|
32 | SubOperators[i].GetVariable("Index").Value = new IntData(i);
|
---|
33 | }
|
---|
34 | if (SubOperators[i].GetVariableInfo("Items") != null) {
|
---|
35 | SubOperators[i].GetVariableInfo("Items").ActualName = GetVariableInfo("Items").ActualName;
|
---|
36 | }
|
---|
37 | }
|
---|
38 | for (int i = 0; i < children; i++) {
|
---|
39 | IScope child = (IScope)scope.SubScopes[0].Clone();
|
---|
40 | for (int j = 0; j < parents; j++) {
|
---|
41 | IScope parent = scope.SubScopes[0];
|
---|
42 | child.AddSubScope(parent);
|
---|
43 | scope.RemoveSubScope(parent);
|
---|
44 | }
|
---|
45 | scope.AddSubScope(child);
|
---|
46 | for (int n = 0 ; n < SubOperators.Count ; n++)
|
---|
47 | co.AddOperation(new AtomicOperation(SubOperators[n], child));
|
---|
48 | co.AddOperation(new AtomicOperation(new SubScopesRemover(), child));
|
---|
49 | }
|
---|
50 | return co;
|
---|
51 | }
|
---|
52 | }
|
---|
53 | }
|
---|