Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/3D/Packer/BinPackerFreeVolumeBestFit.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: 2.9 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    public override IList<BinPacking3D> PackItems(Permutation sortedItems, PackingShape binShape, IList<PackingItem> items, bool useStackingConstraints) {
39      IList<BinPacking3D> packingList = new List<BinPacking3D>();
40      IList<int> remainingIds = new List<int>(sortedItems);
41
42
43      foreach (int remainingId in remainingIds) {
44        var sortedBins = packingList.OrderBy(x => x.FreeVolume);
45        var z = sortedBins.ToList();
46
47        PackingItem item = items[remainingId];
48        bool positionFound = false;
49
50        foreach (var packingBin in sortedBins) {
51          PackingPosition position = FindPackingPositionForItem(packingBin, item, useStackingConstraints, false);
52          positionFound = position != null;
53          var bin = packingBin;
54          if (positionFound) {
55            PackItem(ref bin, remainingId, item, position, useStackingConstraints);
56            break;
57          }
58        }
59
60        if (!positionFound) {
61          BinPacking3D packingBin = new BinPacking3D(binShape);
62          PackingPosition position = FindPackingPositionForItem(packingBin, item, useStackingConstraints, false);
63
64          if (position == null) {
65            throw new InvalidOperationException("Item " + remainingId + " cannot be packed in empty bin.");
66          }
67
68          PackItem(ref packingBin, remainingId, item, position, useStackingConstraints);
69          packingList.Add(packingBin);
70        }
71      }
72      return packingList.ToList();
73    }
74  }
75}
Note: See TracBrowser for help on using the repository browser.