Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2839_HiveProjectManagement/HeuristicLab.Problems.BinPacking/3.3/3D/PermutationEncoding/ResidualSpaceBestFitExtremePointPermutationDecoder.cs @ 17991

Last change on this file since 17991 was 16057, checked in by jkarder, 6 years ago

#2839:

File size: 4.3 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Encodings.PermutationEncoding;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Problems.BinPacking3D {
31  [Item("Residual Space Best Fit Extreme-point Permutation Decoder (3d)", "Decodes the permutation and creates a packing solution candidate")]
32  [StorableClass]
33  public class ResidualSpaceBestFitExtremePointPermutationDecoder : ExtremePointPermutationDecoderBase {
34
35    [StorableConstructor]
36    protected ResidualSpaceBestFitExtremePointPermutationDecoder(bool deserializing) : base(deserializing) { }
37    protected ResidualSpaceBestFitExtremePointPermutationDecoder(ResidualSpaceBestFitExtremePointPermutationDecoder original, Cloner cloner)
38      : base(original, cloner) {
39    }
40    public ResidualSpaceBestFitExtremePointPermutationDecoder() : base() { }
41    public override IDeepCloneable Clone(Cloner cloner) {
42      return new ResidualSpaceBestFitExtremePointPermutationDecoder(this, cloner);
43    }
44
45    public override Solution Decode(Permutation permutation, PackingShape binShape, IList<PackingItem> items, bool useStackingConstraints) {
46      return Apply(permutation, binShape, items, useStackingConstraints);
47    }
48
49    public static Solution Apply(Permutation permutation, PackingShape binShape, IList<PackingItem> items, bool useStackingConstraints) {
50      Solution result = new Solution(binShape, useExtremePoints: true, stackingConstraints: useStackingConstraints);
51      IList<int> remainingIDs = new List<int>(permutation);
52      var bpg = new BinPacking3D(binShape);
53      bpg.ExtremePointBasedPacking(ref remainingIDs, items, stackingConstraints: useStackingConstraints);
54      result.Bins.Add(bpg);
55      foreach (int ID in remainingIDs) {
56        var item = items[ID];
57        var points = GetResidualSpaceAllPoints(result, item);
58        var sortedPoints = points.OrderBy(x => x.Item3);
59        var packed = false;
60        foreach (var p in sortedPoints) {
61          packed = p.Item1.PackItemIfFeasible(ID, item, p.Item2, useStackingConstraints);
62          if (packed) break;
63        }
64        if (!packed) {
65          // pack item in a new bin
66          var bp = new BinPacking3D(binShape);
67          var positionFound = bp.FindExtremePointForItem(item, false, useStackingConstraints);
68          if (positionFound != null) {
69            bp.PackItem(ID, item, positionFound);
70          } else throw new InvalidOperationException("Item " + ID + " cannot be packed in an empty bin.");
71          result.Bins.Add(bp);
72        }
73      }
74      result.UpdateBinPackings();
75      return result;
76    }
77
78    public static IList<Tuple<BinPacking3D, PackingPosition, int>> GetResidualSpaceAllPoints(Solution solution, PackingItem item) {
79      var result = new List<Tuple<BinPacking3D, PackingPosition, int>>();
80      foreach (BinPacking3D bp in solution.Bins) {
81        foreach (var ep in bp.ExtremePoints) {
82          var rs = bp.ResidualSpace[ep];
83          if (rs.Item1 < item.Width || rs.Item2 < item.Height || rs.Item3 < item.Depth) continue;
84          result.Add(Tuple.Create(bp, ep, GetResidualMerit(rs, item, ep)));
85        }
86      }
87      return result;
88    }
89
90    private static int GetResidualMerit(Tuple<int, int, int> rs, PackingItem item, PackingPosition ep) {
91      return ((rs.Item1 - item.Width) +
92          (rs.Item2 - item.Height) +
93          (rs.Item3 - item.Depth));
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.