Free cookie consent management tool by TermsFeed Policy Generator

source: branches/MemPRAlgorithm/HeuristicLab.Problems.BinPacking/3.3/2D/BinPacking2D.cs @ 14534

Last change on this file since 14534 was 14162, checked in by gkronber, 8 years ago

#2641: first import of bin packing problem (TODO: persistence and sample)

File size: 9.0 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
87      int epIndex = 0;
88      while (epIndex < ExtremePoints.Count && (!IsPositionFeasible(rotatedItem, ExtremePoints.ElementAt(epIndex)))) { epIndex++; }
89
90      if (epIndex < ExtremePoints.Count) {
91        var currentPoint = ExtremePoints.ElementAt(epIndex);
92
93        var result = new PackingPosition(currentPoint.AssignedBin, currentPoint.X, currentPoint.Y, rotated);
94        return result;
95      }
96      return null;
97    }
98    public override PackingPosition FindPositionBySliding(PackingItem item, bool rotated) {
99      PackingPosition currentPosition = new PackingPosition(0,
100        BinShape.Width - (rotated ? item.Height : item.Width),
101        BinShape.Height - (rotated ? item.Width : item.Height), rotated);
102      //Slide the item as far as possible to the left
103      while (IsPositionFeasible(item, PackingPosition.MoveLeft(currentPosition))
104        || IsPositionFeasible(item, PackingPosition.MoveDown(currentPosition))) {
105        //Slide the item as far as possible to the bottom
106        while (IsPositionFeasible(item, PackingPosition.MoveDown(currentPosition))) {
107          currentPosition = PackingPosition.MoveDown(currentPosition);
108        }
109        if (IsPositionFeasible(item, PackingPosition.MoveLeft(currentPosition)))
110          currentPosition = PackingPosition.MoveLeft(currentPosition);
111      }
112
113      return IsPositionFeasible(item, currentPosition) ? currentPosition : null;
114    }
115
116    public override void SlidingBasedPacking(ref IList<int> sequence, IList<PackingItem> items) {
117      var temp = new List<int>(sequence);
118      for (int i = 0; i < temp.Count; i++) {
119        var item = items[temp[i]];
120        var position = FindPositionBySliding(item, false);
121        if (position != null) {
122          PackItem(temp[i], item, position);
123          sequence.Remove(temp[i]);
124        }
125      }
126    }
127    public override void SlidingBasedPacking(ref IList<int> sequence, IList<PackingItem> items, Dictionary<int, bool> rotationArray) {
128      var temp = new List<int>(sequence);
129      for (int i = 0; i < temp.Count; i++) {
130        var item = items[temp[i]];
131        var position = FindPositionBySliding(item, rotationArray[temp[i]]);
132        if (position != null) {
133          PackItem(temp[i], item, position);
134          sequence.Remove(temp[i]);
135        }
136      }
137    }
138    public override void ExtremePointBasedPacking(ref IList<int> sequence, IList<PackingItem> items, bool stackingConstraints) {
139      var temp = new List<int>(sequence);
140      foreach (int itemID in temp) {
141        var item = items[itemID];
142        var positionFound = FindExtremePointForItem(item, false, false);
143        if (positionFound != null) {
144          PackItem(itemID, item, positionFound);
145          sequence.Remove(itemID);
146        }
147      }
148    }
149    public override void ExtremePointBasedPacking(ref IList<int> sequence, IList<PackingItem> items, bool stackingConstraints, Dictionary<int, bool> rotationArray) {
150      var temp = new List<int>(sequence);
151      foreach (int itemID in temp) {
152        var item = items[itemID];
153        var positionFound = FindExtremePointForItem(item, rotationArray[itemID], false);
154        if (positionFound != null) {
155          PackItem(itemID, item, positionFound);
156          sequence.Remove(itemID);
157        }
158      }
159    }
160
161    public override int ShortestPossibleSideFromPoint(PackingPosition position) {
162      int shortestSide = int.MaxValue;
163      int width = BinShape.Width;
164      int height = BinShape.Height;
165
166      if (position.X >= width || position.Y >= height)
167        return shortestSide;
168
169      PackingPosition current = new PackingPosition(0, position.X, position.Y);
170      while (current.X < width && IsPointOccupied(current)) { current = PackingPosition.MoveRight(current); }
171      if (current.X - position.X < shortestSide)
172        shortestSide = current.X - position.X;
173
174
175      current = new PackingPosition(0, position.X, position.Y);
176      while (current.Y < height && IsPointOccupied(current)) { current = PackingPosition.MoveUp(current); }
177      if (current.Y - position.Y < shortestSide)
178        shortestSide = current.Y - position.Y;
179
180      return shortestSide;
181    }
182    public override bool IsStaticStable(PackingItem item, PackingPosition position) {
183      throw new NotSupportedException();
184    }
185
186    protected override void InitializeOccupationLayers() {
187      for (int i = 0; i * 10 <= BinShape.Width; i += 1) {
188        OccupationLayers[i] = new List<int>();
189      }
190    }
191
192    protected override void AddNewItemToOccupationLayers(int itemID, PackingItem item, PackingPosition position) {
193      int x1 = position.X / 10;
194      int x2 = (position.X + (position.Rotated ? item.Height : item.Width)) / 10;
195
196      for (int i = x1; i <= x2; i++)
197        OccupationLayers[i].Add(itemID);
198    }
199    protected override List<int> GetLayerItemIDs(PackingPosition position) {
200      return OccupationLayers[position.X / 10];
201    }
202    protected override List<int> GetLayerItemIDs(PackingItem item, PackingPosition position) {
203      List<int> result = new List<int>();
204      int x1 = position.X / 10;
205      int x2 = (position.X + (position.Rotated ? item.Height : item.Width)) / 10;
206
207      for (int i = x1; i <= x2; i++)
208        result.AddRange(OccupationLayers[i]);
209
210      return result;
211    }
212  }
213}
Note: See TracBrowser for help on using the repository browser.