1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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 System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Problems.BinPacking3D {
|
---|
31 | [Item("Free Volume Best Fit Extreme-point Permutation Decoder (3d)", "Decodes the permutation and creates a packing solution candidate")]
|
---|
32 | [StorableClass]
|
---|
33 | public class FreeVolumeBestFitExtremePointPermutationDecoder : ExtremePointPermutationDecoderBase {
|
---|
34 |
|
---|
35 | [StorableConstructor]
|
---|
36 | protected FreeVolumeBestFitExtremePointPermutationDecoder(bool deserializing) : base(deserializing) { }
|
---|
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 | }
|
---|