#region License Information /* HeuristicLab * Copyright (C) 2002-2018 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.PermutationProblems.LinearOrdering { public class LOPParser { public string Name { get; set; } public string Description { get; set; } public int Dimension { get; set; } public double[,] Matrix { get; set; } public int[] BestKnownPermutation { get; set; } public double? BestKnownQuality { get; set; } public LOPParser() { Reset(); } public void Reset() { Name = Description = String.Empty; Dimension = 0; BestKnownQuality = 0; Matrix = null; BestKnownPermutation = 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); } } public void Parse(Stream stream) { using (var reader = new StreamReader(stream, Encoding.UTF8, true, 4092, true)) { Name = reader.ReadLine().Trim(); Description = reader.ReadLine().Trim(); BestKnownQuality = double.Parse(reader.ReadLine().Trim()); var delim = new char[] { ' ', '\t' }; Dimension = int.Parse(reader.ReadLine().Trim()); Matrix = new double[Dimension, Dimension]; for (int k = 0; k < Dimension; 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 < Dimension; i++) { Matrix[k, i] = double.Parse(vals[i], CultureInfo.InvariantCulture.NumberFormat); } } while (!reader.EndOfStream) { var vals = reader.ReadLine().Split(delim, StringSplitOptions.RemoveEmptyEntries); if (vals.Count() == Dimension) { BestKnownPermutation = vals.Select(val => Int32.Parse(val)).ToArray(); } } } } public void Export(Stream stream) { using (var writer = new StreamWriter(stream, Encoding.UTF8, 4092, true)) { writer.WriteLine(Name); writer.WriteLine(Description); writer.WriteLine(BestKnownQuality); writer.WriteLine(Dimension); for (int k = 0; k < Dimension; k++) { for (int i = 0; i < Dimension; i++) { writer.Write(Matrix[k, i] + '\t'); } writer.WriteLine(); } if (BestKnownPermutation != null) { writer.WriteLine(BestKnownPermutation); } writer.Flush(); } } } }