Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking/3.3/Instances/BPPORLIBParser.cs @ 14046

Last change on this file since 14046 was 14046, checked in by gkronber, 8 years ago

#1966: unified namespaces

File size: 5.4 KB
Line 
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
23using System;
24using System.IO;
25
26namespace HeuristicLab.Problems.BinPacking {
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) {
46      using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read)) {
47        Parse(stream);
48      }
49    }
50
51    public void Export(string file) {
52      using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read)) {
53        Export(stream);
54      }
55    }
56
57    /// <summary>
58    /// Reads from the given stream data which is expected to be in the BPP ORLIB format.
59    /// </summary>
60    /// <remarks>
61    /// The stream is not closed or disposed. The caller has to take care of that.
62    /// </remarks>
63    /// <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);
69
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]);
87      }
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    }
123
124    public void Export(Stream stream) {
125      bool isThreeDimensional = BinMeasures.Length == 3;
126      var nameParts = Name.Split('_');
127      int problemClassNr = int.Parse(nameParts[2]);
128
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        else
136          writer.WriteLine("{0,-5} {1,-5} {2,-5}", ItemMeasures[i][0], ItemMeasures[i][1], ItemMeasures[i][2]);
137
138      }
139      writer.Flush();
140    }
141
142  }
143}
Note: See TracBrowser for help on using the repository browser.