Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/3D/Packer/BinPacker.cs @ 15454

Last change on this file since 15454 was 15454, checked in by rhanghof, 6 years ago

#2817

  • Extreme point bin packing does not need the occupation layer anymore
  • Changes at the fitting algorithm. Now they are packing the items as in the paper of S. Martello, D. Pisinger, D. Vigo described
File size: 3.9 KB
Line 
1using HeuristicLab.Core;
2using HeuristicLab.Encodings.PermutationEncoding;
3using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
4using HeuristicLab.Problems.BinPacking3D;
5using System;
6using System.Collections.Generic;
7using System.Linq;
8using System.Text;
9using System.Threading.Tasks;
10
11namespace HeuristicLab.Problems.BinPacking3D.Packer {
12  [Item("BinPacker", "A class for packing bins for the 3D bin-packer problem.")]
13  [StorableClass]
14  public abstract class BinPacker {
15    protected Permutation _permutation;
16    protected PackingShape _binShape;
17    protected IList<PackingItem> _items;
18    protected bool _useStackingConstraints;
19
20    /// <summary>
21    /// Packs all items of the bin packer and returns a collection of BinPacking3D objects
22    /// </summary>
23    /// <returns></returns>
24    public abstract IList<BinPacking3D> PackItems();
25
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    }
44
45    /// <summary>
46    /// Pack a given item into a given bin and updates the residual space and the extreme points
47    /// </summary>
48    /// <param name="packingBin"></param>
49    /// <param name="itemId"></param>
50    /// <param name="packingItem"></param>
51    /// <param name="position"></param>
52    protected void PackItem(ref BinPacking3D packingBin, int itemId, PackingItem packingItem, PackingPosition position, bool useStackingConstraints) {
53
54      packingBin.AddItem(itemId, packingItem, position);
55      packingBin.UpdateResidualSpace(packingItem, position);
56      packingBin.UpdateExtremePoints(packingItem, position, useStackingConstraints);
57    }
58
59    /// <summary>
60    /// This method tries to find a valid packing position for an given item in a given packing bin
61    /// </summary>
62    /// <param name="packingBin"></param>
63    /// <param name="packingItem"></param>
64    /// <param name="useStackingConstraints"></param>
65    /// <param name="rotated"></param>
66    /// <returns>Returns the packing position for an given item. If there could not be found a valid position it returns null</returns>
67    protected PackingPosition FindPackingPositionForItem(BinPacking3D packingBin, PackingItem packingItem, bool useStackingConstraints, bool rotated) {
68      PackingItem newItem = new PackingItem(
69        rotated ? packingItem.Depth : packingItem.Width,
70        packingItem.Height,
71        rotated ? packingItem.Width : packingItem.Depth,
72        packingItem.TargetBin, packingItem.Weight, packingItem.Material);
73
74      // The extremepoints are sortet by Z, X, Y
75
76      return packingBin.ExtremePoints.Where(x => packingBin.IsPositionFeasible(newItem, x, useStackingConstraints)).FirstOrDefault();
77    }
78  }
79}
Note: See TracBrowser for help on using the repository browser.