Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/08/08 12:39:51 (16 years ago)
Author:
abeham
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.SimOpt/SimOptSquentialSubOperatorCrossover.cs

    r591 r637  
    2424using System.Linq;
    2525using System.Text;
     26using System.Xml;
    2627using HeuristicLab.Core;
    2728using HeuristicLab.Data;
    28 using HeuristicLab.Evolutionary;
     29using HeuristicLab.Operators;
     30using HeuristicLab.Selection;
    2931
    3032namespace HeuristicLab.SimOpt {
    3133  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
    3242    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:
     451. The parameters of all parents are extracted [UniformSequentialSubScopeProcessor which applies a SimOptParameterExtractor]
     462. The parents are prepared for crossing by grouping the respective parameters [SimOptCrossoverPreparator]
     473. The parameters are crossed [UniformSequentialSubScopeProcessor which applies SequentialSubScopeProcessor which applies the SubOperators of this operator on each parameter group (except for the last one)]
     484. Assigning the crossed parameters to the respective children [MergingReducer]
     495. Update the parameters [UniformSequentialSubScopeProcessor which applies SimOptParameterPacker]
     50
     51Should the packing fail due to constraint violations, the operator will execute the last of its suboperators.";
     52      }
    3453    }
    3554
     
    3857      AddVariableInfo(new VariableInfo("Items", "The parameter vector", typeof(ConstrainedItemList), VariableKind.In | VariableKind.New));
    3958      AddVariableInfo(new VariableInfo("Parents", "The number of parents per child", typeof(IntData), VariableKind.In));
     59      uptodate = false;
    4060    }
    4161
    4262    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;
    4466
    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;
    4995
    5096      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));
    71102      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;
    72118    }
    73119  }
Note: See TracChangeset for help on using the changeset viewer.