source: branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/2D/BinPacking2D.cs @ 15473

Last change on this file since 15473 was 15473, checked in by rhanghof, 23 months ago

#2817

  • Added unit tests
  • Refactoring of bp 3D
File size: 11.1 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;
28
29namespace HeuristicLab.Problems.BinPacking2D {
30  [Item("BinPacking2D", "Represents a single-bin packing for a 2D bin-packing problem.")]
31  [StorableClass]
32  public class BinPacking2D : BinPacking.BinPacking<PackingPosition, PackingShape, PackingItem> {
33
34    public BinPacking2D(PackingShape binShape)
35      : base(binShape) {
36      OccupationLayers = new Dictionary<int, List<int>>();
37      ExtremePoints.Add(binShape.Origin);
38      InitializeOccupationLayers();
39    }
40
41    [StorableConstructor]
42    protected BinPacking2D(bool deserializing) : base(deserializing) { }
43    protected BinPacking2D(BinPacking2D original, Cloner cloner) : base(original, cloner) {
44      this.OccupationLayers = new Dictionary<int, List<int>>();
45      foreach (var kvp in original.OccupationLayers) {
46        OccupationLayers.Add(kvp.Key, new List<int>(kvp.Value));
47      }
48    }
49    public override IDeepCloneable Clone(Cloner cloner) {
50      return new BinPacking2D(this, cloner);
51    }
52
53    [Storable]
54    protected Dictionary<int, List<int>> OccupationLayers { get; set; }
55
56    protected override void GenerateNewExtremePointsForNewItem(PackingItem newItem, PackingPosition position) {
57
58      int newWidth = position.Rotated ? newItem.Height : newItem.Width;
59      int newHeight = position.Rotated ? newItem.Width : newItem.Height;
60
61      //Find ExtremePoints beginning from sourcepointX
62      var sourcePointX = new PackingPosition(0, position.X + newWidth, position.Y);
63      if (sourcePointX.X < BinShape.Width && sourcePointX.Y < BinShape.Height) {
64        //Traversing down the y-axis       
65        var newPoint = new PackingPosition(0, sourcePointX.X, sourcePointX.Y - 1);
66        while (sourcePointX.Y > 0 && !IsPointOccupied(newPoint)) {
67          sourcePointX = newPoint;
68          newPoint = new PackingPosition(0, sourcePointX.X, sourcePointX.Y - 1);
69        }
70        ExtremePoints.Add(new PackingPosition(0, sourcePointX.X, sourcePointX.Y));
71      }
72
73      //Find ExtremePoints beginning from sourcepointY
74      var sourcePointY = new PackingPosition(0, position.X, position.Y + newHeight);
75      if (sourcePointY.X < BinShape.Width && sourcePointY.Y < BinShape.Height) {
76        //Traversing down the x-axis 
77        var newPoint = new PackingPosition(0, sourcePointY.X - 1, sourcePointY.Y);
78        while (sourcePointY.X > 0 && !IsPointOccupied(newPoint)) {
79          sourcePointY = newPoint;
80          newPoint = new PackingPosition(0, sourcePointY.X - 1, sourcePointY.Y);
81        }
82        ExtremePoints.Add(new PackingPosition(0, sourcePointY.X, sourcePointY.Y));
83      }
84    }
85
86    public PackingPosition FindExtremePointForItem(PackingItem item, bool rotated, bool stackingConstraints) {
87      PackingItem rotatedItem = new PackingItem(
88        rotated ? item.Height : item.Width,
89        rotated ? item.Width : item.Height,
90        item.TargetBin) {
91        Material = item.Material,
92        Weight = item.Weight
93      };
94
95      var ep = ExtremePoints.Where(x => IsPositionFeasible(rotatedItem, x, stackingConstraints)).FirstOrDefault();
96      if (ep != null) {
97        var result = new PackingPosition(ep.AssignedBin, ep.X, ep.Y, rotated);
98        return result;
99      }
100      return null;
101    }
102
103    public override void PackItem(int itemID, PackingItem item, PackingPosition position) {
104      Items[itemID] = item;
105      Positions[itemID] = position;
106      ExtremePoints.Remove(position);
107      GenerateNewExtremePointsForNewItem(item, position);
108
109      AddNewItemToOccupationLayers(itemID, item, position);
110    }
111
112    public bool PackItemIfFeasible(int itemID, PackingItem item, PackingPosition position, bool stackingConstraints) {
113      if (IsPositionFeasible(item, position, stackingConstraints)) {
114        PackItem(itemID, item, position);
115        return true;
116      }
117      return false;
118    }
119
120    /// <summary>
121    ///
122    /// </summary>
123    /// <param name="item"></param>
124    /// <param name="position"></param>
125    /// <param name="stackingConstraints"></param>
126    /// <returns></returns>
127    public override bool IsPositionFeasible(PackingItem item, PackingPosition position, bool stackingConstraints) {
128      //In this case feasability is defined as following:
129      //1. the item fits into the bin-borders;
130      //2. the point is supported by something;
131      //3. the item does not collide with another already packed item
132      if (!BinShape.Encloses(position, item))
133        return false;
134
135      foreach (var id in GetLayerItemIDs(item, position)) {
136        if (Items[id].Overlaps(Positions[id], position, item))
137          return false;
138      }
139
140      return true;
141    }
142
143
144    public PackingPosition FindPositionBySliding(PackingItem item, bool rotated, bool stackingConstraints) {
145      PackingPosition currentPosition = new PackingPosition(0,
146        BinShape.Width - (rotated ? item.Height : item.Width),
147        BinShape.Height - (rotated ? item.Width : item.Height), rotated);
148      //Slide the item as far as possible to the left
149      while (IsPositionFeasible(item, PackingPosition.MoveLeft(currentPosition), stackingConstraints)
150        || IsPositionFeasible(item, PackingPosition.MoveDown(currentPosition), stackingConstraints)) {
151        //Slide the item as far as possible to the bottom
152        while (IsPositionFeasible(item, PackingPosition.MoveDown(currentPosition), stackingConstraints)) {
153          currentPosition = PackingPosition.MoveDown(currentPosition);
154        }
155        if (IsPositionFeasible(item, PackingPosition.MoveLeft(currentPosition), stackingConstraints))
156          currentPosition = PackingPosition.MoveLeft(currentPosition);
157      }
158
159      return IsPositionFeasible(item, currentPosition, stackingConstraints) ? currentPosition : null;
160    }
161
162    public void SlidingBasedPacking(ref IList<int> sequence, IList<PackingItem> items, bool stackingConstraints) {
163      var temp = new List<int>(sequence);
164      for (int i = 0; i < temp.Count; i++) {
165        var item = items[temp[i]];
166        var position = FindPositionBySliding(item, false, stackingConstraints);
167        if (position != null) {
168          PackItem(temp[i], item, position);
169          sequence.Remove(temp[i]);
170        }
171      }
172    }
173    public void SlidingBasedPacking(ref IList<int> sequence, IList<PackingItem> items, Dictionary<int, bool> rotationArray, bool stackingConstraints) {
174      var temp = new List<int>(sequence);
175      for (int i = 0; i < temp.Count; i++) {
176        var item = items[temp[i]];
177        var position = FindPositionBySliding(item, rotationArray[temp[i]], stackingConstraints);
178        if (position != null) {
179          PackItem(temp[i], item, position);
180          sequence.Remove(temp[i]);
181        }
182      }
183    }
184    public void ExtremePointBasedPacking(ref IList<int> sequence, IList<PackingItem> items, bool stackingConstraints) {
185      var temp = new List<int>(sequence);
186      foreach (int itemID in temp) {
187        var item = items[itemID];
188        var positionFound = FindExtremePointForItem(item, false, false);
189        if (positionFound != null) {
190          PackItem(itemID, item, positionFound);
191          sequence.Remove(itemID);
192        }
193      }
194    }
195   
196    public void ExtremePointBasedPacking(ref IList<int> sequence, IList<PackingItem> items, bool stackingConstraints, Dictionary<int, bool> rotationArray) {
197      var temp = new List<int>(sequence);
198      foreach (int itemID in temp) {
199        var item = items[itemID];
200        var positionFound = FindExtremePointForItem(item, rotationArray[itemID], false);
201        if (positionFound != null) {
202          PackItem(itemID, item, positionFound);
203          sequence.Remove(itemID);
204        }
205      }
206    }
207
208    public int ShortestPossibleSideFromPoint(PackingPosition position) {
209      int shortestSide = int.MaxValue;
210      int width = BinShape.Width;
211      int height = BinShape.Height;
212
213      if (position.X >= width || position.Y >= height)
214        return shortestSide;
215
216      PackingPosition current = new PackingPosition(0, position.X, position.Y);
217      while (current.X < width && IsPointOccupied(current)) { current = PackingPosition.MoveRight(current); }
218      if (current.X - position.X < shortestSide)
219        shortestSide = current.X - position.X;
220
221
222      current = new PackingPosition(0, position.X, position.Y);
223      while (current.Y < height && IsPointOccupied(current)) { current = PackingPosition.MoveUp(current); }
224      if (current.Y - position.Y < shortestSide)
225        shortestSide = current.Y - position.Y;
226
227      return shortestSide;
228    }
229    public override bool IsStaticStable(PackingItem item, PackingPosition position) {
230      throw new NotSupportedException();
231    }
232    protected void InitializeOccupationLayers() {
233      for (int i = 0; i * 10 <= BinShape.Width; i += 1) {
234        OccupationLayers[i] = new List<int>();
235      }
236    }
237
238    protected void AddNewItemToOccupationLayers(int itemID, PackingItem item, PackingPosition position) {
239      int x1 = position.X / 10;
240      int x2 = (position.X + (position.Rotated ? item.Height : item.Width)) / 10;
241
242      for (int i = x1; i <= x2; i++)
243        OccupationLayers[i].Add(itemID);
244    }
245    protected List<int> GetLayerItemIDs(PackingPosition position) {
246      return OccupationLayers[position.X / 10];
247    }
248    protected List<int> GetLayerItemIDs(PackingItem item, PackingPosition position) {
249      List<int> result = new List<int>();
250      int x1 = position.X / 10;
251      int x2 = (position.X + (position.Rotated ? item.Height : item.Width)) / 10;
252
253      for (int i = x1; i <= x2; i++)
254        result.AddRange(OccupationLayers[i]);
255
256      return result;
257    }
258   
259
260    public int PointOccupation(PackingPosition position) {
261      foreach (var id in GetLayerItemIDs(position)) {
262        if (Items[id].EnclosesPoint(Positions[id], position))
263          return id;
264      }
265      return -1;
266    }
267
268    public bool IsPointOccupied(PackingPosition position) {
269      foreach (var id in GetLayerItemIDs(position)) {
270        if (Items[id].EnclosesPoint(Positions[id], position))
271          return true;
272      }
273      return false;
274    }
275  }
276}
Note: See TracBrowser for help on using the repository browser.