1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using System.Threading.Tasks;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 | using HeuristicLab.Problems.BinPacking3D.ExtremePointCreation;
|
---|
31 | using HeuristicLab.Problems.BinPacking3D.ExtremePointPruning;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Problems.BinPacking3D.Packer {
|
---|
34 | internal class BinPackerMinRSLeft : BinPacker {
|
---|
35 | #region Constructors for HEAL
|
---|
36 | [StorableConstructor]
|
---|
37 | protected BinPackerMinRSLeft(bool deserializing) : base(deserializing) { }
|
---|
38 |
|
---|
39 | public BinPackerMinRSLeft(BinPackerMinRSLeft original, Cloner cloner) : base(original, cloner) {
|
---|
40 | }
|
---|
41 |
|
---|
42 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
43 | return new BinPackerMinRSLeft(this, cloner);
|
---|
44 | }
|
---|
45 | #endregion
|
---|
46 |
|
---|
47 |
|
---|
48 |
|
---|
49 | public BinPackerMinRSLeft() : base() { }
|
---|
50 |
|
---|
51 | /// <summary>
|
---|
52 | /// This proportion of the residual space left to the item height is used for deciding if a not stackable item should be placed.
|
---|
53 | /// </summary>
|
---|
54 | private const double NOT_STACKABLE_RS_LEFT_TO_ITEM_HEIGHT_PROPORTION = 1.1;
|
---|
55 |
|
---|
56 | public override IList<BinPacking3D> PackItems(Permutation sortedItems, PackingShape binShape, IList<PackingItem> items, ExtremePointCreationMethod epCreationMethod, ExtremePointPruningMethod epPruningMethod, bool useStackingConstraints) {
|
---|
57 | var workingItems = CloneItems(items);
|
---|
58 | IList<BinPacking3D> packingList = new List<BinPacking3D>();
|
---|
59 | IList<int> remainingIds = new List<int>(sortedItems);
|
---|
60 |
|
---|
61 | try {
|
---|
62 | while (remainingIds.Count > 0) {
|
---|
63 | BinPacking3D packingBin = new BinPacking3D(binShape);
|
---|
64 | PackRemainingItems(ref remainingIds, ref packingBin, workingItems, epCreationMethod, useStackingConstraints);
|
---|
65 | packingList.Add(packingBin);
|
---|
66 | }
|
---|
67 | } catch (BinPacking3DException e) {
|
---|
68 | }
|
---|
69 |
|
---|
70 | ExtremePointPruningFactory.CreatePruning().PruneExtremePoints(epPruningMethod, packingList);
|
---|
71 |
|
---|
72 | return packingList;
|
---|
73 | }
|
---|
74 |
|
---|
75 |
|
---|
76 | public override void PackItemsToPackingList(IList<BinPacking3D> packingList, Permutation sortedItems, PackingShape binShape, IList<PackingItem> items, ExtremePointCreationMethod epCreationMethod, ExtremePointPruningMethod epPruningMethod, bool useStackingConstraints) {
|
---|
77 | var workingItems = CloneItems(items);
|
---|
78 | IList<int> remainingIds = new List<int>(sortedItems);
|
---|
79 |
|
---|
80 | try {
|
---|
81 | if (packingList.Count > 0) {
|
---|
82 | BinPacking3D packingBin = packingList.Last();
|
---|
83 | PackRemainingItems(ref remainingIds, ref packingBin, workingItems, epCreationMethod, useStackingConstraints);
|
---|
84 | }
|
---|
85 |
|
---|
86 | while (remainingIds.Count > 0) {
|
---|
87 | BinPacking3D packingBin = new BinPacking3D(binShape);
|
---|
88 | PackRemainingItems(ref remainingIds, ref packingBin, workingItems, epCreationMethod, useStackingConstraints);
|
---|
89 | packingList.Add(packingBin);
|
---|
90 | }
|
---|
91 | } catch (BinPacking3DException e) {
|
---|
92 | }
|
---|
93 |
|
---|
94 | ExtremePointPruningFactory.CreatePruning().PruneExtremePoints(epPruningMethod, packingList);
|
---|
95 | }
|
---|
96 |
|
---|
97 | /// <summary>
|
---|
98 | /// Tries to pack the remainig items into a given BinPacking3D object. Each item could be packed into the BinPacking3D object will be removed from the list of remaining ids
|
---|
99 | /// </summary>
|
---|
100 | /// <param name="remainingIds">List of remaining ids. After the method has been executed the list has to have less items</param>
|
---|
101 | /// <param name="packingBin">This object will be filled with some of the given items</param>
|
---|
102 | /// <param name="items">List of packing items. Some of the items will be assigned to the BinPacking3D object</param>
|
---|
103 | /// <param name="epCreationMethod"></param>
|
---|
104 | /// <param name="useStackingConstraints"></param>
|
---|
105 | protected void PackRemainingItems(ref IList<int> remainingIds, ref BinPacking3D packingBin, IList<PackingItem> items, ExtremePointCreationMethod epCreationMethod, bool useStackingConstraints) {
|
---|
106 | IExtremePointCreator extremePointCreator = ExtremePointCreatorFactory.CreateExtremePointCreator(epCreationMethod, useStackingConstraints);
|
---|
107 | var remainingNotStackableItems = new List<int>();
|
---|
108 | foreach (var itemId in new List<int>(remainingIds)) {
|
---|
109 | var item = items[itemId];
|
---|
110 |
|
---|
111 | // If an item is not stackable it should have a minimum waste of the residual space.
|
---|
112 | // As long as there are stackable items left, put the non stackable items into a collection
|
---|
113 | // and try to find positions where they don't waste too much space.
|
---|
114 | // If there are no stackable items left the non stackable have to be treated as a stackable one.
|
---|
115 | if (!item.IsStackabel && useStackingConstraints && remainingIds.Where(x => items[x].IsStackabel).Any()) {
|
---|
116 | remainingNotStackableItems.Add(itemId);
|
---|
117 | } else {
|
---|
118 | PackingPosition position = FindPackingPositionForItem(packingBin, item, useStackingConstraints);
|
---|
119 | // if a valid packing position could be found, the current item can be added to the given bin
|
---|
120 | if (position != null) {
|
---|
121 | PackItem(packingBin, itemId, item, position, extremePointCreator, useStackingConstraints);
|
---|
122 | remainingIds.Remove(itemId);
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | // try to find a valid position for a non stackable item
|
---|
127 | var stackableLeft = remainingIds.Where(x => items[x].IsStackabel).Any();
|
---|
128 | foreach (var saId in new List<int>(remainingNotStackableItems)) {
|
---|
129 | item = items[saId];
|
---|
130 | PackingPosition position = null;
|
---|
131 | if (stackableLeft) {
|
---|
132 | position = FindPackingPositionForNotStackableItem(packingBin, item, useStackingConstraints);
|
---|
133 | } else {
|
---|
134 | position = FindPackingPositionForItem(packingBin, item, useStackingConstraints);
|
---|
135 | }
|
---|
136 |
|
---|
137 | if (position != null) {
|
---|
138 | PackItem(packingBin, saId, item, position, extremePointCreator, useStackingConstraints);
|
---|
139 | remainingIds.Remove(saId);
|
---|
140 | remainingNotStackableItems.Remove(saId);
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | /// <summary>
|
---|
148 | /// Tries to find a valid position for a non stackable item.
|
---|
149 | /// Positions will only be valid if the height difference of its residual space is smaller then the hight of the item.
|
---|
150 | /// </summary>
|
---|
151 | /// <param name="packingBin"></param>
|
---|
152 | /// <param name="packingItem"></param>
|
---|
153 | /// <param name="useStackingConstraints"></param>
|
---|
154 | /// <returns></returns>
|
---|
155 | private PackingPosition FindPackingPositionForNotStackableItem(BinPacking3D packingBin, PackingItem packingItem, bool useStackingConstraints) {
|
---|
156 | if (!CheckItemDimensions(packingBin, packingItem)) {
|
---|
157 | throw new BinPacking3DException($"The dimensions of the given item exceeds the bin dimensions. " +
|
---|
158 | $"Bin: ({packingBin.BinShape.Width} {packingBin.BinShape.Depth} {packingBin.BinShape.Height})" +
|
---|
159 | $"Item: ({packingItem.Width} {packingItem.Depth} {packingItem.Height})");
|
---|
160 | }
|
---|
161 | var rsds = CalculateResidalSpaceDifferences(packingBin, packingItem, useStackingConstraints).ToList();
|
---|
162 | var rsd = rsds.Where(x => x != null && (x.Y / (double)x.Item.Height) < NOT_STACKABLE_RS_LEFT_TO_ITEM_HEIGHT_PROPORTION).OrderByDescending(x => x.Y % x.Item.Height).FirstOrDefault();
|
---|
163 |
|
---|
164 | if (rsd == null) {
|
---|
165 | return null;
|
---|
166 | }
|
---|
167 |
|
---|
168 | packingItem.Rotated = rsd.Item.Rotated;
|
---|
169 | packingItem.Tilted = rsd.Item.Tilted;
|
---|
170 | return rsd.Position;
|
---|
171 | }
|
---|
172 |
|
---|
173 | protected override PackingPosition FindPackingPositionForItem(BinPacking3D packingBin, PackingItem packingItem, bool useStackingConstraints) {
|
---|
174 | if (!CheckItemDimensions(packingBin, packingItem)) {
|
---|
175 | throw new BinPacking3DException($"The dimensions of the given item exceeds the bin dimensions. " +
|
---|
176 | $"Bin: ({packingBin.BinShape.Width} {packingBin.BinShape.Depth} {packingBin.BinShape.Height})" +
|
---|
177 | $"Item: ({packingItem.Width} {packingItem.Depth} {packingItem.Height})");
|
---|
178 | }
|
---|
179 |
|
---|
180 | var rsd = CalculateResidalSpaceDifferences(packingBin, packingItem, useStackingConstraints).Where(x => x != null).FirstOrDefault();
|
---|
181 |
|
---|
182 | if (rsd == null) {
|
---|
183 | return null;
|
---|
184 | }
|
---|
185 |
|
---|
186 | packingItem.Rotated = rsd.Item.Rotated;
|
---|
187 | packingItem.Tilted = rsd.Item.Tilted;
|
---|
188 | return rsd.Position;
|
---|
189 | }
|
---|
190 |
|
---|
191 | /// <summary>
|
---|
192 | ///
|
---|
193 | /// </summary>
|
---|
194 | /// <param name="packingBin"></param>
|
---|
195 | /// <param name="packingItem"></param>
|
---|
196 | /// <param name="useStackingConstraints"></param>
|
---|
197 | /// <returns></returns>
|
---|
198 | private SortedSet<ResidualSpaceDifference> CalculateResidalSpaceDifferences(BinPacking3D packingBin, PackingItem packingItem, bool useStackingConstraints) {
|
---|
199 | var rsds = new SortedSet<ResidualSpaceDifference>();
|
---|
200 |
|
---|
201 | rsds.Add(FindResidualSpaceDifferenceForItem(packingBin, packingItem, useStackingConstraints, rotated: false, tilted: false));
|
---|
202 |
|
---|
203 | if (packingItem.TiltEnabled) {
|
---|
204 | rsds.Add(FindResidualSpaceDifferenceForItem(packingBin, packingItem, useStackingConstraints, rotated: false, tilted: true));
|
---|
205 | }
|
---|
206 | if (packingItem.RotateEnabled) {
|
---|
207 | rsds.Add(FindResidualSpaceDifferenceForItem(packingBin, packingItem, useStackingConstraints, rotated: true, tilted: false));
|
---|
208 | }
|
---|
209 | if (packingItem.RotateEnabled && packingItem.TiltEnabled) {
|
---|
210 | rsds.Add(FindResidualSpaceDifferenceForItem(packingBin, packingItem, useStackingConstraints, rotated: true, tilted: true));
|
---|
211 | }
|
---|
212 | return rsds;
|
---|
213 | }
|
---|
214 |
|
---|
215 | /// <summary>
|
---|
216 | ///
|
---|
217 | /// </summary>
|
---|
218 | /// <param name="packingBin"></param>
|
---|
219 | /// <param name="packingItem"></param>
|
---|
220 | /// <param name="useStackingConstraints"></param>
|
---|
221 | /// <param name="rotated"></param>
|
---|
222 | /// <param name="tilted"></param>
|
---|
223 | /// <returns></returns>
|
---|
224 | protected ResidualSpaceDifference FindResidualSpaceDifferenceForItem(BinPacking3D packingBin, PackingItem packingItem, bool useStackingConstraints, bool rotated, bool tilted) {
|
---|
225 | PackingItem newItem = new PackingItem(packingItem) {
|
---|
226 | Rotated = rotated,
|
---|
227 | Tilted = tilted
|
---|
228 | };
|
---|
229 |
|
---|
230 | var rsds = new SortedSet<ResidualSpaceDifference>();
|
---|
231 | foreach (var ep in packingBin.ExtremePoints) {
|
---|
232 | var position = ep.Key;
|
---|
233 | foreach (var rs in ep.Value) {
|
---|
234 | var rsd = ResidualSpaceDifference.Create(position, newItem, rs);
|
---|
235 | if (rsd != null) {
|
---|
236 | rsds.Add(rsd);
|
---|
237 | }
|
---|
238 | }
|
---|
239 | }
|
---|
240 | return rsds.Where(rsd => packingBin.IsPositionFeasible(rsd.Item, rsd.Position, useStackingConstraints)).FirstOrDefault();
|
---|
241 | }
|
---|
242 |
|
---|
243 | protected override bool CheckItemDimensions(BinPacking3D packingBin, PackingItem item) {
|
---|
244 | bool ok = false;
|
---|
245 | int width = item.OriginalWidth;
|
---|
246 | int height = item.OriginalHeight;
|
---|
247 | int depth = item.OriginalDepth;
|
---|
248 |
|
---|
249 | ok |= CheckItemDimensions(packingBin, width, height, depth);
|
---|
250 |
|
---|
251 | if (item.RotateEnabled && item.TiltEnabled) {
|
---|
252 | ok |= CheckItemDimensions(packingBin, depth, height, width);//rotated
|
---|
253 | ok |= CheckItemDimensions(packingBin, height, width, depth);//tilted
|
---|
254 | ok |= CheckItemDimensions(packingBin, depth, width, height);//rotated & tilted
|
---|
255 | } else if (item.RotateEnabled) {
|
---|
256 | ok |= CheckItemDimensions(packingBin, depth, height, width);
|
---|
257 | } else if (item.TiltEnabled) {
|
---|
258 | ok |= CheckItemDimensions(packingBin, height, width, depth);
|
---|
259 | }
|
---|
260 |
|
---|
261 | return ok;
|
---|
262 | }
|
---|
263 |
|
---|
264 | private bool CheckItemDimensions(BinPacking3D packingBin, int width, int height, int depth) {
|
---|
265 | return base.CheckItemDimensions(packingBin, new PackingItem() {
|
---|
266 | OriginalWidth = width,
|
---|
267 | OriginalHeight = height,
|
---|
268 | OriginalDepth = depth
|
---|
269 | });
|
---|
270 | }
|
---|
271 |
|
---|
272 |
|
---|
273 | protected class ResidualSpaceDifference : IComparable {
|
---|
274 | public static ResidualSpaceDifference Create(PackingPosition position, PackingItem item, ResidualSpace rs) {
|
---|
275 | var x = rs.Width - item.Width;
|
---|
276 | var y = rs.Height - item.Height;
|
---|
277 | var z = rs.Depth - item.Depth;
|
---|
278 | // the item can't be places in the residual space
|
---|
279 | if (rs.IsZero() || x < 0 || y < 0 || z < 0) {
|
---|
280 | return null;
|
---|
281 | }
|
---|
282 |
|
---|
283 | return new ResidualSpaceDifference() {
|
---|
284 | Position = position,
|
---|
285 | Item = item,
|
---|
286 | X = x,
|
---|
287 | Y = y,
|
---|
288 | Z = z
|
---|
289 | };
|
---|
290 | }
|
---|
291 |
|
---|
292 | public ResidualSpaceDifference() { }
|
---|
293 |
|
---|
294 | public PackingItem Item { get; set; }
|
---|
295 |
|
---|
296 | public PackingPosition Position { get; set; }
|
---|
297 | public int X { get; set; }
|
---|
298 | public int Y { get; set; }
|
---|
299 | public int Z { get; set; }
|
---|
300 |
|
---|
301 |
|
---|
302 | public int CompareTo(object obj) {
|
---|
303 | if (!(obj is ResidualSpaceDifference)) {
|
---|
304 | return 0;
|
---|
305 | }
|
---|
306 | var rsd = obj as ResidualSpaceDifference;
|
---|
307 |
|
---|
308 | var x = this.X - rsd.X;
|
---|
309 | var y = rsd.Y - this.Y;
|
---|
310 | var z = this.Z - rsd.Z;
|
---|
311 |
|
---|
312 | if (x != 0) {
|
---|
313 | return x;
|
---|
314 | } else if (y != 0) {
|
---|
315 | return y;
|
---|
316 | } else if (z != 0) {
|
---|
317 | return z;
|
---|
318 | }
|
---|
319 |
|
---|
320 | return 0;
|
---|
321 | }
|
---|
322 | }
|
---|
323 |
|
---|
324 | }
|
---|
325 | }
|
---|