- Timestamp:
- 11/14/17 15:31:22 (7 years ago)
- Location:
- branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3
- Files:
-
- 2 added
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/3D/BinPacking3D.cs
r15462 r15471 67 67 #region New methods for bin packer class 68 68 69 /// <summary> 70 /// Puts a given item into the bin packing at the given position. 71 /// </summary> 72 /// <param name="itemID">Offset in the internal item array</param> 73 /// <param name="item">Item</param> 74 /// <param name="position">Position of the item in the bin packing</param> 69 75 public override void PackItem(int itemID, PackingItem item, PackingPosition position) { 70 76 Items[itemID] = item; … … 107 113 AddExtremePoint(ep2); 108 114 AddExtremePoint(ep3); 109 110 /* 111 ExtremePoints.Add(ep1); 112 ExtremePoints.Add(ep2); 113 ExtremePoints.Add(ep3); 114 115 var rs1 = CalculateResidualSpace(CreateRs(ep1)); 116 var rs2 = CalculateResidualSpace(CreateRs(ep2)); 117 var rs3 = CalculateResidualSpace(CreateRs(ep3)); 118 ResidualSpace.Add(ep1, rs1); 119 ResidualSpace.Add(ep2, rs2); 120 ResidualSpace.Add(ep3, rs3);*/ 121 } 122 123 124 /*private bool AddExtremePoint(PackingPosition position) { 125 if () 126 127 return true; 128 } 129 130 131 private bool AddExtremePoint(PackingPosition pos) { 132 if (ExtremePoints.Add(pos)) { 133 var rs = CalculateResidualSpace(new Vector3D(pos)); 134 ResidualSpace.Add(pos, rs); 135 // Check if existing extreme points are shadowed by the new point 136 // That is, their residual space fit entirely into the residual space of the new point 137 foreach (var ep in ExtremePoints.Where(x => x != pos && new Vector3D(x).IsInside(pos, rs)).ToList()) { 138 if (IsWithinResidualSpaceOfAnotherExtremePoint(new Vector3D(ep), ResidualSpace[ep], pos, rs)) { 139 ExtremePoints.Remove(ep); 140 ResidualSpace.Remove(ep); 141 } 142 } 143 return true; 144 } 145 return false; 146 }*/ 147 148 115 } 116 149 117 private Tuple<int, int, int> CalculateResidualSpace(Vector3D pos) { 150 118 var itemPos = Items.Select(x => new { Item = x.Value, Position = Positions[x.Key] }); … … 164 132 } 165 133 } 166 167 168 134 169 135 if (limit.X - pos.X <= 0 || limit.Y - pos.Y <= 0 || limit.Z - pos.Z <= 0) { 170 136 return Tuple.Create(0, 0, 0); 171 } 172 173 137 } 174 138 return Tuple.Create(limit.X - pos.X, limit.Y - pos.Y, limit.Z - pos.Z); 175 139 } … … 491 455 } 492 456 457 /// <summary> 458 /// Returns true if all values of a given tuple are not zero 459 /// </summary> 460 /// <param name="rs">Tuple with three integer values which represents a residual space</param> 461 /// <returns></returns> 493 462 private bool IsNonZero(Tuple<int, int, int> rs) { 494 463 return rs.Item1 > 0 && rs.Item2 > 0 && rs.Item3 > 0; 495 464 } 496 465 466 /// <summary> 467 /// 468 /// </summary> 469 /// <param name="pos"></param> 470 /// <param name="residualSpace"></param> 471 /// <returns></returns> 497 472 private bool IsWithinResidualSpaceOfAnotherExtremePoint(Vector3D pos, Tuple<int, int, int> residualSpace) { 498 473 var eps = ExtremePoints.Where(x => !pos.Equals(x) && pos.IsInside(x, ResidualSpace[x])); … … 505 480 } 506 481 482 /// <summary> 483 /// Adds an extrem point to the extreme point collection of the bin packing. 484 /// </summary> 485 /// <param name="pos">Position of the new extreme point</param> 486 /// <returns>Retruns true if the extreme point could be added</returns> 507 487 private bool AddExtremePoint(PackingPosition pos) { 508 488 if (ExtremePoints.Add(pos)) { 509 489 var rs = CalculateResidualSpace(new Vector3D(pos)); 510 490 ResidualSpace.Add(pos, rs); 511 // Check if existing extreme points are shadowed by the new point512 // Th atis, their residual space fit entirely into the residual space of the new point491 // Check if the existing extreme points are shadowed by the new point 492 // This is, their residual space fit entirely into the residual space of the new point 513 493 foreach (var ep in ExtremePoints.Where(x => x != pos && new Vector3D(x).IsInside(pos, rs)).ToList()) { 514 494 if (IsWithinResidualSpaceOfAnotherExtremePoint(new Vector3D(ep), ResidualSpace[ep], pos, rs)) { … … 521 501 return false; 522 502 } 523 524 private Tuple<int, int, int> CalculateResidualSpace1(Vector3D pos) { 525 var itemPos = Items.Select(x => new { Item = x.Value, Position = Positions[x.Key] }); 526 var rightLimit = ProjectRight(pos); 527 var upLimit = ProjectUp(pos); 528 var forwardLimit = ProjectForward(pos); 529 return Tuple.Create(rightLimit.X - pos.X, upLimit.Y - pos.Y, forwardLimit.Z - pos.Z); 530 } 531 503 532 504 #region Projections 533 505 … … 719 691 720 692 693 /// <summary> 694 /// Updates the resiual space for a packing item. 695 /// </summary> 696 /// <param name="item"></param> 697 /// <param name="pos"></param> 721 698 public void UpdateResidualSpace(PackingItem item, PackingPosition pos) { 722 699 foreach (var ep in ExtremePoints.ToList()) { -
branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/3D/Instances/RealWorldContainerPackingInstanceProvider.cs
r15230 r15471 102 102 var parser = new ThreeDInstanceParser(); 103 103 parser.Parse(stream); 104 105 104 return new BPPData() { 106 105 Name = Path.GetFileNameWithoutExtension(path), -
branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/3D/IntegerVectorEncoding/ThreeDInstanceParser.cs
r15463 r15471 64 64 var next = reader.Read(); 65 65 var builder = new StringBuilder(); 66 while (next >= 0 && !char.IsDigit((char)next)) next = reader.Read(); 67 if (next == -1) throw new InvalidOperationException("No further integer available"); 66 while (next >= 0 && !char.IsDigit((char)next)) 67 next = reader.Read(); 68 if (next == -1) 69 throw new InvalidOperationException("No further integer available"); 68 70 while (char.IsDigit((char)next)) { 69 71 builder.Append((char)next); 70 72 next = reader.Read(); 71 if (next == -1) break; 73 if (next == -1) 74 break; 72 75 } 73 76 return int.Parse(builder.ToString()); -
branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/3D/Packer/BinPacker.cs
r15462 r15471 44 44 /// <param name="items">A list of packing items which should be assigned to a bin</param> 45 45 /// <param name="useStackingConstraints">Flag for using stacking constraints</param> 46 /// <returns> </returns>46 /// <returns>Returns a collection of bin packing 3d objects. Each object represents a bin and the packed items</returns> 47 47 public abstract IList<BinPacking3D> PackItems(Permutation sortedItems, PackingShape binShape, IList<PackingItem> items, bool useStackingConstraints); 48 49 48 50 51 49 /// <summary> 52 50 /// Pack a given item into a given bin and updates the residual space and the extreme points … … 78 76 packingItem.TargetBin, packingItem.Weight, packingItem.Material); 79 77 80 // The extremepoints are sortet by Z, X, Y 81 78 // The extremepoints are sortet by Y / Z / X 82 79 return packingBin.ExtremePoints.Where(x => packingBin.IsPositionFeasible(newItem, x, useStackingConstraints)).FirstOrDefault(); 83 80 } -
branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/3D/Packer/BinPackerFactory.cs
r15463 r15471 16 16 /// </summary> 17 17 /// <param name="fittingMethod"></param> 18 /// <returns> </returns>18 /// <returns>Returns a new BinPacker depending on the given fitting method</returns> 19 19 public static BinPacker CreateBinPacker(FittingMethod fittingMethod) { 20 20 BinPacker binPacker = null; -
branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/3D/Packer/BinPackerFirstFit.cs
r15462 r15471 38 38 39 39 /// <summary> 40 /// Packs the items of the object by using a first fit algorithm into an amount of bins and returns them 40 /// Packs the items of the object by using a first fit algorithm into an amount of bins and returns them. 41 41 /// </summary> 42 42 /// <returns>Returns a collection of bin packing 3d objects. Each object represents a bin and the packed items</returns> -
branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/3D/Packer/BinPackerFreeVolumeBestFit.cs
r15462 r15471 36 36 public BinPackerFreeVolumeBestFit() : base() { } 37 37 38 /// <summary> 39 /// Packs all items by using a free volume best fit strategy. 40 /// If there is no bin packing item, a new one will be created an the current item will be packed into it. 41 /// If there exists at least on bin packing item in the packing list they are being sortet by their free volume ascending. 42 /// The current item will be packed into the bin packing with the fewest free volume and enought space for placing it. 43 /// If an item could not be placed in any bin packing, a new one will be created for the item. 44 /// </summary> 45 /// <param name="sortedItems"></param> 46 /// <param name="binShape"></param> 47 /// <param name="items"></param> 48 /// <param name="useStackingConstraints"></param> 49 /// <returns>Returns a collection of bin packing 3d objects. Each object represents a bin and the packed items</returns> 38 50 public override IList<BinPacking3D> PackItems(Permutation sortedItems, PackingShape binShape, IList<PackingItem> items, bool useStackingConstraints) { 39 51 IList<BinPacking3D> packingList = new List<BinPacking3D>(); 40 52 IList<int> remainingIds = new List<int>(sortedItems); 41 42 53 43 54 foreach (int remainingId in remainingIds) { -
branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/3D/Packer/BinPackerResidualSpaceBestFit.cs
r15462 r15471 34 34 public class BinPackerResidualSpaceBestFit : BinPacker { 35 35 36 public BinPackerResidualSpaceBestFit() : base() { }/* 37 public BinPackerResidualSpaceBestFit(Permutation permutation, PackingShape binShape, IList<PackingItem> items, bool useStackingConstraints) 38 : base(permutation, binShape, items, useStackingConstraints) { } 39 */ 36 public BinPackerResidualSpaceBestFit() : base() { } 37 40 38 /// <summary> 41 39 /// Packs the items into the bins by using a best fit residual space algorithm. … … 43 41 /// Each residual space belongs to an extreme point. 44 42 /// </summary> 45 /// <returns> </returns>43 /// <returns>Returns a collection of bin packing 3d objects. Each object represents a bin and the packed items</returns> 46 44 public override IList<BinPacking3D> PackItems(Permutation sortedItems, PackingShape binShape, IList<PackingItem> items, bool useStackingConstraints) { 47 45 IList<BinPacking3D> packingList = new List<BinPacking3D>(); -
branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/3D/PackingPosition.cs
r15462 r15471 105 105 /// <summary> 106 106 /// Compares two packing positions by their coordinates. 107 /// The order of comparing is z -> x -> y.107 /// The order of comparing is y (height) -> z (depth) -> x (width). 108 108 /// </summary> 109 109 /// <param name="other"></param> 110 110 /// <returns></returns> 111 111 public int CompareTo(PackingPosition other) { 112 int result = Y.CompareTo(other.Y); 113 if (result == 0) 114 result = Z.CompareTo(other.Z); 115 if (result == 0) 116 result = X.CompareTo(other.X); 117 /* 112 118 int result = Z.CompareTo(other.Z); 113 119 if (result == 0) … … 115 121 if (result == 0) 116 122 result = Y.CompareTo(other.Y); 117 123 */ 118 124 return result; 119 125 -
branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/3D/ProblemBase.cs
r15276 r15471 236 236 BinShape = data.BinShape; 237 237 var items = new ItemList<PackingItem>(data.Items); 238 items.Sort((x, y) => y.CompareTo(x));239 238 Items = items.AsReadOnly(); 240 239 -
branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/3D/Sorting/PackingItemSorter.cs
r15463 r15471 8 8 9 9 namespace HeuristicLab.Problems.BinPacking3D.Sorting { 10 11 /// <summary> 12 /// This is a extension class for sorting a permutation. 13 /// </summary> 10 14 public static class PackingItemSorter { 15 16 /// <summary> 17 /// Sorts a given permutation first by the volume and secoundly by the height. 18 /// </summary> 19 /// <param name="items">Permuation which should be sorted</param> 20 /// <returns>A new sorted permutation</returns> 11 21 public static Permutation SortByVolumeHeight(this IList<PackingItem> items) { 12 22 return new Permutation(PermutationTypes.Absolute, … … 17 27 } 18 28 29 /// <summary> 30 /// Sorts a given permutation first by the heigth and secoundly by the volume. 31 /// </summary> 32 /// <param name="items">Permuation which should be sorted</param> 33 /// <returns>A new sorted permutation</returns> 19 34 public static Permutation SortByHeightVolume(this IList<PackingItem> items) { 20 35 return new Permutation(PermutationTypes.Absolute, … … 25 40 } 26 41 42 /// <summary> 43 /// Sorts a given permutation first by the area and secondly by the height. 44 /// </summary> 45 /// <param name="items">Permuation which should be sorted</param> 46 /// <returns>A new sorted permutation</returns> 27 47 public static Permutation SortByAreaHeight(this IList<PackingItem> items) { 28 48 return new Permutation(PermutationTypes.Absolute, … … 33 53 } 34 54 55 /// <summary> 56 /// Sorts a given permuation first by the height and secoundly by the area. 57 /// </summary> 58 /// <param name="items">Permuation which should be sorted</param> 59 /// <returns>A new sorted permutation</returns> 35 60 public static Permutation SortByHeightArea(this IList<PackingItem> items) { 36 61 return new Permutation(PermutationTypes.Absolute, … … 41 66 } 42 67 68 /// <summary> 69 /// Sorts a given permutation. The items are being grouped by the cluster id. 70 /// The cluster id is calulated as followed: clusterId = Ceiling( (width * depth) / (width * depth * delta)) 71 /// The permutation is first being sorted by the area and secoundly by the height. 72 /// </summary> 73 /// <param name="items">Permuation which should be sorted</param> 74 /// <param name="bin">The bin is needed for building the cluster</param> 75 /// <param name="delta">The delta is needed for building the cluster</param> 76 /// <returns>A new sorted permutation</returns> 43 77 public static Permutation SortByClusteredAreaHeight(this IList<PackingItem> items, PackingShape bin, double delta) { 44 78 double clusterRange = bin.Width * bin.Depth * delta; … … 52 86 } 53 87 88 /// <summary> 89 /// Sorts a given permutation. The items are being grouped by the cluster id. 90 /// The cluster id is calulated as followed: clusterId = Ceiling( (height) / (height * delta)) 91 /// The permutation is first being sorted by the height and secoundly by the area. 92 /// </summary> 93 /// <param name="items">Permuation which should be sorted</param> 94 /// <param name="bin">The bin is needed for building the cluster</param> 95 /// <param name="delta">The delta is needed for building the cluster</param> 96 /// <returns>A new sorted permutation</returns> 54 97 public static Permutation SortByClusteredHeightArea(this IList<PackingItem> items, PackingShape bin, double delta) { 55 98 double clusterRange2 = bin.Height * delta; … … 63 106 } 64 107 108 /// <summary> 109 /// Sorts a given permutation first by the material, secoundly by the volume and finally by the height. 110 /// </summary> 111 /// <param name="items">Permuation which should be sorted</param> 112 /// <returns>A new sorted permutation</returns> 65 113 public static Permutation SortByMaterialVolumeHeight(this IList<PackingItem> items) { 66 114 return new Permutation(PermutationTypes.Absolute, … … 72 120 } 73 121 122 /// <summary> 123 /// Sorts a given permutation first by the material, secoundly by the heigth and finally by the volume. 124 /// </summary> 125 /// <param name="items">Permuation which should be sorted</param> 126 /// <returns>A new sorted permutation</returns> 74 127 public static Permutation SortByMaterialHeightVolume(this IList<PackingItem> items) { 75 128 return new Permutation(PermutationTypes.Absolute, … … 81 134 } 82 135 136 /// <summary> 137 /// Sorts a given permutation first by the material, secoundly by the area and finally by the height. 138 /// </summary> 139 /// <param name="items">Permuation which should be sorted</param> 140 /// <returns>A new sorted permutation</returns> 83 141 public static Permutation SortByMaterialAreaHeight(this IList<PackingItem> items) { 84 142 return new Permutation(PermutationTypes.Absolute, … … 90 148 } 91 149 150 /// <summary> 151 /// Sorts a given permuation first by the material, secoundly by the height and finally by the area. 152 /// </summary> 153 /// <param name="items">Permuation which should be sorted</param> 154 /// <returns>A new sorted permutation</returns> 92 155 public static Permutation SortByMaterialHeightArea(this IList<PackingItem> items) { 93 156 return new Permutation(PermutationTypes.Absolute, … … 99 162 } 100 163 164 /// <summary> 165 /// Sorts a given permutation. The items are being grouped by the cluster id. 166 /// The cluster id is calulated as followed: clusterId = Ceiling( (width * depth) / (width * depth * delta)) 167 /// The permutation is being clusterd by the area, first sorted by the material, secoundly by the height. 168 /// </summary> 169 /// <param name="items">Permuation which should be sorted</param> 170 /// <param name="bin">The bin is needed for building the cluster</param> 171 /// <param name="delta">The delta is needed for building the cluster</param> 172 /// <returns>A new sorted permutation</returns> 101 173 public static Permutation SortByMaterialClusteredAreaHeight(this IList<PackingItem> items, PackingShape bin, double delta) { 102 174 double clusterRange = bin.Width * bin.Depth * delta; … … 104 176 items.Select((v, i) => new { Index = i, Item = v, ClusterId = (int)(Math.Ceiling(v.Width * v.Depth / clusterRange)) }) 105 177 .GroupBy(x => x.ClusterId) 106 .Select(x => new { Cluster = x.Key, Items = x.OrderByDescending(y => y.Item.Height).ToList() }) 107 .OrderByDescending(x => x.Cluster) 108 .SelectMany(x => x.Items) 109 .Select(x => x.Index).ToArray()); 110 } 111 178 .Select(x => new { Cluster = x.Key, Items = x.OrderByDescending(z => z.Item.Material).ThenByDescending(y => y.Item.Height).ToList() }) 179 .OrderByDescending(x => x.Cluster) 180 .SelectMany(x => x.Items) 181 .Select(x => x.Index).ToArray()); 182 } 183 184 /// <summary> 185 /// Sorts a given permutation. The items are being grouped by the cluster id. 186 /// The cluster id is calulated as followed: clusterId = Ceiling( (height) / (height * delta)) 187 /// The permutation is being clusterd by the height, first sorted by the material, secoundly by the area. 188 /// </summary> 189 /// <param name="items">Permuation which should be sorted</param> 190 /// <param name="bin">The bin is needed for building the cluster</param> 191 /// <param name="delta">The delta is needed for building the cluster</param> 192 /// <returns>A new sorted permutation</returns> 112 193 public static Permutation SortByMaterialClusteredHeightArea(this IList<PackingItem> items, PackingShape bin, double delta) { 113 194 double clusterRange2 = bin.Height * delta; … … 115 196 items.Select((v, i) => new { Index = i, Item = v, ClusterId = (int)(Math.Ceiling(v.Height / clusterRange2)) }) 116 197 .GroupBy(x => x.ClusterId) 117 .Select(x => new { Cluster = x.Key, Items = x.OrderByDescending( y => y.Item.Depth * y.Item.Width).ToList() })198 .Select(x => new { Cluster = x.Key, Items = x.OrderByDescending(z => z.Item.Material).ThenByDescending(y => y.Item.Depth * y.Item.Width).ToList() }) 118 199 .OrderByDescending(x => x.Cluster) 119 200 .SelectMany(x => x.Items) -
branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/Algorithms/3D/ExtremePointAlgorithm.cs
r15462 r15471 182 182 IDecoder<Permutation> decoder = new ExtremePointPermutationDecoder(BinPackerFactory.CreateBinPacker(fit)); 183 183 Permutation sortedItems; 184 184 185 if (SortByMaterialParameter.Value.Value) { 185 186 sortedItems = SortItemsByMaterialAndSortingMethod(bin, items, sort, DeltaParameter.Value.Value); … … 187 188 sortedItems = SortItemsBySortingMethod(bin, items, sort, DeltaParameter.Value.Value); 188 189 } 189 190 190 191 var result = Optimize(sortedItems, bin, items, Problem.UseStackingConstraints, decoder, Problem.SolutionEvaluator); 191 192 192 193 if (double.IsNaN(result.Item2) || double.IsInfinity(result.Item2)) { 193 194 continue;
Note: See TracChangeset
for help on using the changeset viewer.