Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2817:

  • Refactoring BinPackerFormClosure.cs -> BinPackerWidthBestFit.cs
  • Changes on the unit tests
File size: 3.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using HeuristicLab.Common;
7using HeuristicLab.Encodings.PermutationEncoding;
8using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
9using HeuristicLab.Problems.BinPacking3D.ExtremePointCreation;
10
11namespace HeuristicLab.Problems.BinPacking3D.Packer {
12  internal class BinPackerWidthBestFit : BinPackerMinRSLeft {
13
14    #region Constructors for HEAL
15    [StorableConstructor]
16    protected BinPackerWidthBestFit(bool deserializing) : base(deserializing) { }
17
18    public BinPackerWidthBestFit(BinPackerMinRSLeft original, Cloner cloner) : base(original, cloner) {
19    }
20    public override IDeepCloneable Clone(Cloner cloner) {
21      throw new NotImplementedException();
22    }
23    #endregion
24
25    public BinPackerWidthBestFit() : base() { }
26
27
28    protected override void PackRemainingItems(ref IList<int> remainingIds, ref BinPacking3D packingBin, IList<PackingItem> items, ExtremePointCreationMethod epCreationMethod, bool useStackingConstraints) {
29      IExtremePointCreator extremePointCreator = ExtremePointCreatorFactory.CreateExtremePointCreator(epCreationMethod, useStackingConstraints);
30      // If there are any items which width equals to the bin shape width or the result of a modulo division of the width is zero, these items will be packed first
31      //schauen, ob es einen gegenstand gibt, der die ganze breite ausfuellt oder bei einer division mit der breite ein rest von 0 rauskommt.
32      var binShape = packingBin.BinShape;
33      var placeable = remainingIds.ToList().Where(x => ItemFitsBinShapeWidth(x, items[x], binShape))
34                                           .OrderByDescending(x => items[x].Width)
35                                           .ThenByDescending(x => items[x].Height);
36      foreach (var itemId in placeable) {
37        var item = items[itemId];
38        if (TryToPack(packingBin, item, itemId, extremePointCreator, useStackingConstraints)) {
39          remainingIds.Remove(itemId);
40        }
41      }
42      base.PackRemainingItems(ref remainingIds, ref packingBin, items, epCreationMethod, useStackingConstraints);
43    }
44
45
46    private bool ItemFitsBinShapeWidth(int itemId, PackingItem item, PackingShape binShape) {
47      item.Rotated = false;
48      item.Tilted = false;
49      if (binShape.Width % item.Width == 0) {
50        return true;
51      }
52
53      if (item.RotateEnabled && !item.TiltEnabled) {
54        item.Rotated = true;
55        if (binShape.Width % item.Width == 0) {
56          return true;
57        }
58      }
59      if (!item.RotateEnabled && item.TiltEnabled) {
60        item.Rotated = false;
61        item.Tilted = true;
62        if (binShape.Width % item.Width == 0) {
63          return true;
64        }
65      }
66      if (item.RotateEnabled && item.TiltEnabled) {
67        item.Rotated = true;
68        item.Tilted = true;
69        if (binShape.Width % item.Width == 0) {
70          return true;
71        }
72      }
73      item.Rotated = false;
74      item.Tilted = false;
75      return false;
76    }
77
78    private bool TryToPack(BinPacking3D packingBin, PackingItem item, int itemId, IExtremePointCreator extremePointCreator, bool useStackingConstraints) {
79      var position = FindPackingPositionForItem(packingBin, item, useStackingConstraints);
80      if (position != null) {
81        PackItem(packingBin, itemId, item, position, extremePointCreator, useStackingConstraints);
82        packingBin.PackItem(itemId, item, position);
83        return true;
84      }
85      return false;
86    }
87  }
88}
Note: See TracBrowser for help on using the repository browser.