Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/15/13 22:23:36 (11 years ago)
Author:
jhelm
Message:

#1966: Did some major refactoring in Decoder-classes; Added MoveEvaluator classes for different encodings and dimensions; Added new crossover-class for MCV encoding;

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking/3.3/Encodings/MultiComponentVector/Moves/ThreeWay/MultiComponentVectorMoveGenerator.cs

    r9473 r9495  
    4242      get { return (ILookupParameter<MultiComponentVectorEncoding>)Parameters["MultiComponentVector"]; }
    4343    }
    44     public ILookupParameter<MultiComponentVectorMove> MultiComponentVectorMoveParameter {
    45       get { return (LookupParameter<MultiComponentVectorMove>)Parameters["MultiComponentVectorMove"]; }
     44    public ILookupParameter<IPackingMove> PackingMoveParameter {
     45      get { return (LookupParameter<IPackingMove>)Parameters["PackingMove"]; }
    4646    }
    4747    protected ScopeParameter CurrentScopeParameter {
     
    5656      : base() {
    5757        Parameters.Add(new LookupParameter<MultiComponentVectorEncoding>("MultiComponentVector", "The multi component vector for which moves should be generated."));
    58       Parameters.Add(new LookupParameter<MultiComponentVectorMove>("MultiComponentVectorMove", "The moves that should be generated in subscopes."));
     58        Parameters.Add(new LookupParameter<IPackingMove>("PackingMove", "The moves that should be generated in subscopes."));
    5959      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope where the moves should be added as subscopes."));
    6060    }
     
    6262
    6363    public static int NrOfRotationMoves(MultiComponentVectorEncoding mcv) {
    64       return mcv.PackingInformations.Count;
     64      return mcv.NrOfItems;
    6565    }
    6666    public static IEnumerable<MultiComponentVectorMove> GenerateRotationMoves(MultiComponentVectorEncoding multiComponentVector) {
    67       for (int i = 0; i < multiComponentVector.PackingInformations.Count; i++)
    68         yield return new SingleItemRotationMove(i, multiComponentVector);
     67      for (int binNr = 0; binNr < multiComponentVector.PackingInformations.Count; binNr++)
     68        for (int index = 0; index < multiComponentVector.PackingInformations[binNr].Count; index++)
     69          yield return new SingleItemRotationMove(binNr, index, multiComponentVector);
    6970    }
    7071
    7172    public static int NrOfSingleGroupingMoves(MultiComponentVectorEncoding mcv) {
    72       int nrOfBins = 0;
    73       foreach (var pi in mcv.PackingInformations) {
    74         if (pi.AssignedBin > nrOfBins)
    75           nrOfBins = pi.AssignedBin;
    76       }
    77       nrOfBins++;
    78       return mcv.PackingInformations.Count * (nrOfBins - 1);
     73      return mcv.NrOfItems * (mcv.NrOfBins - 1);
    7974    }
    8075    public static IEnumerable<MultiComponentVectorMove> GenerateSingleGroupingMoves(MultiComponentVectorEncoding multiComponentVector) {
    81       int nrOfBins = 0;
    82       foreach (var pi in multiComponentVector.PackingInformations) {
    83         if (pi.AssignedBin > nrOfBins)
    84           nrOfBins = pi.AssignedBin;
    85       }
    86       nrOfBins++;
    87       for (int i = 0; i < multiComponentVector.PackingInformations.Count; i++)
    88         for (int j = 0; j < nrOfBins; j++) {
    89           if (j != multiComponentVector.PackingInformations[i].AssignedBin)
    90             yield return new SingleGroupingMove(i, j, multiComponentVector);
    91         }
     76      for (int oldBin = 0; oldBin < multiComponentVector.PackingInformations.Count; oldBin++)
     77        for (int index = 0; index < multiComponentVector.PackingInformations[oldBin].Count; index++)
     78          for (int newBin = 0; newBin < multiComponentVector.NrOfBins; newBin++)
     79            if (newBin != oldBin)
     80              yield return new SingleGroupingMove(oldBin, index, newBin, multiComponentVector);
    9281    }
    9382
    9483    public static int NrOfSwapPositionMoves(MultiComponentVectorEncoding mcv) {
    95       int nrOfBins = 0;
    96       foreach (var pi in mcv.PackingInformations) {
    97         if (pi.AssignedBin > nrOfBins)
    98           nrOfBins = pi.AssignedBin;
    99       }
    100       nrOfBins++;
    101       int[] itemsPerBin = new int[nrOfBins];
    102       foreach (var pi in mcv.PackingInformations) {
    103         itemsPerBin[pi.AssignedBin]++;
    104       }
    10584      int nrOfSwapPositionMoves = 0;
    106       foreach (int items in itemsPerBin) {
    107         nrOfSwapPositionMoves += (items * (items - 1)) / 2;
     85      foreach (var groupInformations in mcv.PackingInformations) {
     86        nrOfSwapPositionMoves += (groupInformations.Value.Count * (groupInformations.Value.Count - 1)) / 2;
    10887      }
    10988      return nrOfSwapPositionMoves;
    11089    }
    11190    public static IEnumerable<MultiComponentVectorMove> GenerateSwapPositionMoves(MultiComponentVectorEncoding multiComponentVector) {
    112       Dictionary<int, List<int>> indexesPerBin = new Dictionary<int, List<int>>();
    113       for (int i = 0; i < multiComponentVector.PackingInformations.Count; i++) {
    114         int currentBin = multiComponentVector.PackingInformations[i].AssignedBin;
    115         if (!indexesPerBin.ContainsKey(currentBin))
    116           indexesPerBin[currentBin] = new List<int>();
    117         indexesPerBin[currentBin].Add(i);
    118       }
    119       foreach (var entry in indexesPerBin) {
    120         for (int i = 0; i < entry.Value.Count - 1; i++)
    121           for (int j = i + 1; j < entry.Value.Count; j++)
    122             yield return new SwapPositionMove(entry.Value[i], entry.Value[j], multiComponentVector);
    123       }
     91      for (int bin = 0; bin < multiComponentVector.PackingInformations.Count; bin++)
     92        for (int oldIndex = 0; oldIndex < multiComponentVector.PackingInformations[bin].Count - 1; oldIndex++)
     93          for (int newIndex = oldIndex + 1; newIndex < multiComponentVector.PackingInformations[bin].Count; newIndex++)
     94            yield return new SwapPositionMove(bin, oldIndex, newIndex, multiComponentVector);
    12495    }
    12596
    12697    public static int NrOfChangePositionMoves(MultiComponentVectorEncoding mcv) {
    127       int nrOfBins = 0;
    128       foreach (var pi in mcv.PackingInformations) {
    129         if (pi.AssignedBin > nrOfBins)
    130           nrOfBins = pi.AssignedBin;
     98      int nrOfChangePositionMoves = 0;
     99      foreach (var groupInformations in mcv.PackingInformations) {
     100        nrOfChangePositionMoves += (groupInformations.Value.Count * (groupInformations.Value.Count - 1)) / 2;
    131101      }
    132       nrOfBins++;
    133       int[] itemsPerBin = new int[nrOfBins];
    134       foreach (var pi in mcv.PackingInformations) {
    135         itemsPerBin[pi.AssignedBin]++;
    136       }
    137       int nrOfSwapPositionMoves = 0;
    138       foreach (int items in itemsPerBin) {
    139         nrOfSwapPositionMoves += (items * (items - 1)) / 2;
    140       }
    141       return nrOfSwapPositionMoves;
     102      return nrOfChangePositionMoves;
    142103    }
    143104    public static IEnumerable<MultiComponentVectorMove> GenerateChangePositionMoves(MultiComponentVectorEncoding multiComponentVector) {
    144       Dictionary<int, List<int>> indexesPerBin = new Dictionary<int, List<int>>();
    145       for (int i = 0; i < multiComponentVector.PackingInformations.Count; i++) {
    146         int currentBin = multiComponentVector.PackingInformations[i].AssignedBin;
    147         if (!indexesPerBin.ContainsKey(currentBin))
    148           indexesPerBin[currentBin] = new List<int>();
    149         indexesPerBin[currentBin].Add(i);
    150       }
    151       foreach (var entry in indexesPerBin) {
    152         for (int i = 0; i < entry.Value.Count - 1; i++)
    153           for (int j = i + 1; j < entry.Value.Count; j++)
    154             yield return new ChangePositionMove(entry.Value[i], entry.Value[j], multiComponentVector);
    155       }
     105      for (int bin = 0; bin < multiComponentVector.PackingInformations.Count; bin++)
     106        for (int oldIndex = 0; oldIndex < multiComponentVector.PackingInformations[bin].Count - 1; oldIndex++)
     107          for (int newIndex = oldIndex + 1; newIndex < multiComponentVector.PackingInformations[bin].Count; newIndex++)
     108            yield return new ChangePositionMove(bin, oldIndex, newIndex, multiComponentVector);
    156109    }
    157110
     
    162115      for (int i = 0; i < moveScopes.Length; i++) {
    163116        moveScopes[i] = new Scope(i.ToString());
    164         moveScopes[i].Variables.Add(new Variable(MultiComponentVectorMoveParameter.ActualName, moves[i]));
     117        moveScopes[i].Variables.Add(new Variable(PackingMoveParameter.ActualName, moves[i]));
    165118      }
    166119      CurrentScopeParameter.ActualValue.SubScopes.AddRange(moveScopes);
Note: See TracChangeset for help on using the changeset viewer.