#region License Information /* HeuristicLab * Copyright (C) 2002-2015 Joseph Helm and Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.IO; namespace HeuristicLab.Problems.Instances.BinPacking { public class BPPORLIBParser { public string Name { get; set; } public string Description { get; set; } public int Items { get; set; } public int[] BinMeasures { get; set; } public int[][] ItemMeasures { get; set; } public BPPORLIBParser() { Reset(); } public void Reset() { Name = Description = String.Empty; Items = 0; BinMeasures = null; ItemMeasures = null; } public void Parse(string file) { using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read)) { Parse(stream); } } public void Export(string file) { using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read)) { Export(stream); } } /// /// Reads from the given stream data which is expected to be in the BPP ORLIB format. /// /// /// The stream is not closed or disposed. The caller has to take care of that. /// /// The stream to read data from. /// True if the file was successfully read or false otherwise. public void Parse(Stream stream) { var reader = new StreamReader(stream); var delim = new char[] { ' ', '\t' }; var problemClass = reader.ReadLine().Split(delim, StringSplitOptions.RemoveEmptyEntries); var nrOfItems = reader.ReadLine().Split(delim, StringSplitOptions.RemoveEmptyEntries); Items = int.Parse(nrOfItems[0]); //Numbering of instances.. reader.ReadLine(); var binMeasures = reader.ReadLine().Split(delim, StringSplitOptions.RemoveEmptyEntries); int depth; bool isThreeDimensional = false; if (int.TryParse(binMeasures[2], out depth)) { isThreeDimensional = true; BinMeasures = new int[3]; BinMeasures[0] = int.Parse(binMeasures[0]); BinMeasures[1] = int.Parse(binMeasures[1]); BinMeasures[2] = depth; } else { BinMeasures = new int[2]; BinMeasures[0] = int.Parse(binMeasures[0]); BinMeasures[1] = int.Parse(binMeasures[1]); } ItemMeasures = new int[Items][]; for (int k = 0; k < Items; k++) { if (reader.EndOfStream) throw new InvalidDataException("Unexpected End of Stream (at line " + k + ")."); var valLine = reader.ReadLine(); while (String.IsNullOrWhiteSpace(valLine)) valLine = reader.ReadLine(); var vals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries); ItemMeasures[k] = new int[isThreeDimensional ? 3 : 2]; ItemMeasures[k][0] = int.Parse(vals[0]); ItemMeasures[k][1] = int.Parse(vals[1]); if (isThreeDimensional) ItemMeasures[k][2] = int.Parse(vals[2]); } Int32 problemClassNr = -1; if (int.TryParse(problemClass[0], out problemClassNr)) { Name = (isThreeDimensional ? "3" : "2") + "dbpp_class_0" + problemClassNr; if (!isThreeDimensional) { if (problemClassNr >= 1 && problemClassNr <= 5) Description = "Proposed by Berkey and Wang."; else if (problemClassNr >= 6 && problemClassNr <= 10) Description = "Proposed by Martello and Vigo."; } else { if (problemClassNr >= 1 && problemClassNr <= 5) Description = "Proposed by Martello and Vigo."; else if (problemClassNr >= 6 && problemClassNr <= 9) Description = "Proposed by Berkey and Wang."; } } else { Name = "Unknown"; Description = ""; } } public void Export(Stream stream) { bool isThreeDimensional = BinMeasures.Length == 3; var nameParts = Name.Split('_'); int problemClassNr = int.Parse(nameParts[2]); var writer = new StreamWriter(stream); writer.WriteLine(String.Format("{0,-5} PROBLEM CLASS ({1})", problemClassNr, Description)); writer.WriteLine(String.Format("{0,-5} {1,-5} {2,-5} WBIN,HBIN,DBIN", BinMeasures[0], BinMeasures[1], BinMeasures[2])); for (int i = 0; i < Items; i++) { if (i == 0) 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]); else writer.WriteLine("{0,-5} {1,-5} {2,-5}", ItemMeasures[i][0], ItemMeasures[i][1], ItemMeasures[i][2]); } writer.Flush(); } } }