Free cookie consent management tool by TermsFeed Policy Generator

source: branches/BinPackingExtension/HeuristicLab.Problems.BinPacking/3.3/2D/BinPacking2D.cs @ 15182

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

#2762: Merged trunk into branch

File size: 9.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;
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 bool ExtremePointBasedPacking(int itemID, IList<PackingItem> items, bool stackingConstraints) {
153      var item = items[itemID];
154      var positionFound = FindExtremePointForItem(item, false, stackingConstraints);
155      if (positionFound != null) {
156        PackItem(itemID, item, positionFound);
157        return true;
158      }
159      return false;
160    }
161   
162    public override void ExtremePointBasedPacking(ref IList<int> sequence, IList<PackingItem> items, bool stackingConstraints, Dictionary<int, bool> rotationArray) {
163      var temp = new List<int>(sequence);
164      foreach (int itemID in temp) {
165        var item = items[itemID];
166        var positionFound = FindExtremePointForItem(item, rotationArray[itemID], false);
167        if (positionFound != null) {
168          PackItem(itemID, item, positionFound);
169          sequence.Remove(itemID);
170        }
171      }
172    }
173
174    public override int ShortestPossibleSideFromPoint(PackingPosition position) {
175      int shortestSide = int.MaxValue;
176      int width = BinShape.Width;
177      int height = BinShape.Height;
178
179      if (position.X >= width || position.Y >= height)
180        return shortestSide;
181
182      PackingPosition current = new PackingPosition(0, position.X, position.Y);
183      while (current.X < width && IsPointOccupied(current)) { current = PackingPosition.MoveRight(current); }
184      if (current.X - position.X < shortestSide)
185        shortestSide = current.X - position.X;
186
187
188      current = new PackingPosition(0, position.X, position.Y);
189      while (current.Y < height && IsPointOccupied(current)) { current = PackingPosition.MoveUp(current); }
190      if (current.Y - position.Y < shortestSide)
191        shortestSide = current.Y - position.Y;
192
193      return shortestSide;
194    }
195    public override bool IsStaticStable(PackingItem item, PackingPosition position) {
196      throw new NotSupportedException();
197    }
198    protected override void InitializeOccupationLayers() {
199      for (int i = 0; i * 10 <= BinShape.Width; i += 1) {
200        OccupationLayers[i] = new List<int>();
201      }
202    }
203
204    protected override void AddNewItemToOccupationLayers(int itemID, PackingItem item, PackingPosition position) {
205      int x1 = position.X / 10;
206      int x2 = (position.X + (position.Rotated ? item.Height : item.Width)) / 10;
207
208      for (int i = x1; i <= x2; i++)
209        OccupationLayers[i].Add(itemID);
210    }
211    protected override List<int> GetLayerItemIDs(PackingPosition position) {
212      return OccupationLayers[position.X / 10];
213    }
214    protected override List<int> GetLayerItemIDs(PackingItem item, PackingPosition position) {
215      List<int> result = new List<int>();
216      int x1 = position.X / 10;
217      int x2 = (position.X + (position.Rotated ? item.Height : item.Width)) / 10;
218
219      for (int i = x1; i <= x2; i++)
220        result.AddRange(OccupationLayers[i]);
221
222      return result;
223    }
224  }
225}
Note: See TracBrowser for help on using the repository browser.