Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2817:

  • The items can be rotated and tilted now.
  • Added pruning of extreme points in packed bins.
  • Added new packer which packs items by positioning them on the point with the minimum of wasted space. He uses rotating and tilting of items.
  • Added classes for sorting given items.
File size: 4.3 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 BinPackerFirstFit : BinPacker {
[15488]36    #region Constructors for HEAL
37    [StorableConstructor]
38    protected BinPackerFirstFit(bool deserializing) : base(deserializing) { }
[15454]39
[15488]40    protected BinPackerFirstFit(BinPackerFirstFit original, Cloner cloner)
41      : base(original, cloner) {
42    }
43
44    public override IDeepCloneable Clone(Cloner cloner) {
45      return new BinPackerFirstFit(this, cloner);
46    }
47    #endregion
48
[15462]49    public BinPackerFirstFit() : base() { }   
[15454]50
51    /// <summary>
[15471]52    /// Packs the items of the object by using a first fit algorithm into an amount of bins and returns them.
[15454]53    /// </summary>
54    /// <returns>Returns a collection of bin packing 3d objects. Each object represents a bin and the packed items</returns>
[15617]55    public override IList<BinPacking3D> PackItems(Permutation sortedItems, PackingShape binShape, IList<PackingItem> items, ExtremePointCreationMethod epCreationMethod, ExtremePointPruningMethod epPruningMethod, bool useStackingConstraints) {
[15454]56      IList<BinPacking3D> packingList = new List<BinPacking3D>();
[15462]57      IList<int> remainingIds = new List<int>(sortedItems);
[15454]58
59      while (remainingIds.Count > 0) {
[15462]60        BinPacking3D packingBin = new BinPacking3D(binShape);
[15617]61        PackRemainingItems(ref remainingIds, ref packingBin, items, epCreationMethod, useStackingConstraints);
[15454]62        packingList.Add(packingBin);
63      }
64
[15617]65      ExtremePointPruningFactory.CreatePruning().PruneExtremePoints(epPruningMethod, packingList);
[15454]66      return packingList;
67    }
[15462]68
69    /// <summary>
70    /// Tries to pack the remainig items into a given BinPacking3D object. Each item could be packed into the BinPacking3D object will be removed from the list of remaining ids
71    /// </summary>
72    /// <param name="remainingIds">List of remaining ids. After the method has been executed the list has to have less items</param>
[15617]73    /// <param name="packingBin">This object will be filled with some of the given items</param>
[15462]74    /// <param name="items">List of packing items. Some of the items will be assigned to the BinPacking3D object</param>
[15617]75    /// <param name="epCreationMethod"></param>
76    /// <param name="useStackingConstraints"></param>
77    protected void PackRemainingItems(ref IList<int> remainingIds, ref BinPacking3D packingBin, IList<PackingItem> items, ExtremePointCreationMethod epCreationMethod, bool useStackingConstraints) {
[15488]78      IExtremePointCreator extremePointCreator = ExtremePointCreatorFactory.CreateExtremePointCreator(epCreationMethod, useStackingConstraints);
[15462]79      foreach (var itemId in new List<int>(remainingIds)) {
[15617]80        PackingPosition position = FindPackingPositionForItem(packingBin, items[itemId], useStackingConstraints);
[15462]81        // if a valid packing position could be found, the current item can be added to the given bin
82        if (position != null) {
[15488]83          PackItem(packingBin, itemId, items[itemId], position, extremePointCreator, useStackingConstraints);
[15462]84          remainingIds.Remove(itemId);
85        }
86      }
87    }
[15454]88  }
89}
Note: See TracBrowser for help on using the repository browser.