Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2817:

  • The items can be rotated and tilted now.
  • Added pruning of extreme points in packed bins.
  • Added new packer which packs items by positioning them on the point with the minimum of wasted space. He uses rotating and tilting of items.
  • Added classes for sorting given items.
File size: 5.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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
22using HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Encodings.PermutationEncoding;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26using HeuristicLab.Problems.BinPacking3D.ExtremePointCreation;
27using HeuristicLab.Problems.BinPacking3D.ExtremePointPruning;
28using System;
29using System.Collections.Generic;
30using System.Linq;
31using System.Text;
32using System.Threading.Tasks;
33
34namespace HeuristicLab.Problems.BinPacking3D.Packer {
35  internal class BinPackerResidualSpaceBestFit : BinPacker {
36
37    #region Constructors for HEAL
38    [StorableConstructor]
39    protected BinPackerResidualSpaceBestFit(bool deserializing) : base(deserializing) { }
40
41    protected BinPackerResidualSpaceBestFit(BinPackerResidualSpaceBestFit original, Cloner cloner)
42      : base(original, cloner) {
43    }
44
45    public override IDeepCloneable Clone(Cloner cloner) {
46      return new BinPackerResidualSpaceBestFit(this, cloner);
47    }
48    #endregion
49
50    public BinPackerResidualSpaceBestFit() : base() { }
51
52    /// <summary>
53    /// Packs the items into the bins by using a best fit residual space algorithm.
54    /// The order of the chosen items depends on the merit function.
55    /// Each residual space belongs to an extreme point.
56    /// </summary>
57    /// <returns>Returns a collection of bin packing 3d objects. Each object represents a bin and the packed items</returns>
58    public override IList<BinPacking3D> PackItems(Permutation sortedItems, PackingShape binShape, IList<PackingItem> items, ExtremePointCreationMethod epCreationMethod, ExtremePointPruningMethod epPruningMethod, bool useStackingConstraints) {
59      IList<BinPacking3D> packingList = new List<BinPacking3D>();
60      IList<int> remainingIds = new List<int>(sortedItems);
61      IExtremePointCreator extremePointCreator = ExtremePointCreatorFactory.CreateExtremePointCreator(epCreationMethod, useStackingConstraints);
62
63      foreach (var remainingId in remainingIds) {
64        PackingItem item = items[remainingId];
65        var residualSpacePoints = GetResidualSpaceForAllPoints(packingList, item);
66        var sortedPoints = residualSpacePoints.OrderBy(x => x.Item3);
67        var packed = false;
68
69        foreach (var point in sortedPoints) {
70          if (point.Item1.IsPositionFeasible(item, point.Item2, useStackingConstraints)) {
71            var binPacking = point.Item1;
72            PackItem(binPacking, remainingId, item, point.Item2, extremePointCreator, useStackingConstraints);
73            packed = true;
74            break;
75          }
76        }
77
78        if (!packed) {
79          BinPacking3D binPacking = new BinPacking3D(binShape);
80          var position = FindPackingPositionForItem(binPacking, item, useStackingConstraints);
81          if (position != null) {
82            PackItem(binPacking, remainingId, item, position, extremePointCreator, useStackingConstraints);
83          } else {
84            throw new InvalidOperationException("Item " + remainingId + " cannot be packed into an empty bin.");
85          }
86          packingList.Add(binPacking);
87        }
88      }
89
90      ExtremePointPruningFactory.CreatePruning().PruneExtremePoints(epPruningMethod, packingList);
91      return packingList;
92    }
93
94    /// <summary>
95    /// This method returns a list with all bins and their residual space where a given item can be placed.
96    /// It is nessecary to get all bins and their residaul space because this list will be sortet later.
97    /// </summary>
98    /// <param name="packingList"></param>
99    /// <param name="item"></param>
100    /// <returns></returns>
101    public static IList<Tuple<BinPacking3D, PackingPosition, int>> GetResidualSpaceForAllPoints(IList<BinPacking3D> packingList, PackingItem item) {
102      var residualSpacePoints = new List<Tuple<BinPacking3D, PackingPosition, int>>();
103      foreach (BinPacking3D bp in packingList) {
104        foreach (var extremPoints in bp.ExtremePoints) {
105          var ep = extremPoints.Key;
106          var residualSpaces = extremPoints.Value.Where(rs => rs.Width < item.Width || rs.Height < item.Height || rs.Depth < item.Depth);
107          if (residualSpaces.Count() <= 0) {
108            continue;
109          }
110          int merit = residualSpaces.Max(rs => CalculateResidualMerit(rs, item, ep));
111          residualSpacePoints.Add(Tuple.Create(bp, ep, merit));
112        }
113      }
114      return residualSpacePoints;
115    }
116
117    /// <summary>
118    /// The merit function puts an item on the EP that minimizes the difference between its residual space an the item dimension
119    /// </summary>
120    /// <param name="rs"></param>
121    /// <param name="item"></param>
122    /// <param name="ep"></param>
123    /// <returns></returns>
124    private static int CalculateResidualMerit(ResidualSpace rs, PackingItem item, PackingPosition ep) {
125      return ((rs.Width - item.Width) +
126          (rs.Height - item.Height) +
127          (rs.Depth - item.Depth));
128    }
129  }
130}
Note: See TracBrowser for help on using the repository browser.