Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16147 was 16140, checked in by abeham, 6 years ago

#2817: updated to trunk r15680

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