Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2817
-Added unit tests for bin packing 3d.
-The items can now be sorted by the material.

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