Changeset 14055 for branches/HeuristicLab.BinPacking
- Timestamp:
- 07/13/16 09:30:01 (8 years ago)
- Location:
- branches/HeuristicLab.BinPacking
- Files:
-
- 1 deleted
- 11 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.2D/3.3/BinPacking2D.cs
r14049 r14055 85 85 } 86 86 87 public override PackingPosition FindExtremePointForItem(PackingItem measures, bool rotated, bool stackingConstraints) {88 PackingItem item = new PackingItem(89 rotated ? measures.Height : measures.Width,90 rotated ? measures.Width : measures.Height,91 measures.TargetBin);87 public override PackingPosition FindExtremePointForItem(PackingItem item, bool rotated, bool stackingConstraints) { 88 PackingItem rotatedItem = new PackingItem( 89 rotated ? item.Height : item.Width, 90 rotated ? item.Width : item.Height, 91 item.TargetBin); 92 92 93 93 int epIndex = 0; 94 while (epIndex < ExtremePoints.Count && (!IsPositionFeasible( item, ExtremePoints.ElementAt(epIndex)))) { epIndex++; }94 while (epIndex < ExtremePoints.Count && (!IsPositionFeasible(rotatedItem, ExtremePoints.ElementAt(epIndex)))) { epIndex++; } 95 95 96 96 if (epIndex < ExtremePoints.Count) { … … 102 102 return null; 103 103 } 104 public override PackingPosition FindPositionBySliding(PackingItem measures, bool rotated) {104 public override PackingPosition FindPositionBySliding(PackingItem item, bool rotated) { 105 105 PackingPosition currentPosition = new PackingPosition(0, 106 BinMeasures.Width - (rotated ? measures.Height : measures.Width),107 BinMeasures.Height - (rotated ? measures.Width : measures.Height), rotated);106 BinMeasures.Width - (rotated ? item.Height : item.Width), 107 BinMeasures.Height - (rotated ? item.Width : item.Height), rotated); 108 108 //Slide the item as far as possible to the left 109 while (IsPositionFeasible( measures, PackingPosition.MoveLeft(currentPosition))110 || IsPositionFeasible( measures, PackingPosition.MoveDown(currentPosition))) {109 while (IsPositionFeasible(item, PackingPosition.MoveLeft(currentPosition)) 110 || IsPositionFeasible(item, PackingPosition.MoveDown(currentPosition))) { 111 111 //Slide the item as far as possible to the bottom 112 while (IsPositionFeasible( measures, PackingPosition.MoveDown(currentPosition))) {112 while (IsPositionFeasible(item, PackingPosition.MoveDown(currentPosition))) { 113 113 currentPosition = PackingPosition.MoveDown(currentPosition); 114 114 } 115 if (IsPositionFeasible( measures, PackingPosition.MoveLeft(currentPosition)))115 if (IsPositionFeasible(item, PackingPosition.MoveLeft(currentPosition))) 116 116 currentPosition = PackingPosition.MoveLeft(currentPosition); 117 117 } 118 118 119 return IsPositionFeasible( measures, currentPosition) ? currentPosition : null;120 } 121 122 public override void SlidingBasedPacking(ref List<int> sequence, ItemList<PackingItem> item Measures) {119 return IsPositionFeasible(item, currentPosition) ? currentPosition : null; 120 } 121 122 public override void SlidingBasedPacking(ref List<int> sequence, ItemList<PackingItem> items) { 123 123 var temp = new List<int>(sequence); 124 124 for (int i = 0; i < temp.Count; i++) { 125 var item = item Measures[temp[i]];125 var item = items[temp[i]]; 126 126 var position = FindPositionBySliding(item, false); 127 127 if (position != null) { … … 131 131 } 132 132 } 133 public override void SlidingBasedPacking(ref List<int> sequence, ItemList<PackingItem> item Measures, Dictionary<int, bool> rotationArray) {133 public override void SlidingBasedPacking(ref List<int> sequence, ItemList<PackingItem> items, Dictionary<int, bool> rotationArray) { 134 134 var temp = new List<int>(sequence); 135 135 for (int i = 0; i < temp.Count; i++) { 136 var item = item Measures[temp[i]];136 var item = items[temp[i]]; 137 137 var position = FindPositionBySliding(item, rotationArray[temp[i]]); 138 138 if (position != null) { … … 142 142 } 143 143 } 144 public override void ExtremePointBasedPacking(ref List<int> sequence, ItemList<PackingItem> item Measures, bool stackingConstraints) {144 public override void ExtremePointBasedPacking(ref List<int> sequence, ItemList<PackingItem> items, bool stackingConstraints) { 145 145 var temp = new List<int>(sequence); 146 146 foreach (int itemID in temp) { 147 var item = item Measures[itemID];147 var item = items[itemID]; 148 148 var positionFound = FindExtremePointForItem(item, false, false); 149 149 if (positionFound != null) { … … 153 153 } 154 154 } 155 public override void ExtremePointBasedPacking(ref List<int> sequence, ItemList<PackingItem> item Measures, bool stackingConstraints, Dictionary<int, bool> rotationArray) {155 public override void ExtremePointBasedPacking(ref List<int> sequence, ItemList<PackingItem> items, bool stackingConstraints, Dictionary<int, bool> rotationArray) { 156 156 var temp = new List<int>(sequence); 157 157 foreach (int itemID in temp) { 158 var item = item Measures[itemID];158 var item = items[itemID]; 159 159 var positionFound = FindExtremePointForItem(item, rotationArray[itemID], false); 160 160 if (positionFound != null) { -
branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.2D/3.3/HeuristicLab.Problems.BinPacking2D-3.3.csproj
r14053 r14055 169 169 <Compile Include="Instances\BPPORLIBDataDescriptor.cs" /> 170 170 <Compile Include="Instances\BPPORLIBParser.cs" /> 171 <Compile Include="Instances\RealBPPData.cs" />172 171 <Compile Include="Interfaces\I2DGVDecoder.cs" /> 173 172 <Compile Include="Interfaces\I2DMCVDecoder.cs" /> -
branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.2D/3.3/Instances/BPPData.cs
r14053 r14055 23 23 24 24 namespace HeuristicLab.Problems.BinPacking2D { 25 /// <summary>26 /// Describes instances of the regular two dimensional bin packing problem (2DBPP).27 25 28 /// </summary>29 30 // TODO implement specifically for 2d and 3d instances31 26 public class BPPData { 32 27 /// <summary> … … 42 37 /// The number of items. 43 38 /// </summary> 44 public int Items { get; set; } 45 /// <summary> 46 /// The measures of the bin. 47 /// </summary> 48 public int[] BinMeasures { get; set; } 49 /// <summary> 50 /// The measures of the items. 51 /// </summary> 52 public int[][] ItemMeasures { get; set; } 53 39 public int NumItems { get { return Items == null ? 0 : Items.Length; } } 40 public PackingShape BinShape { get; set; } 41 public PackingItem[] Items { get; set; } 54 42 /// <summary> 55 43 /// Optional! The quality of the best-known solution. -
branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.2D/3.3/Instances/BPPInstanceProvider.cs
r14053 r14055 42 42 43 43 public override Uri WebLink { 44 get { return new Uri("http://people.brunel.ac.uk/~mastjjb/jeb/orlib/binpacktwoinfo.html , http://www.diku.dk/~pisinger/new3dbpp/readme.3dbpp"); }44 get { return new Uri("http://people.brunel.ac.uk/~mastjjb/jeb/orlib/binpacktwoinfo.html"); } 45 45 } 46 46 … … 74 74 75 75 using (var stream = entry.Open()) { 76 var parser = new BPPORLIBParser(); 77 parser.Parse(stream); 78 var instance = Load(parser); 76 var instance = BPPORLIBParser.Parse(stream); 79 77 instance.Name = id.Name; 80 78 instance.Description = id.Description; … … 89 87 } 90 88 public override BPPData ImportData(string path) { 91 var parser = new BPPORLIBParser(); 92 parser.Parse(path); 93 var instance = Load(parser); 89 var instance = BPPORLIBParser.Parse(path); 94 90 instance.Name = Path.GetFileName(path); 95 91 instance.Description = "Loaded from file \"" + path + "\" on " + DateTime.Now; 96 return instance;97 }98 99 private BPPData Load(BPPORLIBParser parser) {100 var instance = new BPPData {101 Items = parser.Items,102 BinMeasures = parser.BinMeasures,103 ItemMeasures = parser.ItemMeasures104 };105 92 return instance; 106 93 } … … 111 98 112 99 public override void ExportData(BPPData instance, string path) { 113 var parser = new BPPORLIBParser { 114 Name = instance.Name, 115 Description = instance.Description, 116 Items = instance.Items, 117 BinMeasures = instance.BinMeasures, 118 ItemMeasures = instance.ItemMeasures 119 }; 120 parser.Export(path); 100 BPPORLIBParser.Export(instance, path); 121 101 } 122 102 -
branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.2D/3.3/Instances/BPPORLIBParser.cs
r14053 r14055 25 25 26 26 namespace HeuristicLab.Problems.BinPacking2D { 27 public class BPPORLIBParser { 28 public string Name { get; set; } 29 public string Description { get; set; } 30 public int Items { get; set; } 31 public int[] BinMeasures { get; set; } 32 public int[][] ItemMeasures { get; set; } 33 34 public BPPORLIBParser() { 35 Reset(); 36 } 37 38 public void Reset() { 39 Name = Description = String.Empty; 40 Items = 0; 41 BinMeasures = null; 42 ItemMeasures = null; 43 } 44 45 public void Parse(string file) { 27 public static class BPPORLIBParser { 28 public static BPPData Parse(string file) { 46 29 using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read)) { 47 Parse(stream);30 return Parse(stream); 48 31 } 49 32 } 50 33 51 public void Export(string file) {34 public static void Export(BPPData instance, string file) { 52 35 using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read)) { 53 Export( stream);36 Export(instance, stream); 54 37 } 55 38 } … … 62 45 /// </remarks> 63 46 /// <param name="stream">The stream to read data from.</param> 64 /// <returns>True if the file was successfully read or false otherwise.</returns> 65 public void Parse(Stream stream) { 66 var reader = new StreamReader(stream); 67 var delim = new char[] { ' ', '\t' }; 68 var problemClass = reader.ReadLine().Split(delim, StringSplitOptions.RemoveEmptyEntries); 47 /// <returns>The parsed problem instance</returns> 48 public static BPPData Parse(Stream stream) { 49 using (var reader = new StreamReader(stream)) { 50 var instance = new BPPData(); 51 var delim = new char[] { ' ', '\t' }; 52 var problemClass = reader.ReadLine().Split(delim, StringSplitOptions.RemoveEmptyEntries); 69 53 70 var nrOfItems = reader.ReadLine().Split(delim, StringSplitOptions.RemoveEmptyEntries); 71 Items = int.Parse(nrOfItems[0]); 72 //Numbering of instances.. 73 reader.ReadLine(); 74 var binMeasures = reader.ReadLine().Split(delim, StringSplitOptions.RemoveEmptyEntries); 75 int depth; 76 bool isThreeDimensional = false; 77 if (int.TryParse(binMeasures[2], out depth)) { 78 isThreeDimensional = true; 79 BinMeasures = new int[3]; 80 BinMeasures[0] = int.Parse(binMeasures[0]); 81 BinMeasures[1] = int.Parse(binMeasures[1]); 82 BinMeasures[2] = depth; 83 } else { 84 BinMeasures = new int[2]; 85 BinMeasures[0] = int.Parse(binMeasures[0]); 86 BinMeasures[1] = int.Parse(binMeasures[1]); 54 var tok = reader.ReadLine().Split(delim, StringSplitOptions.RemoveEmptyEntries); 55 var numItems = int.Parse(tok[0]); 56 //Numbering of instances.. 57 reader.ReadLine(); 58 tok = reader.ReadLine().Split(delim, StringSplitOptions.RemoveEmptyEntries); 59 instance.BinShape = new PackingShape(int.Parse(tok[0]), int.Parse(tok[1])); 60 instance.Items = new PackingItem[numItems]; 61 62 for (int k = 0; k < numItems; k++) { 63 if (reader.EndOfStream) throw new InvalidDataException("Unexpected End of Stream (at line " + k + ")."); 64 var valLine = reader.ReadLine(); 65 while (String.IsNullOrWhiteSpace(valLine)) valLine = reader.ReadLine(); 66 tok = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries); 67 instance.Items[k] = new PackingItem(int.Parse(tok[0]), int.Parse(tok[1]), instance.BinShape) { Material = 1, Weight = 1.0 }; // these problem instances do not specify weights and materials 68 } 69 70 int problemClassNr = -1; 71 if (int.TryParse(problemClass[0], out problemClassNr)) { 72 instance.Name = "dbpp_class_0" + problemClassNr; 73 if (problemClassNr >= 1 && problemClassNr <= 5) 74 instance.Description = "Proposed by Berkey and Wang."; 75 else if (problemClassNr >= 6 && problemClassNr <= 10) 76 instance.Description = "Proposed by Martello and Vigo."; 77 } else { 78 instance.Name = "Unknown"; 79 instance.Description = "<Missing>"; 80 } 81 82 return instance; 87 83 } 88 ItemMeasures = new int[Items][];89 90 for (int k = 0; k < Items; k++) {91 if (reader.EndOfStream) throw new InvalidDataException("Unexpected End of Stream (at line " + k + ").");92 var valLine = reader.ReadLine();93 while (String.IsNullOrWhiteSpace(valLine)) valLine = reader.ReadLine();94 var vals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries);95 ItemMeasures[k] = new int[isThreeDimensional ? 3 : 2];96 ItemMeasures[k][0] = int.Parse(vals[0]);97 ItemMeasures[k][1] = int.Parse(vals[1]);98 if (isThreeDimensional)99 ItemMeasures[k][2] = int.Parse(vals[2]);100 }101 102 Int32 problemClassNr = -1;103 if (int.TryParse(problemClass[0], out problemClassNr)) {104 Name = (isThreeDimensional ? "3" : "2") + "dbpp_class_0" + problemClassNr;105 if (!isThreeDimensional) {106 if (problemClassNr >= 1 && problemClassNr <= 5)107 Description = "Proposed by Berkey and Wang.";108 else if (problemClassNr >= 6 && problemClassNr <= 10)109 Description = "Proposed by Martello and Vigo.";110 } else {111 if (problemClassNr >= 1 && problemClassNr <= 5)112 Description = "Proposed by Martello and Vigo.";113 else if (problemClassNr >= 6 && problemClassNr <= 9)114 Description = "Proposed by Berkey and Wang.";115 }116 } else {117 Name = "Unknown";118 Description = "<Missing>";119 }120 121 122 84 } 123 85 124 public void Export(Stream stream) { 125 bool isThreeDimensional = BinMeasures.Length == 3; 126 var nameParts = Name.Split('_'); 86 public static void Export(BPPData instance, Stream stream) { 87 var nameParts = instance.Name.Split('_'); 127 88 int problemClassNr = int.Parse(nameParts[2]); 128 89 129 var writer = new StreamWriter(stream);130 writer.WriteLine(String.Format("{0,-5} PROBLEM CLASS ({1})", problemClassNr,Description));131 writer.WriteLine(String.Format("{0,-5} {1,-5} {2,-5} WBIN,HBIN,DBIN", BinMeasures[0], BinMeasures[1], BinMeasures[2]));132 for (int i = 0; i <Items; i++) {133 if (i == 0)134 writer.WriteLine("{0,-5} {1,-5} {2,-5} W(I),H(I),D(I),I=1,...,N", ItemMeasures[i][0], ItemMeasures[i][1], ItemMeasures[i][2]);135 else136 writer.WriteLine("{0,-5} {1,-5} {2,-5}", ItemMeasures[i][0], ItemMeasures[i][1], ItemMeasures[i][2]);90 using (var writer = new StreamWriter(stream)) { 91 writer.WriteLine(String.Format("{0,-5} PROBLEM CLASS ({1})", problemClassNr, instance.Description)); 92 writer.WriteLine(String.Format("{0,-5} {1,-5} WBIN,HBIN", instance.BinShape.Width, instance.BinShape.Height)); 93 for (int i = 0; i < instance.NumItems; i++) { 94 if (i == 0) 95 writer.WriteLine("{0,-5} {1,-5} W(I),H(I),I=1,...,N", instance.Items[i].Width, instance.Items[i].Height); 96 else 97 writer.WriteLine("{0,-5} {1,-5}", instance.Items[i].Width, instance.Items[i].Height); 137 98 99 } 100 writer.Flush(); 138 101 } 139 writer.Flush();140 102 } 141 103 -
branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.2D/3.3/Problem.cs
r14054 r14055 44 44 Name = "2D BPP Default Instance", 45 45 Description = "The default instance for 2D Bin Packing.", 46 Bin Measures = new int[] { 20, 16 },47 Item Measures = new int[][] {48 new int[] {3,8},49 new int[] {5,3},50 new int[] {9,3},51 new int[] {2,7},52 new int[] {5,3},53 new int[] {9,3},54 new int[] {2,7},55 new int[] {5,3},56 new int[] {9,3},57 new int[] {2,7},58 new int[] {5,3},59 new int[] {9,3},60 new int[] {2,7},61 new int[] {5,3},62 new int[] {9,3},63 new int[] {2,7},64 new int[] {5,3},65 new int[] {9,3},66 new int[] {2,7},67 new int[] {5,3},68 new int[] {9,3},69 new int[] {2,7},70 new int[] {5,3},71 new int[] {9,3},72 new int[] {2,7},73 new int[] {5,3},74 new int[] {9,3},75 new int[] {2,7},76 new int[] {5,3},77 new int[] {9,3},78 new int[] {2,7},79 new int[] {5,3},80 new int[] {9,3},81 new int[] {2,7},82 new int[] {5,3},83 new int[] {9,3},84 new int[] {2,7}46 BinShape = new PackingShape(20, 16), 47 Items = new PackingItem[] { 48 new PackingItem(3, 8, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, 49 new PackingItem(5, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, 50 new PackingItem(9, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, 51 new PackingItem(2, 7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, 52 new PackingItem(5, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, 53 new PackingItem(9, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, 54 new PackingItem(2, 7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, 55 new PackingItem(5, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, 56 new PackingItem(9, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, 57 new PackingItem(2, 7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, 58 new PackingItem(5, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, 59 new PackingItem(9, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, 60 new PackingItem(2, 7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, 61 new PackingItem(5, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, 62 new PackingItem(9, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, 63 new PackingItem(2, 7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, 64 new PackingItem(5, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, 65 new PackingItem(9, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, 66 new PackingItem(2, 7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, 67 new PackingItem(5, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, 68 new PackingItem(9, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, 69 new PackingItem(2, 7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, 70 new PackingItem(5, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, 71 new PackingItem(9, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, 72 new PackingItem(2, 7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, 73 new PackingItem(5, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, 74 new PackingItem(9, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, 75 new PackingItem(2, 7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, 76 new PackingItem(5, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, 77 new PackingItem(9, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, 78 new PackingItem(2, 7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, 79 new PackingItem(5, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, 80 new PackingItem(9, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, 81 new PackingItem(2, 7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, 82 new PackingItem(5, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, 83 new PackingItem(9, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, 84 new PackingItem(2, 7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0} 85 85 }, 86 Items = 3087 86 }; 88 87 #endregion … … 91 90 protected Problem(bool deserializing) : base(deserializing) { } 92 91 protected Problem(Problem original, Cloner cloner) 93 : base(original, cloner) {92 : base(original, cloner) { 94 93 } 95 94 public override IDeepCloneable Clone(Cloner cloner) { … … 97 96 } 98 97 public Problem() : base( 99 new DecodingEvaluator<Permutation, PackingPosition, PackingShape, PackingItem>()) {98 new DecodingEvaluator<Permutation, PackingPosition, PackingShape, PackingItem>()) { 100 99 } 101 100 102 101 #region Problem instance handling 103 102 public void Load(BPPData data) { 104 var realData = data as RealBPPData;105 var binData = new PackingShape(data.BinMeasures[0], data.BinMeasures[1]);106 107 var itemData = new ItemList<PackingItem>(data.Items);108 for (int j = 0; j < data.Items; j++) {109 var bin = new PackingShape(data.BinMeasures[0], data.BinMeasures[1]);110 var item = new PackingItem(data.ItemMeasures[j][0], data.ItemMeasures[j][1], bin);111 if (realData != null) {112 item.Weight = realData.ItemWeights[j];113 item.Material = realData.ItemMaterials[j];114 }115 itemData.Add(item);116 }117 103 118 104 BestKnownQuality = data.BestKnownQuality.HasValue ? new DoubleValue(data.BestKnownQuality.Value) : null; 119 105 120 PackingBinMeasures = binData;121 PackingItemMeasures = itemData;106 PackingBinMeasures = data.BinShape; 107 PackingItemMeasures = new ItemList<PackingItem>(data.Items); 122 108 123 109 ApplyHorizontalOrientation(); … … 129 115 130 116 public BPPData Export() { 131 var result =new BPPData {117 return new BPPData { 132 118 Name = Name, 133 119 Description = Description, 134 Items = PackingItemsParameter.Value.Value,135 BinMeasures = new int[] { PackingBinMeasures.Width, PackingBinMeasures.Height }120 BinShape = PackingBinMeasures, 121 Items = PackingItemMeasures.ToArray() 136 122 }; 137 138 var itemMeasures = new int[result.Items][];139 int i = 0;140 foreach (var item in PackingItemMeasures) {141 itemMeasures[i] = new int[] { item.Width, item.Height };142 i++;143 }144 result.ItemMeasures = itemMeasures;145 return result;146 123 } 147 124 #endregion -
branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.3D/3.3/HeuristicLab.Problems.BinPacking3D-3.3.csproj
r14053 r14055 173 173 <Compile Include="Instances\BPPORLIBDataDescriptor.cs" /> 174 174 <Compile Include="Instances\BPPORLIBParser.cs" /> 175 <Compile Include="Instances\RealBPPData.cs" />176 175 <Compile Include="MoveEvaluators\PackingMoveEvaluator3DGV.cs" /> 177 176 <Compile Include="MoveEvaluators\PackingMoveEvaluator3DMCV.cs" /> -
branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.3D/3.3/Instances/BPPData.cs
r14053 r14055 23 23 24 24 namespace HeuristicLab.Problems.BinPacking3D { 25 /// <summary> 26 /// Describes instances of the regular three dimensional bin packing problem (3DBPP). 27 /// </summary> 25 28 26 public class BPPData { 29 27 /// <summary> … … 39 37 /// The number of items. 40 38 /// </summary> 41 public int Items { get; set; } 42 /// <summary> 43 /// The measures of the bin. 44 /// </summary> 45 public int[] BinMeasures { get; set; } 46 /// <summary> 47 /// The measures of the items. 48 /// </summary> 49 public int[][] ItemMeasures { get; set; } 50 39 public int NumItems { get { return Items == null ? 0 : Items.Length; } } 40 public PackingShape BinShape { get; set; } 41 public PackingItem[] Items { get; set; } 51 42 /// <summary> 52 43 /// Optional! The quality of the best-known solution. -
branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.3D/3.3/Instances/BPPInstanceProvider.cs
r14053 r14055 30 30 using HeuristicLab.Problems.Instances; 31 31 32 namespace HeuristicLab.Problems.BinPacking3D { 32 namespace HeuristicLab.Problems.BinPacking3D { 33 33 public class BPPORLIBInstanceProvider : ProblemInstanceProvider<BPPData>, IProblemInstanceProvider<BPPData> { 34 34 … … 42 42 43 43 public override Uri WebLink { 44 get { return new Uri("http:// people.brunel.ac.uk/~mastjjb/jeb/orlib/binpacktwoinfo.html , http://www.diku.dk/~pisinger/new3dbpp/readme.3dbpp"); }44 get { return new Uri("http://www.diku.dk/~pisinger/new3dbpp/readme.3dbpp"); } 45 45 } 46 46 … … 59 59 if (string.IsNullOrWhiteSpace(entry.Name)) continue; 60 60 yield return new BPPORLIBDataDescriptor( 61 name: Path.GetFileNameWithoutExtension(entry.Name), 62 description: GetDescription(), 63 instanceIdentifier: entry.FullName, 61 name: Path.GetFileNameWithoutExtension(entry.Name), 62 description: GetDescription(), 63 instanceIdentifier: entry.FullName, 64 64 solutionIdentifier: null); 65 65 } … … 74 74 75 75 using (var stream = entry.Open()) { 76 var parser = new BPPORLIBParser(); 77 parser.Parse(stream); 78 var instance = Load(parser); 76 var instance = BPPORLIBParser.Parse(stream); 79 77 instance.Name = id.Name; 80 78 instance.Description = id.Description; … … 89 87 } 90 88 public override BPPData ImportData(string path) { 91 var parser = new BPPORLIBParser(); 92 parser.Parse(path); 93 var instance = Load(parser); 89 var instance = BPPORLIBParser.Parse(path); 94 90 instance.Name = Path.GetFileName(path); 95 91 instance.Description = "Loaded from file \"" + path + "\" on " + DateTime.Now; 96 return instance;97 }98 99 private BPPData Load(BPPORLIBParser parser) {100 var instance = new BPPData {101 Items = parser.Items,102 BinMeasures = parser.BinMeasures,103 ItemMeasures = parser.ItemMeasures104 };105 92 return instance; 106 93 } … … 111 98 112 99 public override void ExportData(BPPData instance, string path) { 113 var parser = new BPPORLIBParser { 114 Name = instance.Name, 115 Description = instance.Description, 116 Items = instance.Items, 117 BinMeasures = instance.BinMeasures, 118 ItemMeasures = instance.ItemMeasures 119 }; 120 parser.Export(path); 100 BPPORLIBParser.Export(instance, path); 121 101 } 122 102 -
branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.3D/3.3/Instances/BPPORLIBParser.cs
r14053 r14055 25 25 26 26 namespace HeuristicLab.Problems.BinPacking3D { 27 public class BPPORLIBParser { 28 public string Name { get; set; } 29 public string Description { get; set; } 30 public int Items { get; set; } 31 public int[] BinMeasures { get; set; } 32 public int[][] ItemMeasures { get; set; } 33 34 public BPPORLIBParser() { 35 Reset(); 36 } 37 38 public void Reset() { 39 Name = Description = String.Empty; 40 Items = 0; 41 BinMeasures = null; 42 ItemMeasures = null; 43 } 44 45 public void Parse(string file) { 27 public static class BPPORLIBParser { 28 public static BPPData Parse(string file) { 46 29 using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read)) { 47 Parse(stream);30 return Parse(stream); 48 31 } 49 32 } 50 33 51 public void Export(string file) {34 public static void Export(BPPData instance, string file) { 52 35 using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read)) { 53 Export( stream);36 Export(instance, stream); 54 37 } 55 38 } … … 62 45 /// </remarks> 63 46 /// <param name="stream">The stream to read data from.</param> 64 /// <returns>True if the file was successfully read or false otherwise.</returns> 65 public void Parse(Stream stream) { 66 var reader = new StreamReader(stream); 67 var delim = new char[] { ' ', '\t' }; 68 var problemClass = reader.ReadLine().Split(delim, StringSplitOptions.RemoveEmptyEntries); 47 /// <returns>The parsed problem instance</returns> 48 public static BPPData Parse(Stream stream) { 49 using (var reader = new StreamReader(stream)) { 50 var instance = new BPPData(); 51 var delim = new char[] { ' ', '\t' }; 52 var problemClass = reader.ReadLine().Split(delim, StringSplitOptions.RemoveEmptyEntries); 69 53 70 var nrOfItems = reader.ReadLine().Split(delim, StringSplitOptions.RemoveEmptyEntries); 71 Items = int.Parse(nrOfItems[0]); 72 //Numbering of instances.. 73 reader.ReadLine(); 74 var binMeasures = reader.ReadLine().Split(delim, StringSplitOptions.RemoveEmptyEntries); 75 int depth; 76 bool isThreeDimensional = false; 77 if (int.TryParse(binMeasures[2], out depth)) { 78 isThreeDimensional = true; 79 BinMeasures = new int[3]; 80 BinMeasures[0] = int.Parse(binMeasures[0]); 81 BinMeasures[1] = int.Parse(binMeasures[1]); 82 BinMeasures[2] = depth; 83 } else { 84 BinMeasures = new int[2]; 85 BinMeasures[0] = int.Parse(binMeasures[0]); 86 BinMeasures[1] = int.Parse(binMeasures[1]); 54 var tok = reader.ReadLine().Split(delim, StringSplitOptions.RemoveEmptyEntries); 55 var numItems = int.Parse(tok[0]); 56 //Numbering of instances.. 57 reader.ReadLine(); 58 tok = reader.ReadLine().Split(delim, StringSplitOptions.RemoveEmptyEntries); 59 instance.BinShape = new PackingShape(int.Parse(tok[0]), int.Parse(tok[1]), int.Parse(tok[2])); 60 instance.Items = new PackingItem[numItems]; 61 62 for (int k = 0; k < numItems; k++) { 63 if (reader.EndOfStream) throw new InvalidDataException("Unexpected End of Stream (at line " + k + ")."); 64 var valLine = reader.ReadLine(); 65 while (String.IsNullOrWhiteSpace(valLine)) valLine = reader.ReadLine(); 66 tok = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries); 67 instance.Items[k] = new PackingItem(int.Parse(tok[0]), int.Parse(tok[1]), int.Parse(tok[2]), instance.BinShape) { Material = 1, Weight = 1.0 }; // these problem instances do not specify weights and materials 68 } 69 70 int problemClassNr = -1; 71 if (int.TryParse(problemClass[0], out problemClassNr)) { 72 instance.Name = "dbpp_class_0" + problemClassNr; 73 if (problemClassNr >= 1 && problemClassNr <= 5) 74 instance.Description = "Proposed by Martello and Vigo."; 75 else if (problemClassNr >= 6 && problemClassNr <= 9) 76 instance.Description = "Proposed by Berkey and Wang."; 77 } else { 78 instance.Name = "Unknown"; 79 instance.Description = "<Missing>"; 80 } 81 82 return instance; 87 83 } 88 ItemMeasures = new int[Items][];89 90 for (int k = 0; k < Items; k++) {91 if (reader.EndOfStream) throw new InvalidDataException("Unexpected End of Stream (at line " + k + ").");92 var valLine = reader.ReadLine();93 while (String.IsNullOrWhiteSpace(valLine)) valLine = reader.ReadLine();94 var vals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries);95 ItemMeasures[k] = new int[isThreeDimensional ? 3 : 2];96 ItemMeasures[k][0] = int.Parse(vals[0]);97 ItemMeasures[k][1] = int.Parse(vals[1]);98 if (isThreeDimensional)99 ItemMeasures[k][2] = int.Parse(vals[2]);100 }101 102 Int32 problemClassNr = -1;103 if (int.TryParse(problemClass[0], out problemClassNr)) {104 Name = (isThreeDimensional ? "3" : "2") + "dbpp_class_0" + problemClassNr;105 if (!isThreeDimensional) {106 if (problemClassNr >= 1 && problemClassNr <= 5)107 Description = "Proposed by Berkey and Wang.";108 else if (problemClassNr >= 6 && problemClassNr <= 10)109 Description = "Proposed by Martello and Vigo.";110 } else {111 if (problemClassNr >= 1 && problemClassNr <= 5)112 Description = "Proposed by Martello and Vigo.";113 else if (problemClassNr >= 6 && problemClassNr <= 9)114 Description = "Proposed by Berkey and Wang.";115 }116 } else {117 Name = "Unknown";118 Description = "<Missing>";119 }120 121 122 84 } 123 85 124 public void Export(Stream stream) { 125 bool isThreeDimensional = BinMeasures.Length == 3; 126 var nameParts = Name.Split('_'); 86 public static void Export(BPPData instance, Stream stream) { 87 var nameParts = instance.Name.Split('_'); 127 88 int problemClassNr = int.Parse(nameParts[2]); 128 89 129 var writer = new StreamWriter(stream);130 writer.WriteLine(String.Format("{0,-5} PROBLEM CLASS ({1})", problemClassNr,Description));131 writer.WriteLine(String.Format("{0,-5} {1,-5} {2,-5} WBIN,HBIN,DBIN", BinMeasures[0], BinMeasures[1], BinMeasures[2]));132 for (int i = 0; i <Items; i++) {133 if (i == 0)134 writer.WriteLine("{0,-5} {1,-5} {2,-5} W(I),H(I),D(I),I=1,...,N", ItemMeasures[i][0], ItemMeasures[i][1], ItemMeasures[i][2]);135 else136 writer.WriteLine("{0,-5} {1,-5} {2,-5}", ItemMeasures[i][0], ItemMeasures[i][1], ItemMeasures[i][2]);90 using (var writer = new StreamWriter(stream)) { 91 writer.WriteLine(String.Format("{0,-5} PROBLEM CLASS ({1})", problemClassNr, instance.Description)); 92 writer.WriteLine(String.Format("{0,-5} {1,-5} {2,-5} WBIN,HBIN", instance.BinShape.Width, instance.BinShape.Height, instance.BinShape.Depth)); 93 for (int i = 0; i < instance.NumItems; i++) { 94 if (i == 0) 95 writer.WriteLine("{0,-5} {1,-5} {2,-5} W(I),H(I)D(I),I=1,...,N", instance.Items[i].Width, instance.Items[i].Height, instance.Items[i].Depth); 96 else 97 writer.WriteLine("{0,-5} {1,-5} {2,-5}", instance.Items[i].Width, instance.Items[i].Height, instance.Items[i].Depth); 137 98 99 } 100 writer.Flush(); 138 101 } 139 writer.Flush();140 102 } 141 103 -
branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.3D/3.3/Problem.cs
r14054 r14055 37 37 [StorableClass] 38 38 [Creatable(CreatableAttribute.Categories.CombinatorialProblems, Priority = 310)] 39 // TODO don't support generic BPPData but only 3d BPPData40 39 public class Problem : Problem<PackingPosition, PackingShape, PackingItem>, IProblemInstanceConsumer<BPPData>, IProblemInstanceExporter<BPPData> { 41 42 43 40 #region Default Instance 44 41 private static readonly BPPData DefaultInstance = new BPPData() { 45 42 Name = "3D BPP Default Instance", 46 43 Description = "The default instance for 3D Bin Packing.", 47 Bin Measures = new int[] { 25, 25, 35 },48 Item Measures = new int[][] {49 new int[] {12,5,10},50 new int[] {10,18,20},51 new int[] {9,7,7},52 new int[] {21,12,4},53 new int[] {8,8,12},54 new int[] {3,6,14},55 new int[] {20,4,9},56 new int[] {5,9,8},57 new int[] {7,17,3},58 new int[] {13,20,15},59 new int[] {9,11,9},60 new int[] {10,18,20},61 new int[] {9,7,7},62 new int[] {21,12,4},63 new int[] {8,8,12},64 new int[] {3,6,14},65 new int[] {20,4,9},66 new int[] {5,9,8},67 new int[] {7,17,3},68 new int[] {13,20,15},69 new int[] {9,11,9},70 new int[] {10,18,20},71 new int[] {9,7,7},72 new int[] {21,12,4},73 new int[] {8,8,12},74 new int[] {3,6,14},75 new int[] {20,4,9},76 new int[] {5,9,8},77 new int[] {7,17,3},78 new int[] {13,20,15},79 new int[] {9,11,9},80 new int[] {10,18,20},81 new int[] {9,7,7},82 new int[] {21,12,4},83 new int[] {8,8,12},84 new int[] {3,6,14},85 new int[] {20,4,9},86 new int[] {5,9,8},87 new int[] {7,17,3},88 new int[] {13,20,15},89 new int[] {9,11,9}44 BinShape = new PackingShape(25, 25, 35), 45 Items = new PackingItem[] { 46 new PackingItem(12,5,10, new PackingShape(25,25,35)), 47 new PackingItem(10,18,20, new PackingShape(25,25,35)), 48 new PackingItem(9,7,7, new PackingShape(25,25,35)), 49 new PackingItem(21,12,4, new PackingShape(25,25,35)), 50 new PackingItem(8,8,12, new PackingShape(25,25,35)), 51 new PackingItem(3,6,14, new PackingShape(25,25,35)), 52 new PackingItem(20,4,9, new PackingShape(25,25,35)), 53 new PackingItem(5,9,8, new PackingShape(25,25,35)), 54 new PackingItem(7,17,3, new PackingShape(25,25,35)), 55 new PackingItem(13,20,15, new PackingShape(25,25,35)), 56 new PackingItem(9,11,9, new PackingShape(25,25,35)), 57 new PackingItem(10,18,20, new PackingShape(25,25,35)), 58 new PackingItem(9,7,7, new PackingShape(25,25,35)), 59 new PackingItem(21,12,4, new PackingShape(25,25,35)), 60 new PackingItem(8,8,12, new PackingShape(25,25,35)), 61 new PackingItem(3,6,14, new PackingShape(25,25,35)), 62 new PackingItem(20,4,9, new PackingShape(25,25,35)), 63 new PackingItem(5,9,8, new PackingShape(25,25,35)), 64 new PackingItem(7,17,3, new PackingShape(25,25,35)), 65 new PackingItem(13,20,15, new PackingShape(25,25,35)), 66 new PackingItem(9,11,9, new PackingShape(25,25,35)), 67 new PackingItem(10,18,20, new PackingShape(25,25,35)), 68 new PackingItem(9,7,7, new PackingShape(25,25,35)), 69 new PackingItem(21,12,4, new PackingShape(25,25,35)), 70 new PackingItem(8,8,12, new PackingShape(25,25,35)), 71 new PackingItem(3,6,14, new PackingShape(25,25,35)), 72 new PackingItem(20,4,9, new PackingShape(25,25,35)), 73 new PackingItem(5,9,8, new PackingShape(25,25,35)), 74 new PackingItem(7,17,3, new PackingShape(25,25,35)), 75 new PackingItem(13,20,15, new PackingShape(25,25,35)), 76 new PackingItem(9,11, 9,new PackingShape(25,25,35)), 77 new PackingItem(10,18,20, new PackingShape(25,25,35)), 78 new PackingItem(9,7,7, new PackingShape(25,25,35)), 79 new PackingItem(21,12,4, new PackingShape(25,25,35)), 80 new PackingItem(8,8,12, new PackingShape(25,25,35)), 81 new PackingItem(3,6,14, new PackingShape(25,25,35)), 82 new PackingItem(20,4,9, new PackingShape(25,25,35)), 83 new PackingItem(5,9,8, new PackingShape(25,25,35)), 84 new PackingItem(7,17,3, new PackingShape(25,25,35)), 85 new PackingItem(13,20,15, new PackingShape(25,25,35)), 86 new PackingItem(9,11,9, new PackingShape(25,25,35)) 90 87 }, 91 Items = 3092 88 }; 93 89 #endregion … … 105 101 } 106 102 103 public void Load(BPPData data) { 107 104 108 109 public void Load(BPPData data) {110 var realData = data as RealBPPData;111 var binData = new PackingShape(data.BinMeasures[0], data.BinMeasures[1], data.BinMeasures[2]);112 113 var itemData = new ItemList<PackingItem>(data.Items);114 for (int j = 0; j < data.Items; j++) {115 var bin = new PackingShape(data.BinMeasures[0], data.BinMeasures[1], data.BinMeasures[2]);116 var item = new PackingItem(data.ItemMeasures[j][0], data.ItemMeasures[j][1], data.ItemMeasures[j][2], bin);117 if (realData != null) {118 item.Weight = realData.ItemWeights[j];119 item.Material = realData.ItemMaterials[j];120 }121 itemData.Add(item);122 }123 105 124 106 BestKnownQuality = data.BestKnownQuality.HasValue ? new DoubleValue(data.BestKnownQuality.Value) : null; 125 107 126 PackingBinMeasures = binData;127 PackingItemMeasures = itemData;108 PackingBinMeasures = data.BinShape; 109 PackingItemMeasures = new ItemList<PackingItem>(data.Items); 128 110 129 111 ApplyHorizontalOrientation(); … … 134 116 135 117 public BPPData Export() { 136 var result =new BPPData {118 return new BPPData { 137 119 Name = Name, 138 120 Description = Description, 139 Items = PackingItemsParameter.Value.Value,140 BinMeasures = new int[] { PackingBinMeasures.Width, PackingBinMeasures.Height, PackingBinMeasures.Depth }121 BinShape = PackingBinMeasures, 122 Items = PackingItemMeasures.ToArray() 141 123 }; 142 143 var itemMeasures = new int[result.Items][];144 int i = 0;145 foreach (var item in PackingItemMeasures) {146 itemMeasures[i] = new int[] { item.Width, item.Height, item.Depth };147 i++;148 }149 result.ItemMeasures = itemMeasures;150 return result;151 124 } 152 125 -
branches/HeuristicLab.BinPacking/old files/RealBPPData.cs
r14054 r14055 35 35 /// </summary> 36 36 public int[] ItemMaterials { get; set; } 37 38 37 } 39 38 }
Note: See TracChangeset
for help on using the changeset viewer.