Changeset 15229
- Timestamp:
- 07/13/17 15:22:59 (7 years ago)
- Location:
- branches/BinPackingExtension/HeuristicLab.Problems.BinPacking/3.3
- Files:
-
- 14 edited
- 2 moved
Legend:
- Unmodified
- Added
- Removed
-
branches/BinPackingExtension/HeuristicLab.Problems.BinPacking/3.3/2D/BinPacking2D.cs
r15182 r15229 88 88 }; 89 89 90 int epIndex = 0; 91 while (epIndex < ExtremePoints.Count && (!IsPositionFeasible(rotatedItem, ExtremePoints.ElementAt(epIndex)))) { epIndex++; } 92 93 if (epIndex < ExtremePoints.Count) { 94 var currentPoint = ExtremePoints.ElementAt(epIndex); 95 96 var result = new PackingPosition(currentPoint.AssignedBin, currentPoint.X, currentPoint.Y, rotated); 90 var ep = ExtremePoints.Where(x => IsPositionFeasible(rotatedItem, x, stackingConstraints)).FirstOrDefault(); 91 if (ep != null) { 92 var result = new PackingPosition(ep.AssignedBin, ep.X, ep.Y, rotated); 97 93 return result; 98 94 } 99 95 return null; 100 96 } 101 public override PackingPosition FindPositionBySliding(PackingItem item, bool rotated ) {97 public override PackingPosition FindPositionBySliding(PackingItem item, bool rotated, bool stackingConstraints) { 102 98 PackingPosition currentPosition = new PackingPosition(0, 103 99 BinShape.Width - (rotated ? item.Height : item.Width), 104 100 BinShape.Height - (rotated ? item.Width : item.Height), rotated); 105 101 //Slide the item as far as possible to the left 106 while (IsPositionFeasible(item, PackingPosition.MoveLeft(currentPosition) )107 || IsPositionFeasible(item, PackingPosition.MoveDown(currentPosition) )) {102 while (IsPositionFeasible(item, PackingPosition.MoveLeft(currentPosition), stackingConstraints) 103 || IsPositionFeasible(item, PackingPosition.MoveDown(currentPosition), stackingConstraints)) { 108 104 //Slide the item as far as possible to the bottom 109 while (IsPositionFeasible(item, PackingPosition.MoveDown(currentPosition) )) {105 while (IsPositionFeasible(item, PackingPosition.MoveDown(currentPosition), stackingConstraints)) { 110 106 currentPosition = PackingPosition.MoveDown(currentPosition); 111 107 } 112 if (IsPositionFeasible(item, PackingPosition.MoveLeft(currentPosition) ))108 if (IsPositionFeasible(item, PackingPosition.MoveLeft(currentPosition), stackingConstraints)) 113 109 currentPosition = PackingPosition.MoveLeft(currentPosition); 114 110 } 115 111 116 return IsPositionFeasible(item, currentPosition ) ? currentPosition : null;117 } 118 119 public override void SlidingBasedPacking(ref IList<int> sequence, IList<PackingItem> items ) {112 return IsPositionFeasible(item, currentPosition, stackingConstraints) ? currentPosition : null; 113 } 114 115 public override void SlidingBasedPacking(ref IList<int> sequence, IList<PackingItem> items, bool stackingConstraints) { 120 116 var temp = new List<int>(sequence); 121 117 for (int i = 0; i < temp.Count; i++) { 122 118 var item = items[temp[i]]; 123 var position = FindPositionBySliding(item, false );119 var position = FindPositionBySliding(item, false, stackingConstraints); 124 120 if (position != null) { 125 121 PackItem(temp[i], item, position); … … 128 124 } 129 125 } 130 public override void SlidingBasedPacking(ref IList<int> sequence, IList<PackingItem> items, Dictionary<int, bool> rotationArray ) {126 public override void SlidingBasedPacking(ref IList<int> sequence, IList<PackingItem> items, Dictionary<int, bool> rotationArray, bool stackingConstraints) { 131 127 var temp = new List<int>(sequence); 132 128 for (int i = 0; i < temp.Count; i++) { 133 129 var item = items[temp[i]]; 134 var position = FindPositionBySliding(item, rotationArray[temp[i]] );130 var position = FindPositionBySliding(item, rotationArray[temp[i]], stackingConstraints); 135 131 if (position != null) { 136 132 PackItem(temp[i], item, position); … … 150 146 } 151 147 } 152 public override bool ExtremePointBasedPacking(int itemID, IList<PackingItem> items, bool stackingConstraints) {153 var item = items[itemID];154 var positionFound = FindExtremePointForItem(item, false, stackingConstraints);155 if (positionFound != null) {156 PackItem(itemID, item, positionFound);157 return true;158 }159 return false;160 }161 148 162 149 public override void ExtremePointBasedPacking(ref IList<int> sequence, IList<PackingItem> items, bool stackingConstraints, Dictionary<int, bool> rotationArray) { -
branches/BinPackingExtension/HeuristicLab.Problems.BinPacking/3.3/2D/IntegerVectorEncoding/BottomLeftIntegerVectorDecoder.cs
r14167 r15229 47 47 48 48 protected override PackingPosition FindPositionForItem(BinPacking2D bp, PackingItem item) { 49 return bp.FindPositionBySliding(item, rotated: false );49 return bp.FindPositionBySliding(item, rotated: false, stackingConstraints: false); 50 50 } 51 51 … … 54 54 ref IList<int> remainingIDs, IList<PackingItem> items) { 55 55 var bp = new BinPacking2D(partialSolution.BinShape); 56 bp.SlidingBasedPacking(ref remainingIDs, items );56 bp.SlidingBasedPacking(ref remainingIDs, items, stackingConstraints: false); 57 57 return bp; 58 58 } -
branches/BinPackingExtension/HeuristicLab.Problems.BinPacking/3.3/2D/PermutationEncoding/BottomLeftPermutationDecoder.cs
r14167 r15229 46 46 while (remainingIDs.Count > 0) { 47 47 var bp = new BinPacking2D(binShape); 48 bp.SlidingBasedPacking(ref remainingIDs, items );48 bp.SlidingBasedPacking(ref remainingIDs, items, stackingConstraints: false); 49 49 result.Bins.Add(bp); 50 50 } -
branches/BinPackingExtension/HeuristicLab.Problems.BinPacking/3.3/3D/BinPacking3D.cs
r14977 r15229 144 144 145 145 public override PackingPosition FindExtremePointForItem(PackingItem item, bool rotated, bool stackingConstraints) { 146 147 146 PackingItem newItem = new PackingItem( 148 147 rotated ? item.Depth : item.Width, … … 151 150 item.TargetBin, item.Weight, item.Material); 152 151 153 int epIndex = 0; 154 while (epIndex < ExtremePoints.Count && ( 155 !IsPositionFeasible(newItem, ExtremePoints.ElementAt(epIndex)) 156 || !IsSupportedByAtLeastOnePoint(newItem, ExtremePoints.ElementAt(epIndex)) 157 || (stackingConstraints && !IsStaticStable(newItem, ExtremePoints.ElementAt(epIndex))) 158 || (stackingConstraints && !IsWeightSupported(newItem, ExtremePoints.ElementAt(epIndex))) 159 )) { epIndex++; } 160 161 if (epIndex < ExtremePoints.Count) { 162 var origPoint = ExtremePoints.ElementAt(epIndex); 163 var result = new PackingPosition(origPoint.AssignedBin, origPoint.X, origPoint.Y, origPoint.Z, rotated); 152 var ep = ExtremePoints.Where(x => IsPositionFeasible(newItem, x, stackingConstraints)).FirstOrDefault(); 153 if (ep != null) { 154 var result = new PackingPosition(ep.AssignedBin, ep.X, ep.Y, ep.Z, rotated); 164 155 return result; 165 156 } … … 167 158 } 168 159 169 public override PackingPosition FindPositionBySliding(PackingItem item, bool rotated) { 170 //TODO: does not support stacking constraints yet 160 public override bool IsPositionFeasible(PackingItem item, PackingPosition position, bool stackingConstraints) { 161 var feasible = base.IsPositionFeasible(item, position, stackingConstraints); 162 return feasible 163 && IsSupportedByAtLeastOnePoint(item, position) 164 && (!stackingConstraints || (IsStaticStable(item, position) && IsWeightSupported(item, position))); 165 } 166 167 public override PackingPosition FindPositionBySliding(PackingItem item, bool rotated, bool stackingConstraints) { 171 168 //Starting-position at upper right corner (=left bottom point of item-rectangle is at position item.width,item.height) 172 169 PackingPosition currentPosition = new PackingPosition(0, … … 175 172 BinShape.Depth - (rotated ? item.Width : item.Depth), rotated); 176 173 //Slide the item as far as possible to the bottom 177 while (IsPositionFeasible(item, PackingPosition.MoveDown(currentPosition) )178 || IsPositionFeasible(item, PackingPosition.MoveLeft(currentPosition) )179 || IsPositionFeasible(item, PackingPosition.MoveBack(currentPosition) )) {174 while (IsPositionFeasible(item, PackingPosition.MoveDown(currentPosition), stackingConstraints) 175 || IsPositionFeasible(item, PackingPosition.MoveLeft(currentPosition), stackingConstraints) 176 || IsPositionFeasible(item, PackingPosition.MoveBack(currentPosition), stackingConstraints)) { 180 177 //Slide the item as far as possible to the left 181 while (IsPositionFeasible(item, PackingPosition.MoveLeft(currentPosition) )182 || IsPositionFeasible(item, PackingPosition.MoveBack(currentPosition) )) {178 while (IsPositionFeasible(item, PackingPosition.MoveLeft(currentPosition), stackingConstraints) 179 || IsPositionFeasible(item, PackingPosition.MoveBack(currentPosition), stackingConstraints)) { 183 180 //Slide the item as far as possible to the back 184 while (IsPositionFeasible(item, PackingPosition.MoveBack(currentPosition) )) {181 while (IsPositionFeasible(item, PackingPosition.MoveBack(currentPosition), stackingConstraints)) { 185 182 currentPosition = PackingPosition.MoveBack(currentPosition); 186 183 } 187 if (IsPositionFeasible(item, PackingPosition.MoveLeft(currentPosition) ))184 if (IsPositionFeasible(item, PackingPosition.MoveLeft(currentPosition), stackingConstraints)) 188 185 currentPosition = PackingPosition.MoveLeft(currentPosition); 189 186 } 190 if (IsPositionFeasible(item, PackingPosition.MoveDown(currentPosition) ))187 if (IsPositionFeasible(item, PackingPosition.MoveDown(currentPosition), stackingConstraints)) 191 188 currentPosition = PackingPosition.MoveDown(currentPosition); 192 189 } 193 190 194 return IsPositionFeasible(item, currentPosition ) ? currentPosition : null;195 } 196 197 public override void SlidingBasedPacking(ref IList<int> sequence, IList<PackingItem> items ) {191 return IsPositionFeasible(item, currentPosition, stackingConstraints) ? currentPosition : null; 192 } 193 194 public override void SlidingBasedPacking(ref IList<int> sequence, IList<PackingItem> items, bool stackingConstraints) { 198 195 var temp = new List<int>(sequence); 199 196 for (int i = 0; i < temp.Count; i++) { 200 197 var item = items[temp[i]]; 201 var position = FindPositionBySliding(item, false );198 var position = FindPositionBySliding(item, false, stackingConstraints); 202 199 if (position != null) { 203 200 PackItem(temp[i], item, position); … … 206 203 } 207 204 } 208 public override void SlidingBasedPacking(ref IList<int> sequence, IList<PackingItem> items, Dictionary<int, bool> rotationArray ) {205 public override void SlidingBasedPacking(ref IList<int> sequence, IList<PackingItem> items, Dictionary<int, bool> rotationArray, bool stackingConstraints) { 209 206 var temp = new List<int>(sequence); 210 207 for (int i = 0; i < temp.Count; i++) { 211 208 var item = items[temp[i]]; 212 var position = FindPositionBySliding(item, rotationArray[i] );209 var position = FindPositionBySliding(item, rotationArray[i], stackingConstraints); 213 210 if (position != null) { 214 211 PackItem(temp[i], item, position); … … 231 228 } 232 229 } 233 public override bool ExtremePointBasedPacking(int itemID, IList<PackingItem> items, bool stackingConstraints) {234 var item = items[itemID];235 var positionFound = FindExtremePointForItem(item, false, stackingConstraints);236 if (positionFound != null) {237 PackItem(itemID, item, positionFound);238 return true;239 }240 return false;241 }242 230 public override void ExtremePointBasedPacking(ref IList<int> sequence, IList<PackingItem> items, bool stackingConstraints, Dictionary<int, bool> rotationArray) { 243 231 var temp = new List<int>(sequence); … … 319 307 return false; 320 308 } 321 322 309 323 310 protected override void InitializeOccupationLayers() { … … 370 357 return; 371 358 } 372 public int GetResidualSpace(PackingItem item, PackingPosition ep) {373 return ((ResidualSpace[ep].Item1 - item.Width) +374 (ResidualSpace[ep].Item2 - item.Height) +375 (ResidualSpace[ep].Item3 - item.Depth));376 }377 }378 359 } 360 } -
branches/BinPackingExtension/HeuristicLab.Problems.BinPacking/3.3/3D/Instances/RealWorldContainerPackingInstanceProvider.cs
r15228 r15229 30 30 31 31 namespace HeuristicLab.Problems.BinPacking3D { 32 public class RealisticInstanceProvider : ProblemInstanceProvider<BPPData> { 33 protected virtual string FileName { get { return "3D-Instances"; } } 34 35 protected int binWidth, binHeight, binDepth; 36 32 public sealed class RealWorldContainerPackingInstanceProvider : ProblemInstanceProvider<BPPData> { 33 private string FileName { get { return "ContainerPackingInstances"; } } 34 37 35 public override string Name { 38 get { return "Real istic"; }36 get { return "Real-world Container Packing"; } 39 37 } 40 38 41 39 public override string Description { 42 get { return "Problem instances derived from real-world data."; }40 get { return "Problem instances derived from real-world container packing data."; } 43 41 } 44 42 … … 51 49 } 52 50 53 public Real isticInstanceProvider() : base() { }51 public RealWorldContainerPackingInstanceProvider() : base() { } 54 52 55 53 public override IEnumerable<IDataDescriptor> GetDataDescriptors() { -
branches/BinPackingExtension/HeuristicLab.Problems.BinPacking/3.3/3D/Instances/ThreeDInstanceParser.cs
r14838 r15229 34 34 public void Parse(Stream stream) { 35 35 var reader = new StreamReader(stream); 36 var length = GetNextInteger(reader) .Value;37 var width = GetNextInteger(reader) .Value;38 var height = GetNextInteger(reader) .Value;39 var maxWeight = GetNextInteger(reader) .Value;36 var length = GetNextInteger(reader); 37 var width = GetNextInteger(reader); 38 var height = GetNextInteger(reader); 39 var maxWeight = GetNextInteger(reader); 40 40 Bin = new PackingShape(width, height, length); 41 41 Items = new List<PackingItem>(); 42 42 while (true) { 43 43 try { 44 var id = GetNextInteger(reader).Value; 45 var pieces = GetNextInteger(reader).Value; 46 length = GetNextInteger(reader).Value; 47 width = GetNextInteger(reader).Value; 48 height = GetNextInteger(reader).Value; 49 var weight = GetNextInteger(reader).Value; 50 var stack = GetNextInteger(reader).Value; 51 var rotate = GetNextInteger(reader).Value; 52 var tilt = GetNextInteger(reader).Value; 44 var id = GetNextInteger(reader); 45 var pieces = GetNextInteger(reader); 46 length = GetNextInteger(reader); 47 width = GetNextInteger(reader); 48 height = GetNextInteger(reader); 49 var weight = GetNextInteger(reader); 50 var stack = GetNextInteger(reader); 51 var material = GetNextInteger(reader); 52 var rotate = GetNextInteger(reader); 53 var tilt = GetNextInteger(reader); 53 54 for (var i = 0; i < pieces; i++) { 54 Items.Add(new PackingItem(width, height, length, Bin, weight, 1));55 Items.Add(new PackingItem(width, height, length, Bin, weight, material)); 55 56 } 56 57 } catch (InvalidOperationException) { … … 60 61 } 61 62 62 private int ?GetNextInteger(StreamReader reader) {63 private int GetNextInteger(StreamReader reader) { 63 64 var next = reader.Read(); 64 65 var builder = new StringBuilder(); 65 66 while (next >= 0 && !char.IsDigit((char)next)) next = reader.Read(); 66 if (next == -1) return null;67 if (next == -1) throw new InvalidOperationException("No further integer available"); 67 68 while (char.IsDigit((char)next)) { 68 69 builder.Append((char)next); -
branches/BinPackingExtension/HeuristicLab.Problems.BinPacking/3.3/3D/IntegerVectorEncoding/BottomLeftIntegerVectorDecoder.cs
r14167 r15229 54 54 55 55 protected override PackingPosition FindPositionForItem(BinPacking3D bp, PackingItem item, bool useStackingConstraints) { 56 return bp.FindPositionBySliding(item, rotated: false );56 return bp.FindPositionBySliding(item, rotated: false, stackingConstraints: useStackingConstraints); 57 57 } 58 58 … … 61 61 ref IList<int> remainingIDs, IList<PackingItem> items, bool useStackingConstraints) { 62 62 var bp = new BinPacking3D(partialSolution.BinShape); 63 bp.SlidingBasedPacking(ref remainingIDs, items );63 bp.SlidingBasedPacking(ref remainingIDs, items, useStackingConstraints); 64 64 return bp; 65 65 } -
branches/BinPackingExtension/HeuristicLab.Problems.BinPacking/3.3/3D/PermutationEncoding/BottomLeftPermutationDecoder.cs
r14167 r15229 46 46 while (remainingIDs.Count > 0) { 47 47 var bp = new BinPacking3D(binShape); 48 bp.SlidingBasedPacking(ref remainingIDs, items );48 bp.SlidingBasedPacking(ref remainingIDs, items, useStackingConstraints); 49 49 result.Bins.Add(bp); 50 50 } -
branches/BinPackingExtension/HeuristicLab.Problems.BinPacking/3.3/3D/PermutationEncoding/ExtremePointPermutationDecoder.cs
r14976 r15229 45 45 46 46 public override Solution Decode(Permutation permutation, PackingShape binShape, IList<PackingItem> items, bool useStackingConstraints) { 47 return Apply(permutation, binShape, items, useStackingConstraints); 48 } 49 50 public static Solution Apply(Permutation permutation, PackingShape binShape, IList<PackingItem> items, bool useStackingConstraints) { 47 51 Solution result = new Solution(binShape, useExtremePoints: true, stackingConstraints: useStackingConstraints); 48 52 IList<int> remainingIDs = new List<int>(permutation); -
branches/BinPackingExtension/HeuristicLab.Problems.BinPacking/3.3/3D/PermutationEncoding/ExtremePointPermutationDecoderBase.cs
r14976 r15229 19 19 */ 20 20 #endregion 21 22 using System; 21 23 22 using System.Collections.Generic; 24 using System.Linq;25 23 using HeuristicLab.Common; 26 24 using HeuristicLab.Core; … … 41 39 42 40 public abstract Solution Decode(Permutation permutation, PackingShape binShape, IList<PackingItem> items, bool useStackingConstraints); 43 44 public virtual bool FindExtremePointForItem(Dictionary<Tuple<BinPacking3D, PackingPosition>, int> PointsSpace, PackingItem item, bool rotated, bool stackingConstraints, out BinPacking3D bin, out PackingPosition pos) {45 46 PackingItem newItem = new PackingItem(47 rotated ? item.Depth : item.Width,48 item.Height,49 rotated ? item.Width : item.Depth,50 item.TargetBin, item.Weight, item.Material);51 52 var EPoints = PointsSpace.Keys.ToList();53 BinPacking3D bp;54 PackingPosition position;55 int epIndex = 0;56 while (epIndex < EPoints.Count && (57 !(((bp = EPoints.ElementAt(epIndex).Item1)).IsPositionFeasible(newItem, (position = EPoints.ElementAt(epIndex).Item2)))58 || !bp.IsSupportedByAtLeastOnePoint(newItem, position)59 || (stackingConstraints && !bp.IsStaticStable(newItem, position))60 || (stackingConstraints && !bp.IsWeightSupported(newItem, position))61 )) { epIndex++; }62 63 if (epIndex < EPoints.Count) {64 bin = EPoints.ElementAt(epIndex).Item1;65 var origPoint = EPoints.ElementAt(epIndex).Item2;66 pos = new PackingPosition(origPoint.AssignedBin, origPoint.X, origPoint.Y, origPoint.Z, rotated);67 68 return true;69 }70 bin = null;71 pos = null;72 return false;73 }74 75 public virtual Dictionary<Tuple<BinPacking3D, PackingPosition>, int> GetResidualSpaceAllPoints(Solution solution, PackingItem item) {76 Dictionary<Tuple<BinPacking3D, PackingPosition>, int> result = new Dictionary<Tuple<BinPacking3D, PackingPosition>, int>();77 foreach (BinPacking3D bp in solution.Bins) {78 foreach (var ep in bp.ExtremePoints) {79 result.Add(Tuple.Create(bp, ep), bp.GetResidualSpace(item, ep));80 }81 }82 return result;83 }84 85 public virtual bool ExtremePointBasedPacking(Dictionary<Tuple<BinPacking3D, PackingPosition>, int> PointsSpace, int itemID, IList<PackingItem> items, bool stackingConstraints) {86 BinPacking3D bp;87 PackingPosition positionFound;88 var item = items[itemID];89 if (FindExtremePointForItem(PointsSpace, item, false, stackingConstraints, out bp, out positionFound)) {90 bp.PackItem(itemID, item, positionFound);91 return true;92 }93 return false;94 }95 41 } 96 42 } -
branches/BinPackingExtension/HeuristicLab.Problems.BinPacking/3.3/3D/PermutationEncoding/FreeVolumeBestFitExtremePointPermutationDecoder.cs
r14976 r15229 44 44 45 45 public override Solution Decode(Permutation permutation, PackingShape binShape, IList<PackingItem> items, bool useStackingConstraints) { 46 return Apply(permutation, binShape, items, useStackingConstraints); 47 } 48 49 public static Solution Apply(Permutation permutation, PackingShape binShape, IList<PackingItem> items, bool useStackingConstraints) { 46 50 Solution result = new Solution(binShape, useExtremePoints: true, stackingConstraints: useStackingConstraints); 47 51 IList<int> remainingIDs = new List<int>(permutation); … … 51 55 foreach (int ID in remainingIDs) { 52 56 var sortedBins = result.Bins.OrderBy(x => x.FreeVolume); 53 var notPacked = 0; 54 foreach (BinPacking3D bp in sortedBins) { 55 if (!bp.ExtremePointBasedPacking(ID, items, stackingConstraints: useStackingConstraints)) { notPacked++; } else break; 57 var item = items[ID]; 58 var posFound = false; 59 foreach (var bp in sortedBins) { 60 var pos = bp.FindExtremePointForItem(item, false, useStackingConstraints); 61 posFound = pos != null; 62 if (posFound) { 63 bp.PackItem(ID, item, pos); 64 break; 65 } 56 66 } 57 if ( notPacked == result.NrOfBins) {67 if (!posFound) { 58 68 var bp = new BinPacking3D(binShape); 59 bp.ExtremePointBasedPacking(ID, items, stackingConstraints: useStackingConstraints); 69 var pos = bp.FindExtremePointForItem(item, false, useStackingConstraints); 70 if (pos == null) throw new InvalidOperationException("Item " + ID + " cannot be packed in empty bin."); 71 bp.PackItem(ID, item, pos); 60 72 result.Bins.Add(bp); 61 73 } -
branches/BinPackingExtension/HeuristicLab.Problems.BinPacking/3.3/3D/PermutationEncoding/ResidualSpaceBestFitExtremePointPermutationDecoder.cs
r14976 r15229 44 44 45 45 public override Solution Decode(Permutation permutation, PackingShape binShape, IList<PackingItem> items, bool useStackingConstraints) { 46 return Apply(permutation, binShape, items, useStackingConstraints); 47 } 48 49 public static Solution Apply(Permutation permutation, PackingShape binShape, IList<PackingItem> items, bool useStackingConstraints) { 46 50 Solution result = new Solution(binShape, useExtremePoints: true, stackingConstraints: useStackingConstraints); 47 51 IList<int> remainingIDs = new List<int>(permutation); 48 Dictionary<Tuple<BinPacking3D,PackingPosition>, int> points = new Dictionary<Tuple<BinPacking3D,PackingPosition>,int>();49 52 var bpg = new BinPacking3D(binShape); 50 53 bpg.ExtremePointBasedPacking(ref remainingIDs, items, stackingConstraints: useStackingConstraints); 51 54 result.Bins.Add(bpg); 52 55 foreach (int ID in remainingIDs) { 53 points = GetResidualSpaceAllPoints(result, items[ID]); 54 var sortedPoints = points.OrderByDescending(x => x.Value).ToDictionary(x => x.Key, x => x.Value); 55 if (!ExtremePointBasedPacking(sortedPoints, ID, items, stackingConstraints: useStackingConstraints)) { 56 var item = items[ID]; 57 var points = GetResidualSpaceAllPoints(result, item); 58 var sortedPoints = points.OrderBy(x => x.Item3); 59 var packed = false; 60 foreach (var p in sortedPoints) { 61 packed = p.Item1.PackItemIfFeasible(ID, item, p.Item2, useStackingConstraints); 62 if (packed) break; 63 } 64 if (!packed) { 65 // pack item in a new bin 56 66 var bp = new BinPacking3D(binShape); 57 bp.ExtremePointBasedPacking(ID, items, stackingConstraints: useStackingConstraints); 67 var positionFound = bp.FindExtremePointForItem(item, false, useStackingConstraints); 68 if (positionFound != null) { 69 bp.PackItem(ID, item, positionFound); 70 } else throw new InvalidOperationException("Item " + ID + " cannot be packed in an empty bin."); 58 71 result.Bins.Add(bp); 59 72 } … … 62 75 return result; 63 76 } 77 78 public static IList<Tuple<BinPacking3D, PackingPosition, int>> GetResidualSpaceAllPoints(Solution solution, PackingItem item) { 79 var result = new List<Tuple<BinPacking3D, PackingPosition, int>>(); 80 foreach (BinPacking3D bp in solution.Bins) { 81 foreach (var ep in bp.ExtremePoints) { 82 var rs = bp.ResidualSpace[ep]; 83 if (rs.Item1 < item.Width || rs.Item2 < item.Height || rs.Item3 < item.Depth) continue; 84 result.Add(Tuple.Create(bp, ep, GetResidualMerit(rs, item, ep))); 85 } 86 } 87 return result; 88 } 89 90 private static int GetResidualMerit(Tuple<int, int, int> rs, PackingItem item, PackingPosition ep) { 91 return ((rs.Item1 - item.Width) + 92 (rs.Item2 - item.Height) + 93 (rs.Item3 - item.Depth)); 94 } 64 95 } 65 96 } -
branches/BinPackingExtension/HeuristicLab.Problems.BinPacking/3.3/Algorithms/3D/ExtremePointAlgorithm.cs
r15200 r15229 41 41 [StorableClass] 42 42 [Creatable(CreatableAttribute.Categories.SingleSolutionAlgorithms, Priority = 180)] 43 public class ExtremePointAlgorithm : BasicAlgorithm {43 public sealed class ExtremePointAlgorithm : BasicAlgorithm { 44 44 45 45 public override Type ProblemType { … … 57 57 58 58 [Storable] 59 private IValueParameter<EnumValue<SortingMethod>> sortingMethodParameter;59 private readonly IValueParameter<EnumValue<SortingMethod>> sortingMethodParameter; 60 60 public IValueParameter<EnumValue<SortingMethod>> SortingMethodParameter { 61 61 get { return sortingMethodParameter; } … … 63 63 64 64 [Storable] 65 private IValueParameter<EnumValue<FittingMethod>> fittingMethodParameter;65 private readonly IValueParameter<EnumValue<FittingMethod>> fittingMethodParameter; 66 66 public IValueParameter<EnumValue<FittingMethod>> FittingMethodParameter { 67 67 get { return fittingMethodParameter; } … … 69 69 70 70 [Storable] 71 private IValueParameter<PercentValue> deltaParameter;71 private readonly IValueParameter<PercentValue> deltaParameter; 72 72 public IValueParameter<PercentValue> DeltaParameter { 73 73 get { return deltaParameter; } … … 75 75 76 76 [StorableConstructor] 77 pr otectedExtremePointAlgorithm(bool deserializing) : base(deserializing) { }78 pr otectedExtremePointAlgorithm(ExtremePointAlgorithm original, Cloner cloner)77 private ExtremePointAlgorithm(bool deserializing) : base(deserializing) { } 78 private ExtremePointAlgorithm(ExtremePointAlgorithm original, Cloner cloner) 79 79 : base(original, cloner) { 80 80 sortingMethodParameter = cloner.Clone(original.sortingMethodParameter); … … 112 112 if (result == null) throw new InvalidOperationException("No result obtained!"); 113 113 114 Results.Add(new Result("Best Solution", result.Item1)); 115 Results.Add(new Result("Best Solution Quality", new DoubleValue(result.Item2))); 114 Results.Add(new Result("Best Solution", 115 "The best found solution", 116 result.Item1)); 117 Results.Add(new Result("Best Solution Quality", 118 "The quality of the best found solution according to the evaluator", 119 new DoubleValue(result.Item2))); 116 120 117 121 var binUtil = new BinUtilizationEvaluator(); 118 122 var packRatio = new PackingRatioEvaluator(); 119 Results.Add(new Result("Best Solution Bin Count", new IntValue(result.Item1.NrOfBins))); 120 Results.Add(new Result("Best Solution Bin Utilization", new PercentValue(Math.Round(binUtil.Evaluate(result.Item1), 3)))); 123 Results.Add(new Result("Best Solution Bin Count", 124 "The number of bins in the best found solution", 125 new IntValue(result.Item1.NrOfBins))); 126 Results.Add(new Result("Best Solution Bin Utilization", 127 "The utilization given in percentage as calculated by the BinUtilizationEvaluator (total used space / total available space)", 128 new PercentValue(Math.Round(binUtil.Evaluate(result.Item1), 3)))); 121 129 122 130 if (result.Item3.HasValue && sorting.Length > 1) 123 Results.Add(new Result("Best Sorting Method", new EnumValue<SortingMethod>(result.Item3.Value))); 131 Results.Add(new Result("Best Sorting Method", 132 "The sorting method that found the best solution", 133 new EnumValue<SortingMethod>(result.Item3.Value))); 124 134 if (result.Item4.HasValue && fitting.Length > 1) 125 Results.Add(new Result("Best Fitting Method", new EnumValue<FittingMethod>(result.Item4.Value))); 135 Results.Add(new Result("Best Fitting Method", 136 "The fitting method that found the best solution", 137 new EnumValue<FittingMethod>(result.Item4.Value))); 126 138 } 127 139 -
branches/BinPackingExtension/HeuristicLab.Problems.BinPacking/3.3/BinPacking.cs
r14976 r15229 90 90 91 91 public abstract TPos FindExtremePointForItem(TItem item, bool rotated, bool stackingConstraints); 92 public abstract TPos FindPositionBySliding(TItem item, bool rotated );92 public abstract TPos FindPositionBySliding(TItem item, bool rotated, bool stackingConstraints); 93 93 94 public abstract void SlidingBasedPacking(ref IList<int> sequence, IList<TItem> items );95 public abstract void SlidingBasedPacking(ref IList<int> sequence, IList<TItem> items, Dictionary<int, bool> rotationArray );94 public abstract void SlidingBasedPacking(ref IList<int> sequence, IList<TItem> items, bool stackingConstraints); 95 public abstract void SlidingBasedPacking(ref IList<int> sequence, IList<TItem> items, Dictionary<int, bool> rotationArray, bool stackingConstraints); 96 96 public abstract void ExtremePointBasedPacking(ref IList<int> sequence, IList<TItem> items, bool stackingConstraints); 97 public abstract bool ExtremePointBasedPacking(int ID, IList<TItem> items, bool stackingConstraints);98 97 public abstract void ExtremePointBasedPacking(ref IList<int> sequence, IList<TItem> items, bool stackingConstraints, Dictionary<int, bool> rotationArray); 99 98 100 public v oid PackItem(int itemID, TItem item, TPos position) {99 public virtual void PackItem(int itemID, TItem item, TPos position) { 101 100 Items[itemID] = item; 102 101 Positions[itemID] = position; … … 107 106 108 107 AddNewItemToOccupationLayers(itemID, item, position); 108 } 109 public virtual bool PackItemIfFeasible(int itemID, TItem item, TPos position, bool stackingConstraints) { 110 if (IsPositionFeasible(item, position, stackingConstraints)) { 111 PackItem(itemID, item, position); 112 return true; 113 } 114 return false; 109 115 } 110 116 … … 135 141 return false; 136 142 } 137 public bool IsPositionFeasible(TItem item, TPos position) {143 public virtual bool IsPositionFeasible(TItem item, TPos position, bool stackingConstraints) { 138 144 //In this case feasability is defined as following: 1. the item fits into the bin-borders; 2. the point is supported by something; 3. the item does not collide with another already packed item 139 145 if (!BinShape.Encloses(position, item)) … … 147 153 return true; 148 154 } 155 149 156 public abstract int ShortestPossibleSideFromPoint(TPos position); 150 157 public abstract bool IsStaticStable(TItem measures, TPos position); -
branches/BinPackingExtension/HeuristicLab.Problems.BinPacking/3.3/HeuristicLab.Problems.BinPacking-3.3.csproj
r15183 r15229 172 172 <Compile Include="3D\Instances\BPPData.cs" /> 173 173 <Compile Include="3D\Instances\RandomDataDescriptor.cs" /> 174 <Compile Include="3D\Instances\Real isticInstanceProvider.cs" />174 <Compile Include="3D\Instances\RealWorldContainerPackingInstanceProvider.cs" /> 175 175 <Compile Include="3D\Instances\RandomInstanceProvider.cs" /> 176 176 <Compile Include="3D\Instances\ThreeDInstanceParser.cs" /> … … 213 213 </ItemGroup> 214 214 <ItemGroup> 215 <EmbeddedResource Include="3D\Instances\ 3D-Instances.zip" />215 <EmbeddedResource Include="3D\Instances\ContainerPackingInstances.zip" /> 216 216 <None Include="HeuristicLab.snk" /> 217 217 <None Include="Properties\AssemblyInfo.cs.frame" />
Note: See TracChangeset
for help on using the changeset viewer.