Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2817:

  • Changed the calculation algorithm for creating extreme points by using line based projection
  • Changed the calculation of the residual spaces for line based projection
File size: 5.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  public 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 epGenerationMethod, bool useStackingConstraints) {
58      IList<BinPacking3D> packingList = new List<BinPacking3D>();
59      IList<int> remainingIds = new List<int>(sortedItems);
60      IExtremePointCreator extremePointCreator = ExtremePointCreatorFactory.CreateExtremePointCreator(epGenerationMethod, 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 ep in bp.ExtremePoints) {
103          var rs = bp.ResidualSpaces[ep];
104          if (rs.Width < item.Width || rs.Height < item.Height || rs.Depth < item.Depth) {
105            continue;
106          }
107          residualSpacePoints.Add(Tuple.Create(bp, ep, CalculateResidualMerit(rs, item, ep)));
108        }
109      }
110      return residualSpacePoints;
111    }
112
113    /// <summary>
114    /// The merit function puts an item on the EP that minimizes the difference between its residual space an the item dimension
115    /// </summary>
116    /// <param name="rs"></param>
117    /// <param name="item"></param>
118    /// <param name="ep"></param>
119    /// <returns></returns>
120    private static int CalculateResidualMerit(ResidualSpace rs, PackingItem item, PackingPosition ep) {
121      return ((rs.Width - item.Width) +
122          (rs.Height - item.Height) +
123          (rs.Depth - item.Depth));
124    }
125  }
126}
Note: See TracBrowser for help on using the repository browser.