[7445] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11594] | 3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[7445] | 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 | using System;
|
---|
| 23 | using System.IO;
|
---|
| 24 |
|
---|
| 25 | namespace HeuristicLab.Problems.Instances.QAPLIB {
|
---|
| 26 | public class QAPLIBParser {
|
---|
| 27 | public int Size { get; private set; }
|
---|
| 28 | public double[,] Distances { get; private set; }
|
---|
| 29 | public double[,] Weights { get; private set; }
|
---|
| 30 |
|
---|
| 31 | public QAPLIBParser() {
|
---|
| 32 | Reset();
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | public void Reset() {
|
---|
| 36 | Size = 0;
|
---|
| 37 | Distances = null;
|
---|
| 38 | Weights = null;
|
---|
| 39 | }
|
---|
| 40 |
|
---|
[7538] | 41 | public void Parse(string file) {
|
---|
[7445] | 42 | using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read)) {
|
---|
[7538] | 43 | Parse(stream);
|
---|
[7445] | 44 | }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | /// <summary>
|
---|
| 48 | /// Reads from the given stream data which is expected to be in the QAPLIB format.
|
---|
| 49 | /// </summary>
|
---|
| 50 | /// <remarks>
|
---|
| 51 | /// The stream is not closed or disposed. The caller has to take care of that.
|
---|
| 52 | /// </remarks>
|
---|
| 53 | /// <param name="stream">The stream to read data from.</param>
|
---|
| 54 | /// <returns>True if the file was successfully read or false otherwise.</returns>
|
---|
[7538] | 55 | public void Parse(Stream stream) {
|
---|
| 56 | var reader = new StreamReader(stream);
|
---|
| 57 | Size = int.Parse(reader.ReadLine());
|
---|
| 58 | Distances = new double[Size, Size];
|
---|
| 59 | Weights = new double[Size, Size];
|
---|
[8909] | 60 | char[] delim = new char[] { ' ', '\t' };
|
---|
[7445] | 61 |
|
---|
[7558] | 62 | Weights = ParseMatrix(reader, delim);
|
---|
[7538] | 63 | Distances = ParseMatrix(reader, delim);
|
---|
| 64 | }
|
---|
[7445] | 65 |
|
---|
[7538] | 66 | private double[,] ParseMatrix(StreamReader reader, char[] delim) {
|
---|
| 67 | int read = 0, k = 0;
|
---|
| 68 | double[,] result = new double[Size, Size];
|
---|
| 69 | while (k < Size) {
|
---|
| 70 | if (reader.EndOfStream) throw new InvalidDataException("Reached end of stream while reading second matrix.");
|
---|
| 71 | string valLine = reader.ReadLine();
|
---|
| 72 | while (String.IsNullOrWhiteSpace(valLine)) valLine = reader.ReadLine();
|
---|
| 73 | string[] vals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries);
|
---|
| 74 | foreach (string val in vals) {
|
---|
[7558] | 75 | result[k, read++] = double.Parse(val);
|
---|
[7538] | 76 | if (read == Size) {
|
---|
| 77 | read = 0;
|
---|
| 78 | k++;
|
---|
[7445] | 79 | }
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
[7538] | 82 | return result;
|
---|
[7445] | 83 | }
|
---|
| 84 | }
|
---|
| 85 | }
|
---|