Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1966: removed types for *PackingBin because PackingBins and PackingShapes have the same capabilities

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