Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15652 was 15652, checked in by rhanghof, 7 years ago

#2817:

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