Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11740 was 9596, checked in by jhelm, 11 years ago

#1966: More refactoring; Added more sophisticated structures for packing-plan and bin-packing representation; Transferred parts of the decoding-algorithms to these structures; Did some more refactoring and cleanup;

File size: 5.4 KB
Line 
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
22using System;
23using System.Globalization;
24using System.IO;
25using System.Linq;
26
27namespace 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 (at line " + k + ").");
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      Int32 problemClassNr = -1;
104      if (int.TryParse(problemClass[0], out problemClassNr)) {
105        Name = (isThreeDimensional ? "3" : "2") + "dbpp_class_0" + problemClassNr;
106        if (!isThreeDimensional) {
107          if (problemClassNr >= 1 && problemClassNr <= 5)
108            Description = "Proposed by Berkey and Wang.";
109          else if (problemClassNr >= 6 && problemClassNr <= 10)
110            Description = "Proposed by Martello and Vigo.";
111        } else {
112          if (problemClassNr >= 1 && problemClassNr <= 5)
113            Description = "Proposed by Martello and Vigo.";
114          else if (problemClassNr >= 6 && problemClassNr <= 9)
115            Description = "Proposed by Berkey and Wang.";
116        }
117      } else {
118        Name = "Unknown";
119        Description = "<Missing>";
120      }
121     
122
123    }
124
125    public void Export(Stream stream) {
126      bool isThreeDimensional = BinMeasures.Length == 3;
127      var nameParts = Name.Split('_');
128      int problemClassNr = int.Parse(nameParts[2]);
129
130      var writer = new StreamWriter(stream);
131      writer.WriteLine(String.Format("{0,-5}               PROBLEM CLASS ({1})", problemClassNr, Description));
132      writer.WriteLine(String.Format("{0,-5} {1,-5} {2,-5}   WBIN,HBIN,DBIN", BinMeasures[0], BinMeasures[1], BinMeasures[2]));
133      for (int i = 0; i < Items; i++) {
134        if (i == 0)
135          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]);
136        else
137          writer.WriteLine("{0,-5} {1,-5} {2,-5}", ItemMeasures[i][0], ItemMeasures[i][1], ItemMeasures[i][2]);
138      }
139      writer.Flush();
140    }
141
142  }
143}
Note: See TracBrowser for help on using the repository browser.