source: branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/3D/Packer/BinPackerResidualSpaceBestFit.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: 5.6 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 BinPackerResidualSpaceBestFit : BinPacker {
35
36    #region Constructors for HEAL
37    [StorableConstructor]
38    protected BinPackerResidualSpaceBestFit(bool deserializing) : base(deserializing) { }
39
40    protected BinPackerResidualSpaceBestFit(BinPackerResidualSpaceBestFit original, Cloner cloner) 
41      : base(original, cloner) {
42    }
43
44    public override IDeepCloneable Clone(Cloner cloner) {
45      return new BinPackerResidualSpaceBestFit(this, cloner);
46    }
47    #endregion
48
49    public BinPackerResidualSpaceBestFit() : base() { }
50
51    /// <summary>
52    /// Packs the items into the bins by using a best fit residual space algorithm.
53    /// The order of the chosen items depends on the merit function.
54    /// Each residual space belongs to an extreme point.
55    /// </summary>
56    /// <returns>Returns a collection of bin packing 3d objects. Each object represents a bin and the packed items</returns>
57    public override IList<BinPacking3D> PackItems(Permutation sortedItems, PackingShape binShape, IList<PackingItem> items, ExtremePointCreationMethod epCreationMethod, bool useStackingConstraints) {
58      IList<BinPacking3D> packingList = new List<BinPacking3D>();
59      IList<int> remainingIds = new List<int>(sortedItems);
60      IExtremePointCreator extremePointCreator = ExtremePointCreatorFactory.CreateExtremePointCreator(epCreationMethod, useStackingConstraints);
61      bool rotated = false;
62
63      foreach (var remainingId in remainingIds) {
64        PackingItem item = items[remainingId];
65        var residualSpacePoints = GetResidualSpaceForAllPoints(packingList, item);
66        var sortedPoints = residualSpacePoints.OrderBy(x => x.Item3);
67        var packed = false;
68
69        foreach (var point in sortedPoints) {
70          if (point.Item1.IsPositionFeasible(item, point.Item2, useStackingConstraints)) {
71            var binPacking = point.Item1;
72            PackItem(binPacking, remainingId, item, point.Item2, extremePointCreator, useStackingConstraints);
73            packed = true;
74            break;
75          }
76        }
77
78        if (!packed) {
79          BinPacking3D binPacking = new BinPacking3D(binShape);
80          var position = FindPackingPositionForItem(binPacking, item, useStackingConstraints, rotated);
81          if (position != null) {
82            PackItem(binPacking, remainingId, item, position, extremePointCreator, useStackingConstraints);
83          } else {
84            throw new InvalidOperationException("Item " + remainingId + " cannot be packed into an empty bin.");
85          }
86          packingList.Add(binPacking);
87        }
88      }
89      return packingList;
90    }
91
92    /// <summary>
93    /// This method returns a list with all bins and their residual space where a given item can be placed.
94    /// It is nessecary to get all bins and their residaul space because this list will be sortet later.
95    /// </summary>
96    /// <param name="packingList"></param>
97    /// <param name="item"></param>
98    /// <returns></returns>
99    public static IList<Tuple<BinPacking3D, PackingPosition, int>> GetResidualSpaceForAllPoints(IList<BinPacking3D> packingList, PackingItem item) {
100      var residualSpacePoints = new List<Tuple<BinPacking3D, PackingPosition, int>>();
101      foreach (BinPacking3D bp in packingList) {
102        foreach (var extremPoints in bp.ExtremePoints) {
103          var ep = extremPoints.Key;
104          var residualSpaces = extremPoints.Value.Where(rs => rs.Width < item.Width || rs.Height < item.Height || rs.Depth < item.Depth);
105          if (residualSpaces.Count() <= 0) {
106            continue;
107          }
108          int merit = residualSpaces.Max(rs => CalculateResidualMerit(rs, item, ep));
109          residualSpacePoints.Add(Tuple.Create(bp, ep, merit));
110        }
111      }
112      return residualSpacePoints;
113    }
114
115    /// <summary>
116    /// The merit function puts an item on the EP that minimizes the difference between its residual space an the item dimension
117    /// </summary>
118    /// <param name="rs"></param>
119    /// <param name="item"></param>
120    /// <param name="ep"></param>
121    /// <returns></returns>
122    private static int CalculateResidualMerit(ResidualSpace rs, PackingItem item, PackingPosition ep) {
123      return ((rs.Width - item.Width) +
124          (rs.Height - item.Height) +
125          (rs.Depth - item.Depth));
126    }
127  }
128}
Note: See TracBrowser for help on using the repository browser.