1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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 System.Text;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Problems.BinPacking.Dimensions;
|
---|
29 | using HeuristicLab.Problems.BinPacking.PackingBin;
|
---|
30 | using HeuristicLab.Problems.BinPacking.PackingItem;
|
---|
31 | using HeuristicLab.Problems.BinPacking.PackingPlans;
|
---|
32 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
33 | using HeuristicLab.Common;
|
---|
34 | using HeuristicLab.Data;
|
---|
35 | using HeuristicLab.Collections;
|
---|
36 | using HeuristicLab.Encodings.PackingEncoding.PackingSequence;
|
---|
37 | using HeuristicLab.Problems.BinPacking.Interfaces;
|
---|
38 | using HeuristicLab.Encodings.PackingEncoding.GroupingVector;
|
---|
39 |
|
---|
40 | namespace HeuristicLab.Problems.BinPacking.Decoders {
|
---|
41 | [Item("Identical bin two dimensional grouping vector decoder", "<Description missing...>")]
|
---|
42 | [StorableClass]
|
---|
43 | public class TwoDimensionalBottomLeftGroupingVectorDecoder : IdenticalBinPackingSolutionDecoder<
|
---|
44 | TwoDimensionalPacking,
|
---|
45 | RectangularPackingBin,
|
---|
46 | RectangularPackingItem,
|
---|
47 | PackingPlan<TwoDimensionalPacking, RectangularPackingBin, RectangularPackingItem>> {
|
---|
48 |
|
---|
49 | public TwoDimensionalBottomLeftGroupingVectorDecoder()
|
---|
50 | : base() {
|
---|
51 | //EncodedSolutionParameter.ActualName = "EncodedSolution";
|
---|
52 | }
|
---|
53 | [StorableConstructor]
|
---|
54 | protected TwoDimensionalBottomLeftGroupingVectorDecoder(bool deserializing) : base(deserializing) { }
|
---|
55 | protected TwoDimensionalBottomLeftGroupingVectorDecoder(TwoDimensionalBottomLeftGroupingVectorDecoder original, Cloner cloner)
|
---|
56 | : base(original, cloner) {
|
---|
57 | }
|
---|
58 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
59 | return new TwoDimensionalBottomLeftGroupingVectorDecoder(this, cloner);
|
---|
60 | }
|
---|
61 |
|
---|
62 |
|
---|
63 | protected override PackingPlan<TwoDimensionalPacking, RectangularPackingBin, RectangularPackingItem> CreatePackingPlanFromEncoding(IPackingSolutionEncoding encodedSolution) {
|
---|
64 |
|
---|
65 | var solution = encodedSolution as GroupingVectorEncoding;
|
---|
66 | if (solution == null) throw new InvalidOperationException("Encoding is not of type GroupingVector");
|
---|
67 |
|
---|
68 | RegularSimpleRotationPackingPlan<TwoDimensionalPacking, RectangularPackingBin, RectangularPackingItem> result =
|
---|
69 | new RegularSimpleRotationPackingPlan<TwoDimensionalPacking, RectangularPackingBin, RectangularPackingItem>(PackingBinMeasuresParameter.ActualValue, PackingItemMeasuresParameter.ActualValue);
|
---|
70 |
|
---|
71 | int nrOfBins = solution.GroupingVector.Max() + 1;
|
---|
72 |
|
---|
73 | //Get all indexes of items for every bin according to grouping-vector
|
---|
74 | //It is assumed, that at this point the item-indexes are sorted according to their item-size!
|
---|
75 | Dictionary<int, List<int>> unpackedItemIndexesPerBin = new Dictionary<int, List<int>>();
|
---|
76 |
|
---|
77 | for (int i = 0; i < nrOfBins; i++) {
|
---|
78 | unpackedItemIndexesPerBin[i] = solution.GroupingVector
|
---|
79 | .Select((Value, Index) => new { Value, Index })
|
---|
80 | .Where(temp => temp.Value == i)
|
---|
81 | .Select(temp => temp.Index).ToList();
|
---|
82 | }
|
---|
83 |
|
---|
84 | ObservableDictionary<int, TwoDimensionalPacking> packingPositions = new ObservableDictionary<int, TwoDimensionalPacking>();
|
---|
85 | var remainingItems = new List<int>();
|
---|
86 |
|
---|
87 | //Iterate over all bin-lists
|
---|
88 | for (int binNr = 0; binNr < nrOfBins; binNr++ ) {
|
---|
89 | //Iterate over the current bin-item-list and find feasible positions in the current bin for each item
|
---|
90 | var unpackedItems = unpackedItemIndexesPerBin [binNr];
|
---|
91 | for (int i = 0; i < unpackedItems.Count; i++ ) {
|
---|
92 | var itemIndex = unpackedItems[i];
|
---|
93 | var item = PackingItemMeasuresParameter.ActualValue[itemIndex];
|
---|
94 | TwoDimensionalPacking position = null;
|
---|
95 |
|
---|
96 | //Look for space in current bin
|
---|
97 | position = PackingHeuristics.BottomLeftPosition(PackingBinMeasuresParameter.ActualValue, binNr, item, packingPositions, PackingItemMeasuresParameter.ActualValue);
|
---|
98 |
|
---|
99 | //Did not find enough space in current bin
|
---|
100 | if (position == null) {
|
---|
101 | remainingItems.Add(itemIndex);
|
---|
102 | //if (binNr + 1 >= nrOfBins) {
|
---|
103 | // nrOfBins++;
|
---|
104 | // unpackedItemIndexesPerBin[binNr + 1] = new List<int>();
|
---|
105 | // unpackedItemIndexesPerBin[binNr + 1].Add(itemIndex);
|
---|
106 | // unpackedItemIndexesPerBin[binNr].Remove(itemIndex);
|
---|
107 | //} else {
|
---|
108 | // unpackedItemIndexesPerBin[binNr + 1].Add(itemIndex);
|
---|
109 | // unpackedItemIndexesPerBin[binNr + 1].Sort();
|
---|
110 | //}
|
---|
111 | //solution.GroupingVector[itemIndex] = binNr + 1;
|
---|
112 | } else
|
---|
113 | packingPositions[itemIndex] = position;
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | //Packing of remaining items
|
---|
118 | //Iterate over all bin-lists
|
---|
119 | for (int binNr = 0; binNr < nrOfBins && remainingItems.Count > 0; binNr++) {
|
---|
120 | var unpackedItems = new List<int>(remainingItems);
|
---|
121 | for (int i = 0; i < unpackedItems.Count; i++) {
|
---|
122 | var itemIndex = unpackedItems[i];
|
---|
123 | var item = PackingItemMeasuresParameter.ActualValue[itemIndex];
|
---|
124 | TwoDimensionalPacking position = null;
|
---|
125 |
|
---|
126 | //Look for space in current bin
|
---|
127 | position = PackingHeuristics.BottomLeftPosition(PackingBinMeasuresParameter.ActualValue, binNr, item, packingPositions, PackingItemMeasuresParameter.ActualValue);
|
---|
128 |
|
---|
129 | if (position != null) {
|
---|
130 | packingPositions[itemIndex] = position;
|
---|
131 | remainingItems.Remove(itemIndex);
|
---|
132 | solution.GroupingVector[itemIndex] = binNr;
|
---|
133 | }
|
---|
134 | }
|
---|
135 | if (remainingItems.Count > 0 && binNr + 1 >= nrOfBins) {
|
---|
136 | nrOfBins++;
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | result.PackingItemPositions = packingPositions;
|
---|
141 |
|
---|
142 |
|
---|
143 | return result;
|
---|
144 | }
|
---|
145 | }
|
---|
146 | }
|
---|