Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13606 was 13606, checked in by gkronber, 9 years ago

#1966 refactoring (moved 2d-specific classes into separate project)

File size: 10.0 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 HashSet<TwoDimensionalPacking>();
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        while (sourcePointX.Y > 0 && !IsPointOccupied(new TwoDimensionalPacking(0, sourcePointX.X, sourcePointX.Y - 1))) {
63          sourcePointX.Y--;
64        }
65        ExtremePoints.Add(new TwoDimensionalPacking(0, sourcePointX.X, sourcePointX.Y));
66      }
67
68
69
70
71      //Find ExtremePoints beginning from sourcepointY
72      var sourcePointY = new TwoDimensionalPacking(0, position.X, position.Y + newItem.Height);
73      if (sourcePointY.X < BinMeasures.Width && sourcePointY.Y < BinMeasures.Height) {
74        //Traversing down the x-axis 
75        while (sourcePointY.X > 0 && !IsPointOccupied(new TwoDimensionalPacking (0,sourcePointY.X - 1, sourcePointY.Y))) {
76          sourcePointY.X--;
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 result = ExtremePoints.ElementAt(epIndex);
100        result.Rotated = rotated;
101        return result;
102      }
103      return null;
104    }
105    public override TwoDimensionalPacking FindPositionBySliding(RectangularPackingItem measures, bool rotated) {
106      TwoDimensionalPacking currentPosition = new TwoDimensionalPacking(0,
107        BinMeasures.Width - (rotated ? measures.Height : measures.Width),
108        BinMeasures.Height - (rotated ? measures.Width : measures.Height), rotated);
109      //Slide the item as far as possible to the left
110      while (IsPositionFeasible(measures, TwoDimensionalPacking.MoveLeft(currentPosition))
111        || IsPositionFeasible(measures, TwoDimensionalPacking.MoveDown(currentPosition))) {
112        //Slide the item as far as possible to the bottom
113          while (IsPositionFeasible(measures, TwoDimensionalPacking.MoveDown(currentPosition))) {
114            currentPosition = TwoDimensionalPacking.MoveDown(currentPosition);
115        }
116          if (IsPositionFeasible(measures, TwoDimensionalPacking.MoveLeft(currentPosition)))
117          currentPosition = TwoDimensionalPacking.MoveLeft(currentPosition);
118      }
119
120      return IsPositionFeasible(measures, currentPosition) ? currentPosition : null;
121    }
122   
123    public override void SlidingBasedPacking(ref List<int> sequence, ItemList<RectangularPackingItem> itemMeasures) {
124      var temp = new List<int>(sequence);
125      for (int i = 0; i < temp.Count; i++) {
126        var item = itemMeasures[temp[i]];
127        var position = FindPositionBySliding(item, false);
128        if (position != null) {
129          PackItem(temp[i], item, position);
130          sequence.Remove(temp[i]);
131        }
132      }
133    }
134    public override void SlidingBasedPacking(ref List<int> sequence, ItemList<RectangularPackingItem> itemMeasures, Dictionary<int, bool> rotationArray) {
135      var temp = new List<int>(sequence);
136      for (int i = 0; i < temp.Count; i++) {
137        var item = itemMeasures[temp[i]];
138        var position = FindPositionBySliding(item, rotationArray[temp[i]]);
139        if (position != null) {
140          PackItem(temp[i], item, position);
141          sequence.Remove(temp[i]);
142        }
143      }
144    }
145    public override void ExtremePointBasedPacking(ref List<int> sequence, ItemList<RectangularPackingItem> itemMeasures, bool stackingConstraints) {
146      var temp = new List<int>(sequence);
147      foreach (int itemID in temp) {
148        var item = itemMeasures[itemID];
149        var positionFound = FindExtremePointForItem(item, false, false);
150        if (positionFound != null) {
151          PackItem(itemID, item, positionFound);
152          sequence.Remove(itemID);
153        }
154      }
155    }
156    public override void ExtremePointBasedPacking(ref List<int> sequence, ItemList<RectangularPackingItem> itemMeasures, bool stackingConstraints, Dictionary<int, bool> rotationArray) {
157      var temp = new List<int>(sequence);
158      foreach (int itemID in temp) {
159        var item = itemMeasures[itemID];
160        var positionFound = FindExtremePointForItem(item, rotationArray[itemID], false);
161        if (positionFound != null) {
162          PackItem(itemID, item, positionFound);
163          sequence.Remove(itemID);
164        }
165      }
166    }
167
168    public override int ShortestPossibleSideFromPoint(TwoDimensionalPacking position) {
169      int shortestSide = int.MaxValue;
170      int width = BinMeasures.Width;
171      int height = BinMeasures.Height;
172
173      if (position.X >= width || position.Y >= height)
174        return shortestSide;
175
176      TwoDimensionalPacking current = new TwoDimensionalPacking(0, position.X, position.Y);
177      while (current.X < width && IsPointOccupied(current)) { current = TwoDimensionalPacking.MoveRight(current); }
178      if (current.X - position.X < shortestSide)
179        shortestSide = current.X - position.X;
180
181
182      current = new TwoDimensionalPacking(0, position.X, position.Y);
183      while (current.Y < height && IsPointOccupied(current)) { current = TwoDimensionalPacking.MoveUp(current); }
184      if (current.Y - position.Y < shortestSide)
185        shortestSide = current.Y - position.Y;
186
187      return shortestSide;
188    }
189    public override bool IsStaticStable(RectangularPackingItem item, TwoDimensionalPacking position) {
190      throw new NotImplementedException();
191    }
192
193
194    protected override void InitializeOccupationLayers() {
195      for (int i = 0; i*10 <= BinMeasures.Width; i += 1 ) {
196        OccupationLayers[i] = new List<int>();
197      }
198    }
199    protected override void AddNewItemToOccupationLayers(int itemID, RectangularPackingItem measures, TwoDimensionalPacking position) {
200      int x1 = position.X / 10;
201      int x2 = (position.X + (position.Rotated ? measures.Height : measures.Width)) / 10;
202
203      for (int i = x1; i <= x2; i++)
204        OccupationLayers[i].Add(itemID);
205    }
206    protected override List<int> GetLayerItemIDs(TwoDimensionalPacking position) {
207      return OccupationLayers[position.X / 10];
208    }
209    protected override List<int> GetLayerItemIDs(RectangularPackingItem measures, TwoDimensionalPacking position) {
210      List<int> result = new List<int> ();
211      int x1 = position.X / 10;
212      int x2 = (position.X + (position.Rotated ? measures.Height : measures.Width)) / 10;
213
214      for (int i = x1; i <= x2; i++)
215        result.AddRange(OccupationLayers[i]);
216
217      return result;
218    }
219  }
220  public class EPComparer2D : IComparer<TwoDimensionalPacking> {
221    public int Compare(TwoDimensionalPacking a, TwoDimensionalPacking b) {
222      int result = a.X.CompareTo(b.X);
223      if (result == 0)
224        result = a.Y.CompareTo(b.Y);
225
226      return result;
227    }
228  }
229}
Note: See TracBrowser for help on using the repository browser.