- Timestamp:
- 02/21/18 16:32:53 (7 years ago)
- Location:
- branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/3D/Packer
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/3D/Packer/BinPackerFormClosure.cs
r15731 r15801 10 10 11 11 namespace HeuristicLab.Problems.BinPacking3D.Packer { 12 internal class BinPackerFormClosure : BinPacker {12 internal class BinPackerFormClosure : BinPackerMinRSLeft { 13 13 14 14 #region Constructors for HEAL … … 25 25 public BinPackerFormClosure() : base() { } 26 26 27 /// <summary> 28 /// 29 /// </summary> 30 /// <param name="sortedItems"></param> 31 /// <param name="binShape"></param> 32 /// <param name="items"></param> 33 /// <param name="epCreationMethod"></param> 34 /// <param name="epPruningMethod"></param> 35 /// <param name="useStackingConstraints"></param> 36 /// <returns></returns> 37 public override IList<BinPacking3D> PackItems(Permutation sortedItems, PackingShape binShape, IList<PackingItem> items, ExtremePointCreationMethod epCreationMethod, ExtremePointPruningMethod epPruningMethod, bool useStackingConstraints) { 38 var workingItems = CloneItems(items); 39 IList<BinPacking3D> packingList = new List<BinPacking3D>(); 40 IList<int> remainingIds = new List<int>(sortedItems); 27 28 protected override void PackRemainingItems(ref IList<int> remainingIds, ref BinPacking3D packingBin, IList<PackingItem> items, ExtremePointCreationMethod epCreationMethod, bool useStackingConstraints) { 41 29 IExtremePointCreator extremePointCreator = ExtremePointCreatorFactory.CreateExtremePointCreator(epCreationMethod, useStackingConstraints); 42 43 try { 44 var groupedItems = items.Select((x, index) => new { Item = x, Id = index }) 45 .GroupBy(x => x.Item.Layer) 46 .OrderBy(x => x.Key); 47 48 BinPacking3D packingBin = new BinPacking3D(binShape); 49 packingList.Add(packingBin); 50 foreach (var grouping in groupedItems) { 51 var notPacked = new List<KeyValuePair<int, PackingItem>>(); 52 var gr = grouping.ToList(); 53 var selectedItems = gr.Where(x => ItemBinWidth(x.Item, binShape)) 54 .OrderByDescending(x => x.Item.Width) 55 .ThenByDescending(x => x.Item.Height) 56 .ThenByDescending(x => x.Item.Depth); 57 var otherItems = gr.Except(selectedItems).OrderByDescending(x => x.Item.Width * x.Item.Height); 58 foreach (var item in selectedItems) { 59 if (TryToPack(packingBin, item.Item, item.Id, extremePointCreator, useStackingConstraints)) { 60 remainingIds.Remove(item.Id); 61 } else { 62 notPacked.Add(new KeyValuePair<int, PackingItem>(item.Id, item.Item)); 63 } 64 } 65 foreach (var item in otherItems) { 66 if (TryToPack(packingBin, item.Item, item.Id, extremePointCreator, useStackingConstraints)) { 67 remainingIds.Remove(item.Id); 68 } else { 69 notPacked.Add(new KeyValuePair<int, PackingItem>(item.Id, item.Item)); 70 } 71 } 72 73 while (notPacked.Count > 0) { 74 packingBin = new BinPacking3D(binShape); 75 packingList.Add(packingBin); 76 77 foreach (var item in notPacked.ToList()) { 78 if (TryToPack(packingBin, item.Value, item.Key, extremePointCreator, useStackingConstraints)) { 79 remainingIds.Remove(item.Key); 80 notPacked.Remove(item); 81 } 82 } 83 } 30 // If there are any items which width equals to the bin shape width or the result of a modulo division of the width is zero, these items will be packed first 31 //schauen, ob es einen gegenstand gibt, der die ganze breite ausfuellt oder bei einer division mit der breite ein rest von 0 rauskommt. 32 var binShape = packingBin.BinShape; 33 var placeable = remainingIds.ToList().Where(x => ItemFitsBinShapeWidth(x, items[x], binShape)) 34 .OrderByDescending(x => items[x].Width) 35 .ThenByDescending(x => items[x].Height); 36 foreach (var itemId in placeable) { 37 var item = items[itemId]; 38 if (TryToPack(packingBin, item, itemId, extremePointCreator, useStackingConstraints)) { 39 remainingIds.Remove(itemId); 84 40 } 85 } catch { } 41 } 42 base.PackRemainingItems(ref remainingIds, ref packingBin, items, epCreationMethod, useStackingConstraints); 43 } 86 44 87 45 88 return packingList; 46 private bool ItemFitsBinShapeWidth(int itemId, PackingItem item, PackingShape binShape) { 47 item.Rotated = false; 48 item.Tilted = false; 49 if (binShape.Width % item.Width == 0) { 50 return true; 51 } 52 53 if (item.RotateEnabled && !item.TiltEnabled) { 54 item.Rotated = true; 55 if (binShape.Width % item.Width == 0) { 56 return true; 57 } 58 } 59 if (!item.RotateEnabled && item.TiltEnabled) { 60 item.Rotated = false; 61 item.Tilted = true; 62 if (binShape.Width % item.Width == 0) { 63 return true; 64 } 65 } 66 if (item.RotateEnabled && item.TiltEnabled) { 67 item.Rotated = true; 68 item.Tilted = true; 69 if (binShape.Width % item.Width == 0) { 70 return true; 71 } 72 } 73 item.Rotated = false; 74 item.Tilted = false; 75 return false; 89 76 } 90 77 … … 98 85 return false; 99 86 } 100 101 protected override PackingPosition FindPackingPositionForItem(BinPacking3D packingBin, PackingItem packingItem, bool useStackingConstraints) {102 if (!CheckItemDimensions(packingBin, packingItem)) {103 throw new BinPacking3DException($"The dimensions of the given item exceeds the bin dimensions. " +104 $"Bin: ({packingBin.BinShape.Width} {packingBin.BinShape.Depth} {packingBin.BinShape.Height})" +105 $"Item: ({packingItem.Width} {packingItem.Depth} {packingItem.Height})");106 }107 108 var newPosition = packingBin.ExtremePoints.Where(x => packingBin.IsPositionFeasible(packingItem, x.Key, useStackingConstraints)).FirstOrDefault();109 110 return newPosition.Key;111 }112 113 114 private static bool ItemBinWidth(PackingItem item, PackingShape bin) {115 var itemCopy = item.Clone() as PackingItem;116 int binWith = bin.Width;117 if (itemCopy.Width <= binWith && binWith % itemCopy.Width == 0) {118 return true;119 }120 121 itemCopy.Rotated = true;122 if (itemCopy.Width <= binWith && binWith % itemCopy.Width == 0) {123 item.Rotated = true;124 return true;125 }126 127 itemCopy.Rotated = false;128 itemCopy.Tilted = true;129 if (itemCopy.Width <= binWith && binWith % itemCopy.Width == 0) {130 item.Tilted = true;131 return true;132 }133 134 itemCopy.Rotated = true;135 itemCopy.Tilted = true;136 if (itemCopy.Width <= binWith && binWith % itemCopy.Width == 0) {137 item.Rotated = true;138 item.Tilted = true;139 return true;140 }141 return false;142 }143 144 public override void PackItemsToPackingList(IList<BinPacking3D> packingList, Permutation sortedItems, PackingShape binShape, IList<PackingItem> items, ExtremePointCreationMethod epCreationMethod, ExtremePointPruningMethod epPruningMethod, bool useStackingConstraints) {145 throw new NotImplementedException();146 }147 87 } 148 88 } -
branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/3D/Packer/BinPackerMinRSLeft.cs
r15731 r15801 94 94 ExtremePointPruningFactory.CreatePruning().PruneExtremePoints(epPruningMethod, packingList); 95 95 } 96 96 97 97 98 … … 104 105 /// <param name="epCreationMethod"></param> 105 106 /// <param name="useStackingConstraints"></param> 106 protected v oid PackRemainingItems(ref IList<int> remainingIds, ref BinPacking3D packingBin, IList<PackingItem> items, ExtremePointCreationMethod epCreationMethod, bool useStackingConstraints) {107 protected virtual void PackRemainingItems(ref IList<int> remainingIds, ref BinPacking3D packingBin, IList<PackingItem> items, ExtremePointCreationMethod epCreationMethod, bool useStackingConstraints) { 107 108 IExtremePointCreator extremePointCreator = ExtremePointCreatorFactory.CreateExtremePointCreator(epCreationMethod, useStackingConstraints); 109 108 110 var remainingNotWeightSupportedItems = new List<int>(); 109 111 foreach (var itemId in new List<int>(remainingIds)) { … … 149 151 } 150 152 } 151 152 153 } 153 154 }
Note: See TracChangeset
for help on using the changeset viewer.