Changeset 14154 for branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.3D/3.3/BinPacking3D.cs
- Timestamp:
- 07/21/16 15:08:21 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.3D/3.3/BinPacking3D.cs
r14153 r14154 32 32 public class BinPacking3D : BinPacking<PackingPosition, PackingShape, PackingItem> { 33 33 34 public BinPacking3D(PackingShape bin Measures)35 : base(bin Measures) {34 public BinPacking3D(PackingShape binShape) 35 : base(binShape) { 36 36 ExtremePoints = new SortedSet<PackingPosition>(new EPComparer3D()); 37 ExtremePoints.Add(binMeasures.Origin); 37 ExtremePoints.Add(binShape.Origin); 38 InitializeOccupationLayers(); 38 39 } 39 40 [StorableConstructor] … … 41 42 protected BinPacking3D(BinPacking3D original, Cloner cloner) 42 43 : base(original, cloner) { 43 this.ExtremePoints = new SortedSet<PackingPosition>(original.ExtremePoints , new EPComparer3D());44 this.ExtremePoints = new SortedSet<PackingPosition>(original.ExtremePoints.Select(p => cloner.Clone(p)), new EPComparer3D()); 44 45 } 45 46 public override IDeepCloneable Clone(Cloner cloner) { … … 53 54 //Find ExtremePoints beginning from sourcepointX 54 55 var sourcePointX = new PackingPosition(0, position.X + newWidth, position.Y, position.Z); 55 if (sourcePointX.X < Bin Measures.Width && sourcePointX.Y < BinMeasures.Height && sourcePointX.Z < BinMeasures.Depth) {56 if (sourcePointX.X < BinShape.Width && sourcePointX.Y < BinShape.Height && sourcePointX.Z < BinShape.Depth) { 56 57 //Traversing down the y-axis 57 58 PackingPosition current = new PackingPosition(0, sourcePointX.X, sourcePointX.Y, sourcePointX.Z); … … 79 80 //Find ExtremePoints beginning from sourcepointY 80 81 var sourcePointY = new PackingPosition(0, position.X, position.Y + newItem.Height, position.Z); 81 if (sourcePointY.X < Bin Measures.Width && sourcePointY.Y < BinMeasures.Height && sourcePointY.Z < BinMeasures.Depth) {82 if (sourcePointY.X < BinShape.Width && sourcePointY.Y < BinShape.Height && sourcePointY.Z < BinShape.Depth) { 82 83 //Traversing down the x-axis 83 84 PackingPosition current = new PackingPosition(0, sourcePointY.X, sourcePointY.Y, sourcePointY.Z); … … 105 106 //Find ExtremePoints beginning from sourcepointZ 106 107 var sourcePointZ = new PackingPosition(0, position.X, position.Y, position.Z + newDepth); 107 if (sourcePointZ.X < Bin Measures.Width && sourcePointZ.Y < BinMeasures.Height && sourcePointZ.Z < BinMeasures.Depth) {108 if (sourcePointZ.X < BinShape.Width && sourcePointZ.Y < BinShape.Height && sourcePointZ.Z < BinShape.Depth) { 108 109 //Traversing down the x-axis 109 110 PackingPosition current = new PackingPosition(0, sourcePointZ.X, sourcePointZ.Y, sourcePointZ.Z); … … 130 131 } 131 132 132 public override PackingPosition FindExtremePointForItem(PackingItem measures, bool rotated, bool stackingConstraints) {133 134 PackingItem item = new PackingItem(135 rotated ? measures.Depth : measures.Width,136 measures.Height,137 rotated ? measures.Width : measures.Depth,138 measures.TargetBin);133 public override PackingPosition FindExtremePointForItem(PackingItem item, bool rotated, bool stackingConstraints) { 134 135 PackingItem newItem = new PackingItem( 136 rotated ? item.Depth : item.Width, 137 item.Height, 138 rotated ? item.Width : item.Depth, 139 item.TargetBin); 139 140 140 141 int epIndex = 0; 141 142 while (epIndex < ExtremePoints.Count && ( 142 !IsPositionFeasible( item, ExtremePoints.ElementAt(epIndex))143 || !IsSupportedByAtLeastOnePoint( item, ExtremePoints.ElementAt(epIndex))144 || (stackingConstraints && !IsStaticStable( item, ExtremePoints.ElementAt(epIndex)))145 || (stackingConstraints && !IsWeightSupported( item, ExtremePoints.ElementAt(epIndex)))143 !IsPositionFeasible(newItem, ExtremePoints.ElementAt(epIndex)) 144 || !IsSupportedByAtLeastOnePoint(newItem, ExtremePoints.ElementAt(epIndex)) 145 || (stackingConstraints && !IsStaticStable(newItem, ExtremePoints.ElementAt(epIndex))) 146 || (stackingConstraints && !IsWeightSupported(newItem, ExtremePoints.ElementAt(epIndex))) 146 147 )) { epIndex++; } 147 148 … … 154 155 } 155 156 156 public override PackingPosition FindPositionBySliding(PackingItem measures, bool rotated) {157 public override PackingPosition FindPositionBySliding(PackingItem item, bool rotated) { 157 158 //TODO: does not support stacking constraints yet 158 159 //Starting-position at upper right corner (=left bottom point of item-rectangle is at position item.width,item.height) 159 160 PackingPosition currentPosition = new PackingPosition(0, 160 Bin Measures.Width - (rotated ? measures.Depth : measures.Width),161 Bin Measures.Height - measures.Height,162 Bin Measures.Depth - (rotated ? measures.Width : measures.Depth), rotated);161 BinShape.Width - (rotated ? item.Depth : item.Width), 162 BinShape.Height - item.Height, 163 BinShape.Depth - (rotated ? item.Width : item.Depth), rotated); 163 164 //Slide the item as far as possible to the bottom 164 while (IsPositionFeasible( measures, PackingPosition.MoveDown(currentPosition))165 || IsPositionFeasible( measures, PackingPosition.MoveLeft(currentPosition))166 || IsPositionFeasible( measures, PackingPosition.MoveBack(currentPosition))) {165 while (IsPositionFeasible(item, PackingPosition.MoveDown(currentPosition)) 166 || IsPositionFeasible(item, PackingPosition.MoveLeft(currentPosition)) 167 || IsPositionFeasible(item, PackingPosition.MoveBack(currentPosition))) { 167 168 //Slide the item as far as possible to the left 168 while (IsPositionFeasible( measures, PackingPosition.MoveLeft(currentPosition))169 || IsPositionFeasible( measures, PackingPosition.MoveBack(currentPosition))) {169 while (IsPositionFeasible(item, PackingPosition.MoveLeft(currentPosition)) 170 || IsPositionFeasible(item, PackingPosition.MoveBack(currentPosition))) { 170 171 //Slide the item as far as possible to the back 171 while (IsPositionFeasible( measures, PackingPosition.MoveBack(currentPosition))) {172 while (IsPositionFeasible(item, PackingPosition.MoveBack(currentPosition))) { 172 173 currentPosition = PackingPosition.MoveBack(currentPosition); 173 174 } 174 if (IsPositionFeasible( measures, PackingPosition.MoveLeft(currentPosition)))175 if (IsPositionFeasible(item, PackingPosition.MoveLeft(currentPosition))) 175 176 currentPosition = PackingPosition.MoveLeft(currentPosition); 176 177 } 177 if (IsPositionFeasible( measures, PackingPosition.MoveDown(currentPosition)))178 if (IsPositionFeasible(item, PackingPosition.MoveDown(currentPosition))) 178 179 currentPosition = PackingPosition.MoveDown(currentPosition); 179 180 } 180 181 181 return IsPositionFeasible( measures, currentPosition) ? currentPosition : null;182 } 183 184 public override void SlidingBasedPacking(ref IList<int> sequence, IList<PackingItem> item Measures) {182 return IsPositionFeasible(item, currentPosition) ? currentPosition : null; 183 } 184 185 public override void SlidingBasedPacking(ref IList<int> sequence, IList<PackingItem> items) { 185 186 var temp = new List<int>(sequence); 186 187 for (int i = 0; i < temp.Count; i++) { 187 var item = item Measures[temp[i]];188 var item = items[temp[i]]; 188 189 var position = FindPositionBySliding(item, false); 189 190 if (position != null) { … … 193 194 } 194 195 } 195 public override void SlidingBasedPacking(ref IList<int> sequence, IList<PackingItem> item Measures, Dictionary<int, bool> rotationArray) {196 public override void SlidingBasedPacking(ref IList<int> sequence, IList<PackingItem> items, Dictionary<int, bool> rotationArray) { 196 197 var temp = new List<int>(sequence); 197 198 for (int i = 0; i < temp.Count; i++) { 198 var item = item Measures[temp[i]];199 var item = items[temp[i]]; 199 200 var position = FindPositionBySliding(item, rotationArray[i]); 200 201 if (position != null) { … … 204 205 } 205 206 } 206 public override void ExtremePointBasedPacking(ref IList<int> sequence, IList<PackingItem> item Measures, bool stackingConstraints) {207 public override void ExtremePointBasedPacking(ref IList<int> sequence, IList<PackingItem> items, bool stackingConstraints) { 207 208 var temp = new List<int>(sequence); 208 209 foreach (int itemID in temp) { 209 var item = item Measures[itemID];210 var item = items[itemID]; 210 211 var positionFound = FindExtremePointForItem(item, false, stackingConstraints); 211 212 if (positionFound != null) { … … 215 216 } 216 217 } 217 public override void ExtremePointBasedPacking(ref IList<int> sequence, IList<PackingItem> item Measures, bool stackingConstraints, Dictionary<int, bool> rotationArray) {218 public override void ExtremePointBasedPacking(ref IList<int> sequence, IList<PackingItem> items, bool stackingConstraints, Dictionary<int, bool> rotationArray) { 218 219 var temp = new List<int>(sequence); 219 220 foreach (int itemID in temp) { 220 var item = item Measures[itemID];221 var item = items[itemID]; 221 222 var positionFound = FindExtremePointForItem(item, rotationArray[itemID], stackingConstraints); 222 223 if (positionFound != null) { … … 230 231 231 232 int shortestSide = int.MaxValue; 232 int width = Bin Measures.Width;233 int height = Bin Measures.Height;234 int depth = Bin Measures.Depth;233 int width = BinShape.Width; 234 int height = BinShape.Height; 235 int depth = BinShape.Depth; 235 236 236 237 if (position.X >= width || position.Y >= height || position.Z >= depth) … … 287 288 return true; 288 289 289 if (Item Measures[PointOccupation(new PackingPosition(0, ep.X, ep.Y - 1, ep.Z))].SupportsStacking(item)290 && Item Measures[PointOccupation(new PackingPosition(0, ep.X + item.Width - 1, ep.Y - 1, ep.Z))].SupportsStacking(item)291 && Item Measures[PointOccupation(new PackingPosition(0, ep.X, ep.Y - 1, ep.Z + item.Depth - 1))].SupportsStacking(item)292 && Item Measures[PointOccupation(new PackingPosition(0, ep.X + item.Width - 1, ep.Y - 1, ep.Z + item.Depth - 1))].SupportsStacking(item))290 if (Items[PointOccupation(new PackingPosition(0, ep.X, ep.Y - 1, ep.Z))].SupportsStacking(item) 291 && Items[PointOccupation(new PackingPosition(0, ep.X + item.Width - 1, ep.Y - 1, ep.Z))].SupportsStacking(item) 292 && Items[PointOccupation(new PackingPosition(0, ep.X, ep.Y - 1, ep.Z + item.Depth - 1))].SupportsStacking(item) 293 && Items[PointOccupation(new PackingPosition(0, ep.X + item.Width - 1, ep.Y - 1, ep.Z + item.Depth - 1))].SupportsStacking(item)) 293 294 return true; 294 295 … … 298 299 299 300 protected override void InitializeOccupationLayers() { 300 for (int i = 0; i * 10 <= Bin Measures.Depth; i += 1) {301 for (int i = 0; i * 10 <= BinShape.Depth; i += 1) { 301 302 OccupationLayers[i] = new List<int>(); 302 303 } 303 304 } 304 protected override void AddNewItemToOccupationLayers(int itemID, PackingItem measures, PackingPosition position) {305 protected override void AddNewItemToOccupationLayers(int itemID, PackingItem item, PackingPosition position) { 305 306 int z1 = position.Z / 10; 306 int z2 = (position.Z + (position.Rotated ? measures.Width : measures.Depth)) / 10;307 int z2 = (position.Z + (position.Rotated ? item.Width : item.Depth)) / 10; 307 308 308 309 for (int i = z1; i <= z2; i++) … … 312 313 return OccupationLayers[position.Z / 10]; 313 314 } 314 protected override List<int> GetLayerItemIDs(PackingItem measures, PackingPosition position) {315 protected override List<int> GetLayerItemIDs(PackingItem item, PackingPosition position) { 315 316 List<int> result = new List<int>(); 316 317 int z1 = position.Z / 10; 317 int z2 = (position.Z + (position.Rotated ? measures.Width : measures.Depth)) / 10;318 int z2 = (position.Z + (position.Rotated ? item.Width : item.Depth)) / 10; 318 319 319 320 for (int i = z1; i <= z2; i++)
Note: See TracChangeset
for help on using the changeset viewer.