Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15617 was 15617, checked in by rhanghof, 7 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: 4.5 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 BinPackerFreeVolumeBestFit : BinPacker {
36   
37    #region Constructors for HEAL
38    [StorableConstructor]
39    protected BinPackerFreeVolumeBestFit(bool deserializing) : base(deserializing) { }
40
41    protected BinPackerFreeVolumeBestFit(BinPackerFreeVolumeBestFit original, Cloner cloner)
42      : base(original, cloner) {
43    }
44
45    public override IDeepCloneable Clone(Cloner cloner) {
46      return new BinPackerFreeVolumeBestFit(this, cloner);
47    }
48    #endregion
49
50    public BinPackerFreeVolumeBestFit() : base() { }
51
52    /// <summary>
53    /// Packs all items by using a free volume best fit strategy.
54    /// If there is no bin packing item, a new one will be created an the current item will be packed into it.
55    /// If there exists at least on bin packing item in the packing list they are being sortet by their free volume ascending.
56    /// The current item will be packed into the bin packing with the fewest free volume and enought space for placing it.
57    /// If an item could not be placed in any bin packing, a new one will be created for the item.
58    /// </summary>
59    /// <param name="sortedItems"></param>
60    /// <param name="binShape"></param>
61    /// <param name="items"></param>
62    /// <param name="useStackingConstraints"></param>
63    /// <returns>Returns a collection of bin packing 3d objects. Each object represents a bin and the packed items</returns>
64    public override IList<BinPacking3D> PackItems(Permutation sortedItems, PackingShape binShape, IList<PackingItem> items, ExtremePointCreationMethod epGenerationMethod, ExtremePointPruningMethod epPruningMethod, bool useStackingConstraints) {
65      IList<BinPacking3D> packingList = new List<BinPacking3D>();
66      IList<int> remainingIds = new List<int>(sortedItems);
67      IExtremePointCreator extremePointCreator = ExtremePointCreatorFactory.CreateExtremePointCreator(epGenerationMethod, useStackingConstraints);
68      foreach (int remainingId in remainingIds) {
69        var sortedBins = packingList.OrderBy(x => x.FreeVolume);
70        var z = sortedBins.ToList();
71
72        PackingItem item = items[remainingId];
73        bool positionFound = false;
74
75        foreach (var packingBin in sortedBins) {
76          PackingPosition position = FindPackingPositionForItem(packingBin, item, useStackingConstraints);
77          positionFound = position != null;
78          var bin = packingBin;
79          if (positionFound) {
80            PackItem(bin, remainingId, item, position, extremePointCreator, useStackingConstraints);
81            break;
82          }
83        }
84
85        if (!positionFound) {
86          BinPacking3D packingBin = new BinPacking3D(binShape);
87          PackingPosition position = FindPackingPositionForItem(packingBin, item, useStackingConstraints);
88
89          if (position == null) {
90            throw new InvalidOperationException("Item " + remainingId + " cannot be packed into an empty bin.");
91          }
92
93          PackItem(packingBin, remainingId, item, position, extremePointCreator, useStackingConstraints);
94          packingList.Add(packingBin);
95        }
96      }
97      ExtremePointPruningFactory.CreatePruning().PruneExtremePoints(epPruningMethod, packingList);
98      return packingList.ToList();
99    }
100  }
101}
Note: See TracBrowser for help on using the repository browser.