Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/3D/Packer/BinPacker.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.3 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;
27using HeuristicLab.Problems.BinPacking3D.ExtremePointCreation;
28using System;
29using System.Collections.Generic;
30using System.Linq;
31using System.Text;
32using System.Threading.Tasks;
33
34namespace HeuristicLab.Problems.BinPacking3D.Packer {
35  public abstract class BinPacker : Item {
36
37    /*
38    [Storable]
39    IPositionFinder PositionFinder
40   
41    */
42
43    #region Constructors for HEAL
44
45   
46    [StorableConstructor]
47    protected BinPacker(bool deserializing) : base(deserializing) { }
48
49    protected BinPacker(BinPacker original, Cloner cloner)
50      : base(original, cloner) {
51      //this.PositionFinder = original.PositionFinder;
52    }
53
54    #endregion
55
56    public BinPacker() { }
57
58    /// <summary>
59    /// Packs all items of the bin packer and returns a collection of BinPacking3D objects
60    /// </summary>
61    /// <param name="sortedItems">Permutation of items sorted by a sorting method. The value of each permutation index references to the index of the items list</param>
62    /// <param name="binShape">Bin for storing the items</param>
63    /// <param name="items">A list of packing items which should be assigned to a bin</param>
64    /// <param name="useStackingConstraints">Flag for using stacking constraints</param>
65    /// <returns>Returns a collection of bin packing 3d objects. Each object represents a bin and the packed items</returns>
66    public abstract IList<BinPacking3D> PackItems(Permutation sortedItems, PackingShape binShape, IList<PackingItem> items, ExtremePointCreationMethod epCreationMethod, bool useStackingConstraints);
67   
68    /// <summary>
69    /// Pack a given item into a given bin and updates the residual space and the extreme points
70    /// </summary>
71    /// <param name="packingBin"></param>
72    /// <param name="itemId"></param>
73    /// <param name="packingItem"></param>
74    /// <param name="position"></param>
75    protected virtual void PackItem(BinPacking3D packingBin, int itemId, PackingItem packingItem, PackingPosition position, IExtremePointCreator extremePointCreator, bool useStackingConstraints) {
76      if (!CheckItemDimensions(packingBin, packingItem, position)) {
77        throw new BinPacking3DException($"The dimensions of the given item exceeds the bin dimensions. " +
78          $"Bin: ({packingBin.BinShape.Width} {packingBin.BinShape.Depth} {packingBin.BinShape.Height})" +
79          $"Item: ({packingItem.Width} {packingItem.Depth} {packingItem.Height})");
80      }
81      packingBin.PackItem(itemId, packingItem, position);
82      extremePointCreator.UpdateBinPacking(packingBin, packingItem, position);
83    }
84       
85    /// <summary>
86    /// This method tries to find a valid packing position for an given item in a given packing bin
87    /// </summary>
88    /// <param name="packingBin"></param>
89    /// <param name="packingItem"></param>
90    /// <param name="useStackingConstraints"></param>
91    /// <param name="rotated"></param>
92    /// <returns>Returns the packing position for an given item. If there could not be found a valid position it returns null</returns>
93    protected PackingPosition FindPackingPositionForItem(BinPacking3D packingBin, PackingItem packingItem, bool useStackingConstraints, bool rotated) {
94      PackingItem newItem = new PackingItem(
95        rotated ? packingItem.Depth : packingItem.Width,
96        packingItem.Height,
97        rotated ? packingItem.Width : packingItem.Depth,
98        packingItem.TargetBin, packingItem.Weight, packingItem.Material);
99
100      // The extremepoints are sortet by Y / Z / X
101      return packingBin.ExtremePoints.Where(x => packingBin.IsPositionFeasible(newItem, x, useStackingConstraints)).FirstOrDefault();
102    }
103
104    /// <summary>
105    /// Compares the dimensions of a given item and the current bin.
106    /// </summary>
107    /// <param name="item"></param>
108    /// <returns>Returns true if the dimensions of an given item are less or equal to the bin.</returns>
109    private bool CheckItemDimensions(BinPacking3D packingBin, PackingItem item, PackingPosition itemPosition) {
110      var width = itemPosition.Rotated ? item.Depth : item.Width;
111      var depth = itemPosition.Rotated ? item.Width : item.Depth;
112      return packingBin.BinShape.Width >= width && packingBin.BinShape.Height >= item.Height && packingBin.BinShape.Depth >= depth;
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.