1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 Joseph Helm and 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.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Problems.BinPacking;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Problems.BinPacking3D {
|
---|
30 | [Item("BinPacking3D", "Represents a single-bin packing for a 3D bin-packing problem.")]
|
---|
31 | [StorableClass]
|
---|
32 | public class BinPacking3D : BinPacking<PackingPosition, PackingShape, PackingItem> {
|
---|
33 |
|
---|
34 | public BinPacking3D(PackingShape binShape)
|
---|
35 | : base(binShape) {
|
---|
36 | ExtremePoints = new SortedSet<PackingPosition>();
|
---|
37 | ExtremePoints.Add(binShape.Origin);
|
---|
38 | InitializeOccupationLayers();
|
---|
39 | }
|
---|
40 | [StorableConstructor]
|
---|
41 | protected BinPacking3D(bool deserializing) : base(deserializing) { }
|
---|
42 | protected BinPacking3D(BinPacking3D original, Cloner cloner)
|
---|
43 | : base(original, cloner) {
|
---|
44 | this.ExtremePoints = new SortedSet<PackingPosition>(original.ExtremePoints.Select(p => cloner.Clone(p)));
|
---|
45 | }
|
---|
46 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
47 | return new BinPacking3D(this, cloner);
|
---|
48 | }
|
---|
49 |
|
---|
50 | protected override void GenerateNewExtremePointsForNewItem(PackingItem newItem, PackingPosition position) {
|
---|
51 | int newWidth = position.Rotated ? newItem.Depth : newItem.Width;
|
---|
52 | int newDepth = position.Rotated ? newItem.Width : newItem.Depth;
|
---|
53 |
|
---|
54 | //Find ExtremePoints beginning from sourcepointX
|
---|
55 | var sourcePointX = new PackingPosition(0, position.X + newWidth, position.Y, position.Z);
|
---|
56 | if (sourcePointX.X < BinShape.Width && sourcePointX.Y < BinShape.Height && sourcePointX.Z < BinShape.Depth) {
|
---|
57 | //Traversing down the y-axis
|
---|
58 | PackingPosition current = new PackingPosition(0, sourcePointX.X, sourcePointX.Y, sourcePointX.Z);
|
---|
59 | while (current.Y > 0 && !IsPointOccupied(PackingPosition.MoveDown(current))) {
|
---|
60 | current = PackingPosition.MoveDown(current);
|
---|
61 | }
|
---|
62 | ExtremePoints.Add((PackingPosition)current.Clone());
|
---|
63 | while (current.X > 0 && !IsPointOccupied(PackingPosition.MoveLeft(current))) {
|
---|
64 | current = PackingPosition.MoveLeft(current);
|
---|
65 | }
|
---|
66 | ExtremePoints.Add(current);
|
---|
67 |
|
---|
68 | //Traversing down the z-axis
|
---|
69 | current = new PackingPosition(0, sourcePointX.X, sourcePointX.Y, sourcePointX.Z);
|
---|
70 | while (current.Z > 0 && !IsPointOccupied(PackingPosition.MoveBack(current))) {
|
---|
71 | current = PackingPosition.MoveBack(current);
|
---|
72 | }
|
---|
73 | ExtremePoints.Add((PackingPosition)current.Clone());
|
---|
74 | while (current.Y > 0 && !IsPointOccupied(PackingPosition.MoveDown(current))) {
|
---|
75 | current = PackingPosition.MoveDown(current);
|
---|
76 | }
|
---|
77 | ExtremePoints.Add(current);
|
---|
78 | }
|
---|
79 |
|
---|
80 | //Find ExtremePoints beginning from sourcepointY
|
---|
81 | var sourcePointY = new PackingPosition(0, position.X, position.Y + newItem.Height, position.Z);
|
---|
82 | if (sourcePointY.X < BinShape.Width && sourcePointY.Y < BinShape.Height && sourcePointY.Z < BinShape.Depth) {
|
---|
83 | //Traversing down the x-axis
|
---|
84 | PackingPosition current = new PackingPosition(0, sourcePointY.X, sourcePointY.Y, sourcePointY.Z);
|
---|
85 | while (current.X > 0 && !IsPointOccupied(PackingPosition.MoveLeft(current))) {
|
---|
86 | current = PackingPosition.MoveLeft(current);
|
---|
87 | }
|
---|
88 | ExtremePoints.Add((PackingPosition)current.Clone());
|
---|
89 | while (current.Y > 0 && !IsPointOccupied(PackingPosition.MoveDown(current))) {
|
---|
90 | current = PackingPosition.MoveDown(current);
|
---|
91 | }
|
---|
92 | ExtremePoints.Add(current);
|
---|
93 |
|
---|
94 | //Traversing down the z-axis
|
---|
95 | current = new PackingPosition(0, sourcePointY.X, sourcePointY.Y, sourcePointY.Z);
|
---|
96 | while (current.Z > 0 && !IsPointOccupied(PackingPosition.MoveBack(current))) {
|
---|
97 | current = PackingPosition.MoveBack(current);
|
---|
98 | }
|
---|
99 | ExtremePoints.Add((PackingPosition)current.Clone());
|
---|
100 | while (current.Y > 0 && !IsPointOccupied(PackingPosition.MoveDown(current))) {
|
---|
101 | current = PackingPosition.MoveDown(current);
|
---|
102 | }
|
---|
103 | ExtremePoints.Add(current);
|
---|
104 | }
|
---|
105 |
|
---|
106 | //Find ExtremePoints beginning from sourcepointZ
|
---|
107 | var sourcePointZ = new PackingPosition(0, position.X, position.Y, position.Z + newDepth);
|
---|
108 | if (sourcePointZ.X < BinShape.Width && sourcePointZ.Y < BinShape.Height && sourcePointZ.Z < BinShape.Depth) {
|
---|
109 | //Traversing down the x-axis
|
---|
110 | PackingPosition current = new PackingPosition(0, sourcePointZ.X, sourcePointZ.Y, sourcePointZ.Z);
|
---|
111 | while (current.X > 0 && !IsPointOccupied(PackingPosition.MoveLeft(current))) {
|
---|
112 | current = PackingPosition.MoveLeft(current);
|
---|
113 | }
|
---|
114 | ExtremePoints.Add((PackingPosition)current.Clone());
|
---|
115 | while (current.Y > 0 && !IsPointOccupied(PackingPosition.MoveDown(current))) {
|
---|
116 | current = PackingPosition.MoveDown(current);
|
---|
117 | }
|
---|
118 | ExtremePoints.Add(current);
|
---|
119 |
|
---|
120 | //Traversing down the y-axis
|
---|
121 | current = new PackingPosition(0, sourcePointZ.X, sourcePointZ.Y, sourcePointZ.Z);
|
---|
122 | while (current.Y > 0 && !IsPointOccupied(PackingPosition.MoveDown(current))) {
|
---|
123 | current = PackingPosition.MoveDown(current);
|
---|
124 | }
|
---|
125 | ExtremePoints.Add((PackingPosition)current.Clone());
|
---|
126 | while (current.X > 0 && !IsPointOccupied(PackingPosition.MoveLeft(current))) {
|
---|
127 | current = PackingPosition.MoveLeft(current);
|
---|
128 | }
|
---|
129 | ExtremePoints.Add(current);
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | public override PackingPosition FindExtremePointForItem(PackingItem item, bool rotated, bool stackingConstraints) {
|
---|
134 |
|
---|
135 | PackingItem newItem = new PackingItem(
|
---|
136 | rotated ? item.Depth : item.Width,
|
---|
137 | item.Height,
|
---|
138 | rotated ? item.Width : item.Depth,
|
---|
139 | item.TargetBin, item.Weight, item.Material);
|
---|
140 |
|
---|
141 | int epIndex = 0;
|
---|
142 | while (epIndex < ExtremePoints.Count && (
|
---|
143 | !IsPositionFeasible(newItem, ExtremePoints.ElementAt(epIndex))
|
---|
144 | || !IsSupportedByAtLeastOnePoint(newItem, ExtremePoints.ElementAt(epIndex))
|
---|
145 | || (stackingConstraints && !IsStaticStable(newItem, ExtremePoints.ElementAt(epIndex)))
|
---|
146 | || (stackingConstraints && !IsWeightSupported(newItem, ExtremePoints.ElementAt(epIndex)))
|
---|
147 | )) { epIndex++; }
|
---|
148 |
|
---|
149 | if (epIndex < ExtremePoints.Count) {
|
---|
150 | var origPoint = ExtremePoints.ElementAt(epIndex);
|
---|
151 | var result = new PackingPosition(origPoint.AssignedBin, origPoint.X, origPoint.Y, origPoint.Z, rotated);
|
---|
152 | return result;
|
---|
153 | }
|
---|
154 | return null;
|
---|
155 | }
|
---|
156 |
|
---|
157 | public override PackingPosition FindPositionBySliding(PackingItem item, bool rotated) {
|
---|
158 | //TODO: does not support stacking constraints yet
|
---|
159 | //Starting-position at upper right corner (=left bottom point of item-rectangle is at position item.width,item.height)
|
---|
160 | PackingPosition currentPosition = new PackingPosition(0,
|
---|
161 | BinShape.Width - (rotated ? item.Depth : item.Width),
|
---|
162 | BinShape.Height - item.Height,
|
---|
163 | BinShape.Depth - (rotated ? item.Width : item.Depth), rotated);
|
---|
164 | //Slide the item as far as possible to the bottom
|
---|
165 | while (IsPositionFeasible(item, PackingPosition.MoveDown(currentPosition))
|
---|
166 | || IsPositionFeasible(item, PackingPosition.MoveLeft(currentPosition))
|
---|
167 | || IsPositionFeasible(item, PackingPosition.MoveBack(currentPosition))) {
|
---|
168 | //Slide the item as far as possible to the left
|
---|
169 | while (IsPositionFeasible(item, PackingPosition.MoveLeft(currentPosition))
|
---|
170 | || IsPositionFeasible(item, PackingPosition.MoveBack(currentPosition))) {
|
---|
171 | //Slide the item as far as possible to the back
|
---|
172 | while (IsPositionFeasible(item, PackingPosition.MoveBack(currentPosition))) {
|
---|
173 | currentPosition = PackingPosition.MoveBack(currentPosition);
|
---|
174 | }
|
---|
175 | if (IsPositionFeasible(item, PackingPosition.MoveLeft(currentPosition)))
|
---|
176 | currentPosition = PackingPosition.MoveLeft(currentPosition);
|
---|
177 | }
|
---|
178 | if (IsPositionFeasible(item, PackingPosition.MoveDown(currentPosition)))
|
---|
179 | currentPosition = PackingPosition.MoveDown(currentPosition);
|
---|
180 | }
|
---|
181 |
|
---|
182 | return IsPositionFeasible(item, currentPosition) ? currentPosition : null;
|
---|
183 | }
|
---|
184 |
|
---|
185 | public override void SlidingBasedPacking(ref IList<int> sequence, IList<PackingItem> items) {
|
---|
186 | var temp = new List<int>(sequence);
|
---|
187 | for (int i = 0; i < temp.Count; i++) {
|
---|
188 | var item = items[temp[i]];
|
---|
189 | var position = FindPositionBySliding(item, false);
|
---|
190 | if (position != null) {
|
---|
191 | PackItem(temp[i], item, position);
|
---|
192 | sequence.Remove(temp[i]);
|
---|
193 | }
|
---|
194 | }
|
---|
195 | }
|
---|
196 | public override void SlidingBasedPacking(ref IList<int> sequence, IList<PackingItem> items, Dictionary<int, bool> rotationArray) {
|
---|
197 | var temp = new List<int>(sequence);
|
---|
198 | for (int i = 0; i < temp.Count; i++) {
|
---|
199 | var item = items[temp[i]];
|
---|
200 | var position = FindPositionBySliding(item, rotationArray[i]);
|
---|
201 | if (position != null) {
|
---|
202 | PackItem(temp[i], item, position);
|
---|
203 | sequence.Remove(temp[i]);
|
---|
204 | }
|
---|
205 | }
|
---|
206 | }
|
---|
207 | public override void ExtremePointBasedPacking(ref IList<int> sequence, IList<PackingItem> items, bool stackingConstraints) {
|
---|
208 | var temp = new List<int>(sequence);
|
---|
209 | foreach (int itemID in temp) {
|
---|
210 | var item = items[itemID];
|
---|
211 | var positionFound = FindExtremePointForItem(item, false, stackingConstraints);
|
---|
212 | if (positionFound != null) {
|
---|
213 | PackItem(itemID, item, positionFound);
|
---|
214 | sequence.Remove(itemID);
|
---|
215 | }
|
---|
216 | }
|
---|
217 | }
|
---|
218 | public override void ExtremePointBasedPacking(ref IList<int> sequence, IList<PackingItem> items, bool stackingConstraints, Dictionary<int, bool> rotationArray) {
|
---|
219 | var temp = new List<int>(sequence);
|
---|
220 | foreach (int itemID in temp) {
|
---|
221 | var item = items[itemID];
|
---|
222 | var positionFound = FindExtremePointForItem(item, rotationArray[itemID], stackingConstraints);
|
---|
223 | if (positionFound != null) {
|
---|
224 | PackItem(itemID, item, positionFound);
|
---|
225 | sequence.Remove(itemID);
|
---|
226 | }
|
---|
227 | }
|
---|
228 | }
|
---|
229 |
|
---|
230 | public override int ShortestPossibleSideFromPoint(PackingPosition position) {
|
---|
231 |
|
---|
232 | int shortestSide = int.MaxValue;
|
---|
233 | int width = BinShape.Width;
|
---|
234 | int height = BinShape.Height;
|
---|
235 | int depth = BinShape.Depth;
|
---|
236 |
|
---|
237 | if (position.X >= width || position.Y >= height || position.Z >= depth)
|
---|
238 | return shortestSide;
|
---|
239 |
|
---|
240 | PackingPosition current = new PackingPosition(0, position.X, position.Y, position.Z);
|
---|
241 | while (current.X < width && IsPointOccupied(current)) { current = PackingPosition.MoveRight(current); }
|
---|
242 | if (current.X - position.X < shortestSide)
|
---|
243 | shortestSide = current.X - position.X;
|
---|
244 |
|
---|
245 |
|
---|
246 | current = new PackingPosition(0, position.X, position.Y, position.Z);
|
---|
247 | while (current.Y < height && IsPointOccupied(current)) { current = PackingPosition.MoveUp(current); }
|
---|
248 | if (current.Y - position.Y < shortestSide)
|
---|
249 | shortestSide = current.Y - position.Y;
|
---|
250 |
|
---|
251 |
|
---|
252 | current = new PackingPosition(0, position.X, position.Y, position.Z);
|
---|
253 | while (current.Z < depth && IsPointOccupied(current)) { current = PackingPosition.MoveFront(current); }
|
---|
254 | if (current.Z - position.Z < shortestSide)
|
---|
255 | shortestSide = current.Z - position.Z;
|
---|
256 |
|
---|
257 | return shortestSide;
|
---|
258 | }
|
---|
259 | public override bool IsStaticStable(PackingItem item, PackingPosition position) {
|
---|
260 | //Static stability is given, if item is placed on the ground
|
---|
261 | if (position.Y == 0)
|
---|
262 | return true;
|
---|
263 |
|
---|
264 | if (IsPointOccupied(new PackingPosition(0, position.X, position.Y - 1, position.Z))
|
---|
265 | && IsPointOccupied(new PackingPosition(0, position.X + item.Width - 1, position.Y - 1, position.Z))
|
---|
266 | && IsPointOccupied(new PackingPosition(0, position.X, position.Y - 1, position.Z + item.Depth - 1))
|
---|
267 | && IsPointOccupied(new PackingPosition(0, position.X + item.Width - 1, position.Y - 1, position.Z + item.Depth - 1)))
|
---|
268 | return true;
|
---|
269 |
|
---|
270 | return false;
|
---|
271 | }
|
---|
272 |
|
---|
273 |
|
---|
274 | public bool IsSupportedByAtLeastOnePoint(PackingItem item, PackingPosition position) {
|
---|
275 | if (position.Y == 0)
|
---|
276 | return true;
|
---|
277 |
|
---|
278 | int y = position.Y - 1;
|
---|
279 | for (int x = position.X; x < position.X + item.Width; x++)
|
---|
280 | for (int z = position.Z; z < position.Z + item.Depth; z++)
|
---|
281 | if (IsPointOccupied(new PackingPosition(0, x, y, z)))
|
---|
282 | return true;
|
---|
283 |
|
---|
284 | return false;
|
---|
285 | }
|
---|
286 | public bool IsWeightSupported(PackingItem item, PackingPosition ep) {
|
---|
287 | if (ep.Y == 0)
|
---|
288 | return true;
|
---|
289 |
|
---|
290 | if (Items[PointOccupation(new PackingPosition(0, ep.X, ep.Y - 1, ep.Z))].SupportsStacking(item)
|
---|
291 | && Items[PointOccupation(new PackingPosition(0, ep.X + item.Width - 1, ep.Y - 1, ep.Z))].SupportsStacking(item)
|
---|
292 | && Items[PointOccupation(new PackingPosition(0, ep.X, ep.Y - 1, ep.Z + item.Depth - 1))].SupportsStacking(item)
|
---|
293 | && Items[PointOccupation(new PackingPosition(0, ep.X + item.Width - 1, ep.Y - 1, ep.Z + item.Depth - 1))].SupportsStacking(item))
|
---|
294 | return true;
|
---|
295 |
|
---|
296 | return false;
|
---|
297 | }
|
---|
298 |
|
---|
299 |
|
---|
300 | protected override void InitializeOccupationLayers() {
|
---|
301 | for (int i = 0; i * 10 <= BinShape.Depth; i += 1) {
|
---|
302 | OccupationLayers[i] = new List<int>();
|
---|
303 | }
|
---|
304 | }
|
---|
305 | protected override void AddNewItemToOccupationLayers(int itemID, PackingItem item, PackingPosition position) {
|
---|
306 | int z1 = position.Z / 10;
|
---|
307 | int z2 = (position.Z + (position.Rotated ? item.Width : item.Depth)) / 10;
|
---|
308 |
|
---|
309 | for (int i = z1; i <= z2; i++)
|
---|
310 | OccupationLayers[i].Add(itemID);
|
---|
311 | }
|
---|
312 | protected override List<int> GetLayerItemIDs(PackingPosition position) {
|
---|
313 | return OccupationLayers[position.Z / 10];
|
---|
314 | }
|
---|
315 | protected override List<int> GetLayerItemIDs(PackingItem item, PackingPosition position) {
|
---|
316 | List<int> result = new List<int>();
|
---|
317 | int z1 = position.Z / 10;
|
---|
318 | int z2 = (position.Z + (position.Rotated ? item.Width : item.Depth)) / 10;
|
---|
319 |
|
---|
320 | for (int i = z1; i <= z2; i++)
|
---|
321 | result.AddRange(OccupationLayers[i]);
|
---|
322 |
|
---|
323 | return result;
|
---|
324 | }
|
---|
325 | }
|
---|
326 |
|
---|
327 | }
|
---|