- Timestamp:
- 11/08/17 16:30:41 (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
r15454 r15462 1 using HeuristicLab.Core; 1 #region License Information 2 /* HeuristicLab 3 * Copyright (C) 2002-2016 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 22 using HeuristicLab.Core; 2 23 using HeuristicLab.Encodings.PermutationEncoding; 3 24 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 13 34 [StorableClass] 14 35 public abstract class BinPacker { 15 protected Permutation _permutation; 16 protected PackingShape _binShape; 17 protected IList<PackingItem> _items; 18 protected bool _useStackingConstraints; 36 37 public BinPacker() { } 19 38 20 39 /// <summary> 21 40 /// Packs all items of the bin packer and returns a collection of BinPacking3D objects 22 41 /// </summary> 42 /// <param name="sortedItems">Permutation of items sorted by a sorting method. The value of each permutation index references to the index of the items list</param> 43 /// <param name="binShape">Bin for storing the items</param> 44 /// <param name="items">A list of packing items which should be assigned to a bin</param> 45 /// <param name="useStackingConstraints">Flag for using stacking constraints</param> 23 46 /// <returns></returns> 24 public abstract IList<BinPacking3D> PackItems( );47 public abstract IList<BinPacking3D> PackItems(Permutation sortedItems, PackingShape binShape, IList<PackingItem> items, bool useStackingConstraints); 25 48 26 /// <summary> 27 /// Tries to pack the remainig items into a given BinPacking3D object. Each item could be packed into the BinPacking3D object will be removed from the list of remaining ids 28 /// </summary> 29 /// <param name="remainingIds">List of remaining ids. After the method has been executed the list has to have less items</param> 30 /// <param name="items">List of packing items. Some of the items will be assigned to the BinPacking3D object</param> 31 /// <param name="packingBin">This object will be filled with some of the given items</param> 32 protected void PackRemainingItems(ref IList<int> remainingIds, ref BinPacking3D packingBin, IList<PackingItem> items, bool useStackingConstraints, Dictionary<int, bool> rotationArray) { 33 34 foreach (var itemId in new List<int>(remainingIds)) { 35 bool rotated = rotationArray == null ? false : rotationArray[itemId]; 36 PackingPosition position = FindPackingPositionForItem(packingBin, items[itemId], useStackingConstraints, rotated); 37 // if a valid packing position could be found, the current item can be added to the given bin 38 if (position != null) { 39 PackItem(ref packingBin, itemId, items[itemId], position, useStackingConstraints); 40 remainingIds.Remove(itemId); 41 } 42 } 43 } 49 44 50 45 51 /// <summary> … … 52 58 protected void PackItem(ref BinPacking3D packingBin, int itemId, PackingItem packingItem, PackingPosition position, bool useStackingConstraints) { 53 59 54 packingBin. AddItem(itemId, packingItem, position);60 packingBin.PackItem(itemId, packingItem, position); 55 61 packingBin.UpdateResidualSpace(packingItem, position); 56 62 packingBin.UpdateExtremePoints(packingItem, position, useStackingConstraints); -
branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/3D/Packer/BinPackerFirstFit.cs
r15454 r15462 1 using HeuristicLab.Core; 1 #region License Information 2 /* HeuristicLab 3 * Copyright (C) 2002-2016 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 22 using HeuristicLab.Core; 2 23 using HeuristicLab.Encodings.PermutationEncoding; 3 24 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 14 35 public class BinPackerFirstFit : BinPacker { 15 36 16 public BinPackerFirstFit(Permutation permutation, PackingShape binShape, IList<PackingItem> items, bool useStackingConstraints) { 17 _permutation = permutation; 18 _binShape = binShape; 19 _items = items; 20 _useStackingConstraints = useStackingConstraints; 21 } 22 37 public BinPackerFirstFit() : base() { } 23 38 24 39 /// <summary> … … 26 41 /// </summary> 27 42 /// <returns>Returns a collection of bin packing 3d objects. Each object represents a bin and the packed items</returns> 28 public override IList<BinPacking3D> PackItems( ) {43 public override IList<BinPacking3D> PackItems(Permutation sortedItems, PackingShape binShape, IList<PackingItem> items, bool useStackingConstraints) { 29 44 IList<BinPacking3D> packingList = new List<BinPacking3D>(); 30 IList<int> remainingIds = new List<int>( _permutation);45 IList<int> remainingIds = new List<int>(sortedItems); 31 46 32 47 while (remainingIds.Count > 0) { 33 BinPacking3D packingBin = new BinPacking3D( _binShape);34 PackRemainingItems(ref remainingIds, ref packingBin, _items, _useStackingConstraints, null);48 BinPacking3D packingBin = new BinPacking3D(binShape); 49 PackRemainingItems(ref remainingIds, ref packingBin, items, useStackingConstraints, null); 35 50 packingList.Add(packingBin); 36 51 } … … 38 53 return packingList; 39 54 } 55 56 /// <summary> 57 /// Tries to pack the remainig items into a given BinPacking3D object. Each item could be packed into the BinPacking3D object will be removed from the list of remaining ids 58 /// </summary> 59 /// <param name="remainingIds">List of remaining ids. After the method has been executed the list has to have less items</param> 60 /// <param name="items">List of packing items. Some of the items will be assigned to the BinPacking3D object</param> 61 /// <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 64 foreach (var itemId in new List<int>(remainingIds)) { 65 bool rotated = rotationArray == null ? false : rotationArray[itemId]; 66 PackingPosition position = FindPackingPositionForItem(packingBin, items[itemId], useStackingConstraints, rotated); 67 // if a valid packing position could be found, the current item can be added to the given bin 68 if (position != null) { 69 PackItem(ref packingBin, itemId, items[itemId], position, useStackingConstraints); 70 remainingIds.Remove(itemId); 71 } 72 } 73 } 40 74 } 41 75 } -
branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/3D/Packer/BinPackerFreeVolumeBestFit.cs
r15454 r15462 1 using HeuristicLab.Core; 1 #region License Information 2 /* HeuristicLab 3 * Copyright (C) 2002-2016 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 22 using HeuristicLab.Core; 2 23 using HeuristicLab.Encodings.PermutationEncoding; 3 24 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 12 33 [StorableClass] 13 34 public class BinPackerFreeVolumeBestFit : BinPacker { 14 public BinPackerFreeVolumeBestFit(Permutation permutation, PackingShape binShape, IList<PackingItem> items, bool useStackingConstraints) {15 _permutation = permutation;16 _binShape = binShape;17 _items = items;18 _useStackingConstraints = useStackingConstraints;19 }20 35 21 public override IList<BinPacking3D> PackItems() { 36 public BinPackerFreeVolumeBestFit() : base() { } 37 38 public override IList<BinPacking3D> PackItems(Permutation sortedItems, PackingShape binShape, IList<PackingItem> items, bool useStackingConstraints) { 22 39 IList<BinPacking3D> packingList = new List<BinPacking3D>(); 23 IList<int> remainingIds = new List<int>( _permutation);24 40 IList<int> remainingIds = new List<int>(sortedItems); 41 25 42 26 43 foreach (int remainingId in remainingIds) { 27 44 var sortedBins = packingList.OrderBy(x => x.FreeVolume); 28 PackingItem item = _items[remainingId]; 45 var z = sortedBins.ToList(); 46 47 PackingItem item = items[remainingId]; 29 48 bool positionFound = false; 30 49 31 50 foreach (var packingBin in sortedBins) { 32 PackingPosition position = FindPackingPositionForItem(packingBin, item, _useStackingConstraints, false);51 PackingPosition position = FindPackingPositionForItem(packingBin, item, useStackingConstraints, false); 33 52 positionFound = position != null; 34 53 var bin = packingBin; 35 54 if (positionFound) { 36 PackItem(ref bin, remainingId, item, position, _useStackingConstraints);55 PackItem(ref bin, remainingId, item, position, useStackingConstraints); 37 56 break; 38 } 57 } 39 58 } 40 59 41 60 if (!positionFound) { 42 BinPacking3D packingBin = new BinPacking3D( _binShape);43 PackingPosition position = FindPackingPositionForItem(packingBin, item, _useStackingConstraints, false);61 BinPacking3D packingBin = new BinPacking3D(binShape); 62 PackingPosition position = FindPackingPositionForItem(packingBin, item, useStackingConstraints, false); 44 63 45 64 if (position == null) { … … 47 66 } 48 67 49 PackItem(ref packingBin, remainingId, item, position, _useStackingConstraints);68 PackItem(ref packingBin, remainingId, item, position, useStackingConstraints); 50 69 packingList.Add(packingBin); 51 70 } -
branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/3D/Packer/BinPackerResidualSpaceBestFit.cs
r15454 r15462 1 using HeuristicLab.Core; 1 #region License Information 2 /* HeuristicLab 3 * Copyright (C) 2002-2016 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 22 using HeuristicLab.Core; 2 23 using HeuristicLab.Encodings.PermutationEncoding; 3 24 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 12 33 [StorableClass] 13 34 public class BinPackerResidualSpaceBestFit : BinPacker { 14 public BinPackerResidualSpaceBestFit(Permutation permutation, PackingShape binShape, IList<PackingItem> items, bool useStackingConstraints) {15 _permutation = permutation;16 _binShape = binShape;17 _items = items;18 _useStackingConstraints = useStackingConstraints;19 }20 35 36 public BinPackerResidualSpaceBestFit() : base() { }/* 37 public BinPackerResidualSpaceBestFit(Permutation permutation, PackingShape binShape, IList<PackingItem> items, bool useStackingConstraints) 38 : base(permutation, binShape, items, useStackingConstraints) { } 39 */ 21 40 /// <summary> 22 41 /// Packs the items into the bins by using a best fit residual space algorithm. … … 25 44 /// </summary> 26 45 /// <returns></returns> 27 public override IList<BinPacking3D> PackItems( ) {46 public override IList<BinPacking3D> PackItems(Permutation sortedItems, PackingShape binShape, IList<PackingItem> items, bool useStackingConstraints) { 28 47 IList<BinPacking3D> packingList = new List<BinPacking3D>(); 29 IList<int> remainingIds = new List<int>( _permutation);48 IList<int> remainingIds = new List<int>(sortedItems); 30 49 bool rotated = false; 31 50 32 51 foreach (var remainingId in remainingIds) { 33 PackingItem item = _items[remainingId];52 PackingItem item = items[remainingId]; 34 53 var residualSpacePoints = GetResidualSpaceForAllPoints(packingList, item); 35 54 var sortedPoints = residualSpacePoints.OrderBy(x => x.Item3); … … 37 56 38 57 foreach (var point in sortedPoints) { 39 if (point.Item1.IsPositionFeasible(item, point.Item2, _useStackingConstraints)) {58 if (point.Item1.IsPositionFeasible(item, point.Item2, useStackingConstraints)) { 40 59 var binPacking = point.Item1; 41 PackItem(ref binPacking, remainingId, item, point.Item2, _useStackingConstraints);60 PackItem(ref binPacking, remainingId, item, point.Item2, useStackingConstraints); 42 61 packed = true; 43 62 break; … … 46 65 47 66 if (!packed) { 48 BinPacking3D binPacking = new BinPacking3D( _binShape);49 var position = FindPackingPositionForItem(binPacking, item, _useStackingConstraints, rotated);67 BinPacking3D binPacking = new BinPacking3D(binShape); 68 var position = FindPackingPositionForItem(binPacking, item, useStackingConstraints, rotated); 50 69 if (position != null) { 51 PackItem(ref binPacking, remainingId, item, position, _useStackingConstraints);70 PackItem(ref binPacking, remainingId, item, position, useStackingConstraints); 52 71 } else { 53 72 throw new InvalidOperationException("Item " + remainingId + " cannot be packed in an empty bin.");
Note: See TracChangeset
for help on using the changeset viewer.