Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.BinPacking/3.3/3D/PermutationEncoding/FreeVolumeBestFitExtremePointPermutationDecoder.cs @ 16724

Last change on this file since 16724 was 16565, checked in by gkronber, 6 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

File size: 3.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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 HEAL.Attic;
29
30namespace HeuristicLab.Problems.BinPacking3D {
31  [Item("Free Volume Best Fit Extreme-point Permutation Decoder (3d)", "Decodes the permutation and creates a packing solution candidate")]
32  [StorableType("3CF690C8-EB29-40DC-ADE3-9B9A83928772")]
33  public class FreeVolumeBestFitExtremePointPermutationDecoder : ExtremePointPermutationDecoderBase {
34
35    [StorableConstructor]
36    protected FreeVolumeBestFitExtremePointPermutationDecoder(StorableConstructorFlag _) : base(_) { }
37    protected FreeVolumeBestFitExtremePointPermutationDecoder(FreeVolumeBestFitExtremePointPermutationDecoder original, Cloner cloner)
38      : base(original, cloner) {
39    }
40    public FreeVolumeBestFitExtremePointPermutationDecoder() : base() { }
41    public override IDeepCloneable Clone(Cloner cloner) {
42      return new FreeVolumeBestFitExtremePointPermutationDecoder(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 sortedBins = result.Bins.OrderBy(x => x.FreeVolume);
57        var item = items[ID];
58        var posFound = false;
59        foreach (var bp in sortedBins) {
60          var pos = bp.FindExtremePointForItem(item, false, useStackingConstraints);
61          posFound = pos != null;
62          if (posFound) {
63            bp.PackItem(ID, item, pos);
64            break;
65          }
66        }
67        if (!posFound) {
68          var bp = new BinPacking3D(binShape);
69          var pos = bp.FindExtremePointForItem(item, false, useStackingConstraints);
70          if (pos == null) throw new InvalidOperationException("Item " + ID + " cannot be packed in empty bin.");
71          bp.PackItem(ID, item, pos);
72          result.Bins.Add(bp);
73        }
74      }
75      result.UpdateBinPackings();
76      return result;
77    }
78  }
79}
Note: See TracBrowser for help on using the repository browser.