Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2817:
-Added unit tests
-Refactoring of bp 3D

File size: 3.5 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
33  [Item("BinPackerFirstFit", "A class for packing bins for the 3D bin-packer problem. It uses a first fit algorithm")]
34  [StorableClass]
35  public class BinPackerFirstFit : BinPacker {
36
37    public BinPackerFirstFit() : base() { }   
38
39    /// <summary>
40    /// Packs the items of the object by using a first fit algorithm into an amount of bins and returns them.
41    /// </summary>
42    /// <returns>Returns a collection of bin packing 3d objects. Each object represents a bin and the packed items</returns>
43    public override IList<BinPacking3D> PackItems(Permutation sortedItems, PackingShape binShape, IList<PackingItem> items, bool useStackingConstraints) {
44      IList<BinPacking3D> packingList = new List<BinPacking3D>();
45      IList<int> remainingIds = new List<int>(sortedItems);
46
47      while (remainingIds.Count > 0) {
48        BinPacking3D packingBin = new BinPacking3D(binShape);
49        PackRemainingItems(ref remainingIds, ref packingBin, items, useStackingConstraints, null);
50        packingList.Add(packingBin);
51      }
52
53      return packingList;
54    }
55
56    /// <summary>
57    /// 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
58    /// </summary>
59    /// <param name="remainingIds">List of remaining ids. After the method has been executed the list has to have less items</param>
60    /// <param name="items">List of packing items. Some of the items will be assigned to the BinPacking3D object</param>
61    /// <param name="packingBin">This object will be filled with some of the given items</param>
62    protected void PackRemainingItems(ref IList<int> remainingIds, ref BinPacking3D packingBin, IList<PackingItem> items, bool useStackingConstraints, Dictionary<int, bool> rotationArray) {
63
64      foreach (var itemId in new List<int>(remainingIds)) {
65        bool rotated = rotationArray == null ? false : rotationArray[itemId];
66        PackingPosition position = FindPackingPositionForItem(packingBin, items[itemId], useStackingConstraints, rotated);
67        // if a valid packing position could be found, the current item can be added to the given bin
68        if (position != null) {
69          PackItem(packingBin, itemId, items[itemId], position, useStackingConstraints);
70          remainingIds.Remove(itemId);
71        }
72      }
73    }
74  }
75}
Note: See TracBrowser for help on using the repository browser.