Free cookie consent management tool by TermsFeed Policy Generator

source: branches/dataset-ids-2695/HeuristicLab.Problems.BinPacking/3.3/3D/BinPacking3D.cs @ 15428

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

#2762:

  • (hopefully) fixed unit test (the instances' items were loaded on sort, this was removed and thus the instance changed)
  • corrected license header of newly created decoders
  • changed a small query in BinPacking3D
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 (Items.Count > 1)
240            UpdateResidualSpace(item, positionFound);
241          sequence.Remove(itemID);
242        }
243      }
244    }
245    public override void ExtremePointBasedPacking(ref IList<int> sequence, IList<PackingItem> items, bool stackingConstraints, Dictionary<int, bool> rotationArray) {
246      var temp = new List<int>(sequence);
247      foreach (int itemID in temp) {
248        var item = items[itemID];
249        var positionFound = FindExtremePointForItem(item, rotationArray[itemID], stackingConstraints);
250        if (positionFound != null) {
251          PackItem(itemID, item, positionFound);
252          sequence.Remove(itemID);
253        }
254      }
255    }
256    public override int ShortestPossibleSideFromPoint(PackingPosition position) {
257
258      int shortestSide = int.MaxValue;
259      int width = BinShape.Width;
260      int height = BinShape.Height;
261      int depth = BinShape.Depth;
262
263      if (position.X >= width || position.Y >= height || position.Z >= depth)
264        return shortestSide;
265
266      PackingPosition current = new PackingPosition(0, position.X, position.Y, position.Z);
267      while (current.X < width && IsPointOccupied(current)) { current = PackingPosition.MoveRight(current); }
268      if (current.X - position.X < shortestSide)
269        shortestSide = current.X - position.X;
270
271
272      current = new PackingPosition(0, position.X, position.Y, position.Z);
273      while (current.Y < height && IsPointOccupied(current)) { current = PackingPosition.MoveUp(current); }
274      if (current.Y - position.Y < shortestSide)
275        shortestSide = current.Y - position.Y;
276
277
278      current = new PackingPosition(0, position.X, position.Y, position.Z);
279      while (current.Z < depth && IsPointOccupied(current)) { current = PackingPosition.MoveFront(current); }
280      if (current.Z - position.Z < shortestSide)
281        shortestSide = current.Z - position.Z;
282
283      return shortestSide;
284    }
285    public override bool IsStaticStable(PackingItem item, PackingPosition position) {
286      //Static stability is given, if item is placed on the ground
287      if (position.Y == 0)
288        return true;
289
290      if (IsPointOccupied(new PackingPosition(0, position.X, position.Y - 1, position.Z))
291        && IsPointOccupied(new PackingPosition(0, position.X + item.Width - 1, position.Y - 1, position.Z))
292        && IsPointOccupied(new PackingPosition(0, position.X, position.Y - 1, position.Z + item.Depth - 1))
293        && IsPointOccupied(new PackingPosition(0, position.X + item.Width - 1, position.Y - 1, position.Z + item.Depth - 1)))
294        return true;
295
296      return false;
297    }
298
299
300    public bool IsSupportedByAtLeastOnePoint(PackingItem item, PackingPosition position) {
301      if (position.Y == 0)
302        return true;
303
304      int y = position.Y - 1;
305      for (int x = position.X; x < position.X + item.Width; x++)
306        for (int z = position.Z; z < position.Z + item.Depth; z++)
307          if (IsPointOccupied(new PackingPosition(0, x, y, z)))
308            return true;
309
310      return false;
311    }
312    public bool IsWeightSupported(PackingItem item, PackingPosition ep) {
313      if (ep.Y == 0)
314        return true;
315
316      if (Items[PointOccupation(new PackingPosition(0, ep.X, ep.Y - 1, ep.Z))].SupportsStacking(item)
317        && Items[PointOccupation(new PackingPosition(0, ep.X + item.Width - 1, ep.Y - 1, ep.Z))].SupportsStacking(item)
318        && Items[PointOccupation(new PackingPosition(0, ep.X, ep.Y - 1, ep.Z + item.Depth - 1))].SupportsStacking(item)
319        && Items[PointOccupation(new PackingPosition(0, ep.X + item.Width - 1, ep.Y - 1, ep.Z + item.Depth - 1))].SupportsStacking(item))
320        return true;
321
322      return false;
323    }
324
325    protected override void InitializeOccupationLayers() {
326      for (int i = 0; i * 10 <= BinShape.Depth; i += 1) {
327        OccupationLayers[i] = new List<int>();
328      }
329    }
330    protected override void AddNewItemToOccupationLayers(int itemID, PackingItem item, PackingPosition position) {
331      int z1 = position.Z / 10;
332      int z2 = (position.Z + (position.Rotated ? item.Width : item.Depth)) / 10;
333
334      for (int i = z1; i <= z2; i++)
335        OccupationLayers[i].Add(itemID);
336    }
337    protected override List<int> GetLayerItemIDs(PackingPosition position) {
338      return OccupationLayers[position.Z / 10];
339    }
340    protected override List<int> GetLayerItemIDs(PackingItem item, PackingPosition position) {
341      List<int> result = new List<int>();
342      int z1 = position.Z / 10;
343      int z2 = (position.Z + (position.Rotated ? item.Width : item.Depth)) / 10;
344
345      for (int i = z1; i <= z2; i++)
346        result.AddRange(OccupationLayers[i]);
347      return result;
348    }
349   
350    public void UpdateResidualSpace(PackingItem item, PackingPosition pos) {
351      foreach (var ep in ExtremePoints) {
352        if (ep.Z >= pos.Z && ep.Z <= pos.Z + item.Depth) {
353          if (ep.X <= pos.X && ep.Y > pos.Y && ep.Y < pos.Y + item.Height) {
354            var diff = pos.X - ep.X;
355            var newRSX = ResidualSpace[ep].Item1 < diff ? ResidualSpace[ep].Item1 : diff;
356            ResidualSpace[ep] = Tuple.Create(newRSX, ResidualSpace[ep].Item2, ResidualSpace[ep].Item3);
357          }
358          if (ep.Y <= pos.Y && ep.X > pos.X && ep.X < pos.X + item.Width) {
359            var diff = pos.Y - ep.Y;
360            var newRSY = ResidualSpace[ep].Item2 < diff ? ResidualSpace[ep].Item2 : diff;
361            ResidualSpace[ep] = Tuple.Create(ResidualSpace[ep].Item1, newRSY, ResidualSpace[ep].Item3);
362          }
363        }
364        if (ep.Z <= pos.Z &&
365          ep.Y > pos.Y && ep.Y < pos.Y + item.Height &&
366          ep.X > pos.X && ep.X < pos.X + item.Width) {
367            var diff = pos.Z - ep.Z;
368            var newRSZ = ResidualSpace[ep].Item3 < diff ? ResidualSpace[ep].Item3 : diff;
369            ResidualSpace[ep] = Tuple.Create(ResidualSpace[ep].Item1, ResidualSpace[ep].Item2, newRSZ);
370        }
371      }
372      return;
373    }
374  }
375}
Note: See TracBrowser for help on using the repository browser.