- Timestamp:
- 11/28/17 16:10:31 (7 years ago)
- Location:
- branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/3D/Packer
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/3D/Packer/BinPacker.cs
r15473 r15488 20 20 #endregion 21 21 22 using HeuristicLab.Common; 22 23 using HeuristicLab.Core; 23 24 using HeuristicLab.Encodings.PermutationEncoding; 24 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 25 26 using HeuristicLab.Problems.BinPacking3D; 27 using HeuristicLab.Problems.BinPacking3D.ExtremePointCreation; 26 28 using System; 27 29 using System.Collections.Generic; … … 31 33 32 34 namespace HeuristicLab.Problems.BinPacking3D.Packer { 33 [Item("BinPacker", "A class for packing bins for the 3D bin-packer problem.")] 34 [StorableClass] 35 public abstract class BinPacker { 35 public abstract class BinPacker : Item { 36 37 /* 38 [Storable] 39 IPositionFinder PositionFinder 36 40 41 */ 42 43 #region Constructors for HEAL 44 45 46 [StorableConstructor] 47 protected BinPacker(bool deserializing) : base(deserializing) { } 48 49 protected BinPacker(BinPacker original, Cloner cloner) 50 : base(original, cloner) { 51 //this.PositionFinder = original.PositionFinder; 52 } 53 54 #endregion 55 37 56 public BinPacker() { } 38 57 … … 45 64 /// <param name="useStackingConstraints">Flag for using stacking constraints</param> 46 65 /// <returns>Returns a collection of bin packing 3d objects. Each object represents a bin and the packed items</returns> 47 public abstract IList<BinPacking3D> PackItems(Permutation sortedItems, PackingShape binShape, IList<PackingItem> items, bool useStackingConstraints);66 public abstract IList<BinPacking3D> PackItems(Permutation sortedItems, PackingShape binShape, IList<PackingItem> items, ExtremePointCreationMethod epCreationMethod, bool useStackingConstraints); 48 67 49 68 /// <summary> … … 54 73 /// <param name="packingItem"></param> 55 74 /// <param name="position"></param> 56 protected virtual void PackItem(BinPacking3D packingBin, int itemId, PackingItem packingItem, PackingPosition position, bool useStackingConstraints) { 57 75 protected virtual void PackItem(BinPacking3D packingBin, int itemId, PackingItem packingItem, PackingPosition position, IExtremePointCreator extremePointCreator, bool useStackingConstraints) { 76 if (!CheckItemDimensions(packingBin, packingItem, position)) { 77 throw new BinPacking3DException($"The dimensions of the given item exceeds the bin dimensions. " + 78 $"Bin: ({packingBin.BinShape.Width} {packingBin.BinShape.Depth} {packingBin.BinShape.Height})" + 79 $"Item: ({packingItem.Width} {packingItem.Depth} {packingItem.Height})"); 80 } 58 81 packingBin.PackItem(itemId, packingItem, position); 59 packingBin.UpdateResidualSpace(packingItem, position);60 packingBin.UpdateExtremePoints(packingItem, position, useStackingConstraints);82 extremePointCreator.UpdateExtremePoints(packingBin, packingItem, position); 83 extremePointCreator.UpdateResidualSpace(packingBin, packingItem, position); 61 84 } 62 85 … … 79 102 return packingBin.ExtremePoints.Where(x => packingBin.IsPositionFeasible(newItem, x, useStackingConstraints)).FirstOrDefault(); 80 103 } 104 105 /// <summary> 106 /// Compares the dimensions of a given item and the current bin. 107 /// </summary> 108 /// <param name="item"></param> 109 /// <returns>Returns true if the dimensions of an given item are less or equal to the bin.</returns> 110 private bool CheckItemDimensions(BinPacking3D packingBin, PackingItem item, PackingPosition itemPosition) { 111 var width = itemPosition.Rotated ? item.Depth : item.Width; 112 var depth = itemPosition.Rotated ? item.Width : item.Depth; 113 return packingBin.BinShape.Width >= width && packingBin.BinShape.Height >= item.Height && packingBin.BinShape.Depth >= depth; 114 } 81 115 } 82 116 } -
branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/3D/Packer/BinPackerFirstFit.cs
r15473 r15488 20 20 #endregion 21 21 22 using HeuristicLab.Common; 22 23 using HeuristicLab.Core; 23 24 using HeuristicLab.Encodings.PermutationEncoding; 24 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 26 using HeuristicLab.Problems.BinPacking3D.ExtremePointCreation; 25 27 using System; 26 28 using System.Collections.Generic; … … 30 32 31 33 namespace HeuristicLab.Problems.BinPacking3D.Packer { 34 public class BinPackerFirstFit : BinPacker { 35 #region Constructors for HEAL 36 [StorableConstructor] 37 protected BinPackerFirstFit(bool deserializing) : base(deserializing) { } 32 38 33 [Item("BinPackerFirstFit", "A class for packing bins for the 3D bin-packer problem. It uses a first fit algorithm")] 34 [StorableClass] 35 public class BinPackerFirstFit : BinPacker { 39 protected BinPackerFirstFit(BinPackerFirstFit original, Cloner cloner) 40 : base(original, cloner) { 41 } 42 43 public override IDeepCloneable Clone(Cloner cloner) { 44 return new BinPackerFirstFit(this, cloner); 45 } 46 #endregion 36 47 37 48 public BinPackerFirstFit() : base() { } … … 41 52 /// </summary> 42 53 /// <returns>Returns a collection of bin packing 3d objects. Each object represents a bin and the packed items</returns> 43 public override IList<BinPacking3D> PackItems(Permutation sortedItems, PackingShape binShape, IList<PackingItem> items, bool useStackingConstraints) {54 public override IList<BinPacking3D> PackItems(Permutation sortedItems, PackingShape binShape, IList<PackingItem> items, ExtremePointCreationMethod epGenerationMethod, bool useStackingConstraints) { 44 55 IList<BinPacking3D> packingList = new List<BinPacking3D>(); 45 56 IList<int> remainingIds = new List<int>(sortedItems); … … 47 58 while (remainingIds.Count > 0) { 48 59 BinPacking3D packingBin = new BinPacking3D(binShape); 49 PackRemainingItems(ref remainingIds, ref packingBin, items, useStackingConstraints, null);60 PackRemainingItems(ref remainingIds, ref packingBin, items, epGenerationMethod, useStackingConstraints, null); 50 61 packingList.Add(packingBin); 51 62 } … … 60 71 /// <param name="items">List of packing items. Some of the items will be assigned to the BinPacking3D object</param> 61 72 /// <param name="packingBin">This object will be filled with some of the given items</param> 62 protected void PackRemainingItems(ref IList<int> remainingIds, ref BinPacking3D packingBin, IList<PackingItem> items, bool useStackingConstraints, Dictionary<int, bool> rotationArray) {63 73 protected void PackRemainingItems(ref IList<int> remainingIds, ref BinPacking3D packingBin, IList<PackingItem> items, ExtremePointCreationMethod epCreationMethod, bool useStackingConstraints, Dictionary<int, bool> rotationArray) { 74 IExtremePointCreator extremePointCreator = ExtremePointCreatorFactory.CreateExtremePointCreator(epCreationMethod, useStackingConstraints); 64 75 foreach (var itemId in new List<int>(remainingIds)) { 65 76 bool rotated = rotationArray == null ? false : rotationArray[itemId]; … … 67 78 // if a valid packing position could be found, the current item can be added to the given bin 68 79 if (position != null) { 69 PackItem(packingBin, itemId, items[itemId], position, useStackingConstraints);80 PackItem(packingBin, itemId, items[itemId], position, extremePointCreator, useStackingConstraints); 70 81 remainingIds.Remove(itemId); 71 82 } -
branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/3D/Packer/BinPackerFreeVolumeBestFit.cs
r15473 r15488 20 20 #endregion 21 21 22 using HeuristicLab.Common; 22 23 using HeuristicLab.Core; 23 24 using HeuristicLab.Encodings.PermutationEncoding; 24 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 26 using HeuristicLab.Problems.BinPacking3D.ExtremePointCreation; 25 27 using System; 26 28 using System.Collections.Generic; … … 30 32 31 33 namespace HeuristicLab.Problems.BinPacking3D.Packer { 32 [Item("BinPackerFreeVolumeBestFit", "A class for packing bins for the 3D bin-packer problem. It uses a best fit algortihm depending on the free volume.")]33 [StorableClass]34 34 public class BinPackerFreeVolumeBestFit : BinPacker { 35 36 #region Constructors for HEAL 37 [StorableConstructor] 38 protected BinPackerFreeVolumeBestFit(bool deserializing) : base(deserializing) { } 39 40 protected BinPackerFreeVolumeBestFit(BinPackerFreeVolumeBestFit original, Cloner cloner) 41 : base(original, cloner) { 42 } 43 44 public override IDeepCloneable Clone(Cloner cloner) { 45 return new BinPackerFreeVolumeBestFit(this, cloner); 46 } 47 #endregion 35 48 36 49 public BinPackerFreeVolumeBestFit() : base() { } … … 48 61 /// <param name="useStackingConstraints"></param> 49 62 /// <returns>Returns a collection of bin packing 3d objects. Each object represents a bin and the packed items</returns> 50 public override IList<BinPacking3D> PackItems(Permutation sortedItems, PackingShape binShape, IList<PackingItem> items, bool useStackingConstraints) {63 public override IList<BinPacking3D> PackItems(Permutation sortedItems, PackingShape binShape, IList<PackingItem> items, ExtremePointCreationMethod epGenerationMethod, bool useStackingConstraints) { 51 64 IList<BinPacking3D> packingList = new List<BinPacking3D>(); 52 65 IList<int> remainingIds = new List<int>(sortedItems); 53 66 IExtremePointCreator extremePointCreator = ExtremePointCreatorFactory.CreateExtremePointCreator(epGenerationMethod, useStackingConstraints); 54 67 foreach (int remainingId in remainingIds) { 55 68 var sortedBins = packingList.OrderBy(x => x.FreeVolume); … … 64 77 var bin = packingBin; 65 78 if (positionFound) { 66 PackItem(bin, remainingId, item, position, useStackingConstraints);79 PackItem(bin, remainingId, item, position, extremePointCreator, useStackingConstraints); 67 80 break; 68 81 } … … 74 87 75 88 if (position == null) { 76 throw new InvalidOperationException("Item " + remainingId + " cannot be packed in empty bin.");89 throw new InvalidOperationException("Item " + remainingId + " cannot be packed into an empty bin."); 77 90 } 78 91 79 PackItem(packingBin, remainingId, item, position, useStackingConstraints);92 PackItem(packingBin, remainingId, item, position, extremePointCreator, useStackingConstraints); 80 93 packingList.Add(packingBin); 81 94 } -
branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/3D/Packer/BinPackerResidualSpaceBestFit.cs
r15473 r15488 20 20 #endregion 21 21 22 using HeuristicLab.Common; 22 23 using HeuristicLab.Core; 23 24 using HeuristicLab.Encodings.PermutationEncoding; 24 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 26 using HeuristicLab.Problems.BinPacking3D.ExtremePointCreation; 25 27 using System; 26 28 using System.Collections.Generic; … … 30 32 31 33 namespace HeuristicLab.Problems.BinPacking3D.Packer { 32 [Item("BinPackerResidualSpaceBestFit", "A class for packing bins for the 3D bin-packer problem. It uses a best fit algortihm depending on the residual space.")]33 [StorableClass]34 34 public class BinPackerResidualSpaceBestFit : BinPacker { 35 36 #region Constructors for HEAL 37 [StorableConstructor] 38 protected BinPackerResidualSpaceBestFit(bool deserializing) : base(deserializing) { } 39 40 protected BinPackerResidualSpaceBestFit(BinPackerResidualSpaceBestFit original, Cloner cloner) 41 : base(original, cloner) { 42 } 43 44 public override IDeepCloneable Clone(Cloner cloner) { 45 return new BinPackerResidualSpaceBestFit(this, cloner); 46 } 47 #endregion 35 48 36 49 public BinPackerResidualSpaceBestFit() : base() { } … … 42 55 /// </summary> 43 56 /// <returns>Returns a collection of bin packing 3d objects. Each object represents a bin and the packed items</returns> 44 public override IList<BinPacking3D> PackItems(Permutation sortedItems, PackingShape binShape, IList<PackingItem> items, bool useStackingConstraints) {57 public override IList<BinPacking3D> PackItems(Permutation sortedItems, PackingShape binShape, IList<PackingItem> items, ExtremePointCreationMethod epGenerationMethod, bool useStackingConstraints) { 45 58 IList<BinPacking3D> packingList = new List<BinPacking3D>(); 46 59 IList<int> remainingIds = new List<int>(sortedItems); 60 IExtremePointCreator extremePointCreator = ExtremePointCreatorFactory.CreateExtremePointCreator(epGenerationMethod, useStackingConstraints); 47 61 bool rotated = false; 48 62 … … 56 70 if (point.Item1.IsPositionFeasible(item, point.Item2, useStackingConstraints)) { 57 71 var binPacking = point.Item1; 58 PackItem(binPacking, remainingId, item, point.Item2, useStackingConstraints);72 PackItem(binPacking, remainingId, item, point.Item2, extremePointCreator, useStackingConstraints); 59 73 packed = true; 60 74 break; … … 66 80 var position = FindPackingPositionForItem(binPacking, item, useStackingConstraints, rotated); 67 81 if (position != null) { 68 PackItem(binPacking, remainingId, item, position, useStackingConstraints);82 PackItem(binPacking, remainingId, item, position, extremePointCreator, useStackingConstraints); 69 83 } else { 70 throw new InvalidOperationException("Item " + remainingId + " cannot be packed in an empty bin.");84 throw new InvalidOperationException("Item " + remainingId + " cannot be packed into an empty bin."); 71 85 } 72 86 packingList.Add(binPacking); 73 87 } 74 88 } 75 76 89 return packingList; 77 90 } … … 110 123 (rs.Item3 - item.Depth)); 111 124 } 112 113 125 } 114 126 }
Note: See TracChangeset
for help on using the changeset viewer.