1 | using HeuristicLab.Core;
|
---|
2 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
3 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
4 | using HeuristicLab.Problems.BinPacking3D;
|
---|
5 | using System;
|
---|
6 | using System.Collections.Generic;
|
---|
7 | using System.Linq;
|
---|
8 | using System.Text;
|
---|
9 | using System.Threading.Tasks;
|
---|
10 |
|
---|
11 | namespace 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 | }
|
---|