Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1966:

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