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.Encodings.PackingEncoding.PackingPlan;
|
---|
32 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
33 | using HeuristicLab.Common;
|
---|
34 | using HeuristicLab.Data;
|
---|
35 | using HeuristicLab.Collections;
|
---|
36 | using HeuristicLab.Problems.BinPacking.Interfaces;
|
---|
37 | using HeuristicLab.Encodings.PackingEncoding.GroupingVector;
|
---|
38 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
39 |
|
---|
40 | namespace HeuristicLab.Problems.BinPacking.Decoders {
|
---|
41 | [Item("Lowest Gap Fill Grouping-Vector Decoder", "<Description missing...>")]
|
---|
42 | [StorableClass]
|
---|
43 | public class IB2DLowestGapFillGroupingVectorDecoder : IdenticalBinPackingSolutionDecoder<
|
---|
44 | TwoDimensionalPacking,
|
---|
45 | RectangularPackingBin,
|
---|
46 | RectangularPackingItem,
|
---|
47 | PackingPlan<TwoDimensionalPacking, RectangularPackingBin, RectangularPackingItem>> {
|
---|
48 |
|
---|
49 | public IB2DLowestGapFillGroupingVectorDecoder()
|
---|
50 | : base() {
|
---|
51 | //EncodedSolutionParameter.ActualName = "EncodedSolution";
|
---|
52 | }
|
---|
53 | [StorableConstructor]
|
---|
54 | protected IB2DLowestGapFillGroupingVectorDecoder(bool deserializing) : base(deserializing) { }
|
---|
55 | protected IB2DLowestGapFillGroupingVectorDecoder(IB2DLowestGapFillGroupingVectorDecoder original, Cloner cloner)
|
---|
56 | : base(original, cloner) {
|
---|
57 | }
|
---|
58 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
59 | return new IB2DLowestGapFillGroupingVectorDecoder(this, cloner);
|
---|
60 | }
|
---|
61 |
|
---|
62 |
|
---|
63 | protected override PackingPlan<TwoDimensionalPacking, RectangularPackingBin, RectangularPackingItem> CreatePackingPlanFromEncoding(IPackingSolutionEncoding encodedSolution) {
|
---|
64 |
|
---|
65 | var solution = encodedSolution as IntegerVector;
|
---|
66 | if (solution == null) throw new InvalidOperationException("Encoding is not of type PackingSequence");
|
---|
67 |
|
---|
68 | var binMeasures = PackingBinMeasuresParameter.ActualValue;
|
---|
69 | var itemMeasures = PackingItemMeasuresParameter.ActualValue;
|
---|
70 |
|
---|
71 | ObservableDictionary<int, TwoDimensionalPacking> packingPositions = new ObservableDictionary<int, TwoDimensionalPacking>();
|
---|
72 |
|
---|
73 | int nrOfBins = solution.Max();
|
---|
74 |
|
---|
75 | //Get all indexes of items for every bin according to grouping-vector
|
---|
76 | //It is assumed, that at this point the item-indexes are sorted according to their item-size!
|
---|
77 | Dictionary<int, List<int>> unpackedItemIndexesPerBin = new Dictionary<int, List<int>>();
|
---|
78 |
|
---|
79 | for (int i = 0; i < nrOfBins; i++) {
|
---|
80 | unpackedItemIndexesPerBin[i] = solution
|
---|
81 | .Select((Value, Index) => new { Value, Index })
|
---|
82 | .Where (temp => temp.Value == i)
|
---|
83 | .Select (temp => temp.Index).ToList();
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 |
|
---|
88 | //Steps 1-6
|
---|
89 | //2
|
---|
90 | int currentBin = 0;
|
---|
91 |
|
---|
92 | //3
|
---|
93 | //Take the first position from the first bin
|
---|
94 | TwoDimensionalPacking currentPoint = new TwoDimensionalPacking(currentBin, 0, 0);
|
---|
95 |
|
---|
96 | //4
|
---|
97 | //Assuming that Height is always the shorter edge due to normalizing..
|
---|
98 | int currentGap = binMeasures.Height;
|
---|
99 |
|
---|
100 | //5
|
---|
101 | //Take the first (and largest) item for the first bin
|
---|
102 | int currentItemIndex = unpackedItemIndexesPerBin[currentBin][0];
|
---|
103 | RectangularPackingItem currentItem = itemMeasures[currentItemIndex];
|
---|
104 | packingPositions[currentItemIndex] = currentPoint;
|
---|
105 | //6
|
---|
106 | unpackedItemIndexesPerBin[currentBin].RemoveAt(0);
|
---|
107 |
|
---|
108 |
|
---|
109 | //Steps 7-13
|
---|
110 |
|
---|
111 | //TODO: the first parameter (bin-list) is not finished yet.. it should resemble the used bins in the packing plan..
|
---|
112 | PackingPlan<TwoDimensionalPacking, RectangularPackingBin, RectangularPackingItem> result =
|
---|
113 | new PackingPlan<TwoDimensionalPacking, RectangularPackingBin, RectangularPackingItem>(
|
---|
114 | binMeasures,
|
---|
115 | itemMeasures);
|
---|
116 |
|
---|
117 | result.PackingItemPositions = packingPositions;
|
---|
118 | return result;
|
---|
119 | }
|
---|
120 |
|
---|
121 | //protected override TwoDimensionalPacking FindFeasiblePositionForItem(RectangularPackingBin binMeasures, int binNr, RectangularPackingItem currentItem, ObservableDictionary<int, TwoDimensionalPacking> itemPositions, ItemList<RectangularPackingItem> itemMeasures) {
|
---|
122 | // //Starting-position at upper right corner (=left bottom point of item-rectangle is at position item.width,item.height)
|
---|
123 | // TwoDimensionalPacking currentPosition = new TwoDimensionalPacking(binNr, binMeasures.Width - currentItem.Width, binMeasures.Height - currentItem.Height);
|
---|
124 | // //Slide the item as far as possible to the bottom
|
---|
125 | // while (IsPositionFeasible(binMeasures, binNr, currentItem, MoveDown(currentPosition), itemPositions, itemMeasures)) {
|
---|
126 | // currentPosition = MoveDown(currentPosition);
|
---|
127 | // }
|
---|
128 | // //Slide the item as far as possible to the left
|
---|
129 | // while (IsPositionFeasible(binMeasures, binNr, currentItem, MoveLeft(currentPosition), itemPositions, itemMeasures)) {
|
---|
130 | // currentPosition = MoveLeft(currentPosition);
|
---|
131 | // }
|
---|
132 |
|
---|
133 | // return IsPositionFeasible(binMeasures, binNr, currentItem, currentPosition, itemPositions, itemMeasures) ? currentPosition : null;
|
---|
134 | //}
|
---|
135 |
|
---|
136 | //private bool IsPositionFeasible(RectangularPackingBin binMeasures, int binNr, RectangularPackingItem currentItem,
|
---|
137 | // TwoDimensionalPacking currentPosition, ObservableDictionary<int, TwoDimensionalPacking> itemPositions, ItemList<RectangularPackingItem> itemMeasures) {
|
---|
138 | // //In this case feasability is defined as following: 1. the item fits into the bin-borders; 2. the item does not collide with another already packed item
|
---|
139 | // if (!binMeasures.Encloses(currentPosition, currentItem))
|
---|
140 | // return false;
|
---|
141 | // var itemsWithPositions = itemMeasures.
|
---|
142 | // Select((Value, Index) => new { Value, Index }).
|
---|
143 | // Where(s => itemPositions.ContainsKey(s.Index)).
|
---|
144 | // Select((s) => new { Measures = itemMeasures[s.Index], Position = itemPositions[s.Index]});
|
---|
145 |
|
---|
146 | // foreach (var itemWithPosition in itemsWithPositions) {
|
---|
147 | // if (itemWithPosition.Position.AssignedBin == binNr && itemWithPosition.Measures.Overlaps(itemWithPosition.Position, currentPosition, currentItem))
|
---|
148 | // return false;
|
---|
149 | // }
|
---|
150 | // return true;
|
---|
151 | //}
|
---|
152 |
|
---|
153 | //private TwoDimensionalPacking MoveLeft(TwoDimensionalPacking original) {
|
---|
154 | // return new TwoDimensionalPacking(original.AssignedBin, original.X - 1, original.Y);
|
---|
155 | //}
|
---|
156 | //private TwoDimensionalPacking MoveDown(TwoDimensionalPacking original) {
|
---|
157 | // return new TwoDimensionalPacking(original.AssignedBin, original.X, original.Y - 1);
|
---|
158 | //}
|
---|
159 | }
|
---|
160 | }
|
---|