Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2817

  • Added some unit tests
  • Enhanced the documentation
File size: 3.8 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("BinPackerFreeVolumeBestFit", "A class for packing bins for the 3D bin-packer problem. It uses a best fit algortihm depending on the free volume.")]
33  [StorableClass]
34  public class BinPackerFreeVolumeBestFit : BinPacker {
35
36    public BinPackerFreeVolumeBestFit() : base() { }
37
38    /// <summary>
39    /// Packs all items by using a free volume best fit strategy.
40    /// If there is no bin packing item, a new one will be created an the current item will be packed into it.
41    /// If there exists at least on bin packing item in the packing list they are being sortet by their free volume ascending.
42    /// The current item will be packed into the bin packing with the fewest free volume and enought space for placing it.
43    /// If an item could not be placed in any bin packing, a new one will be created for the item.
44    /// </summary>
45    /// <param name="sortedItems"></param>
46    /// <param name="binShape"></param>
47    /// <param name="items"></param>
48    /// <param name="useStackingConstraints"></param>
49    /// <returns>Returns a collection of bin packing 3d objects. Each object represents a bin and the packed items</returns>
50    public override IList<BinPacking3D> PackItems(Permutation sortedItems, PackingShape binShape, IList<PackingItem> items, bool useStackingConstraints) {
51      IList<BinPacking3D> packingList = new List<BinPacking3D>();
52      IList<int> remainingIds = new List<int>(sortedItems);
53
54      foreach (int remainingId in remainingIds) {
55        var sortedBins = packingList.OrderBy(x => x.FreeVolume);
56        var z = sortedBins.ToList();
57
58        PackingItem item = items[remainingId];
59        bool positionFound = false;
60
61        foreach (var packingBin in sortedBins) {
62          PackingPosition position = FindPackingPositionForItem(packingBin, item, useStackingConstraints, false);
63          positionFound = position != null;
64          var bin = packingBin;
65          if (positionFound) {
66            PackItem(ref bin, remainingId, item, position, useStackingConstraints);
67            break;
68          }
69        }
70
71        if (!positionFound) {
72          BinPacking3D packingBin = new BinPacking3D(binShape);
73          PackingPosition position = FindPackingPositionForItem(packingBin, item, useStackingConstraints, false);
74
75          if (position == null) {
76            throw new InvalidOperationException("Item " + remainingId + " cannot be packed in empty bin.");
77          }
78
79          PackItem(ref packingBin, remainingId, item, position, useStackingConstraints);
80          packingList.Add(packingBin);
81        }
82      }
83      return packingList.ToList();
84    }
85  }
86}
Note: See TracBrowser for help on using the repository browser.