Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1966: refactoring 2d problem

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