Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2817:

  • Unittests
  • Bugfixes on the line projection based extreme point creation method
File size: 4.4 KB
RevLine 
[15462]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
[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;
[15454]27using System;
28using System.Collections.Generic;
29using System.Linq;
30using System.Text;
31using System.Threading.Tasks;
32
33namespace HeuristicLab.Problems.BinPacking3D.Packer {
[15554]34  internal class BinPackerFreeVolumeBestFit : BinPacker {
[15488]35   
36    #region Constructors for HEAL
37    [StorableConstructor]
38    protected BinPackerFreeVolumeBestFit(bool deserializing) : base(deserializing) { }
[15454]39
[15488]40    protected BinPackerFreeVolumeBestFit(BinPackerFreeVolumeBestFit original, Cloner cloner)
41      : base(original, cloner) {
42    }
43
44    public override IDeepCloneable Clone(Cloner cloner) {
45      return new BinPackerFreeVolumeBestFit(this, cloner);
46    }
47    #endregion
48
[15462]49    public BinPackerFreeVolumeBestFit() : base() { }
50
[15471]51    /// <summary>
52    /// Packs all items by using a free volume best fit strategy.
53    /// If there is no bin packing item, a new one will be created an the current item will be packed into it.
54    /// If there exists at least on bin packing item in the packing list they are being sortet by their free volume ascending.
55    /// The current item will be packed into the bin packing with the fewest free volume and enought space for placing it.
56    /// If an item could not be placed in any bin packing, a new one will be created for the item.
57    /// </summary>
58    /// <param name="sortedItems"></param>
59    /// <param name="binShape"></param>
60    /// <param name="items"></param>
61    /// <param name="useStackingConstraints"></param>
62    /// <returns>Returns a collection of bin packing 3d objects. Each object represents a bin and the packed items</returns>
[15488]63    public override IList<BinPacking3D> PackItems(Permutation sortedItems, PackingShape binShape, IList<PackingItem> items, ExtremePointCreationMethod epGenerationMethod, bool useStackingConstraints) {
[15454]64      IList<BinPacking3D> packingList = new List<BinPacking3D>();
[15462]65      IList<int> remainingIds = new List<int>(sortedItems);
[15488]66      IExtremePointCreator extremePointCreator = ExtremePointCreatorFactory.CreateExtremePointCreator(epGenerationMethod, useStackingConstraints);
[15454]67      foreach (int remainingId in remainingIds) {
68        var sortedBins = packingList.OrderBy(x => x.FreeVolume);
[15462]69        var z = sortedBins.ToList();
70
71        PackingItem item = items[remainingId];
[15454]72        bool positionFound = false;
73
74        foreach (var packingBin in sortedBins) {
[15462]75          PackingPosition position = FindPackingPositionForItem(packingBin, item, useStackingConstraints, false);
[15454]76          positionFound = position != null;
77          var bin = packingBin;
78          if (positionFound) {
[15488]79            PackItem(bin, remainingId, item, position, extremePointCreator, useStackingConstraints);
[15454]80            break;
[15462]81          }
[15454]82        }
83
84        if (!positionFound) {
[15462]85          BinPacking3D packingBin = new BinPacking3D(binShape);
86          PackingPosition position = FindPackingPositionForItem(packingBin, item, useStackingConstraints, false);
[15454]87
88          if (position == null) {
[15488]89            throw new InvalidOperationException("Item " + remainingId + " cannot be packed into an empty bin.");
[15454]90          }
91
[15488]92          PackItem(packingBin, remainingId, item, position, extremePointCreator, useStackingConstraints);
[15454]93          packingList.Add(packingBin);
94        }
95      }
96      return packingList.ToList();
97    }
98  }
99}
Note: See TracBrowser for help on using the repository browser.