source: stable/HeuristicLab.Problems.BinPacking/3.3/2D/BinPacking2D.cs @ 14966

Last change on this file since 14966 was 14966, checked in by abeham, 3 years ago

#2785: merged 14916 to stable

File size: 9.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      ExtremePoints = new SortedSet<PackingPosition>();
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)
44      : base(original, cloner) {
45      this.ExtremePoints = new SortedSet<PackingPosition>(original.ExtremePoints.Select(p => cloner.Clone(p)));
46    }
47    public override IDeepCloneable Clone(Cloner cloner) {
48      return new BinPacking2D(this, cloner);
49    }
50
51    protected override void GenerateNewExtremePointsForNewItem(PackingItem newItem, PackingPosition position) {
52
53      int newWidth = position.Rotated ? newItem.Height : newItem.Width;
54      int newHeight = position.Rotated ? newItem.Width : newItem.Height;
55
56      //Find ExtremePoints beginning from sourcepointX
57      var sourcePointX = new PackingPosition(0, position.X + newWidth, position.Y);
58      if (sourcePointX.X < BinShape.Width && sourcePointX.Y < BinShape.Height) {
59        //Traversing down the y-axis       
60        var newPoint = new PackingPosition(0, sourcePointX.X, sourcePointX.Y - 1);
61        while (sourcePointX.Y > 0 && !IsPointOccupied(newPoint)) {
62          sourcePointX = newPoint;
63          newPoint = new PackingPosition(0, sourcePointX.X, sourcePointX.Y - 1);
64        }
65        ExtremePoints.Add(new PackingPosition(0, sourcePointX.X, sourcePointX.Y));
66      }
67
68      //Find ExtremePoints beginning from sourcepointY
69      var sourcePointY = new PackingPosition(0, position.X, position.Y + newHeight);
70      if (sourcePointY.X < BinShape.Width && sourcePointY.Y < BinShape.Height) {
71        //Traversing down the x-axis 
72        var newPoint = new PackingPosition(0, sourcePointY.X - 1, sourcePointY.Y);
73        while (sourcePointY.X > 0 && !IsPointOccupied(newPoint)) {
74          sourcePointY = newPoint;
75          newPoint = new PackingPosition(0, sourcePointY.X - 1, sourcePointY.Y);
76        }
77        ExtremePoints.Add(new PackingPosition(0, sourcePointY.X, sourcePointY.Y));
78      }
79    }
80
81    public override PackingPosition FindExtremePointForItem(PackingItem item, bool rotated, bool stackingConstraints) {
82      PackingItem rotatedItem = new PackingItem(
83        rotated ? item.Height : item.Width,
84        rotated ? item.Width : item.Height,
85        item.TargetBin) {
86        Material = item.Material,
87        Weight = item.Weight
88      };
89
90      int epIndex = 0;
91      while (epIndex < ExtremePoints.Count && (!IsPositionFeasible(rotatedItem, ExtremePoints.ElementAt(epIndex)))) { epIndex++; }
92
93      if (epIndex < ExtremePoints.Count) {
94        var currentPoint = ExtremePoints.ElementAt(epIndex);
95
96        var result = new PackingPosition(currentPoint.AssignedBin, currentPoint.X, currentPoint.Y, rotated);
97        return result;
98      }
99      return null;
100    }
101    public override PackingPosition FindPositionBySliding(PackingItem item, bool rotated) {
102      PackingPosition currentPosition = new PackingPosition(0,
103        BinShape.Width - (rotated ? item.Height : item.Width),
104        BinShape.Height - (rotated ? item.Width : item.Height), rotated);
105      //Slide the item as far as possible to the left
106      while (IsPositionFeasible(item, PackingPosition.MoveLeft(currentPosition))
107        || IsPositionFeasible(item, PackingPosition.MoveDown(currentPosition))) {
108        //Slide the item as far as possible to the bottom
109        while (IsPositionFeasible(item, PackingPosition.MoveDown(currentPosition))) {
110          currentPosition = PackingPosition.MoveDown(currentPosition);
111        }
112        if (IsPositionFeasible(item, PackingPosition.MoveLeft(currentPosition)))
113          currentPosition = PackingPosition.MoveLeft(currentPosition);
114      }
115
116      return IsPositionFeasible(item, currentPosition) ? currentPosition : null;
117    }
118
119    public override void SlidingBasedPacking(ref IList<int> sequence, IList<PackingItem> items) {
120      var temp = new List<int>(sequence);
121      for (int i = 0; i < temp.Count; i++) {
122        var item = items[temp[i]];
123        var position = FindPositionBySliding(item, false);
124        if (position != null) {
125          PackItem(temp[i], item, position);
126          sequence.Remove(temp[i]);
127        }
128      }
129    }
130    public override void SlidingBasedPacking(ref IList<int> sequence, IList<PackingItem> items, Dictionary<int, bool> rotationArray) {
131      var temp = new List<int>(sequence);
132      for (int i = 0; i < temp.Count; i++) {
133        var item = items[temp[i]];
134        var position = FindPositionBySliding(item, rotationArray[temp[i]]);
135        if (position != null) {
136          PackItem(temp[i], item, position);
137          sequence.Remove(temp[i]);
138        }
139      }
140    }
141    public override void ExtremePointBasedPacking(ref IList<int> sequence, IList<PackingItem> items, bool stackingConstraints) {
142      var temp = new List<int>(sequence);
143      foreach (int itemID in temp) {
144        var item = items[itemID];
145        var positionFound = FindExtremePointForItem(item, false, false);
146        if (positionFound != null) {
147          PackItem(itemID, item, positionFound);
148          sequence.Remove(itemID);
149        }
150      }
151    }
152    public override void ExtremePointBasedPacking(ref IList<int> sequence, IList<PackingItem> items, bool stackingConstraints, Dictionary<int, bool> rotationArray) {
153      var temp = new List<int>(sequence);
154      foreach (int itemID in temp) {
155        var item = items[itemID];
156        var positionFound = FindExtremePointForItem(item, rotationArray[itemID], false);
157        if (positionFound != null) {
158          PackItem(itemID, item, positionFound);
159          sequence.Remove(itemID);
160        }
161      }
162    }
163
164    public override int ShortestPossibleSideFromPoint(PackingPosition position) {
165      int shortestSide = int.MaxValue;
166      int width = BinShape.Width;
167      int height = BinShape.Height;
168
169      if (position.X >= width || position.Y >= height)
170        return shortestSide;
171
172      PackingPosition current = new PackingPosition(0, position.X, position.Y);
173      while (current.X < width && IsPointOccupied(current)) { current = PackingPosition.MoveRight(current); }
174      if (current.X - position.X < shortestSide)
175        shortestSide = current.X - position.X;
176
177
178      current = new PackingPosition(0, position.X, position.Y);
179      while (current.Y < height && IsPointOccupied(current)) { current = PackingPosition.MoveUp(current); }
180      if (current.Y - position.Y < shortestSide)
181        shortestSide = current.Y - position.Y;
182
183      return shortestSide;
184    }
185    public override bool IsStaticStable(PackingItem item, PackingPosition position) {
186      throw new NotSupportedException();
187    }
188
189    protected override void InitializeOccupationLayers() {
190      for (int i = 0; i * 10 <= BinShape.Width; i += 1) {
191        OccupationLayers[i] = new List<int>();
192      }
193    }
194
195    protected override void AddNewItemToOccupationLayers(int itemID, PackingItem item, PackingPosition position) {
196      int x1 = position.X / 10;
197      int x2 = (position.X + (position.Rotated ? item.Height : item.Width)) / 10;
198
199      for (int i = x1; i <= x2; i++)
200        OccupationLayers[i].Add(itemID);
201    }
202    protected override List<int> GetLayerItemIDs(PackingPosition position) {
203      return OccupationLayers[position.X / 10];
204    }
205    protected override List<int> GetLayerItemIDs(PackingItem item, PackingPosition position) {
206      List<int> result = new List<int>();
207      int x1 = position.X / 10;
208      int x2 = (position.X + (position.Rotated ? item.Height : item.Width)) / 10;
209
210      for (int i = x1; i <= x2; i++)
211        result.AddRange(OccupationLayers[i]);
212
213      return result;
214    }
215  }
216}
Note: See TracBrowser for help on using the repository browser.