Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.SimOpt/SimOptSquentialSubOperatorCrossover.cs @ 584

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

merged communication framework to trunk (ticket #279)

File size: 2.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Core;
6using HeuristicLab.Data;
7using HeuristicLab.Evolutionary;
8
9namespace 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}
Note: See TracBrowser for help on using the repository browser.