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, 6 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
RevLine 
[15462]1#region License Information
2/* HeuristicLab
[15617]3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[15462]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
[15488]22using HeuristicLab.Common;
[15462]23using HeuristicLab.Core;
[15454]24using HeuristicLab.Encodings.PermutationEncoding;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[15488]26using HeuristicLab.Problems.BinPacking3D.ExtremePointCreation;
[15617]27using HeuristicLab.Problems.BinPacking3D.ExtremePointPruning;
[15454]28using System;
29using System.Collections.Generic;
30using System.Linq;
31using System.Text;
32using System.Threading.Tasks;
33
34namespace HeuristicLab.Problems.BinPacking3D.Packer {
[15554]35  internal class BinPackerFreeVolumeBestFit : BinPacker {
[15488]36   
37    #region Constructors for HEAL
38    [StorableConstructor]
39    protected BinPackerFreeVolumeBestFit(bool deserializing) : base(deserializing) { }
[15454]40
[15488]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
[15462]50    public BinPackerFreeVolumeBestFit() : base() { }
51
[15471]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>
[15617]64    public override IList<BinPacking3D> PackItems(Permutation sortedItems, PackingShape binShape, IList<PackingItem> items, ExtremePointCreationMethod epGenerationMethod, ExtremePointPruningMethod epPruningMethod, bool useStackingConstraints) {
[15454]65      IList<BinPacking3D> packingList = new List<BinPacking3D>();
[15462]66      IList<int> remainingIds = new List<int>(sortedItems);
[15488]67      IExtremePointCreator extremePointCreator = ExtremePointCreatorFactory.CreateExtremePointCreator(epGenerationMethod, useStackingConstraints);
[15454]68      foreach (int remainingId in remainingIds) {
69        var sortedBins = packingList.OrderBy(x => x.FreeVolume);
[15462]70        var z = sortedBins.ToList();
71
72        PackingItem item = items[remainingId];
[15454]73        bool positionFound = false;
74
75        foreach (var packingBin in sortedBins) {
[15617]76          PackingPosition position = FindPackingPositionForItem(packingBin, item, useStackingConstraints);
[15454]77          positionFound = position != null;
78          var bin = packingBin;
79          if (positionFound) {
[15488]80            PackItem(bin, remainingId, item, position, extremePointCreator, useStackingConstraints);
[15454]81            break;
[15462]82          }
[15454]83        }
84
85        if (!positionFound) {
[15462]86          BinPacking3D packingBin = new BinPacking3D(binShape);
[15617]87          PackingPosition position = FindPackingPositionForItem(packingBin, item, useStackingConstraints);
[15454]88
89          if (position == null) {
[15488]90            throw new InvalidOperationException("Item " + remainingId + " cannot be packed into an empty bin.");
[15454]91          }
92
[15488]93          PackItem(packingBin, remainingId, item, position, extremePointCreator, useStackingConstraints);
[15454]94          packingList.Add(packingBin);
95        }
96      }
[15617]97      ExtremePointPruningFactory.CreatePruning().PruneExtremePoints(epPruningMethod, packingList);
[15454]98      return packingList.ToList();
99    }
100  }
101}
Note: See TracBrowser for help on using the repository browser.