- Timestamp:
- 10/08/08 12:39:51 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.SimOpt/SimOptSquentialSubOperatorCrossover.cs
r591 r637 24 24 using System.Linq; 25 25 using System.Text; 26 using System.Xml; 26 27 using HeuristicLab.Core; 27 28 using HeuristicLab.Data; 28 using HeuristicLab.Evolutionary; 29 using HeuristicLab.Operators; 30 using HeuristicLab.Selection; 29 31 30 32 namespace HeuristicLab.SimOpt { 31 33 public class SimOptSquentialSubOperatorCrossover : OperatorBase { 34 private UniformSequentialSubScopesProcessor usssp, usssp2, usssp3, usssp4; 35 private SequentialSubScopesProcessor sssp; 36 private SimOptParameterExtractor extractor; 37 private SimOptParameterPacker packer; 38 private SimOptCrossoverPreparator preparator; 39 private MergingReducer merger; 40 private bool uptodate; 41 32 42 public override string Description { 33 get { return @"Takes the parameter vector of two items and on each index applies the respectively indexed suboperator"; } 43 get { 44 return @"This operator encapsulates the functionality of crossing the parameters of a simulation parameter vector. It works as follows: 45 1. The parameters of all parents are extracted [UniformSequentialSubScopeProcessor which applies a SimOptParameterExtractor] 46 2. The parents are prepared for crossing by grouping the respective parameters [SimOptCrossoverPreparator] 47 3. The parameters are crossed [UniformSequentialSubScopeProcessor which applies SequentialSubScopeProcessor which applies the SubOperators of this operator on each parameter group (except for the last one)] 48 4. Assigning the crossed parameters to the respective children [MergingReducer] 49 5. Update the parameters [UniformSequentialSubScopeProcessor which applies SimOptParameterPacker] 50 51 Should the packing fail due to constraint violations, the operator will execute the last of its suboperators."; 52 } 34 53 } 35 54 … … 38 57 AddVariableInfo(new VariableInfo("Items", "The parameter vector", typeof(ConstrainedItemList), VariableKind.In | VariableKind.New)); 39 58 AddVariableInfo(new VariableInfo("Parents", "The number of parents per child", typeof(IntData), VariableKind.In)); 59 uptodate = false; 40 60 } 41 61 42 62 public override IOperation Apply(IScope scope) { 43 int parents = GetVariableValue<IntData>("Parents", scope, true).Data; 63 string itemsActualName = GetVariableInfo("Items").ActualName; 64 string parentsActualName = GetVariableInfo("Parents").ActualName; 65 int parameters = SubOperators.Count - 1; 44 66 45 int subScopesCount = scope.SubScopes.Count; 46 if (subScopesCount < parents || (subScopesCount % parents) != 0) 47 throw new InvalidOperationException("Size of mating pool is not a multiple (>1) of the number of parents per child"); 48 int children = subScopesCount / parents; 67 if (!uptodate) { 68 usssp = new UniformSequentialSubScopesProcessor(); 69 extractor = new SimOptParameterExtractor(); 70 usssp.AddSubOperator(extractor); 71 72 preparator = new SimOptCrossoverPreparator(); 73 74 usssp2 = new UniformSequentialSubScopesProcessor(); 75 sssp = new SequentialSubScopesProcessor(); 76 for (int i = 0; i < parameters; i++) { 77 sssp.AddSubOperator(SubOperators[i]); 78 } 79 usssp2.AddSubOperator(sssp); 80 81 usssp3 = new UniformSequentialSubScopesProcessor(); 82 merger = new MergingReducer(); 83 usssp3.AddSubOperator(merger); 84 85 usssp4 = new UniformSequentialSubScopesProcessor(); 86 packer = new SimOptParameterPacker(); 87 packer.AddSubOperator(SubOperators[SubOperators.Count - 1]); 88 usssp4.AddSubOperator(packer); 89 uptodate = true; 90 } 91 // Setting the actual names is necessary as the operator does not know if they've changed 92 extractor.GetVariableInfo("Items").ActualName = itemsActualName; 93 preparator.GetVariableInfo("Parents").ActualName = parentsActualName; 94 packer.GetVariableInfo("Items").ActualName = itemsActualName; 49 95 50 96 CompositeOperation co = new CompositeOperation(); 51 for (int i = 0; i < SubOperators.Count; i++) { 52 if (SubOperators[i].GetVariable("Index") != null) { 53 SubOperators[i].GetVariable("Index").Value = new IntData(i); 54 } 55 if (SubOperators[i].GetVariableInfo("Items") != null) { 56 SubOperators[i].GetVariableInfo("Items").ActualName = GetVariableInfo("Items").ActualName; 57 } 58 } 59 for (int i = 0; i < children; i++) { 60 IScope child = (IScope)scope.SubScopes[0].Clone(); 61 for (int j = 0; j < parents; j++) { 62 IScope parent = scope.SubScopes[0]; 63 child.AddSubScope(parent); 64 scope.RemoveSubScope(parent); 65 } 66 scope.AddSubScope(child); 67 for (int n = 0 ; n < SubOperators.Count ; n++) 68 co.AddOperation(new AtomicOperation(SubOperators[n], child)); 69 co.AddOperation(new AtomicOperation(new Operators.SubScopesRemover(), child)); 70 } 97 co.AddOperation(new AtomicOperation(usssp, scope)); 98 co.AddOperation(new AtomicOperation(preparator, scope)); 99 co.AddOperation(new AtomicOperation(usssp2, scope)); 100 co.AddOperation(new AtomicOperation(usssp3, scope)); 101 co.AddOperation(new AtomicOperation(usssp4, scope)); 71 102 return co; 103 } 104 105 protected override void OnSubOperatorAdded(IOperator subOperator, int index) { 106 base.OnSubOperatorAdded(subOperator, index); 107 uptodate = false; 108 } 109 110 protected override void OnSubOperatorRemoved(IOperator subOperator, int index) { 111 base.OnSubOperatorRemoved(subOperator, index); 112 uptodate = false; 113 } 114 115 public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) { 116 base.Populate(node, restoredObjects); 117 uptodate = false; 72 118 } 73 119 }
Note: See TracChangeset
for help on using the changeset viewer.