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 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Problems.BinPacking.PackingItem;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Common;
|
---|
29 | using HeuristicLab.Problems.BinPacking.Dimensions;
|
---|
30 | using HeuristicLab.Problems.BinPacking.PackingBin;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Encodings.PackingEncoding.PackingPlan {
|
---|
33 | [Item("BinPacking2D", "Represents a single-bin packing for a 2D bin-packing problem.")]
|
---|
34 | [StorableClass]
|
---|
35 | public class BinPacking2D : BinPacking<TwoDimensionalPacking, RectangularPackingBin, RectangularPackingItem> {
|
---|
36 |
|
---|
37 | public BinPacking2D(RectangularPackingBin binMeasures)
|
---|
38 | : base(binMeasures) {
|
---|
39 | ExtremePoints = new SortedSet<TwoDimensionalPacking>(new EPComparer2D());
|
---|
40 | ExtremePoints.Add(binMeasures.Origin);
|
---|
41 | }
|
---|
42 | [StorableConstructor]
|
---|
43 | protected BinPacking2D(bool deserializing) : base(deserializing) { }
|
---|
44 | protected BinPacking2D(BinPacking2D original, Cloner cloner)
|
---|
45 | : base(original, cloner) {
|
---|
46 | this.ExtremePoints = new SortedSet<TwoDimensionalPacking>(original.ExtremePoints, new EPComparer2D());
|
---|
47 | }
|
---|
48 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
49 | return new BinPacking2D(this, cloner);
|
---|
50 | }
|
---|
51 |
|
---|
52 | protected override void GenerateNewExtremePointsForNewItem(RectangularPackingItem newItem, TwoDimensionalPacking position) {
|
---|
53 |
|
---|
54 | int newWidth = position.Rotated ? newItem.Height : newItem.Width;
|
---|
55 | int newHeight = position.Rotated ? newItem.Width : newItem.Height;
|
---|
56 |
|
---|
57 | //Find ExtremePoints beginning from sourcepointX
|
---|
58 | var sourcePointX = new TwoDimensionalPacking(0, position.X + newWidth, position.Y);
|
---|
59 | if (sourcePointX.X < BinMeasures.Width && sourcePointX.Y < BinMeasures.Height) {
|
---|
60 | //Traversing down the y-axis
|
---|
61 | while (sourcePointX.Y > 0 && !IsPointOccupied(new TwoDimensionalPacking(0, sourcePointX.X, sourcePointX.Y - 1))) {
|
---|
62 | sourcePointX.Y--;
|
---|
63 | }
|
---|
64 | ExtremePoints.Add(new TwoDimensionalPacking(0, sourcePointX.X, sourcePointX.Y));
|
---|
65 | }
|
---|
66 |
|
---|
67 |
|
---|
68 |
|
---|
69 |
|
---|
70 | //Find ExtremePoints beginning from sourcepointY
|
---|
71 | var sourcePointY = new TwoDimensionalPacking(0, position.X, position.Y + newItem.Height);
|
---|
72 | if (sourcePointY.X < BinMeasures.Width && sourcePointY.Y < BinMeasures.Height) {
|
---|
73 | //Traversing down the x-axis
|
---|
74 | while (sourcePointY.X > 0 && !IsPointOccupied(new TwoDimensionalPacking(0, sourcePointY.X - 1, sourcePointY.Y))) {
|
---|
75 | sourcePointY.X--;
|
---|
76 | }
|
---|
77 | ExtremePoints.Add(new TwoDimensionalPacking(0, sourcePointY.X, sourcePointY.Y));
|
---|
78 | }
|
---|
79 |
|
---|
80 | //ExtremePoints.RemoveWhere(ep => IsPointOccupied(ep));
|
---|
81 |
|
---|
82 | //ExtremePoints = new HashSet<TwoDimensionalPacking>(ExtremePoints.
|
---|
83 | // OrderBy(ep => ep.X).
|
---|
84 | // ThenBy(ep => ep.Y).
|
---|
85 | // ThenBy(ep => ShortestPossibleSideFromPoint(ep)));
|
---|
86 | }
|
---|
87 |
|
---|
88 | public override TwoDimensionalPacking FindExtremePointForItem(RectangularPackingItem measures, bool rotated, bool stackingConstraints) {
|
---|
89 | RectangularPackingItem item = new RectangularPackingItem(
|
---|
90 | rotated ? measures.Height : measures.Width,
|
---|
91 | rotated ? measures.Width : measures.Height,
|
---|
92 | measures.TargetBin);
|
---|
93 |
|
---|
94 | int epIndex = 0;
|
---|
95 | while (epIndex < ExtremePoints.Count && (!IsPositionFeasible(item, ExtremePoints.ElementAt(epIndex)))) { epIndex++; }
|
---|
96 |
|
---|
97 | if (epIndex < ExtremePoints.Count) {
|
---|
98 | var result = ExtremePoints.ElementAt(epIndex);
|
---|
99 | result.Rotated = rotated;
|
---|
100 | return result;
|
---|
101 | }
|
---|
102 | return null;
|
---|
103 | }
|
---|
104 | public override TwoDimensionalPacking FindPositionBySliding(RectangularPackingItem measures, bool rotated) {
|
---|
105 | TwoDimensionalPacking currentPosition = new TwoDimensionalPacking(0,
|
---|
106 | BinMeasures.Width - (rotated ? measures.Height : measures.Width),
|
---|
107 | BinMeasures.Height - (rotated ? measures.Width : measures.Height), rotated);
|
---|
108 | //Slide the item as far as possible to the left
|
---|
109 | while (IsPositionFeasible(measures, TwoDimensionalPacking.MoveLeft(currentPosition))
|
---|
110 | || IsPositionFeasible(measures, TwoDimensionalPacking.MoveDown(currentPosition))) {
|
---|
111 | //Slide the item as far as possible to the bottom
|
---|
112 | while (IsPositionFeasible(measures, TwoDimensionalPacking.MoveDown(currentPosition))) {
|
---|
113 | currentPosition = TwoDimensionalPacking.MoveDown(currentPosition);
|
---|
114 | }
|
---|
115 | if (IsPositionFeasible(measures, TwoDimensionalPacking.MoveLeft(currentPosition)))
|
---|
116 | currentPosition = TwoDimensionalPacking.MoveLeft(currentPosition);
|
---|
117 | }
|
---|
118 |
|
---|
119 | return IsPositionFeasible(measures, currentPosition) ? currentPosition : null;
|
---|
120 | }
|
---|
121 |
|
---|
122 | public override void SlidingBasedPacking(ref List<int> sequence, ItemList<RectangularPackingItem> itemMeasures) {
|
---|
123 | var temp = new List<int>(sequence);
|
---|
124 | for (int i = 0; i < temp.Count; i++) {
|
---|
125 | var item = itemMeasures[temp[i]];
|
---|
126 | var position = FindPositionBySliding(item, false);
|
---|
127 | if (position != null) {
|
---|
128 | PackItem(temp[i], item, position);
|
---|
129 | sequence.Remove(temp[i]);
|
---|
130 | }
|
---|
131 | }
|
---|
132 | }
|
---|
133 | public override void SlidingBasedPacking(ref List<int> sequence, ItemList<RectangularPackingItem> itemMeasures, Dictionary<int, bool> rotationArray) {
|
---|
134 | var temp = new List<int>(sequence);
|
---|
135 | for (int i = 0; i < temp.Count; i++) {
|
---|
136 | var item = itemMeasures[temp[i]];
|
---|
137 | var position = FindPositionBySliding(item, rotationArray[temp[i]]);
|
---|
138 | if (position != null) {
|
---|
139 | PackItem(temp[i], item, position);
|
---|
140 | sequence.Remove(temp[i]);
|
---|
141 | }
|
---|
142 | }
|
---|
143 | }
|
---|
144 | public override void ExtremePointBasedPacking(ref List<int> sequence, ItemList<RectangularPackingItem> itemMeasures, bool stackingConstraints) {
|
---|
145 | var temp = new List<int>(sequence);
|
---|
146 | foreach (int itemID in temp) {
|
---|
147 | var item = itemMeasures[itemID];
|
---|
148 | var positionFound = FindExtremePointForItem(item, false, false);
|
---|
149 | if (positionFound != null) {
|
---|
150 | PackItem(itemID, item, positionFound);
|
---|
151 | sequence.Remove(itemID);
|
---|
152 | }
|
---|
153 | }
|
---|
154 | }
|
---|
155 | public override void ExtremePointBasedPacking(ref List<int> sequence, ItemList<RectangularPackingItem> itemMeasures, bool stackingConstraints, Dictionary<int, bool> rotationArray) {
|
---|
156 | var temp = new List<int>(sequence);
|
---|
157 | foreach (int itemID in temp) {
|
---|
158 | var item = itemMeasures[itemID];
|
---|
159 | var positionFound = FindExtremePointForItem(item, rotationArray[itemID], false);
|
---|
160 | if (positionFound != null) {
|
---|
161 | PackItem(itemID, item, positionFound);
|
---|
162 | sequence.Remove(itemID);
|
---|
163 | }
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 | public override int ShortestPossibleSideFromPoint(TwoDimensionalPacking position) {
|
---|
168 | int shortestSide = int.MaxValue;
|
---|
169 | int width = BinMeasures.Width;
|
---|
170 | int height = BinMeasures.Height;
|
---|
171 |
|
---|
172 | if (position.X >= width || position.Y >= height)
|
---|
173 | return shortestSide;
|
---|
174 |
|
---|
175 | TwoDimensionalPacking current = new TwoDimensionalPacking(0, position.X, position.Y);
|
---|
176 | while (current.X < width && IsPointOccupied(current)) { current = TwoDimensionalPacking.MoveRight(current); }
|
---|
177 | if (current.X - position.X < shortestSide)
|
---|
178 | shortestSide = current.X - position.X;
|
---|
179 |
|
---|
180 |
|
---|
181 | current = new TwoDimensionalPacking(0, position.X, position.Y);
|
---|
182 | while (current.Y < height && IsPointOccupied(current)) { current = TwoDimensionalPacking.MoveUp(current); }
|
---|
183 | if (current.Y - position.Y < shortestSide)
|
---|
184 | shortestSide = current.Y - position.Y;
|
---|
185 |
|
---|
186 | return shortestSide;
|
---|
187 | }
|
---|
188 | public override bool IsStaticStable(RectangularPackingItem item, TwoDimensionalPacking position) {
|
---|
189 | throw new NotImplementedException();
|
---|
190 | }
|
---|
191 |
|
---|
192 |
|
---|
193 | protected override void InitializeOccupationLayers() {
|
---|
194 | for (int i = 0; i * 10 <= BinMeasures.Width; i += 1) {
|
---|
195 | OccupationLayers[i] = new List<int>();
|
---|
196 | }
|
---|
197 | }
|
---|
198 | protected override void AddNewItemToOccupationLayers(int itemID, RectangularPackingItem measures, TwoDimensionalPacking position) {
|
---|
199 | int x1 = position.X / 10;
|
---|
200 | int x2 = (position.X + (position.Rotated ? measures.Height : measures.Width)) / 10;
|
---|
201 |
|
---|
202 | for (int i = x1; i <= x2; i++)
|
---|
203 | OccupationLayers[i].Add(itemID);
|
---|
204 | }
|
---|
205 | protected override List<int> GetLayerItemIDs(TwoDimensionalPacking position) {
|
---|
206 | return OccupationLayers[position.X / 10];
|
---|
207 | }
|
---|
208 | protected override List<int> GetLayerItemIDs(RectangularPackingItem measures, TwoDimensionalPacking position) {
|
---|
209 | List<int> result = new List<int>();
|
---|
210 | int x1 = position.X / 10;
|
---|
211 | int x2 = (position.X + (position.Rotated ? measures.Height : measures.Width)) / 10;
|
---|
212 |
|
---|
213 | for (int i = x1; i <= x2; i++)
|
---|
214 | result.AddRange(OccupationLayers[i]);
|
---|
215 |
|
---|
216 | return result;
|
---|
217 | }
|
---|
218 | }
|
---|
219 | public class EPComparer2D : IComparer<TwoDimensionalPacking> {
|
---|
220 | public int Compare(TwoDimensionalPacking a, TwoDimensionalPacking b) {
|
---|
221 | int result = a.X.CompareTo(b.X);
|
---|
222 | if (result == 0)
|
---|
223 | result = a.Y.CompareTo(b.Y);
|
---|
224 |
|
---|
225 | return result;
|
---|
226 | }
|
---|
227 | }
|
---|
228 | }
|
---|