1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2018 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 |
|
---|
22 | using HeuristicLab.Common;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
26 | using HeuristicLab.Problems.BinPacking3D;
|
---|
27 | using HeuristicLab.Problems.BinPacking3D.ExtremePointCreation;
|
---|
28 | using System;
|
---|
29 | using System.Collections.Generic;
|
---|
30 | using System.Linq;
|
---|
31 | using System.Text;
|
---|
32 | using System.Threading.Tasks;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Problems.BinPacking3D.Packer {
|
---|
35 | internal abstract class BinPacker : Item, IBinPacker {
|
---|
36 |
|
---|
37 | #region Constructors for HEAL
|
---|
38 |
|
---|
39 |
|
---|
40 | [StorableConstructor]
|
---|
41 | protected BinPacker(bool deserializing) : base(deserializing) { }
|
---|
42 |
|
---|
43 | protected BinPacker(BinPacker original, Cloner cloner)
|
---|
44 | : base(original, cloner) {
|
---|
45 | }
|
---|
46 |
|
---|
47 | #endregion
|
---|
48 |
|
---|
49 | public BinPacker() { }
|
---|
50 |
|
---|
51 |
|
---|
52 | /// <summary>
|
---|
53 | /// Packs all items of the bin packer and returns a collection of BinPacking3D objects
|
---|
54 | /// </summary>
|
---|
55 | /// <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>
|
---|
56 | /// <param name="binShape">Bin for storing the items</param>
|
---|
57 | /// <param name="items">A list of packing items which should be assigned to a bin</param>
|
---|
58 | /// <param name="useStackingConstraints">Flag for using stacking constraints</param>
|
---|
59 | /// <returns>Returns a collection of bin packing 3d objects. Each object represents a bin and the packed items</returns>
|
---|
60 | public abstract IList<BinPacking3D> PackItems(Permutation sortedItems, PackingShape binShape, IList<PackingItem> items, ExtremePointCreationMethod epCreationMethod, ExtremePointPruningMethod epPruningMethod, bool useStackingConstraints);
|
---|
61 |
|
---|
62 |
|
---|
63 |
|
---|
64 | public abstract void PackItemsToPackingList(IList<BinPacking3D> packingList, Permutation sortedItems, PackingShape binShape, IList<PackingItem> items, ExtremePointCreationMethod epCreationMethod, ExtremePointPruningMethod epPruningMethod, bool useStackingConstraints);
|
---|
65 |
|
---|
66 | /// <summary>
|
---|
67 | /// Pack a given item into a given bin and updates the residual space and the extreme points
|
---|
68 | /// </summary>
|
---|
69 | /// <param name="packingBin"></param>
|
---|
70 | /// <param name="itemId"></param>
|
---|
71 | /// <param name="packingItem"></param>
|
---|
72 | /// <param name="position"></param>
|
---|
73 | protected virtual void PackItem(BinPacking3D packingBin, int itemId, PackingItem packingItem, PackingPosition position, IExtremePointCreator extremePointCreator, bool useStackingConstraints) {
|
---|
74 | packingBin.PackItem(itemId, packingItem, position);
|
---|
75 | extremePointCreator.UpdateBinPacking(packingBin, packingItem, position);
|
---|
76 | }
|
---|
77 |
|
---|
78 | /// <summary>
|
---|
79 | /// This method tries to find a valid packing position for an given item in a given packing bin
|
---|
80 | /// </summary>
|
---|
81 | /// <param name="packingBin"></param>
|
---|
82 | /// <param name="packingItem"></param>
|
---|
83 | /// <param name="useStackingConstraints"></param>
|
---|
84 | /// <returns>Returns the packing position for an given item. If there could not be found a valid position it returns null</returns>
|
---|
85 | protected virtual PackingPosition FindPackingPositionForItem(BinPacking3D packingBin, PackingItem packingItem, bool useStackingConstraints) {
|
---|
86 | if (!CheckItemDimensions(packingBin, packingItem)) {
|
---|
87 | throw new BinPacking3DException($"The dimensions of the given item exceeds the bin dimensions. " +
|
---|
88 | $"Bin: ({packingBin.BinShape.Width} {packingBin.BinShape.Depth} {packingBin.BinShape.Height})" +
|
---|
89 | $"Item: ({packingItem.Width} {packingItem.Depth} {packingItem.Height})");
|
---|
90 | }
|
---|
91 |
|
---|
92 | PackingItem newItem = new PackingItem(
|
---|
93 | packingItem.Width,
|
---|
94 | packingItem.Height,
|
---|
95 | packingItem.Depth,
|
---|
96 | packingItem.TargetBin,
|
---|
97 | packingItem.Weight,
|
---|
98 | packingItem.SequenceGroup,
|
---|
99 | packingItem.Layer);
|
---|
100 |
|
---|
101 | // The extremepoints are sortet by Y / Z / X
|
---|
102 | var newPosition = packingBin.ExtremePoints.Where(x => packingBin.IsPositionFeasible(newItem, x.Key, useStackingConstraints)).FirstOrDefault();
|
---|
103 |
|
---|
104 | return newPosition.Key;
|
---|
105 | }
|
---|
106 |
|
---|
107 | /// <summary>
|
---|
108 | /// Compares the dimensions of a given item and the current bin.
|
---|
109 | /// </summary>
|
---|
110 | /// <param name="item"></param>
|
---|
111 | /// <returns>Returns true if the dimensions of an given item are less or equal to the bin.</returns>
|
---|
112 | protected virtual bool CheckItemDimensions(BinPacking3D packingBin, PackingItem item) {
|
---|
113 | var width = item.Width;
|
---|
114 | var depth = item.Depth;
|
---|
115 | return packingBin.BinShape.Width >= width && packingBin.BinShape.Height >= item.Height && packingBin.BinShape.Depth >= depth;
|
---|
116 | }
|
---|
117 |
|
---|
118 | /// <summary>
|
---|
119 | /// Clones a given list of packing items.
|
---|
120 | /// </summary>
|
---|
121 | /// <param name="items"></param>
|
---|
122 | /// <returns></returns>
|
---|
123 | protected static IList<PackingItem> CloneItems(IList<PackingItem> items) {
|
---|
124 | var clonedItems = new List<PackingItem>();
|
---|
125 | foreach (var item in items) {
|
---|
126 | clonedItems.Add(item.Clone() as PackingItem);
|
---|
127 | }
|
---|
128 | return clonedItems;
|
---|
129 | }
|
---|
130 |
|
---|
131 | }
|
---|
132 | }
|
---|