#region License Information /* HeuristicLab * Copyright (C) 2002-2016 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.Globalization; using System.IO; using System.Linq; using System.Text; namespace HeuristicLab.Problems.Instances.PFSP { public class FSSPTAILIBParser { public string Name { get; set; } public string Description { get; set; } public int Jobs { get; set; } public int Machines { get; set; } public double[,] ProcessingTimes { get; set; } public int[] BestKnownSchedule { get; set; } public int BestKnownQuality { get; set; } public FSSPTAILIBParser() { Reset(); } public void Reset() { Name = Description = String.Empty; Jobs = Machines = BestKnownQuality = 0; ProcessingTimes = null; BestKnownSchedule = 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.Create, FileAccess.Write)) { Export(stream); } } /// /// Reads from the given stream data which is expected to be in the JSSP ORLIB format. /// /// /// The stream is not closed or disposed. The caller has to take care of that. /// /// The stream to read data from. public void Parse(Stream stream) { using (var reader = new StreamReader(stream, Encoding.UTF8, true, 4092, true)) { Name = reader.ReadLine().Trim(); Description = reader.ReadLine().Trim(); var delim = new char[] { ' ', '\t' }; var info = reader.ReadLine().Split(delim, StringSplitOptions.RemoveEmptyEntries); Jobs = int.Parse(info[0]); Machines = int.Parse(info[1]); var bestKnownQuality = reader.ReadLine().Trim(); BestKnownQuality = int.Parse(bestKnownQuality); ProcessingTimes = new double[Machines, Jobs]; for (int k = 0; k < Machines; k++) { if (reader.EndOfStream) throw new InvalidDataException("Unexpected End of Stream."); var valLine = reader.ReadLine(); while (String.IsNullOrWhiteSpace(valLine)) valLine = reader.ReadLine(); var vals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < Jobs; i++) { ProcessingTimes[k, i] = double.Parse(vals[i], CultureInfo.InvariantCulture.NumberFormat); } } while (!reader.EndOfStream) { var vals = reader.ReadLine().Split(delim, StringSplitOptions.RemoveEmptyEntries); if (vals.Count() == Jobs) { BestKnownSchedule = vals.Select(val => Int32.Parse(val)).ToArray(); } } } } /// /// Writes to the given stream data which is expected to be in the JSSP ORLIB format. /// /// /// The stream is not closed or disposed. The caller has to take care of that. /// /// The stream to write data to. public void Export(Stream stream) { //TODO: complete Export, seems as if it is just a dummy (copy-paste)-implementation using (var writer = new StreamWriter(stream, Encoding.UTF8, 4092, true)) { writer.WriteLine(Name); writer.WriteLine(Description); writer.WriteLine(Jobs.ToString(CultureInfo.InvariantCulture.NumberFormat) + " " + Machines.ToString(CultureInfo.InvariantCulture.NumberFormat)); for (int i = 0; i < Machines; i++) { for (int j = 0; j < Jobs; j++) { writer.Write(ProcessingTimes[i, j] + " "); } writer.WriteLine(); } writer.Flush(); } } } }