Changeset 14055 for branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.3D/3.3/Instances
- Timestamp:
- 07/13/16 09:30:01 (8 years ago)
- Location:
- branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.3D/3.3/Instances
- Files:
-
- 1 deleted
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
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
Note: See TracChangeset
for help on using the changeset viewer.