Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking/3.3/Decoders/3D/DBL/DeepestBottomLeftFunctions.cs @ 9440

Last change on this file since 9440 was 9440, checked in by jhelm, 12 years ago

#1966: Implemented new encoding (MultiComponentVector/MCV); Implemented move-operators for MCV and GV encodings; Implemented new decoding-methods for PS, GV and MCV encodings (ExtremePoint-based packing);

File size: 7.5 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Collections;
6using HeuristicLab.Core;
7using HeuristicLab.Encodings.PackingEncoding.GroupingVector;
8using HeuristicLab.Encodings.PackingEncoding.PackingSequence;
9using HeuristicLab.Problems.BinPacking.Dimensions;
10using HeuristicLab.Problems.BinPacking.Interfaces;
11using HeuristicLab.Problems.BinPacking.PackingBin;
12using HeuristicLab.Problems.BinPacking.PackingItem;
13using HeuristicLab.Problems.BinPacking.Shapes;
14
15namespace HeuristicLab.Problems.BinPacking.Decoders {
16  public static class DeepestBottomLeftFunctions {
17
18    public static ObservableDictionary<int, ThreeDimensionalPacking> DeepestLeftBottomPacking(GroupingVectorEncoding solution, CuboidPackingBin binMeasures, ItemList<CuboidPackingItem> itemMeasures) {
19      int nrOfBins = solution.GroupingVector.Max() + 1;
20
21      //Get all indexes of items for every bin according to grouping-vector
22      //It is assumed, that at this point the item-indexes are sorted according to their item-size!
23      Dictionary<int, List<int>> unpackedItemIndexesPerBin = new Dictionary<int, List<int>>();
24
25      for (int i = 0; i < nrOfBins; i++) {
26        unpackedItemIndexesPerBin[i] = solution.GroupingVector
27          .Select((Value, Index) => new { Value, Index })
28          .Where(temp => temp.Value == i)
29          .Select(temp => temp.Index).ToList();
30      }
31
32      ObservableDictionary<int, ThreeDimensionalPacking> packingPositions = new ObservableDictionary<int, ThreeDimensionalPacking>();
33      var remainingItems = new List<int>();
34
35      //Iterate over all bin-lists
36      for (int binNr = 0; binNr < nrOfBins; binNr++) {
37        //Iterate over the current bin-item-list and find feasible positions in the current bin for each item
38        var unpackedItems = unpackedItemIndexesPerBin[binNr];
39        for (int i = 0; i < unpackedItems.Count; i++) {
40          var itemIndex = unpackedItems[i];
41          var item = itemMeasures[itemIndex];
42          ThreeDimensionalPacking position = null;
43
44          position = DeepestBottomLeftFunctions.DeepestLeftBottomPosition(binMeasures, binNr, item, packingPositions, itemMeasures);
45
46          if (position == null) {
47            remainingItems.Add(itemIndex);
48          } else
49            packingPositions[itemIndex] = position;
50        }
51      }
52
53      //Packing of remaining items
54      //Iterate over all bin-lists
55      for (int binNr = 0; binNr < nrOfBins && remainingItems.Count > 0; binNr++) {
56        var unpackedItems = new List<int>(remainingItems);
57        for (int i = 0; i < unpackedItems.Count; i++) {
58          var itemIndex = unpackedItems[i];
59          var item = itemMeasures[itemIndex];
60          ThreeDimensionalPacking position = null;
61
62          //Look for space in current bin
63          position = DeepestBottomLeftFunctions.DeepestLeftBottomPosition(binMeasures, binNr, item, packingPositions, itemMeasures);
64
65          if (position != null) {
66            packingPositions[itemIndex] = position;
67            remainingItems.Remove(itemIndex);
68            //solution.GroupingVector[itemIndex] = binNr;
69          }
70        }
71        if (remainingItems.Count > 0 && binNr + 1 >= nrOfBins) {
72          nrOfBins++;
73        }
74      }
75
76      return packingPositions;
77    }
78
79
80
81    public static ThreeDimensionalPacking DeepestLeftBottomPosition(CuboidPackingBin binMeasures, int binNr, CuboidPackingItem currentItem, ObservableDictionary<int, ThreeDimensionalPacking> itemPositions, ItemList<CuboidPackingItem> itemMeasures) {
82      return DeepestLeftBottomPosition(binMeasures, binNr, currentItem, itemPositions, itemMeasures, false);
83    }
84    public static ThreeDimensionalPacking DeepestLeftBottomPosition(CuboidPackingBin binMeasures, int binNr, CuboidPackingItem currentItem, ObservableDictionary<int, ThreeDimensionalPacking> itemPositions, ItemList<CuboidPackingItem> itemMeasures, bool rotated) {
85      //Starting-position at upper right corner (=left bottom point of item-rectangle is at position item.width,item.height)
86      ThreeDimensionalPacking currentPosition = new ThreeDimensionalPacking(binNr,
87        binMeasures.Width - (rotated ? currentItem.Depth : currentItem.Width),
88        binMeasures.Height - currentItem.Height,
89        binMeasures.Depth - (rotated ? currentItem.Width : currentItem.Depth), rotated);
90      //Slide the item as far as possible to the bottom
91      while (IsPositionFeasible(binMeasures, binNr, currentItem, MoveDown(currentPosition), itemPositions, itemMeasures)
92        || IsPositionFeasible(binMeasures, binNr, currentItem, MoveLeft(currentPosition), itemPositions, itemMeasures)
93        || IsPositionFeasible(binMeasures, binNr, currentItem, MoveBack(currentPosition), itemPositions, itemMeasures)) {
94        //Slide the item as far as possible to the left
95          while (IsPositionFeasible(binMeasures, binNr, currentItem, MoveLeft(currentPosition), itemPositions, itemMeasures)
96          || IsPositionFeasible(binMeasures, binNr, currentItem, MoveBack(currentPosition), itemPositions, itemMeasures)) {
97          //Slide the item as far as possible to the back
98            while (IsPositionFeasible(binMeasures, binNr, currentItem, MoveBack(currentPosition), itemPositions, itemMeasures)) {
99            currentPosition = MoveBack(currentPosition);
100          }
101            if (IsPositionFeasible(binMeasures, binNr, currentItem, MoveLeft(currentPosition), itemPositions, itemMeasures))
102            currentPosition = MoveLeft(currentPosition);
103        }
104          if (IsPositionFeasible(binMeasures, binNr, currentItem, MoveDown(currentPosition), itemPositions, itemMeasures))
105          currentPosition = MoveDown(currentPosition);
106      }
107
108      return IsPositionFeasible(binMeasures, binNr, currentItem, currentPosition, itemPositions, itemMeasures) ? currentPosition : null;
109    }
110
111    public static bool IsPositionFeasible(CuboidPackingBin binMeasures, int binNr, CuboidPackingItem currentItem,
112      ThreeDimensionalPacking currentPosition, ObservableDictionary<int, ThreeDimensionalPacking> itemPositions, ItemList<CuboidPackingItem> itemMeasures) {
113      //In this case feasability is defined as following: 1. the item fits into the bin-borders; 2. the item does not collide with another already packed item
114      if (!binMeasures.Encloses(currentPosition, currentItem))
115        return false;
116
117      var itemsWithPositions = itemMeasures.
118        Select((Value, Index) => new { Value, Index }).
119        Where(s => itemPositions.ContainsKey(s.Index)).
120        Select((s) => new { Measures = itemMeasures[s.Index], Position = itemPositions[s.Index] });
121
122      foreach (var itemWithPosition in itemsWithPositions) {
123        if (itemWithPosition.Position.AssignedBin == binNr && itemWithPosition.Measures.Overlaps(itemWithPosition.Position, currentPosition, currentItem))
124          return false;
125      }
126
127      return true;
128    }
129
130    private static ThreeDimensionalPacking MoveLeft(ThreeDimensionalPacking original) {
131      return new ThreeDimensionalPacking(original.AssignedBin, original.X - 1, original.Y, original.Z, original.Rotated);
132    }
133    private static ThreeDimensionalPacking MoveDown(ThreeDimensionalPacking original) {
134      return new ThreeDimensionalPacking(original.AssignedBin, original.X, original.Y - 1, original.Z, original.Rotated);
135    }
136    private static ThreeDimensionalPacking MoveBack(ThreeDimensionalPacking original) {
137      return new ThreeDimensionalPacking(original.AssignedBin, original.X, original.Y, original.Z - 1, original.Rotated);
138    }
139
140
141 
142  }
143}
Note: See TracBrowser for help on using the repository browser.