1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Collections;
|
---|
6 | using HeuristicLab.Core;
|
---|
7 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
8 | using HeuristicLab.Encodings.PackingEncoding.GroupingVector;
|
---|
9 | using HeuristicLab.Encodings.PackingEncoding.MultiComponentVector;
|
---|
10 | using HeuristicLab.Encodings.PackingEncoding.PackingSequence;
|
---|
11 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
12 | using HeuristicLab.Problems.BinPacking.Dimensions;
|
---|
13 | using HeuristicLab.Problems.BinPacking.Interfaces;
|
---|
14 | using HeuristicLab.Problems.BinPacking.PackingBin;
|
---|
15 | using HeuristicLab.Problems.BinPacking.PackingItem;
|
---|
16 | using HeuristicLab.Problems.BinPacking.Shapes;
|
---|
17 |
|
---|
18 | namespace HeuristicLab.Problems.BinPacking.Decoders {
|
---|
19 | public static class ExtremePointsFunctions3D {
|
---|
20 |
|
---|
21 |
|
---|
22 | public static ObservableDictionary<int, ThreeDimensionalPacking> ExtremePointBasedPacking(ref MultiComponentVectorEncoding solution,
|
---|
23 | ItemList<CuboidPackingItem> itemMeasures, CuboidPackingBin binMeasures) {
|
---|
24 |
|
---|
25 | #region Preperations
|
---|
26 | int nrOfBins = solution.NrOfBins;
|
---|
27 |
|
---|
28 | //Get all indexes of items for every bin according to grouping-vector
|
---|
29 | Dictionary<int, List<PackingInformation>> unpackedItemIndexesPerBin = new Dictionary<int, List<PackingInformation>>();
|
---|
30 | Dictionary<int, HashSet<ThreeDimensionalPacking>> extremePointsForBin = new Dictionary<int, HashSet<ThreeDimensionalPacking>>();
|
---|
31 | var occupiedPoints = new List<int[, ,]>();
|
---|
32 |
|
---|
33 | for (int i = 0; i < nrOfBins; i++) {
|
---|
34 | unpackedItemIndexesPerBin[i] = new List<PackingInformation> (solution.PackingInformations[i]);
|
---|
35 |
|
---|
36 | extremePointsForBin[i] = new HashSet<ThreeDimensionalPacking>();
|
---|
37 | extremePointsForBin[i].Add(new ThreeDimensionalPacking(i, 0, 0, 0, false));
|
---|
38 | occupiedPoints = IncreaseBinCountForOccupiedPoints(occupiedPoints, binMeasures);
|
---|
39 | }
|
---|
40 |
|
---|
41 | ObservableDictionary<int, ThreeDimensionalPacking> itemPositions = new ObservableDictionary<int, ThreeDimensionalPacking>();
|
---|
42 | var remainingItems = new List<PackingInformation>();
|
---|
43 |
|
---|
44 | #endregion Preperations
|
---|
45 |
|
---|
46 |
|
---|
47 | //Iterate over all bin-lists
|
---|
48 | for (int binNr = 0; binNr < nrOfBins; binNr++) {
|
---|
49 | //Iterate over the current bin-item-list and find feasible positions in the current bin for each item
|
---|
50 | var unpackedItems = unpackedItemIndexesPerBin[binNr];
|
---|
51 | for (int i = 0; i < unpackedItems.Count; i++) {
|
---|
52 | var itemIndex = unpackedItems[i].ItemID;
|
---|
53 | var item = itemMeasures[itemIndex];
|
---|
54 |
|
---|
55 | extremePointsForBin[binNr] = OrderExtremePoints(extremePointsForBin[binNr], occupiedPoints, binMeasures);
|
---|
56 | var positionFound = FindExtremePointForItem(itemIndex, itemMeasures, binMeasures, extremePointsForBin[binNr], occupiedPoints, itemPositions, unpackedItems[i].Rotated);
|
---|
57 | if (positionFound != null) {
|
---|
58 | extremePointsForBin[binNr].Remove(positionFound);
|
---|
59 | itemPositions[itemIndex] = positionFound;
|
---|
60 | occupiedPoints = OccupyPointsForNewItem(occupiedPoints, item, positionFound, itemIndex);
|
---|
61 | extremePointsForBin[binNr] = GenerateNewExtremePointsForNewItem(extremePointsForBin[binNr], occupiedPoints, item, positionFound, binMeasures);
|
---|
62 | } else {
|
---|
63 | remainingItems.Add(unpackedItems[i]);
|
---|
64 | solution.PackingInformations[binNr].Remove(unpackedItems[i]);
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 |
|
---|
70 | //Packing of remaining items
|
---|
71 | //Iterate over all bin-lists
|
---|
72 | for (int binNr = 0; binNr < nrOfBins && remainingItems.Count > 0; binNr++) {
|
---|
73 | var unpackedItems = new List<PackingInformation>(remainingItems);
|
---|
74 | //Iterate over all the remaining items
|
---|
75 | for (int i = 0; i < unpackedItems.Count; i++) {
|
---|
76 | var itemIndex = unpackedItems[i].ItemID;
|
---|
77 | var item = itemMeasures[itemIndex];
|
---|
78 |
|
---|
79 | extremePointsForBin[binNr] = OrderExtremePoints(extremePointsForBin[binNr], occupiedPoints, binMeasures);
|
---|
80 | var positionFound = FindExtremePointForItem(itemIndex, itemMeasures, binMeasures, extremePointsForBin[binNr], occupiedPoints, itemPositions, unpackedItems[i].Rotated);
|
---|
81 | if (positionFound != null) {
|
---|
82 | extremePointsForBin[binNr].Remove(positionFound);
|
---|
83 | itemPositions[itemIndex] = positionFound;
|
---|
84 | occupiedPoints = OccupyPointsForNewItem(occupiedPoints, item, positionFound, itemIndex);
|
---|
85 | extremePointsForBin[binNr] = GenerateNewExtremePointsForNewItem(extremePointsForBin[binNr], occupiedPoints, item, positionFound, binMeasures);
|
---|
86 | remainingItems.Remove(unpackedItems[i]);
|
---|
87 | solution.PackingInformations[binNr].Add(unpackedItems[i]);
|
---|
88 | }
|
---|
89 | }
|
---|
90 | if (remainingItems.Count > 0 && binNr + 1 >= nrOfBins) {
|
---|
91 | extremePointsForBin[nrOfBins] = new HashSet<ThreeDimensionalPacking>();
|
---|
92 | extremePointsForBin[nrOfBins].Add(new ThreeDimensionalPacking(nrOfBins, 0, 0, 0, false));
|
---|
93 | occupiedPoints = IncreaseBinCountForOccupiedPoints(occupiedPoints, binMeasures);
|
---|
94 | solution.PackingInformations[nrOfBins] = new ItemList<PackingInformation>();
|
---|
95 | nrOfBins++;
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | return itemPositions;
|
---|
100 | }
|
---|
101 |
|
---|
102 |
|
---|
103 | public static ObservableDictionary<int, ThreeDimensionalPacking> ExtremePointBasedPacking(GroupingVectorEncoding solution,
|
---|
104 | ItemList<CuboidPackingItem> itemMeasures, CuboidPackingBin binMeasures) {
|
---|
105 |
|
---|
106 | #region Preperations
|
---|
107 | int nrOfBins = solution.GroupingVector.Max() + 1;
|
---|
108 |
|
---|
109 | //Get all indexes of items for every bin according to grouping-vector
|
---|
110 | Dictionary<int, List<int>> unpackedItemIndexesPerBin = new Dictionary<int, List<int>>();
|
---|
111 | Dictionary<int, HashSet<ThreeDimensionalPacking>> extremePointsForBin = new Dictionary<int, HashSet<ThreeDimensionalPacking>>();
|
---|
112 | var occupiedPoints = new List<int[, ,]>();
|
---|
113 |
|
---|
114 | for (int i = 0; i < nrOfBins; i++) {
|
---|
115 | unpackedItemIndexesPerBin[i] = solution.GroupingVector
|
---|
116 | .Select((Value, Index) => new { Value, Index })
|
---|
117 | .Where(temp => temp.Value == i)
|
---|
118 | .Select(temp => temp.Index).ToList();
|
---|
119 |
|
---|
120 | extremePointsForBin[i] = new HashSet<ThreeDimensionalPacking>();
|
---|
121 | extremePointsForBin[i].Add(new ThreeDimensionalPacking(i, 0, 0, 0, false));
|
---|
122 | occupiedPoints = IncreaseBinCountForOccupiedPoints(occupiedPoints, binMeasures);
|
---|
123 | }
|
---|
124 |
|
---|
125 | ObservableDictionary<int, ThreeDimensionalPacking> itemPositions = new ObservableDictionary<int, ThreeDimensionalPacking>();
|
---|
126 | var remainingItems = new List<int>();
|
---|
127 |
|
---|
128 | #endregion Preperations
|
---|
129 |
|
---|
130 |
|
---|
131 |
|
---|
132 | //Iterate over all bin-lists
|
---|
133 | for (int binNr = 0; binNr < nrOfBins; binNr++) {
|
---|
134 | //Iterate over the current bin-item-list and find feasible positions in the current bin for each item
|
---|
135 | var unpackedItems = unpackedItemIndexesPerBin[binNr];
|
---|
136 | for (int i = 0; i < unpackedItems.Count; i++) {
|
---|
137 | var itemIndex = unpackedItems[i];
|
---|
138 | var item = itemMeasures[itemIndex];
|
---|
139 |
|
---|
140 | extremePointsForBin[binNr] = OrderExtremePoints(extremePointsForBin[binNr], occupiedPoints, binMeasures);
|
---|
141 | var positionFound = FindExtremePointForItem(itemIndex, itemMeasures, binMeasures, extremePointsForBin[binNr], occupiedPoints, itemPositions);
|
---|
142 | if (positionFound != null) {
|
---|
143 | extremePointsForBin[binNr].Remove(positionFound);
|
---|
144 | itemPositions[itemIndex] = positionFound;
|
---|
145 | occupiedPoints = OccupyPointsForNewItem(occupiedPoints, item, positionFound, itemIndex);
|
---|
146 | extremePointsForBin[binNr] = GenerateNewExtremePointsForNewItem(extremePointsForBin[binNr], occupiedPoints, item, positionFound, binMeasures);
|
---|
147 | } else
|
---|
148 | remainingItems.Add(itemIndex);
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 |
|
---|
153 |
|
---|
154 | //Packing of remaining items
|
---|
155 | //Iterate over all bin-lists
|
---|
156 | for (int binNr = 0; binNr < nrOfBins && remainingItems.Count > 0; binNr++) {
|
---|
157 | var unpackedItems = new List<int>(remainingItems);
|
---|
158 | //Iterate over all the remaining items
|
---|
159 | for (int i = 0; i < unpackedItems.Count; i++) {
|
---|
160 | var itemIndex = unpackedItems[i];
|
---|
161 | var item = itemMeasures[itemIndex];
|
---|
162 |
|
---|
163 | extremePointsForBin[binNr] = OrderExtremePoints(extremePointsForBin[binNr], occupiedPoints, binMeasures);
|
---|
164 | var positionFound = FindExtremePointForItem(itemIndex, itemMeasures, binMeasures, extremePointsForBin[binNr], occupiedPoints, itemPositions);
|
---|
165 | if (positionFound != null) {
|
---|
166 | extremePointsForBin[binNr].Remove(positionFound);
|
---|
167 | itemPositions[itemIndex] = positionFound;
|
---|
168 | occupiedPoints = OccupyPointsForNewItem(occupiedPoints, item, positionFound, itemIndex);
|
---|
169 | extremePointsForBin[binNr] = GenerateNewExtremePointsForNewItem(extremePointsForBin[binNr], occupiedPoints, item, positionFound, binMeasures);
|
---|
170 | remainingItems.Remove(itemIndex);
|
---|
171 | }
|
---|
172 | }
|
---|
173 | if (remainingItems.Count > 0 && binNr + 1 >= nrOfBins) {
|
---|
174 | extremePointsForBin[nrOfBins] = new HashSet<ThreeDimensionalPacking>();
|
---|
175 | extremePointsForBin[nrOfBins].Add(new ThreeDimensionalPacking(nrOfBins, 0, 0, 0, false));
|
---|
176 | occupiedPoints = IncreaseBinCountForOccupiedPoints(occupiedPoints, binMeasures);
|
---|
177 | nrOfBins++;
|
---|
178 | }
|
---|
179 | }
|
---|
180 |
|
---|
181 | return itemPositions;
|
---|
182 | }
|
---|
183 |
|
---|
184 |
|
---|
185 | public static ObservableDictionary<int, ThreeDimensionalPacking> ExtremePointBasedPacking(PackingSequenceEncoding solution,
|
---|
186 | ItemList<CuboidPackingItem> itemMeasures, CuboidPackingBin binMeasures) {
|
---|
187 |
|
---|
188 | #region Preperations
|
---|
189 | var itemPositions = new ObservableDictionary<int, ThreeDimensionalPacking>();
|
---|
190 | int nrOfBins = 1;
|
---|
191 |
|
---|
192 | var occupiedPoints = new List<int[, ,]>();
|
---|
193 | occupiedPoints = IncreaseBinCountForOccupiedPoints(occupiedPoints, binMeasures);
|
---|
194 |
|
---|
195 | HashSet<ThreeDimensionalPacking> extremePoints = new HashSet<ThreeDimensionalPacking>();
|
---|
196 | extremePoints.Add(new ThreeDimensionalPacking(0, 0, 0, 0, false));
|
---|
197 | #endregion
|
---|
198 |
|
---|
199 | foreach (int itemIndex in solution.PackingSequence) {
|
---|
200 | var item = itemMeasures[itemIndex];
|
---|
201 | extremePoints = OrderExtremePoints(extremePoints, occupiedPoints, binMeasures);
|
---|
202 | var positionFound = FindExtremePointForItem(itemIndex, itemMeasures, binMeasures, extremePoints, occupiedPoints, itemPositions);
|
---|
203 | if (positionFound != null) {
|
---|
204 | extremePoints.Remove(positionFound);
|
---|
205 | }
|
---|
206 | else {
|
---|
207 | positionFound = new ThreeDimensionalPacking(nrOfBins, 0, 0, 0);
|
---|
208 | occupiedPoints = IncreaseBinCountForOccupiedPoints(occupiedPoints, binMeasures);
|
---|
209 | nrOfBins++;
|
---|
210 | }
|
---|
211 | itemPositions[itemIndex] = positionFound;
|
---|
212 | occupiedPoints = OccupyPointsForNewItem(occupiedPoints, item, positionFound, itemIndex);
|
---|
213 | extremePoints = GenerateNewExtremePointsForNewItem(extremePoints, occupiedPoints, item, positionFound, binMeasures);
|
---|
214 | }
|
---|
215 |
|
---|
216 | return itemPositions;
|
---|
217 | }
|
---|
218 |
|
---|
219 |
|
---|
220 | public static ThreeDimensionalPacking FindExtremePointForItem(int itemIndex, ItemList<CuboidPackingItem> itemMeasures, CuboidPackingBin binMeasures,
|
---|
221 | HashSet<ThreeDimensionalPacking> extremePoints, List<int[, ,]> occupiedPoints, ObservableDictionary<int, ThreeDimensionalPacking> itemPositions) {
|
---|
222 | return FindExtremePointForItem(itemIndex, itemMeasures, binMeasures, extremePoints, occupiedPoints, itemPositions, false);
|
---|
223 | }
|
---|
224 | public static ThreeDimensionalPacking FindExtremePointForItemUsingMaterialAndWeightConstraint(int itemIndex, ItemList<CuboidPackingItem> itemMeasures, CuboidPackingBin binMeasures,
|
---|
225 | HashSet<ThreeDimensionalPacking> extremePoints, List<int[, ,]> occupiedPoints, ObservableDictionary<int, ThreeDimensionalPacking> itemPositions, bool rotated) {
|
---|
226 |
|
---|
227 | var itemFromList = itemMeasures[itemIndex];
|
---|
228 | CuboidPackingItem item = new CuboidPackingItem(
|
---|
229 | rotated ? itemFromList.Depth : itemFromList.Width,
|
---|
230 | itemFromList.Height,
|
---|
231 | rotated ? itemFromList.Width : itemFromList.Depth,
|
---|
232 | itemFromList.TargetBin, itemFromList.Weight, itemFromList.Material);
|
---|
233 |
|
---|
234 | int epIndex = 0;
|
---|
235 | while (epIndex < extremePoints.Count &&
|
---|
236 | (!DeepestBottomLeftFunctions.IsPositionFeasible(binMeasures, extremePoints.ElementAt(epIndex).AssignedBin, item, extremePoints.ElementAt(epIndex), itemPositions, itemMeasures) ||
|
---|
237 | !IsStaticStable(occupiedPoints[extremePoints.ElementAt(epIndex).AssignedBin], item, extremePoints.ElementAt(epIndex)) ||
|
---|
238 | !WeightIsSupported(occupiedPoints[extremePoints.ElementAt(epIndex).AssignedBin], item, extremePoints.ElementAt(epIndex), itemMeasures))) { epIndex++; }
|
---|
239 |
|
---|
240 | if (epIndex < extremePoints.Count) {
|
---|
241 | var result = extremePoints.ElementAt(epIndex);
|
---|
242 | result.Rotated = rotated;
|
---|
243 | return result;
|
---|
244 | }
|
---|
245 | return null;
|
---|
246 | }
|
---|
247 | public static ThreeDimensionalPacking FindExtremePointForItem(int itemIndex, ItemList<CuboidPackingItem> itemMeasures, CuboidPackingBin binMeasures,
|
---|
248 | HashSet<ThreeDimensionalPacking> extremePoints, List<int[,,]> occupiedPoints, ObservableDictionary<int, ThreeDimensionalPacking> itemPositions, bool rotated) {
|
---|
249 |
|
---|
250 | var itemFromList = itemMeasures[itemIndex];
|
---|
251 | CuboidPackingItem item = new CuboidPackingItem(
|
---|
252 | rotated ? itemFromList.Depth : itemFromList.Width,
|
---|
253 | itemFromList.Height,
|
---|
254 | rotated ? itemFromList.Width : itemFromList.Depth,
|
---|
255 | itemFromList.TargetBin);
|
---|
256 |
|
---|
257 | int epIndex = 0;
|
---|
258 | while (epIndex < extremePoints.Count &&
|
---|
259 | (!DeepestBottomLeftFunctions.IsPositionFeasible(binMeasures, extremePoints.ElementAt(epIndex).AssignedBin, item, extremePoints.ElementAt(epIndex), itemPositions, itemMeasures) ||
|
---|
260 | !IsStaticStable(occupiedPoints[extremePoints.ElementAt(epIndex).AssignedBin], item, extremePoints.ElementAt(epIndex))))
|
---|
261 | { epIndex++; }
|
---|
262 |
|
---|
263 | if (epIndex < extremePoints.Count) {
|
---|
264 | var result = extremePoints.ElementAt(epIndex);
|
---|
265 | result.Rotated = rotated;
|
---|
266 | return result;
|
---|
267 | }
|
---|
268 | return null;
|
---|
269 | }
|
---|
270 | public static List<int[, ,]> IncreaseBinCountForOccupiedPoints(List<int[, ,]> occupiedPoints, CuboidPackingBin binMeasures) {
|
---|
271 | occupiedPoints.Add(new int[binMeasures.Width, binMeasures.Height, binMeasures.Depth]);
|
---|
272 | int lastIndex = occupiedPoints.Count - 1;
|
---|
273 | for (int w = 0; w < binMeasures.Width; w++) {
|
---|
274 | for (int h = 0; h < binMeasures.Height; h++) {
|
---|
275 | for (int d = 0; d < binMeasures.Depth; d++) {
|
---|
276 | occupiedPoints[lastIndex][w, h, d] = -1;
|
---|
277 | }
|
---|
278 | }
|
---|
279 | }
|
---|
280 | return occupiedPoints;
|
---|
281 | }
|
---|
282 | public static bool IsStaticStable(int[, ,] occupiedPoints, CuboidPackingItem item, ThreeDimensionalPacking ep) {
|
---|
283 | //Static stability is given, if item is placed on the ground
|
---|
284 | if (ep.Y == 0)
|
---|
285 | return true;
|
---|
286 |
|
---|
287 | if (occupiedPoints[ep.X, ep.Y - 1, ep.Z] != -1
|
---|
288 | && occupiedPoints[ep.X + item.Width - 1, ep.Y - 1, ep.Z] != -1
|
---|
289 | && occupiedPoints[ep.X, ep.Y - 1, ep.Z + item.Depth - 1] != -1
|
---|
290 | && occupiedPoints[ep.X + item.Width - 1, ep.Y - 1, ep.Z + item.Depth - 1] != -1)
|
---|
291 | return true;
|
---|
292 |
|
---|
293 | //int groundCount = 0;
|
---|
294 | //for (int x = ep.X; x < ep.X + item.Width - 1; x++) {
|
---|
295 | // for (int z = ep.Z; z < ep.Z + item.Depth - 1; z++) {
|
---|
296 | // if (occupiedPoints[x,ep.Y-1, z] != -1)
|
---|
297 | // groundCount++;
|
---|
298 | // }
|
---|
299 | //}
|
---|
300 | //double stableGround = (double)(groundCount) / (double)(item.Width * item.Depth);
|
---|
301 | //if (stableGround > 0.75)
|
---|
302 | // return true;
|
---|
303 |
|
---|
304 | return false;
|
---|
305 | }
|
---|
306 | public static bool WeightIsSupported(int[, ,] occupiedPoints, CuboidPackingItem item, ThreeDimensionalPacking ep, ItemList<CuboidPackingItem> items) {
|
---|
307 | //Static stability is given, if item is placed on the ground
|
---|
308 | if (ep.Y == 0)
|
---|
309 | return true;
|
---|
310 |
|
---|
311 | if (items[occupiedPoints[ep.X, ep.Y - 1, ep.Z]].SupportsStacking(item)
|
---|
312 | && items[occupiedPoints[ep.X + item.Width - 1, ep.Y - 1, ep.Z]].SupportsStacking(item)
|
---|
313 | && items[occupiedPoints[ep.X, ep.Y - 1, ep.Z + item.Depth - 1]].SupportsStacking(item)
|
---|
314 | && items[occupiedPoints[ep.X + item.Width - 1, ep.Y - 1, ep.Z + item.Depth - 1]].SupportsStacking(item))
|
---|
315 | return true;
|
---|
316 |
|
---|
317 | //int groundCount = 0;
|
---|
318 | //for (int x = ep.X; x < ep.X + item.Width - 1; x++) {
|
---|
319 | // for (int z = ep.Z; z < ep.Z + item.Depth - 1; z++) {
|
---|
320 | // if (occupiedPoints[x,ep.Y-1, z] != -1)
|
---|
321 | // groundCount++;
|
---|
322 | // }
|
---|
323 | //}
|
---|
324 | //double stableGround = (double)(groundCount) / (double)(item.Width * item.Depth);
|
---|
325 | //if (stableGround > 0.75)
|
---|
326 | // return true;
|
---|
327 |
|
---|
328 | return false;
|
---|
329 | }
|
---|
330 | public static List<int[, ,]> OccupyPointsForNewItem(List<int[, ,]> occupiedPoints, CuboidPackingItem newItem, ThreeDimensionalPacking position, int itemIndex) {
|
---|
331 | int width = position.Rotated ? newItem.Depth : newItem.Width;
|
---|
332 | int depth = position.Rotated ? newItem.Width : newItem.Depth;
|
---|
333 | for (int w = position.X; w < position.X + width; w++) {
|
---|
334 | for (int h = position.Y; h < position.Y + newItem.Height; h++) {
|
---|
335 | for (int d = position.Z; d < position.Z + depth; d++) {
|
---|
336 | occupiedPoints[position.AssignedBin][w,h,d] = itemIndex;
|
---|
337 | }
|
---|
338 | }
|
---|
339 | }
|
---|
340 | return occupiedPoints;
|
---|
341 | }
|
---|
342 | public static HashSet<ThreeDimensionalPacking> GenerateNewExtremePointsForNewItem(HashSet<ThreeDimensionalPacking> extremePoints, List<int[, ,]> occupiedPoints,
|
---|
343 | CuboidPackingItem newItem, ThreeDimensionalPacking position, CuboidPackingBin binMeasures) {
|
---|
344 | int currentBin = position.AssignedBin;
|
---|
345 |
|
---|
346 | int newWidth = position.Rotated ? newItem.Depth : newItem.Width;
|
---|
347 | int newDepth = position.Rotated ? newItem.Width : newItem.Depth;
|
---|
348 |
|
---|
349 | //Find ExtremePoints beginning from sourcepointX
|
---|
350 | var sourcePointX = new ThreeDimensionalPacking(currentBin, position.X + newWidth, position.Y, position.Z);
|
---|
351 | if (sourcePointX.X < binMeasures.Width && sourcePointX.Y < binMeasures.Height && sourcePointX.Z < binMeasures.Depth) {
|
---|
352 | //Traversing down the y-axis
|
---|
353 | int currentYValue = sourcePointX.Y;
|
---|
354 | while (currentYValue > 0 && occupiedPoints[currentBin][sourcePointX.X, currentYValue, sourcePointX.Z] == -1) {
|
---|
355 | currentYValue--;
|
---|
356 | }
|
---|
357 | extremePoints.Add(new ThreeDimensionalPacking(currentBin, sourcePointX.X, currentYValue, sourcePointX.Z));
|
---|
358 |
|
---|
359 | //Traversing down the z-axis
|
---|
360 | int currentZValue = sourcePointX.Z;
|
---|
361 | while (currentZValue > 0 && occupiedPoints[currentBin][sourcePointX.X, sourcePointX.Y, currentZValue - 1] == -1) {
|
---|
362 | currentZValue--;
|
---|
363 | }
|
---|
364 | extremePoints.Add(new ThreeDimensionalPacking(currentBin, sourcePointX.X, sourcePointX.Y, currentZValue));
|
---|
365 | }
|
---|
366 |
|
---|
367 |
|
---|
368 |
|
---|
369 |
|
---|
370 | //Find ExtremePoints beginning from sourcepointY
|
---|
371 | var sourcePointY = new ThreeDimensionalPacking(currentBin, position.X, position.Y + newItem.Height, position.Z);
|
---|
372 | if (sourcePointY.X < binMeasures.Width && sourcePointY.Y < binMeasures.Height && sourcePointY.Z < binMeasures.Depth) {
|
---|
373 | //Traversing down the x-axis
|
---|
374 | int currentXValue = sourcePointY.X;
|
---|
375 | while (currentXValue > 0 && occupiedPoints[currentBin][currentXValue - 1, sourcePointY.Y, sourcePointY.Z] == -1) {
|
---|
376 | currentXValue--;
|
---|
377 | }
|
---|
378 | extremePoints.Add(new ThreeDimensionalPacking(currentBin, currentXValue, sourcePointY.Y, sourcePointY.Z));
|
---|
379 |
|
---|
380 | //Traversing down the z-axis
|
---|
381 | int currentZValue = sourcePointY.Z;
|
---|
382 | while (currentZValue > 0 && occupiedPoints[currentBin][sourcePointY.X, sourcePointY.Y, currentZValue - 1] == -1) {
|
---|
383 | currentZValue--;
|
---|
384 | }
|
---|
385 | extremePoints.Add(new ThreeDimensionalPacking(currentBin, sourcePointY.X, sourcePointY.Y, currentZValue));
|
---|
386 | }
|
---|
387 |
|
---|
388 |
|
---|
389 |
|
---|
390 |
|
---|
391 |
|
---|
392 | //Find ExtremePoints beginning from sourcepointZ
|
---|
393 | var sourcePointZ = new ThreeDimensionalPacking(currentBin, position.X, position.Y, position.Z + newDepth);
|
---|
394 | if (sourcePointZ.X < binMeasures.Width && sourcePointZ.Y < binMeasures.Height && sourcePointZ.Z < binMeasures.Depth) {
|
---|
395 | //Traversing down the x-axis
|
---|
396 | int currentXValue = sourcePointZ.X;
|
---|
397 | while (currentXValue > 0 && occupiedPoints[currentBin][currentXValue - 1, sourcePointZ.Y, sourcePointZ.Z] == -1) {
|
---|
398 | currentXValue--;
|
---|
399 | }
|
---|
400 | extremePoints.Add(new ThreeDimensionalPacking(currentBin, currentXValue, sourcePointZ.Y, sourcePointZ.Z));
|
---|
401 |
|
---|
402 | //Traversing down the y-axis
|
---|
403 | int currentYValue = sourcePointZ.Y;
|
---|
404 | while (currentYValue > 0 && occupiedPoints[currentBin][sourcePointZ.X, currentYValue, sourcePointZ.Z] == -1) {
|
---|
405 | currentYValue--;
|
---|
406 | }
|
---|
407 | extremePoints.Add(new ThreeDimensionalPacking(currentBin, sourcePointZ.X, currentYValue, sourcePointZ.Z));
|
---|
408 | }
|
---|
409 |
|
---|
410 |
|
---|
411 | return extremePoints;
|
---|
412 | }
|
---|
413 |
|
---|
414 |
|
---|
415 | public static HashSet<ThreeDimensionalPacking> OrderExtremePoints(HashSet<ThreeDimensionalPacking> hashSet, List<int[, ,]> occupiedPoints, CuboidPackingBin binMeasures) {
|
---|
416 | return new HashSet<ThreeDimensionalPacking>(hashSet.OrderBy(ep => ep.AssignedBin).ThenBy(ep => ep.Z).ThenBy(ep => ep.X).ThenBy(ep => ep.Y).ThenBy(ep => ShortestPossibleSideFromEP(ep, occupiedPoints, binMeasures)));
|
---|
417 | }
|
---|
418 | public static int ShortestPossibleSideFromEP(ThreeDimensionalPacking ep, List<int[, ,]> occupiedPoints, CuboidPackingBin binMeasures) {
|
---|
419 | int shortestSide = int.MaxValue;
|
---|
420 |
|
---|
421 | if (ep.X >= binMeasures.Width || ep.Y >= binMeasures.Height || ep.Z >= binMeasures.Depth)
|
---|
422 | return shortestSide;
|
---|
423 |
|
---|
424 | int currentX = ep.X;
|
---|
425 | while (currentX < binMeasures.Width && occupiedPoints[ep.AssignedBin][currentX, ep.Y, ep.Z] == -1) { currentX++; }
|
---|
426 | if (currentX - ep.X < shortestSide)
|
---|
427 | shortestSide = currentX - ep.X;
|
---|
428 |
|
---|
429 | int currentY = ep.Y;
|
---|
430 | while (currentY < binMeasures.Height && occupiedPoints[ep.AssignedBin][ep.X, currentY, ep.Z] == -1) { currentY++; }
|
---|
431 | if (currentY - ep.Y < shortestSide)
|
---|
432 | shortestSide = currentY - ep.Y;
|
---|
433 |
|
---|
434 | int currentZ = ep.Z;
|
---|
435 | while (currentZ < binMeasures.Depth && occupiedPoints[ep.AssignedBin][ep.X, ep.Y, currentZ] == -1) { currentZ++; }
|
---|
436 | if (currentZ - ep.Z < shortestSide)
|
---|
437 | shortestSide = currentZ - ep.Z;
|
---|
438 |
|
---|
439 | return shortestSide;
|
---|
440 | }
|
---|
441 | }
|
---|
442 | }
|
---|