Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.BinPacking/old files/ISOContainerMultiComponentVectorDecoder3D.cs @ 14615

Last change on this file since 14615 was 14042, checked in by gkronber, 8 years ago

#1966: moved obsolete files into a separate folder

File size: 5.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Joseph Helm and 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
23using System;
24using System.Collections.Generic;
25using System.Linq;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Core;
28using HeuristicLab.Problems.BinPacking.Dimensions;
29using HeuristicLab.Problems.BinPacking.PackingBin;
30using HeuristicLab.Problems.BinPacking.PackingItem;
31using HeuristicLab.Encodings.PackingEncoding.PackingPlan;
32using HeuristicLab.Common;
33using HeuristicLab.Collections;
34using HeuristicLab.Problems.BinPacking.Interfaces;
35using HeuristicLab.Encodings.PackingEncoding.MultiComponentVector;
36
37namespace HeuristicLab.Problems.BinPacking.Decoders {
38  [Item("ISO container decoder for the MultiComponentVector encoding.", "<Description missing...>")]
39  [StorableClass]
40  public class ISOContainerMultiComponentVectorDecoder3D : PackingSolutionDecoder<
41      ThreeDimensionalPacking,
42      CuboidPackingBin,
43      CuboidPackingItem>, I3DMCVDecoder {
44
45    public ISOContainerMultiComponentVectorDecoder3D ()
46      : base() {
47    }
48    [StorableConstructor]
49    protected ISOContainerMultiComponentVectorDecoder3D (bool deserializing) : base(deserializing) { }
50    protected ISOContainerMultiComponentVectorDecoder3D(ISOContainerMultiComponentVectorDecoder3D original, Cloner cloner)
51      : base(original, cloner) {
52    }
53    public override IDeepCloneable Clone(Cloner cloner) {
54      return new ISOContainerMultiComponentVectorDecoder3D(this, cloner);
55    }
56
57    public static PackingPlan<ThreeDimensionalPacking, CuboidPackingBin, CuboidPackingItem> Decode(MultiComponentVectorEncoding solution, CuboidPackingBin binMeasures, ItemList<CuboidPackingItem> itemMeasures) {
58      PackingPlan<ThreeDimensionalPacking, CuboidPackingBin, CuboidPackingItem> resultPlan = new PackingPlan3D(binMeasures);
59      ObservableList<BinPacking3D> result = new ObservableList<BinPacking3D>();
60
61      var sequenceMatrix = solution.GenerateSequenceMatrix();
62      Dictionary<int, bool> rotated = solution.GenerateRotationArray();
63
64      //Fill bins according to grouping vector
65      List<int> remainingIDs = new List<int>();
66      foreach (var sequence in sequenceMatrix) {
67        remainingIDs = remainingIDs.Concat(sequence).ToList();
68        var bp = new BinPacking3D(binMeasures);
69        bp.ExtremePointBasedPacking(ref remainingIDs, itemMeasures, true, rotated);
70        if (remainingIDs.Count > 0) {
71          bp.DoubleDepth();
72          bp.ExtremePointBasedPacking(ref remainingIDs, itemMeasures, true, rotated);
73        }
74        result.Add(bp);
75      }
76      result.RemoveAll(x => x.ItemPositions.Count == 0);
77      result = new ObservableList<BinPacking3D>(result.OrderByDescending(bp => bp.PackingDensity));
78
79      //Try to put remaining items in existing bins
80      var temp = new List<int>(remainingIDs);
81      foreach (int id in temp) {
82        foreach (var bp in result) {
83          var position = bp.FindExtremePointForItem(itemMeasures[id], rotated[id], true);
84          if (remainingIDs.Count > 0) {
85            bp.DoubleDepth();
86            position = bp.FindExtremePointForItem(itemMeasures[id], rotated[id], true);
87          }
88          if (position != null) {
89            bp.PackItem(id, itemMeasures[id], position);
90            remainingIDs.Remove(id);
91          }
92        }
93      }
94
95      //Put still remaining items in new bins
96      while (remainingIDs.Count > 0) {
97        var bp = new BinPacking3D(binMeasures);
98        bp.ExtremePointBasedPacking(ref remainingIDs, itemMeasures, true, rotated);
99        if (remainingIDs.Count > 0) {
100          bp.DoubleDepth();
101          bp.ExtremePointBasedPacking(ref remainingIDs, itemMeasures, true, rotated);
102        }
103        result.Add(bp);
104      }
105      result.RemoveAll(x => x.ItemPositions.Count == 0);
106      resultPlan.BinPackings = new ObservableList<BinPacking<ThreeDimensionalPacking, CuboidPackingBin, CuboidPackingItem>>(result.OrderByDescending(bp => bp.PackingDensity));
107      return resultPlan;
108    }
109
110    public override PackingPlan<ThreeDimensionalPacking, CuboidPackingBin, CuboidPackingItem> CreatePackingPlanFromEncoding(IItem encodedSolution, CuboidPackingBin binMeasures, ItemList<CuboidPackingItem> itemMeasures) {
111      var solution = encodedSolution as MultiComponentVectorEncoding;
112      if (solution == null) throw new InvalidOperationException("Encoding is not of type MultiComponentVector");
113      return Decode(solution, binMeasures, itemMeasures);
114    }
115  }
116}
Note: See TracBrowser for help on using the repository browser.