Changeset 14048 for branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.3D/3.3/BinPacking3D.cs
- Timestamp:
- 07/12/16 19:54:35 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.3D/3.3/BinPacking3D.cs
r14046 r14048 30 30 [Item("BinPacking3D", "Represents a single-bin packing for a 3D bin-packing problem.")] 31 31 [StorableClass] 32 public class BinPacking3D : BinPacking< ThreeDimensionalPacking, CuboidPackingShape, CuboidPackingItem> {32 public class BinPacking3D : BinPacking<PackingPosition, CuboidPackingShape, CuboidPackingItem> { 33 33 34 34 public BinPacking3D(CuboidPackingShape binMeasures) 35 35 : base(binMeasures) { 36 ExtremePoints = new SortedSet< ThreeDimensionalPacking>(new EPComparer3D());36 ExtremePoints = new SortedSet<PackingPosition>(new EPComparer3D()); 37 37 ExtremePoints.Add(binMeasures.Origin); 38 38 } … … 42 42 : base(original, cloner) { 43 43 this.depthWasDoubled = original.depthWasDoubled; 44 this.ExtremePoints = new SortedSet< ThreeDimensionalPacking>(original.ExtremePoints, new EPComparer3D());44 this.ExtremePoints = new SortedSet<PackingPosition>(original.ExtremePoints, new EPComparer3D()); 45 45 } 46 46 public override IDeepCloneable Clone(Cloner cloner) { … … 48 48 } 49 49 50 protected override void GenerateNewExtremePointsForNewItem(CuboidPackingItem newItem, ThreeDimensionalPackingposition) {50 protected override void GenerateNewExtremePointsForNewItem(CuboidPackingItem newItem, PackingPosition position) { 51 51 int newWidth = position.Rotated ? newItem.Depth : newItem.Width; 52 52 int newDepth = position.Rotated ? newItem.Width : newItem.Depth; 53 53 54 54 //Find ExtremePoints beginning from sourcepointX 55 var sourcePointX = new ThreeDimensionalPacking(0, position.X + newWidth, position.Y, position.Z);55 var sourcePointX = new PackingPosition(0, position.X + newWidth, position.Y, position.Z); 56 56 if (sourcePointX.X < BinMeasures.Width && sourcePointX.Y < BinMeasures.Height && sourcePointX.Z < BinMeasures.Depth) { 57 57 //Traversing down the y-axis 58 ThreeDimensionalPacking current = new ThreeDimensionalPacking(0, sourcePointX.X, sourcePointX.Y, sourcePointX.Z);59 while (current.Y > 0 && !IsPointOccupied( ThreeDimensionalPacking.MoveDown(current))) {60 current = ThreeDimensionalPacking.MoveDown(current);61 } 62 ExtremePoints.Add(( ThreeDimensionalPacking)current.Clone());63 while (current.X > 0 && !IsPointOccupied( ThreeDimensionalPacking.MoveLeft(current))) {64 current = ThreeDimensionalPacking.MoveLeft(current);58 PackingPosition current = new PackingPosition(0, sourcePointX.X, sourcePointX.Y, sourcePointX.Z); 59 while (current.Y > 0 && !IsPointOccupied(PackingPosition.MoveDown(current))) { 60 current = PackingPosition.MoveDown(current); 61 } 62 ExtremePoints.Add((PackingPosition)current.Clone()); 63 while (current.X > 0 && !IsPointOccupied(PackingPosition.MoveLeft(current))) { 64 current = PackingPosition.MoveLeft(current); 65 65 } 66 66 ExtremePoints.Add(current); 67 67 68 68 //Traversing down the z-axis 69 current = new ThreeDimensionalPacking(0, sourcePointX.X, sourcePointX.Y, sourcePointX.Z);70 while (current.Z > 0 && !IsPointOccupied( ThreeDimensionalPacking.MoveBack(current))) {71 current = ThreeDimensionalPacking.MoveBack(current);72 } 73 ExtremePoints.Add(( ThreeDimensionalPacking)current.Clone());74 while (current.Y > 0 && !IsPointOccupied( ThreeDimensionalPacking.MoveDown(current))) {75 current = ThreeDimensionalPacking.MoveDown(current);69 current = new PackingPosition(0, sourcePointX.X, sourcePointX.Y, sourcePointX.Z); 70 while (current.Z > 0 && !IsPointOccupied(PackingPosition.MoveBack(current))) { 71 current = PackingPosition.MoveBack(current); 72 } 73 ExtremePoints.Add((PackingPosition)current.Clone()); 74 while (current.Y > 0 && !IsPointOccupied(PackingPosition.MoveDown(current))) { 75 current = PackingPosition.MoveDown(current); 76 76 } 77 77 ExtremePoints.Add(current); … … 82 82 83 83 //Find ExtremePoints beginning from sourcepointY 84 var sourcePointY = new ThreeDimensionalPacking(0, position.X, position.Y + newItem.Height, position.Z);84 var sourcePointY = new PackingPosition(0, position.X, position.Y + newItem.Height, position.Z); 85 85 if (sourcePointY.X < BinMeasures.Width && sourcePointY.Y < BinMeasures.Height && sourcePointY.Z < BinMeasures.Depth) { 86 86 //Traversing down the x-axis 87 ThreeDimensionalPacking current = new ThreeDimensionalPacking(0, sourcePointY.X, sourcePointY.Y, sourcePointY.Z);88 while (current.X > 0 && !IsPointOccupied( ThreeDimensionalPacking.MoveLeft(current))) {89 current = ThreeDimensionalPacking.MoveLeft(current);90 } 91 ExtremePoints.Add(( ThreeDimensionalPacking)current.Clone());92 while (current.Y > 0 && !IsPointOccupied( ThreeDimensionalPacking.MoveDown(current))) {93 current = ThreeDimensionalPacking.MoveDown(current);87 PackingPosition current = new PackingPosition(0, sourcePointY.X, sourcePointY.Y, sourcePointY.Z); 88 while (current.X > 0 && !IsPointOccupied(PackingPosition.MoveLeft(current))) { 89 current = PackingPosition.MoveLeft(current); 90 } 91 ExtremePoints.Add((PackingPosition)current.Clone()); 92 while (current.Y > 0 && !IsPointOccupied(PackingPosition.MoveDown(current))) { 93 current = PackingPosition.MoveDown(current); 94 94 } 95 95 ExtremePoints.Add(current); 96 96 97 97 //Traversing down the z-axis 98 current = new ThreeDimensionalPacking(0, sourcePointY.X, sourcePointY.Y, sourcePointY.Z);99 while (current.Z > 0 && !IsPointOccupied( ThreeDimensionalPacking.MoveBack(current))) {100 current = ThreeDimensionalPacking.MoveBack(current);101 } 102 ExtremePoints.Add(( ThreeDimensionalPacking)current.Clone());103 while (current.Y > 0 && !IsPointOccupied( ThreeDimensionalPacking.MoveDown(current))) {104 current = ThreeDimensionalPacking.MoveDown(current);98 current = new PackingPosition(0, sourcePointY.X, sourcePointY.Y, sourcePointY.Z); 99 while (current.Z > 0 && !IsPointOccupied(PackingPosition.MoveBack(current))) { 100 current = PackingPosition.MoveBack(current); 101 } 102 ExtremePoints.Add((PackingPosition)current.Clone()); 103 while (current.Y > 0 && !IsPointOccupied(PackingPosition.MoveDown(current))) { 104 current = PackingPosition.MoveDown(current); 105 105 } 106 106 ExtremePoints.Add(current); … … 112 112 113 113 //Find ExtremePoints beginning from sourcepointZ 114 var sourcePointZ = new ThreeDimensionalPacking(0, position.X, position.Y, position.Z + newDepth);114 var sourcePointZ = new PackingPosition(0, position.X, position.Y, position.Z + newDepth); 115 115 if (sourcePointZ.X < BinMeasures.Width && sourcePointZ.Y < BinMeasures.Height && sourcePointZ.Z < BinMeasures.Depth) { 116 116 //Traversing down the x-axis 117 ThreeDimensionalPacking current = new ThreeDimensionalPacking(0, sourcePointZ.X, sourcePointZ.Y, sourcePointZ.Z);118 while (current.X > 0 && !IsPointOccupied( ThreeDimensionalPacking.MoveLeft(current))) {119 current = ThreeDimensionalPacking.MoveLeft(current);120 } 121 ExtremePoints.Add(( ThreeDimensionalPacking)current.Clone());122 while (current.Y > 0 && !IsPointOccupied( ThreeDimensionalPacking.MoveDown(current))) {123 current = ThreeDimensionalPacking.MoveDown(current);117 PackingPosition current = new PackingPosition(0, sourcePointZ.X, sourcePointZ.Y, sourcePointZ.Z); 118 while (current.X > 0 && !IsPointOccupied(PackingPosition.MoveLeft(current))) { 119 current = PackingPosition.MoveLeft(current); 120 } 121 ExtremePoints.Add((PackingPosition)current.Clone()); 122 while (current.Y > 0 && !IsPointOccupied(PackingPosition.MoveDown(current))) { 123 current = PackingPosition.MoveDown(current); 124 124 } 125 125 ExtremePoints.Add(current); 126 126 127 127 //Traversing down the y-axis 128 current = new ThreeDimensionalPacking(0, sourcePointZ.X, sourcePointZ.Y, sourcePointZ.Z);129 while (current.Y > 0 && !IsPointOccupied( ThreeDimensionalPacking.MoveDown(current))) {130 current = ThreeDimensionalPacking.MoveDown(current);131 } 132 ExtremePoints.Add(( ThreeDimensionalPacking)current.Clone());133 while (current.X > 0 && !IsPointOccupied( ThreeDimensionalPacking.MoveLeft(current))) {134 current = ThreeDimensionalPacking.MoveLeft(current);128 current = new PackingPosition(0, sourcePointZ.X, sourcePointZ.Y, sourcePointZ.Z); 129 while (current.Y > 0 && !IsPointOccupied(PackingPosition.MoveDown(current))) { 130 current = PackingPosition.MoveDown(current); 131 } 132 ExtremePoints.Add((PackingPosition)current.Clone()); 133 while (current.X > 0 && !IsPointOccupied(PackingPosition.MoveLeft(current))) { 134 current = PackingPosition.MoveLeft(current); 135 135 } 136 136 ExtremePoints.Add(current); … … 146 146 } 147 147 148 public override ThreeDimensionalPackingFindExtremePointForItem(CuboidPackingItem measures, bool rotated, bool stackingConstraints) {148 public override PackingPosition FindExtremePointForItem(CuboidPackingItem measures, bool rotated, bool stackingConstraints) { 149 149 150 150 CuboidPackingItem item = new CuboidPackingItem( … … 164 164 if (epIndex < ExtremePoints.Count) { 165 165 var origPoint = ExtremePoints.ElementAt(epIndex); 166 var result = new ThreeDimensionalPacking(origPoint.AssignedBin, origPoint.X, origPoint.Y, origPoint.Z, rotated);166 var result = new PackingPosition(origPoint.AssignedBin, origPoint.X, origPoint.Y, origPoint.Z, rotated); 167 167 return result; 168 168 } … … 170 170 } 171 171 172 public override ThreeDimensionalPackingFindPositionBySliding(CuboidPackingItem measures, bool rotated) {172 public override PackingPosition FindPositionBySliding(CuboidPackingItem measures, bool rotated) { 173 173 //Starting-position at upper right corner (=left bottom point of item-rectangle is at position item.width,item.height) 174 ThreeDimensionalPacking currentPosition = new ThreeDimensionalPacking(0,174 PackingPosition currentPosition = new PackingPosition(0, 175 175 BinMeasures.Width - (rotated ? measures.Depth : measures.Width), 176 176 BinMeasures.Height - measures.Height, 177 177 BinMeasures.Depth - (rotated ? measures.Width : measures.Depth), rotated); 178 178 //Slide the item as far as possible to the bottom 179 while (IsPositionFeasible(measures, ThreeDimensionalPacking.MoveDown(currentPosition))180 || IsPositionFeasible(measures, ThreeDimensionalPacking.MoveLeft(currentPosition))181 || IsPositionFeasible(measures, ThreeDimensionalPacking.MoveBack(currentPosition))) {179 while (IsPositionFeasible(measures, PackingPosition.MoveDown(currentPosition)) 180 || IsPositionFeasible(measures, PackingPosition.MoveLeft(currentPosition)) 181 || IsPositionFeasible(measures, PackingPosition.MoveBack(currentPosition))) { 182 182 //Slide the item as far as possible to the left 183 while (IsPositionFeasible(measures, ThreeDimensionalPacking.MoveLeft(currentPosition))184 || IsPositionFeasible(measures, ThreeDimensionalPacking.MoveBack(currentPosition))) {183 while (IsPositionFeasible(measures, PackingPosition.MoveLeft(currentPosition)) 184 || IsPositionFeasible(measures, PackingPosition.MoveBack(currentPosition))) { 185 185 //Slide the item as far as possible to the back 186 while (IsPositionFeasible(measures, ThreeDimensionalPacking.MoveBack(currentPosition))) {187 currentPosition = ThreeDimensionalPacking.MoveBack(currentPosition);186 while (IsPositionFeasible(measures, PackingPosition.MoveBack(currentPosition))) { 187 currentPosition = PackingPosition.MoveBack(currentPosition); 188 188 } 189 if (IsPositionFeasible(measures, ThreeDimensionalPacking.MoveLeft(currentPosition)))190 currentPosition = ThreeDimensionalPacking.MoveLeft(currentPosition);191 } 192 if (IsPositionFeasible(measures, ThreeDimensionalPacking.MoveDown(currentPosition)))193 currentPosition = ThreeDimensionalPacking.MoveDown(currentPosition);189 if (IsPositionFeasible(measures, PackingPosition.MoveLeft(currentPosition))) 190 currentPosition = PackingPosition.MoveLeft(currentPosition); 191 } 192 if (IsPositionFeasible(measures, PackingPosition.MoveDown(currentPosition))) 193 currentPosition = PackingPosition.MoveDown(currentPosition); 194 194 } 195 195 … … 242 242 } 243 243 244 public override int ShortestPossibleSideFromPoint( ThreeDimensionalPackingposition) {244 public override int ShortestPossibleSideFromPoint(PackingPosition position) { 245 245 246 246 int shortestSide = int.MaxValue; … … 252 252 return shortestSide; 253 253 254 ThreeDimensionalPacking current = new ThreeDimensionalPacking(0, position.X, position.Y, position.Z);255 while (current.X < width && IsPointOccupied(current)) { current = ThreeDimensionalPacking.MoveRight(current); }254 PackingPosition current = new PackingPosition(0, position.X, position.Y, position.Z); 255 while (current.X < width && IsPointOccupied(current)) { current = PackingPosition.MoveRight(current); } 256 256 if (current.X - position.X < shortestSide) 257 257 shortestSide = current.X - position.X; 258 258 259 259 260 current = new ThreeDimensionalPacking(0, position.X, position.Y, position.Z);261 while (current.Y < height && IsPointOccupied(current)) { current = ThreeDimensionalPacking.MoveUp(current); }260 current = new PackingPosition(0, position.X, position.Y, position.Z); 261 while (current.Y < height && IsPointOccupied(current)) { current = PackingPosition.MoveUp(current); } 262 262 if (current.Y - position.Y < shortestSide) 263 263 shortestSide = current.Y - position.Y; 264 264 265 265 266 current = new ThreeDimensionalPacking(0, position.X, position.Y, position.Z);267 while (current.Z < depth && IsPointOccupied(current)) { current = ThreeDimensionalPacking.MoveFront(current); }266 current = new PackingPosition(0, position.X, position.Y, position.Z); 267 while (current.Z < depth && IsPointOccupied(current)) { current = PackingPosition.MoveFront(current); } 268 268 if (current.Z - position.Z < shortestSide) 269 269 shortestSide = current.Z - position.Z; … … 271 271 return shortestSide; 272 272 } 273 public override bool IsStaticStable(CuboidPackingItem item, ThreeDimensionalPackingposition) {273 public override bool IsStaticStable(CuboidPackingItem item, PackingPosition position) { 274 274 //Static stability is given, if item is placed on the ground 275 275 if (position.Y == 0) 276 276 return true; 277 277 278 if (IsPointOccupied(new ThreeDimensionalPacking(0, position.X, position.Y - 1, position.Z))279 && IsPointOccupied(new ThreeDimensionalPacking(0, position.X + item.Width - 1, position.Y - 1, position.Z))280 && IsPointOccupied(new ThreeDimensionalPacking(0, position.X, position.Y - 1, position.Z + item.Depth - 1))281 && IsPointOccupied(new ThreeDimensionalPacking(0, position.X + item.Width - 1, position.Y - 1, position.Z + item.Depth - 1)))278 if (IsPointOccupied(new PackingPosition(0, position.X, position.Y - 1, position.Z)) 279 && IsPointOccupied(new PackingPosition(0, position.X + item.Width - 1, position.Y - 1, position.Z)) 280 && IsPointOccupied(new PackingPosition(0, position.X, position.Y - 1, position.Z + item.Depth - 1)) 281 && IsPointOccupied(new PackingPosition(0, position.X + item.Width - 1, position.Y - 1, position.Z + item.Depth - 1))) 282 282 return true; 283 283 … … 304 304 305 305 [Storable] 306 private bool depthWasDoubled = false; 306 private bool depthWasDoubled = false; // TODO ??? 307 307 public void DoubleDepth() { 308 308 if (!depthWasDoubled) { … … 314 314 } 315 315 316 public bool IsSupportedByAtLeastOnePoint(CuboidPackingItem item, ThreeDimensionalPackingposition) {316 public bool IsSupportedByAtLeastOnePoint(CuboidPackingItem item, PackingPosition position) { 317 317 if (position.Y == 0) 318 318 return true; … … 321 321 for (int x = position.X; x < position.X + item.Width; x++) 322 322 for (int z = position.Z; z < position.Z + item.Depth; z++) 323 if (IsPointOccupied(new ThreeDimensionalPacking(0, x, y, z)))323 if (IsPointOccupied(new PackingPosition(0, x, y, z))) 324 324 return true; 325 325 326 326 return false; 327 327 } 328 public bool IsWeightSupported(CuboidPackingItem item, ThreeDimensionalPackingep) {328 public bool IsWeightSupported(CuboidPackingItem item, PackingPosition ep) { 329 329 if (ep.Y == 0) 330 330 return true; 331 331 332 if (ItemMeasures[PointOccupation(new ThreeDimensionalPacking(0, ep.X, ep.Y - 1, ep.Z))].SupportsStacking(item)333 && ItemMeasures[PointOccupation(new ThreeDimensionalPacking(0, ep.X + item.Width - 1, ep.Y - 1, ep.Z))].SupportsStacking(item)334 && ItemMeasures[PointOccupation(new ThreeDimensionalPacking(0, ep.X, ep.Y - 1, ep.Z + item.Depth - 1))].SupportsStacking(item)335 && ItemMeasures[PointOccupation(new ThreeDimensionalPacking(0, ep.X + item.Width - 1, ep.Y - 1, ep.Z + item.Depth - 1))].SupportsStacking(item))332 if (ItemMeasures[PointOccupation(new PackingPosition(0, ep.X, ep.Y - 1, ep.Z))].SupportsStacking(item) 333 && ItemMeasures[PointOccupation(new PackingPosition(0, ep.X + item.Width - 1, ep.Y - 1, ep.Z))].SupportsStacking(item) 334 && ItemMeasures[PointOccupation(new PackingPosition(0, ep.X, ep.Y - 1, ep.Z + item.Depth - 1))].SupportsStacking(item) 335 && ItemMeasures[PointOccupation(new PackingPosition(0, ep.X + item.Width - 1, ep.Y - 1, ep.Z + item.Depth - 1))].SupportsStacking(item)) 336 336 return true; 337 337 … … 345 345 } 346 346 } 347 protected override void AddNewItemToOccupationLayers(int itemID, CuboidPackingItem measures, ThreeDimensionalPackingposition) {347 protected override void AddNewItemToOccupationLayers(int itemID, CuboidPackingItem measures, PackingPosition position) { 348 348 int z1 = position.Z / 10; 349 349 int z2 = (position.Z + (position.Rotated ? measures.Width : measures.Depth)) / 10; … … 352 352 OccupationLayers[i].Add(itemID); 353 353 } 354 protected override List<int> GetLayerItemIDs( ThreeDimensionalPackingposition) {354 protected override List<int> GetLayerItemIDs(PackingPosition position) { 355 355 return OccupationLayers[position.Z / 10]; 356 356 } 357 protected override List<int> GetLayerItemIDs(CuboidPackingItem measures, ThreeDimensionalPackingposition) {357 protected override List<int> GetLayerItemIDs(CuboidPackingItem measures, PackingPosition position) { 358 358 List<int> result = new List<int>(); 359 359 int z1 = position.Z / 10; … … 366 366 } 367 367 } 368 public class EPComparer3D : IComparer< ThreeDimensionalPacking> {369 public int Compare( ThreeDimensionalPacking a, ThreeDimensionalPackingb) {368 public class EPComparer3D : IComparer<PackingPosition> { 369 public int Compare(PackingPosition a, PackingPosition b) { 370 370 int result = a.Z.CompareTo(b.Z); 371 371 if (result == 0)
Note: See TracChangeset
for help on using the changeset viewer.