1 | using HeuristicLab.Core;
|
---|
2 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
3 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
4 | using System;
|
---|
5 | using System.Collections.Generic;
|
---|
6 | using System.Linq;
|
---|
7 | using System.Text;
|
---|
8 | using System.Threading.Tasks;
|
---|
9 |
|
---|
10 | namespace HeuristicLab.Problems.BinPacking3D.Packer {
|
---|
11 | [Item("BinPackerResidualSpaceBestFit", "A class for packing bins for the 3D bin-packer problem. It uses a best fit algortihm depending on the residual space.")]
|
---|
12 | [StorableClass]
|
---|
13 | 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 |
|
---|
21 | /// <summary>
|
---|
22 | /// Packs the items into the bins by using a best fit residual space algorithm.
|
---|
23 | /// The order of the chosen items depends on the merit function.
|
---|
24 | /// Each residual space belongs to an extreme point.
|
---|
25 | /// </summary>
|
---|
26 | /// <returns></returns>
|
---|
27 | public override IList<BinPacking3D> PackItems() {
|
---|
28 | IList<BinPacking3D> packingList = new List<BinPacking3D>();
|
---|
29 | IList<int> remainingIds = new List<int>(_permutation);
|
---|
30 | bool rotated = false;
|
---|
31 |
|
---|
32 | foreach (var remainingId in remainingIds) {
|
---|
33 | PackingItem item = _items[remainingId];
|
---|
34 | var residualSpacePoints = GetResidualSpaceForAllPoints(packingList, item);
|
---|
35 | var sortedPoints = residualSpacePoints.OrderBy(x => x.Item3);
|
---|
36 | var packed = false;
|
---|
37 |
|
---|
38 | foreach (var point in sortedPoints) {
|
---|
39 | if (point.Item1.IsPositionFeasible(item, point.Item2, _useStackingConstraints)) {
|
---|
40 | var binPacking = point.Item1;
|
---|
41 | PackItem(ref binPacking, remainingId, item, point.Item2, _useStackingConstraints);
|
---|
42 | packed = true;
|
---|
43 | break;
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | if (!packed) {
|
---|
48 | BinPacking3D binPacking = new BinPacking3D(_binShape);
|
---|
49 | var position = FindPackingPositionForItem(binPacking, item, _useStackingConstraints, rotated);
|
---|
50 | if (position != null) {
|
---|
51 | PackItem(ref binPacking, remainingId, item, position, _useStackingConstraints);
|
---|
52 | } else {
|
---|
53 | throw new InvalidOperationException("Item " + remainingId + " cannot be packed in an empty bin.");
|
---|
54 | }
|
---|
55 | packingList.Add(binPacking);
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | return packingList;
|
---|
60 | }
|
---|
61 |
|
---|
62 | /// <summary>
|
---|
63 | /// This method returns a list with all bins and their residual space where a given item can be placed.
|
---|
64 | /// It is nessecary to get all bins and their residaul space because this list will be sortet later.
|
---|
65 | /// </summary>
|
---|
66 | /// <param name="packingList"></param>
|
---|
67 | /// <param name="item"></param>
|
---|
68 | /// <returns></returns>
|
---|
69 | public static IList<Tuple<BinPacking3D, PackingPosition, int>> GetResidualSpaceForAllPoints(IList<BinPacking3D> packingList, PackingItem item) {
|
---|
70 | var residualSpacePoints = new List<Tuple<BinPacking3D, PackingPosition, int>>();
|
---|
71 | foreach (BinPacking3D bp in packingList) {
|
---|
72 | foreach (var ep in bp.ExtremePoints) {
|
---|
73 | var rs = bp.ResidualSpace[ep];
|
---|
74 | if (rs.Item1 < item.Width || rs.Item2 < item.Height || rs.Item3 < item.Depth) {
|
---|
75 | continue;
|
---|
76 | }
|
---|
77 | residualSpacePoints.Add(Tuple.Create(bp, ep, CalculateResidualMerit(rs, item, ep)));
|
---|
78 | }
|
---|
79 | }
|
---|
80 | return residualSpacePoints;
|
---|
81 | }
|
---|
82 |
|
---|
83 | /// <summary>
|
---|
84 | /// The merit function puts an item on the EP that minimizes the difference between its residual space an the item dimension
|
---|
85 | /// </summary>
|
---|
86 | /// <param name="rs"></param>
|
---|
87 | /// <param name="item"></param>
|
---|
88 | /// <param name="ep"></param>
|
---|
89 | /// <returns></returns>
|
---|
90 | private static int CalculateResidualMerit(Tuple<int, int, int> rs, PackingItem item, PackingPosition ep) {
|
---|
91 | return ((rs.Item1 - item.Width) +
|
---|
92 | (rs.Item2 - item.Height) +
|
---|
93 | (rs.Item3 - item.Depth));
|
---|
94 | }
|
---|
95 |
|
---|
96 | }
|
---|
97 | }
|
---|