Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.BinPacking/3.3/3D/BinPacking3D.cs @ 15241

Last change on this file since 15241 was 15241, checked in by abeham, 7 years ago

#2762:

  • Moved initialization / cloning of extreme points list to base class
  • Removed residual space from base class and put it into derived class for 3d bin packing
  • Updated sample
File size: 17.4 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26using HeuristicLab.Core;
27using HeuristicLab.Common;
28using HeuristicLab.Problems.BinPacking;
29
30namespace HeuristicLab.Problems.BinPacking3D {
31  [Item("BinPacking3D", "Represents a single-bin packing for a 3D bin-packing problem.")]
32  [StorableClass]
33  public class BinPacking3D : BinPacking<PackingPosition, PackingShape, PackingItem> {
34
35    [Storable]
36    public Dictionary<PackingPosition, Tuple<int, int, int>> ResidualSpace { get; protected set; }
37
38    public BinPacking3D(PackingShape binShape)
39      : base(binShape) {
40      ResidualSpace = new Dictionary<PackingPosition, Tuple<int,int,int>>();
41      AddExtremePoint(binShape.Origin);
42      InitializeOccupationLayers();
43    }
44    [StorableConstructor]
45    protected BinPacking3D(bool deserializing) : base(deserializing) { }
46    protected BinPacking3D(BinPacking3D original, Cloner cloner)
47      : base(original, cloner) {
48      this.ResidualSpace = new Dictionary<PackingPosition, Tuple<int, int, int>>();
49      foreach (var o in original.ResidualSpace)
50        this.ResidualSpace.Add(cloner.Clone(o.Key), Tuple.Create(o.Value.Item1, o.Value.Item2, o.Value.Item3));
51    }
52    public override IDeepCloneable Clone(Cloner cloner) {
53      return new BinPacking3D(this, cloner);
54    }
55
56
57    [StorableHook(HookType.AfterDeserialization)]
58    private void AfterDeserialization() {
59      // BackwardsCompatibility3.3
60      #region Backwards compatible code, remove with 3.4
61      if (ResidualSpace == null)
62        ResidualSpace = new Dictionary<PackingPosition, Tuple<int, int, int>>();
63      #endregion
64    }
65
66    public override void PackItem(int itemID, PackingItem item, PackingPosition position) {
67      ResidualSpace.Remove(position);
68      base.PackItem(itemID, item, position);
69    }
70
71    protected override void GenerateNewExtremePointsForNewItem(PackingItem newItem, PackingPosition position) {
72      int newWidth = position.Rotated ? newItem.Depth : newItem.Width;
73      int newDepth = position.Rotated ? newItem.Width : newItem.Depth;
74
75      //Find ExtremePoints beginning from sourcepointX
76      var sourcePointX = new PackingPosition(0, position.X + newWidth, position.Y, position.Z);
77      if (sourcePointX.X < BinShape.Width && sourcePointX.Y < BinShape.Height && sourcePointX.Z < BinShape.Depth) {
78        //Traversing down the y-axis 
79        PackingPosition current = new PackingPosition(0, sourcePointX.X, sourcePointX.Y, sourcePointX.Z);
80        while (current.Y > 0 && !IsPointOccupied(PackingPosition.MoveDown(current))) {
81          current = PackingPosition.MoveDown(current);
82        }
83        AddExtremePoint((PackingPosition)current.Clone());
84        while (current.X > 0 && !IsPointOccupied(PackingPosition.MoveLeft(current))) {
85          current = PackingPosition.MoveLeft(current);
86        }
87        AddExtremePoint(current);
88
89        //Traversing down the z-axis
90        current = new PackingPosition(0, sourcePointX.X, sourcePointX.Y, sourcePointX.Z);
91        while (current.Z > 0 && !IsPointOccupied(PackingPosition.MoveBack(current))) {
92          current = PackingPosition.MoveBack(current);
93        }
94        AddExtremePoint((PackingPosition)current.Clone());
95        while (current.Y > 0 && !IsPointOccupied(PackingPosition.MoveDown(current))) {
96          current = PackingPosition.MoveDown(current);
97        }
98        AddExtremePoint(current);
99      }
100
101      //Find ExtremePoints beginning from sourcepointY
102      var sourcePointY = new PackingPosition(0, position.X, position.Y + newItem.Height, position.Z);
103      if (sourcePointY.X < BinShape.Width && sourcePointY.Y < BinShape.Height && sourcePointY.Z < BinShape.Depth) {
104        //Traversing down the x-axis         
105        PackingPosition current = new PackingPosition(0, sourcePointY.X, sourcePointY.Y, sourcePointY.Z);
106        while (current.X > 0 && !IsPointOccupied(PackingPosition.MoveLeft(current))) {
107          current = PackingPosition.MoveLeft(current);
108        }
109        AddExtremePoint((PackingPosition)current.Clone());
110        while (current.Y > 0 && !IsPointOccupied(PackingPosition.MoveDown(current))) {
111          current = PackingPosition.MoveDown(current);
112        }
113        AddExtremePoint(current);
114
115        //Traversing down the z-axis
116        current = new PackingPosition(0, sourcePointY.X, sourcePointY.Y, sourcePointY.Z);
117        while (current.Z > 0 && !IsPointOccupied(PackingPosition.MoveBack(current))) {
118          current = PackingPosition.MoveBack(current);
119        }
120        AddExtremePoint((PackingPosition)current.Clone());
121        while (current.Y > 0 && !IsPointOccupied(PackingPosition.MoveDown(current))) {
122          current = PackingPosition.MoveDown(current);
123        }
124        AddExtremePoint(current);
125      }
126
127      //Find ExtremePoints beginning from sourcepointZ
128      var sourcePointZ = new PackingPosition(0, position.X, position.Y, position.Z + newDepth);
129      if (sourcePointZ.X < BinShape.Width && sourcePointZ.Y < BinShape.Height && sourcePointZ.Z < BinShape.Depth) {
130        //Traversing down the x-axis
131        PackingPosition current = new PackingPosition(0, sourcePointZ.X, sourcePointZ.Y, sourcePointZ.Z);
132        while (current.X > 0 && !IsPointOccupied(PackingPosition.MoveLeft(current))) {
133          current = PackingPosition.MoveLeft(current);
134        }
135        AddExtremePoint((PackingPosition)current.Clone());
136        while (current.Y > 0 && !IsPointOccupied(PackingPosition.MoveDown(current))) {
137          current = PackingPosition.MoveDown(current);
138        }
139        AddExtremePoint(current);
140
141        //Traversing down the y-axis
142        current = new PackingPosition(0, sourcePointZ.X, sourcePointZ.Y, sourcePointZ.Z);
143        while (current.Y > 0 && !IsPointOccupied(PackingPosition.MoveDown(current))) {
144          current = PackingPosition.MoveDown(current);
145        }
146        AddExtremePoint((PackingPosition)current.Clone());
147        while (current.X > 0 && !IsPointOccupied(PackingPosition.MoveLeft(current))) {
148          current = PackingPosition.MoveLeft(current);
149        }
150        AddExtremePoint(current);
151      }
152    }
153
154    private void AddExtremePoint(PackingPosition current) {
155      if (ExtremePoints.Add(current)) {
156        var tuple = Tuple.Create(BinShape.Width - current.X, BinShape.Height - current.Y, BinShape.Depth - current.Z);
157        ResidualSpace.Add(current, tuple);
158      }
159    }
160
161    public override PackingPosition FindExtremePointForItem(PackingItem item, bool rotated, bool stackingConstraints) {
162      PackingItem newItem = new PackingItem(
163        rotated ? item.Depth : item.Width,
164        item.Height,
165        rotated ? item.Width : item.Depth,
166        item.TargetBin, item.Weight, item.Material);
167
168      var ep = ExtremePoints.Where(x => IsPositionFeasible(newItem, x, stackingConstraints)).FirstOrDefault();
169      if (ep != null) {
170        var result = new PackingPosition(ep.AssignedBin, ep.X, ep.Y, ep.Z, rotated);
171        return result;
172      }
173      return null;
174    }
175
176    public override bool IsPositionFeasible(PackingItem item, PackingPosition position, bool stackingConstraints) {
177      var feasible = base.IsPositionFeasible(item, position, stackingConstraints);
178      return feasible
179        && IsSupportedByAtLeastOnePoint(item, position)
180        && (!stackingConstraints || (IsStaticStable(item, position) && IsWeightSupported(item, position)));
181    }
182
183    public override PackingPosition FindPositionBySliding(PackingItem item, bool rotated, bool stackingConstraints) {
184      //Starting-position at upper right corner (=left bottom point of item-rectangle is at position item.width,item.height)
185      PackingPosition currentPosition = new PackingPosition(0,
186        BinShape.Width - (rotated ? item.Depth : item.Width),
187        BinShape.Height - item.Height,
188        BinShape.Depth - (rotated ? item.Width : item.Depth), rotated);
189      //Slide the item as far as possible to the bottom
190      while (IsPositionFeasible(item, PackingPosition.MoveDown(currentPosition), stackingConstraints)
191        || IsPositionFeasible(item, PackingPosition.MoveLeft(currentPosition), stackingConstraints)
192        || IsPositionFeasible(item, PackingPosition.MoveBack(currentPosition), stackingConstraints)) {
193        //Slide the item as far as possible to the left
194        while (IsPositionFeasible(item, PackingPosition.MoveLeft(currentPosition), stackingConstraints)
195      || IsPositionFeasible(item, PackingPosition.MoveBack(currentPosition), stackingConstraints)) {
196          //Slide the item as far as possible to the back
197          while (IsPositionFeasible(item, PackingPosition.MoveBack(currentPosition), stackingConstraints)) {
198            currentPosition = PackingPosition.MoveBack(currentPosition);
199          }
200          if (IsPositionFeasible(item, PackingPosition.MoveLeft(currentPosition), stackingConstraints))
201            currentPosition = PackingPosition.MoveLeft(currentPosition);
202        }
203        if (IsPositionFeasible(item, PackingPosition.MoveDown(currentPosition), stackingConstraints))
204          currentPosition = PackingPosition.MoveDown(currentPosition);
205      }
206
207      return IsPositionFeasible(item, currentPosition, stackingConstraints) ? currentPosition : null;
208    }
209
210    public override void SlidingBasedPacking(ref IList<int> sequence, IList<PackingItem> items, bool stackingConstraints) {
211      var temp = new List<int>(sequence);
212      for (int i = 0; i < temp.Count; i++) {
213        var item = items[temp[i]];
214        var position = FindPositionBySliding(item, false, stackingConstraints);
215        if (position != null) {
216          PackItem(temp[i], item, position);
217          sequence.Remove(temp[i]);
218        }
219      }
220    }
221    public override void SlidingBasedPacking(ref IList<int> sequence, IList<PackingItem> items, Dictionary<int, bool> rotationArray, bool stackingConstraints) {
222      var temp = new List<int>(sequence);
223      for (int i = 0; i < temp.Count; i++) {
224        var item = items[temp[i]];
225        var position = FindPositionBySliding(item, rotationArray[i], stackingConstraints);
226        if (position != null) {
227          PackItem(temp[i], item, position);
228          sequence.Remove(temp[i]);
229        }
230      }
231    }
232    public override void ExtremePointBasedPacking(ref IList<int> sequence, IList<PackingItem> items, bool stackingConstraints) {
233      var temp = new List<int>(sequence);
234      foreach (int itemID in temp) {
235        var item = items[itemID];
236        var positionFound = FindExtremePointForItem(item, false, stackingConstraints);
237        if (positionFound != null) {
238          PackItem(itemID, item, positionFound);
239          if (!(positionFound.X == 0 && positionFound.Y == 0 && positionFound.Z == 0)) {
240            UpdateResidualSpace(item, positionFound);
241          }
242          sequence.Remove(itemID);
243        }
244      }
245    }
246    public override void ExtremePointBasedPacking(ref IList<int> sequence, IList<PackingItem> items, bool stackingConstraints, Dictionary<int, bool> rotationArray) {
247      var temp = new List<int>(sequence);
248      foreach (int itemID in temp) {
249        var item = items[itemID];
250        var positionFound = FindExtremePointForItem(item, rotationArray[itemID], stackingConstraints);
251        if (positionFound != null) {
252          PackItem(itemID, item, positionFound);
253          sequence.Remove(itemID);
254        }
255      }
256    }
257    public override int ShortestPossibleSideFromPoint(PackingPosition position) {
258
259      int shortestSide = int.MaxValue;
260      int width = BinShape.Width;
261      int height = BinShape.Height;
262      int depth = BinShape.Depth;
263
264      if (position.X >= width || position.Y >= height || position.Z >= depth)
265        return shortestSide;
266
267      PackingPosition current = new PackingPosition(0, position.X, position.Y, position.Z);
268      while (current.X < width && IsPointOccupied(current)) { current = PackingPosition.MoveRight(current); }
269      if (current.X - position.X < shortestSide)
270        shortestSide = current.X - position.X;
271
272
273      current = new PackingPosition(0, position.X, position.Y, position.Z);
274      while (current.Y < height && IsPointOccupied(current)) { current = PackingPosition.MoveUp(current); }
275      if (current.Y - position.Y < shortestSide)
276        shortestSide = current.Y - position.Y;
277
278
279      current = new PackingPosition(0, position.X, position.Y, position.Z);
280      while (current.Z < depth && IsPointOccupied(current)) { current = PackingPosition.MoveFront(current); }
281      if (current.Z - position.Z < shortestSide)
282        shortestSide = current.Z - position.Z;
283
284      return shortestSide;
285    }
286    public override bool IsStaticStable(PackingItem item, PackingPosition position) {
287      //Static stability is given, if item is placed on the ground
288      if (position.Y == 0)
289        return true;
290
291      if (IsPointOccupied(new PackingPosition(0, position.X, position.Y - 1, position.Z))
292        && IsPointOccupied(new PackingPosition(0, position.X + item.Width - 1, position.Y - 1, position.Z))
293        && IsPointOccupied(new PackingPosition(0, position.X, position.Y - 1, position.Z + item.Depth - 1))
294        && IsPointOccupied(new PackingPosition(0, position.X + item.Width - 1, position.Y - 1, position.Z + item.Depth - 1)))
295        return true;
296
297      return false;
298    }
299
300
301    public bool IsSupportedByAtLeastOnePoint(PackingItem item, PackingPosition position) {
302      if (position.Y == 0)
303        return true;
304
305      int y = position.Y - 1;
306      for (int x = position.X; x < position.X + item.Width; x++)
307        for (int z = position.Z; z < position.Z + item.Depth; z++)
308          if (IsPointOccupied(new PackingPosition(0, x, y, z)))
309            return true;
310
311      return false;
312    }
313    public bool IsWeightSupported(PackingItem item, PackingPosition ep) {
314      if (ep.Y == 0)
315        return true;
316
317      if (Items[PointOccupation(new PackingPosition(0, ep.X, ep.Y - 1, ep.Z))].SupportsStacking(item)
318        && Items[PointOccupation(new PackingPosition(0, ep.X + item.Width - 1, ep.Y - 1, ep.Z))].SupportsStacking(item)
319        && Items[PointOccupation(new PackingPosition(0, ep.X, ep.Y - 1, ep.Z + item.Depth - 1))].SupportsStacking(item)
320        && Items[PointOccupation(new PackingPosition(0, ep.X + item.Width - 1, ep.Y - 1, ep.Z + item.Depth - 1))].SupportsStacking(item))
321        return true;
322
323      return false;
324    }
325
326    protected override void InitializeOccupationLayers() {
327      for (int i = 0; i * 10 <= BinShape.Depth; i += 1) {
328        OccupationLayers[i] = new List<int>();
329      }
330    }
331    protected override void AddNewItemToOccupationLayers(int itemID, PackingItem item, PackingPosition position) {
332      int z1 = position.Z / 10;
333      int z2 = (position.Z + (position.Rotated ? item.Width : item.Depth)) / 10;
334
335      for (int i = z1; i <= z2; i++)
336        OccupationLayers[i].Add(itemID);
337    }
338    protected override List<int> GetLayerItemIDs(PackingPosition position) {
339      return OccupationLayers[position.Z / 10];
340    }
341    protected override List<int> GetLayerItemIDs(PackingItem item, PackingPosition position) {
342      List<int> result = new List<int>();
343      int z1 = position.Z / 10;
344      int z2 = (position.Z + (position.Rotated ? item.Width : item.Depth)) / 10;
345
346      for (int i = z1; i <= z2; i++)
347        result.AddRange(OccupationLayers[i]);
348      return result;
349    }
350   
351    public void UpdateResidualSpace(PackingItem item, PackingPosition pos) {
352      foreach (var ep in ExtremePoints) {
353        if (ep.Z >= pos.Z && ep.Z <= pos.Z + item.Depth) {
354          if (ep.X <= pos.X && ep.Y > pos.Y && ep.Y < pos.Y + item.Height) {
355            var diff = pos.X - ep.X;
356            var newRSX = ResidualSpace[ep].Item1 < diff ? ResidualSpace[ep].Item1 : diff;
357            ResidualSpace[ep] = Tuple.Create(newRSX, ResidualSpace[ep].Item2, ResidualSpace[ep].Item3);
358          }
359          if (ep.Y <= pos.Y && ep.X > pos.X && ep.X < pos.X + item.Width) {
360            var diff = pos.Y - ep.Y;
361            var newRSY = ResidualSpace[ep].Item2 < diff ? ResidualSpace[ep].Item2 : diff;
362            ResidualSpace[ep] = Tuple.Create(ResidualSpace[ep].Item1, newRSY, ResidualSpace[ep].Item3);
363          }
364        }
365        if (ep.Z <= pos.Z &&
366          ep.Y > pos.Y && ep.Y < pos.Y + item.Height &&
367          ep.X > pos.X && ep.X < pos.X + item.Width) {
368            var diff = pos.Z - ep.Z;
369            var newRSZ = ResidualSpace[ep].Item3 < diff ? ResidualSpace[ep].Item3 : diff;
370            ResidualSpace[ep] = Tuple.Create(ResidualSpace[ep].Item1, ResidualSpace[ep].Item2, newRSZ);
371        }
372      }
373      return;
374    }
375  }
376}
Note: See TracBrowser for help on using the repository browser.