1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 Joseph Helm and 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 |
|
---|
23 | using System;
|
---|
24 | using System.IO;
|
---|
25 |
|
---|
26 | namespace HeuristicLab.Problems.BinPacking2D {
|
---|
27 | public static class BPPORLIBParser {
|
---|
28 | public static BPPData Parse(string file) {
|
---|
29 | using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read)) {
|
---|
30 | return Parse(stream);
|
---|
31 | }
|
---|
32 | }
|
---|
33 |
|
---|
34 | public static void Export(BPPData instance, string file) {
|
---|
35 | using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read)) {
|
---|
36 | Export(instance, stream);
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | /// <summary>
|
---|
41 | /// Reads from the given stream data which is expected to be in the BPP ORLIB format.
|
---|
42 | /// </summary>
|
---|
43 | /// <remarks>
|
---|
44 | /// The stream is not closed or disposed. The caller has to take care of that.
|
---|
45 | /// </remarks>
|
---|
46 | /// <param name="stream">The stream to read data from.</param>
|
---|
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);
|
---|
53 |
|
---|
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;
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | public static void Export(BPPData instance, Stream stream) {
|
---|
87 | var nameParts = instance.Name.Split('_');
|
---|
88 | int problemClassNr = int.Parse(nameParts[2]);
|
---|
89 |
|
---|
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);
|
---|
98 |
|
---|
99 | }
|
---|
100 | writer.Flush();
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | }
|
---|
105 | }
|
---|