1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Globalization;
|
---|
24 | using System.IO;
|
---|
25 | using System.Linq;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Problems.Instances.BinPacking {
|
---|
28 | public class BPPORLIBParser {
|
---|
29 | public string Name { get; set; }
|
---|
30 | public string Description { get; set; }
|
---|
31 | public int Items { get; set; }
|
---|
32 | public int[] BinMeasures { get; set; }
|
---|
33 | public int[][] ItemMeasures { get; set; }
|
---|
34 |
|
---|
35 | public BPPORLIBParser() {
|
---|
36 | Reset();
|
---|
37 | }
|
---|
38 |
|
---|
39 | public void Reset() {
|
---|
40 | Name = Description = String.Empty;
|
---|
41 | Items = 0;
|
---|
42 | BinMeasures = null;
|
---|
43 | ItemMeasures = null;
|
---|
44 | }
|
---|
45 |
|
---|
46 | public void Parse(string file) {
|
---|
47 | using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read)) {
|
---|
48 | Parse(stream);
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | public void Export(string file) {
|
---|
53 | using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read)) {
|
---|
54 | Export(stream);
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | /// <summary>
|
---|
59 | /// Reads from the given stream data which is expected to be in the BPP ORLIB format.
|
---|
60 | /// </summary>
|
---|
61 | /// <remarks>
|
---|
62 | /// The stream is not closed or disposed. The caller has to take care of that.
|
---|
63 | /// </remarks>
|
---|
64 | /// <param name="stream">The stream to read data from.</param>
|
---|
65 | /// <returns>True if the file was successfully read or false otherwise.</returns>
|
---|
66 | public void Parse(Stream stream) {
|
---|
67 | var reader = new StreamReader(stream);
|
---|
68 | var delim = new char[] { ' ', '\t' };
|
---|
69 | var problemClass = reader.ReadLine().Split(delim, StringSplitOptions.RemoveEmptyEntries);
|
---|
70 |
|
---|
71 | var nrOfItems = reader.ReadLine().Split(delim, StringSplitOptions.RemoveEmptyEntries);
|
---|
72 | Items = int.Parse(nrOfItems[0]);
|
---|
73 | //Numbering of instances..
|
---|
74 | reader.ReadLine();
|
---|
75 | var binMeasures = reader.ReadLine().Split(delim, StringSplitOptions.RemoveEmptyEntries);
|
---|
76 | int depth;
|
---|
77 | bool isThreeDimensional = false;
|
---|
78 | if (int.TryParse(binMeasures[2], out depth)) {
|
---|
79 | isThreeDimensional = true;
|
---|
80 | BinMeasures = new int[3];
|
---|
81 | BinMeasures[0] = int.Parse(binMeasures[0]);
|
---|
82 | BinMeasures[1] = int.Parse(binMeasures[1]);
|
---|
83 | BinMeasures[2] = depth;
|
---|
84 | } else {
|
---|
85 | BinMeasures = new int[2];
|
---|
86 | BinMeasures[0] = int.Parse(binMeasures[0]);
|
---|
87 | BinMeasures[1] = int.Parse(binMeasures[1]);
|
---|
88 | }
|
---|
89 | ItemMeasures = new int[Items][];
|
---|
90 |
|
---|
91 | for (int k = 0; k < Items; k++) {
|
---|
92 | if (reader.EndOfStream) throw new InvalidDataException("Unexpected End of Stream.");
|
---|
93 | var valLine = reader.ReadLine();
|
---|
94 | while (String.IsNullOrWhiteSpace(valLine)) valLine = reader.ReadLine();
|
---|
95 | var vals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries);
|
---|
96 | ItemMeasures[k] = new int[isThreeDimensional ? 3 : 2];
|
---|
97 | ItemMeasures[k][0] = int.Parse(vals[0]);
|
---|
98 | ItemMeasures[k][1] = int.Parse(vals[1]);
|
---|
99 | if (isThreeDimensional)
|
---|
100 | ItemMeasures[k][2] = int.Parse(vals[2]);
|
---|
101 | }
|
---|
102 |
|
---|
103 | int problemClassNr = int.Parse(problemClass[0]);
|
---|
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 |
|
---|
117 |
|
---|
118 | }
|
---|
119 |
|
---|
120 | public void Export(Stream stream) {
|
---|
121 | bool isThreeDimensional = BinMeasures.Length == 3;
|
---|
122 | var nameParts = Name.Split('_');
|
---|
123 | int problemClassNr = int.Parse(nameParts[2]);
|
---|
124 |
|
---|
125 | var writer = new StreamWriter(stream);
|
---|
126 | writer.WriteLine(String.Format("{0,-5} PROBLEM CLASS ({1})", problemClassNr, Description));
|
---|
127 | writer.WriteLine(String.Format("{0,-5} {1,-5} {2,-5} WBIN,HBIN,DBIN", BinMeasures[0], BinMeasures[1], BinMeasures[2]));
|
---|
128 | for (int i = 0; i < Items; i++) {
|
---|
129 | if (i == 0)
|
---|
130 | 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]);
|
---|
131 | else
|
---|
132 | writer.WriteLine("{0,-5} {1,-5} {2,-5}", ItemMeasures[i][0], ItemMeasures[i][1], ItemMeasures[i][2]);
|
---|
133 | }
|
---|
134 | writer.Flush();
|
---|
135 | }
|
---|
136 |
|
---|
137 | }
|
---|
138 | }
|
---|