Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1966: refactoring

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