Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.3D/3.3/BinPacking3D.cs @ 15866

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

#1966: refactoring

File size: 15.1 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
25using HeuristicLab.Core;
26using HeuristicLab.Common;
[14151]27using HeuristicLab.Problems.BinPacking;
[9596]28
[14046]29namespace HeuristicLab.Problems.BinPacking3D {
[9596]30  [Item("BinPacking3D", "Represents a single-bin packing for a 3D bin-packing problem.")]
31  [StorableClass]
[14049]32  public class BinPacking3D : BinPacking<PackingPosition, PackingShape, PackingItem> {
[9596]33
[14154]34    public BinPacking3D(PackingShape binShape)
35      : base(binShape) {
[14048]36      ExtremePoints = new SortedSet<PackingPosition>(new EPComparer3D());
[14154]37      ExtremePoints.Add(binShape.Origin);
38      InitializeOccupationLayers();
[9596]39    }
40    [StorableConstructor]
41    protected BinPacking3D(bool deserializing) : base(deserializing) { }
42    protected BinPacking3D(BinPacking3D original, Cloner cloner)
43      : base(original, cloner) {
[14154]44      this.ExtremePoints = new SortedSet<PackingPosition>(original.ExtremePoints.Select(p => cloner.Clone(p)), new EPComparer3D());
[9596]45    }
46    public override IDeepCloneable Clone(Cloner cloner) {
47      return new BinPacking3D(this, cloner);
48    }
[13607]49
[14049]50    protected override void GenerateNewExtremePointsForNewItem(PackingItem newItem, PackingPosition position) {
[9596]51      int newWidth = position.Rotated ? newItem.Depth : newItem.Width;
52      int newDepth = position.Rotated ? newItem.Width : newItem.Depth;
53
54      //Find ExtremePoints beginning from sourcepointX
[14048]55      var sourcePointX = new PackingPosition(0, position.X + newWidth, position.Y, position.Z);
[14154]56      if (sourcePointX.X < BinShape.Width && sourcePointX.Y < BinShape.Height && sourcePointX.Z < BinShape.Depth) {
[13607]57        //Traversing down the y-axis 
[14048]58        PackingPosition current = new PackingPosition(0, sourcePointX.X, sourcePointX.Y, sourcePointX.Z);
59        while (current.Y > 0 && !IsPointOccupied(PackingPosition.MoveDown(current))) {
60          current = PackingPosition.MoveDown(current);
[9596]61        }
[14048]62        ExtremePoints.Add((PackingPosition)current.Clone());
63        while (current.X > 0 && !IsPointOccupied(PackingPosition.MoveLeft(current))) {
64          current = PackingPosition.MoveLeft(current);
[9598]65        }
66        ExtremePoints.Add(current);
[9596]67
[13607]68        //Traversing down the z-axis
[14048]69        current = new PackingPosition(0, sourcePointX.X, sourcePointX.Y, sourcePointX.Z);
70        while (current.Z > 0 && !IsPointOccupied(PackingPosition.MoveBack(current))) {
71          current = PackingPosition.MoveBack(current);
[9596]72        }
[14048]73        ExtremePoints.Add((PackingPosition)current.Clone());
74        while (current.Y > 0 && !IsPointOccupied(PackingPosition.MoveDown(current))) {
75          current = PackingPosition.MoveDown(current);
[9598]76        }
77        ExtremePoints.Add(current);
[9596]78      }
79
80      //Find ExtremePoints beginning from sourcepointY
[14048]81      var sourcePointY = new PackingPosition(0, position.X, position.Y + newItem.Height, position.Z);
[14154]82      if (sourcePointY.X < BinShape.Width && sourcePointY.Y < BinShape.Height && sourcePointY.Z < BinShape.Depth) {
[9598]83        //Traversing down the x-axis         
[14048]84        PackingPosition current = new PackingPosition(0, sourcePointY.X, sourcePointY.Y, sourcePointY.Z);
85        while (current.X > 0 && !IsPointOccupied(PackingPosition.MoveLeft(current))) {
86          current = PackingPosition.MoveLeft(current);
[9596]87        }
[14048]88        ExtremePoints.Add((PackingPosition)current.Clone());
89        while (current.Y > 0 && !IsPointOccupied(PackingPosition.MoveDown(current))) {
90          current = PackingPosition.MoveDown(current);
[9598]91        }
92        ExtremePoints.Add(current);
[9596]93
[14153]94        //Traversing down the z-axis
[14048]95        current = new PackingPosition(0, sourcePointY.X, sourcePointY.Y, sourcePointY.Z);
96        while (current.Z > 0 && !IsPointOccupied(PackingPosition.MoveBack(current))) {
97          current = PackingPosition.MoveBack(current);
[9596]98        }
[14048]99        ExtremePoints.Add((PackingPosition)current.Clone());
100        while (current.Y > 0 && !IsPointOccupied(PackingPosition.MoveDown(current))) {
101          current = PackingPosition.MoveDown(current);
[9598]102        }
103        ExtremePoints.Add(current);
[9596]104      }
105
106      //Find ExtremePoints beginning from sourcepointZ
[14048]107      var sourcePointZ = new PackingPosition(0, position.X, position.Y, position.Z + newDepth);
[14154]108      if (sourcePointZ.X < BinShape.Width && sourcePointZ.Y < BinShape.Height && sourcePointZ.Z < BinShape.Depth) {
[14153]109        //Traversing down the x-axis
[14048]110        PackingPosition current = new PackingPosition(0, sourcePointZ.X, sourcePointZ.Y, sourcePointZ.Z);
111        while (current.X > 0 && !IsPointOccupied(PackingPosition.MoveLeft(current))) {
112          current = PackingPosition.MoveLeft(current);
[9596]113        }
[14048]114        ExtremePoints.Add((PackingPosition)current.Clone());
115        while (current.Y > 0 && !IsPointOccupied(PackingPosition.MoveDown(current))) {
116          current = PackingPosition.MoveDown(current);
[9598]117        }
118        ExtremePoints.Add(current);
[9596]119
[14153]120        //Traversing down the y-axis
[14048]121        current = new PackingPosition(0, sourcePointZ.X, sourcePointZ.Y, sourcePointZ.Z);
122        while (current.Y > 0 && !IsPointOccupied(PackingPosition.MoveDown(current))) {
123          current = PackingPosition.MoveDown(current);
[9596]124        }
[14048]125        ExtremePoints.Add((PackingPosition)current.Clone());
126        while (current.X > 0 && !IsPointOccupied(PackingPosition.MoveLeft(current))) {
127          current = PackingPosition.MoveLeft(current);
[9598]128        }
129        ExtremePoints.Add(current);
[9596]130      }
131    }
132
[14154]133    public override PackingPosition FindExtremePointForItem(PackingItem item, bool rotated, bool stackingConstraints) {
[9596]134
[14154]135      PackingItem newItem = new PackingItem(
136        rotated ? item.Depth : item.Width,
137        item.Height,
138        rotated ? item.Width : item.Depth,
139        item.TargetBin);
[9596]140
141      int epIndex = 0;
142      while (epIndex < ExtremePoints.Count && (
[14154]143        !IsPositionFeasible(newItem, ExtremePoints.ElementAt(epIndex))
144        || !IsSupportedByAtLeastOnePoint(newItem, ExtremePoints.ElementAt(epIndex))
145        || (stackingConstraints && !IsStaticStable(newItem, ExtremePoints.ElementAt(epIndex)))
146        || (stackingConstraints && !IsWeightSupported(newItem, ExtremePoints.ElementAt(epIndex)))
[9596]147      )) { epIndex++; }
148
149      if (epIndex < ExtremePoints.Count) {
[14038]150        var origPoint = ExtremePoints.ElementAt(epIndex);
[14048]151        var result = new PackingPosition(origPoint.AssignedBin, origPoint.X, origPoint.Y, origPoint.Z, rotated);
[9596]152        return result;
153      }
154      return null;
155    }
[9598]156
[14154]157    public override PackingPosition FindPositionBySliding(PackingItem item, bool rotated) {
[14153]158      //TODO: does not support stacking constraints yet
[9596]159      //Starting-position at upper right corner (=left bottom point of item-rectangle is at position item.width,item.height)
[14048]160      PackingPosition currentPosition = new PackingPosition(0,
[14154]161        BinShape.Width - (rotated ? item.Depth : item.Width),
162        BinShape.Height - item.Height,
163        BinShape.Depth - (rotated ? item.Width : item.Depth), rotated);
[9596]164      //Slide the item as far as possible to the bottom
[14154]165      while (IsPositionFeasible(item, PackingPosition.MoveDown(currentPosition))
166        || IsPositionFeasible(item, PackingPosition.MoveLeft(currentPosition))
167        || IsPositionFeasible(item, PackingPosition.MoveBack(currentPosition))) {
[9596]168        //Slide the item as far as possible to the left
[14154]169        while (IsPositionFeasible(item, PackingPosition.MoveLeft(currentPosition))
170      || IsPositionFeasible(item, PackingPosition.MoveBack(currentPosition))) {
[9596]171          //Slide the item as far as possible to the back
[14154]172          while (IsPositionFeasible(item, PackingPosition.MoveBack(currentPosition))) {
[14048]173            currentPosition = PackingPosition.MoveBack(currentPosition);
[9596]174          }
[14154]175          if (IsPositionFeasible(item, PackingPosition.MoveLeft(currentPosition)))
[14048]176            currentPosition = PackingPosition.MoveLeft(currentPosition);
[9596]177        }
[14154]178        if (IsPositionFeasible(item, PackingPosition.MoveDown(currentPosition)))
[14048]179          currentPosition = PackingPosition.MoveDown(currentPosition);
[9596]180      }
181
[14154]182      return IsPositionFeasible(item, currentPosition) ? currentPosition : null;
[9596]183    }
[13607]184
[14154]185    public override void SlidingBasedPacking(ref IList<int> sequence, IList<PackingItem> items) {
[9596]186      var temp = new List<int>(sequence);
187      for (int i = 0; i < temp.Count; i++) {
[14154]188        var item = items[temp[i]];
[9596]189        var position = FindPositionBySliding(item, false);
190        if (position != null) {
191          PackItem(temp[i], item, position);
192          sequence.Remove(temp[i]);
193        }
194      }
195    }
[14154]196    public override void SlidingBasedPacking(ref IList<int> sequence, IList<PackingItem> items, Dictionary<int, bool> rotationArray) {
[9596]197      var temp = new List<int>(sequence);
198      for (int i = 0; i < temp.Count; i++) {
[14154]199        var item = items[temp[i]];
[9596]200        var position = FindPositionBySliding(item, rotationArray[i]);
201        if (position != null) {
202          PackItem(temp[i], item, position);
203          sequence.Remove(temp[i]);
204        }
205      }
206    }
[14154]207    public override void ExtremePointBasedPacking(ref IList<int> sequence, IList<PackingItem> items, bool stackingConstraints) {
[9596]208      var temp = new List<int>(sequence);
209      foreach (int itemID in temp) {
[14154]210        var item = items[itemID];
[9596]211        var positionFound = FindExtremePointForItem(item, false, stackingConstraints);
212        if (positionFound != null) {
213          PackItem(itemID, item, positionFound);
214          sequence.Remove(itemID);
215        }
216      }
217    }
[14154]218    public override void ExtremePointBasedPacking(ref IList<int> sequence, IList<PackingItem> items, bool stackingConstraints, Dictionary<int, bool> rotationArray) {
[9596]219      var temp = new List<int>(sequence);
220      foreach (int itemID in temp) {
[14154]221        var item = items[itemID];
[9596]222        var positionFound = FindExtremePointForItem(item, rotationArray[itemID], stackingConstraints);
223        if (positionFound != null) {
224          PackItem(itemID, item, positionFound);
225          sequence.Remove(itemID);
226        }
227      }
228    }
[13607]229
[14048]230    public override int ShortestPossibleSideFromPoint(PackingPosition position) {
[9596]231
232      int shortestSide = int.MaxValue;
[14154]233      int width = BinShape.Width;
234      int height = BinShape.Height;
235      int depth = BinShape.Depth;
[9596]236
237      if (position.X >= width || position.Y >= height || position.Z >= depth)
238        return shortestSide;
239
[14048]240      PackingPosition current = new PackingPosition(0, position.X, position.Y, position.Z);
241      while (current.X < width && IsPointOccupied(current)) { current = PackingPosition.MoveRight(current); }
[9596]242      if (current.X - position.X < shortestSide)
243        shortestSide = current.X - position.X;
244
245
[14048]246      current = new PackingPosition(0, position.X, position.Y, position.Z);
247      while (current.Y < height && IsPointOccupied(current)) { current = PackingPosition.MoveUp(current); }
[9596]248      if (current.Y - position.Y < shortestSide)
249        shortestSide = current.Y - position.Y;
250
251
[14048]252      current = new PackingPosition(0, position.X, position.Y, position.Z);
253      while (current.Z < depth && IsPointOccupied(current)) { current = PackingPosition.MoveFront(current); }
[9596]254      if (current.Z - position.Z < shortestSide)
255        shortestSide = current.Z - position.Z;
256
257      return shortestSide;
258    }
[14049]259    public override bool IsStaticStable(PackingItem item, PackingPosition position) {
[9596]260      //Static stability is given, if item is placed on the ground
261      if (position.Y == 0)
262        return true;
263
[14048]264      if (IsPointOccupied(new PackingPosition(0, position.X, position.Y - 1, position.Z))
265        && IsPointOccupied(new PackingPosition(0, position.X + item.Width - 1, position.Y - 1, position.Z))
266        && IsPointOccupied(new PackingPosition(0, position.X, position.Y - 1, position.Z + item.Depth - 1))
267        && IsPointOccupied(new PackingPosition(0, position.X + item.Width - 1, position.Y - 1, position.Z + item.Depth - 1)))
[9596]268        return true;
269
270      return false;
271    }
[9598]272
[13607]273
[14049]274    public bool IsSupportedByAtLeastOnePoint(PackingItem item, PackingPosition position) {
[9598]275      if (position.Y == 0)
276        return true;
277
278      int y = position.Y - 1;
279      for (int x = position.X; x < position.X + item.Width; x++)
280        for (int z = position.Z; z < position.Z + item.Depth; z++)
[14048]281          if (IsPointOccupied(new PackingPosition(0, x, y, z)))
[9598]282            return true;
[13607]283
[9598]284      return false;
285    }
[14049]286    public bool IsWeightSupported(PackingItem item, PackingPosition ep) {
[9598]287      if (ep.Y == 0)
288        return true;
289
[14154]290      if (Items[PointOccupation(new PackingPosition(0, ep.X, ep.Y - 1, ep.Z))].SupportsStacking(item)
291        && Items[PointOccupation(new PackingPosition(0, ep.X + item.Width - 1, ep.Y - 1, ep.Z))].SupportsStacking(item)
292        && Items[PointOccupation(new PackingPosition(0, ep.X, ep.Y - 1, ep.Z + item.Depth - 1))].SupportsStacking(item)
293        && Items[PointOccupation(new PackingPosition(0, ep.X + item.Width - 1, ep.Y - 1, ep.Z + item.Depth - 1))].SupportsStacking(item))
[9598]294        return true;
295
296      return false;
[13607]297    }
[9599]298
[13607]299
[9599]300    protected override void InitializeOccupationLayers() {
[14154]301      for (int i = 0; i * 10 <= BinShape.Depth; i += 1) {
[9599]302        OccupationLayers[i] = new List<int>();
303      }
[9598]304    }
[14154]305    protected override void AddNewItemToOccupationLayers(int itemID, PackingItem item, PackingPosition position) {
[9599]306      int z1 = position.Z / 10;
[14154]307      int z2 = (position.Z + (position.Rotated ? item.Width : item.Depth)) / 10;
[9599]308
309      for (int i = z1; i <= z2; i++)
310        OccupationLayers[i].Add(itemID);
311    }
[14048]312    protected override List<int> GetLayerItemIDs(PackingPosition position) {
[9599]313      return OccupationLayers[position.Z / 10];
314    }
[14154]315    protected override List<int> GetLayerItemIDs(PackingItem item, PackingPosition position) {
[9599]316      List<int> result = new List<int>();
317      int z1 = position.Z / 10;
[14154]318      int z2 = (position.Z + (position.Rotated ? item.Width : item.Depth)) / 10;
[9599]319
320      for (int i = z1; i <= z2; i++)
321        result.AddRange(OccupationLayers[i]);
322
323      return result;
324    }
[9596]325  }
[14048]326  public class EPComparer3D : IComparer<PackingPosition> {
327    public int Compare(PackingPosition a, PackingPosition b) {
[13607]328      int result = a.Z.CompareTo(b.Z);
[9599]329      if (result == 0)
[13607]330        result = a.X.CompareTo(b.X);
[9599]331      if (result == 0)
332        result = a.Y.CompareTo(b.Y);
333
334      return result;
335    }
336  }
[9596]337}
Note: See TracBrowser for help on using the repository browser.