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, 21 months ago

#2817:

  • Unittests
  • Bugfixes on the line projection based extreme point creation method
File size: 4.4 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.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Encodings.PermutationEncoding;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26using HeuristicLab.Problems.BinPacking3D.ExtremePointCreation;
27using System;
28using System.Collections.Generic;
29using System.Linq;
30using System.Text;
31using System.Threading.Tasks;
32
33namespace HeuristicLab.Problems.BinPacking3D.Packer {
34  internal class BinPackerFreeVolumeBestFit : BinPacker {
35   
36    #region Constructors for HEAL
37    [StorableConstructor]
38    protected BinPackerFreeVolumeBestFit(bool deserializing) : base(deserializing) { }
39
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
49    public BinPackerFreeVolumeBestFit() : base() { }
50
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>
63    public override IList<BinPacking3D> PackItems(Permutation sortedItems, PackingShape binShape, IList<PackingItem> items, ExtremePointCreationMethod epGenerationMethod, bool useStackingConstraints) {
64      IList<BinPacking3D> packingList = new List<BinPacking3D>();
65      IList<int> remainingIds = new List<int>(sortedItems);
66      IExtremePointCreator extremePointCreator = ExtremePointCreatorFactory.CreateExtremePointCreator(epGenerationMethod, useStackingConstraints);
67      foreach (int remainingId in remainingIds) {
68        var sortedBins = packingList.OrderBy(x => x.FreeVolume);
69        var z = sortedBins.ToList();
70
71        PackingItem item = items[remainingId];
72        bool positionFound = false;
73
74        foreach (var packingBin in sortedBins) {
75          PackingPosition position = FindPackingPositionForItem(packingBin, item, useStackingConstraints, false);
76          positionFound = position != null;
77          var bin = packingBin;
78          if (positionFound) {
79            PackItem(bin, remainingId, item, position, extremePointCreator, useStackingConstraints);
80            break;
81          }
82        }
83
84        if (!positionFound) {
85          BinPacking3D packingBin = new BinPacking3D(binShape);
86          PackingPosition position = FindPackingPositionForItem(packingBin, item, useStackingConstraints, false);
87
88          if (position == null) {
89            throw new InvalidOperationException("Item " + remainingId + " cannot be packed into an empty bin.");
90          }
91
92          PackItem(packingBin, remainingId, item, position, extremePointCreator, useStackingConstraints);
93          packingList.Add(packingBin);
94        }
95      }
96      return packingList.ToList();
97    }
98  }
99}
Note: See TracBrowser for help on using the repository browser.