1 | using System;
|
---|
2 | using System.Globalization;
|
---|
3 | using System.IO;
|
---|
4 |
|
---|
5 | namespace HeuristicLab.Problems.QuadraticAssignment {
|
---|
6 | public class QAPLIBSolutionParser {
|
---|
7 | public int Size { get; private set; }
|
---|
8 | public int[] Assignment { get; private set; }
|
---|
9 | public double Quality { get; private set; }
|
---|
10 | public Exception Error { get; private set; }
|
---|
11 |
|
---|
12 | public QAPLIBSolutionParser() {
|
---|
13 | Reset();
|
---|
14 | }
|
---|
15 |
|
---|
16 | public void Reset() {
|
---|
17 | Size = 0;
|
---|
18 | Assignment = null;
|
---|
19 | Quality = double.NaN;
|
---|
20 | Error = null;
|
---|
21 | }
|
---|
22 |
|
---|
23 | /// <summary>
|
---|
24 | /// Reads from the given stream data which is expected to be in the QAPLIB solution format.
|
---|
25 | ///
|
---|
26 | /// The QAPLIB solution file format (.sln) is as follows:
|
---|
27 | /// First line: Size of the permutation followed by a blank followed by the quality formatted using no thousands separator and if a fractional number with the "." as decimal symbol
|
---|
28 | /// Remaining lines: The values of the permutation separated by blanks. Values must lie in the range [1;size of the permutation].
|
---|
29 | /// </summary>
|
---|
30 | /// <param name="file">The file to read data from.</param>
|
---|
31 | /// <param name="valueAsLocation">The numbers can be interpreted either as facilities or locations.
|
---|
32 | /// HeuristicLab always encodes the permutation such that the index denotes the facility and the value the location.
|
---|
33 | /// If this parameter is true, then the permutation is expected to follow this encoding.
|
---|
34 | /// If this parameter is false, the meaning of value and index will be swapped when reading the permutation.</param>
|
---|
35 | /// <returns></returns>
|
---|
36 | public bool Parse(string file, bool valueAsLocation) {
|
---|
37 | using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read)) {
|
---|
38 | return Parse(stream, valueAsLocation);
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | /// <summary>
|
---|
43 | /// Reads from the given stream data which is expected to be in the QAPLIB solution format.
|
---|
44 | ///
|
---|
45 | /// The QAPLIB solution file format (.sln) is as follows:
|
---|
46 | /// First line: Size of the permutation followed by a blank followed by the quality formatted using no thousands separator and if a fractional number with the "." as decimal symbol
|
---|
47 | /// Remaining lines: The values of the permutation separated by blanks. Values must lie in the range [1;size of the permutation].
|
---|
48 | /// </summary>
|
---|
49 | /// <remarks>
|
---|
50 | /// The stream is not closed or disposed. The caller has to take care of that.
|
---|
51 | /// </remarks>
|
---|
52 | /// <param name="stream">The stream to read data from.</param>
|
---|
53 | /// <param name="valueAsLocation">The numbers can be interpreted either as facilities or locations.
|
---|
54 | /// HeuristicLab always encodes the permutation such that the index denotes the facility and the value the location.
|
---|
55 | /// If this parameter is true, then the permutation is expected to follow this encoding.
|
---|
56 | /// If this parameter is false, the meaning of value and index will be swapped when reading the permutation.</param>
|
---|
57 | /// <returns>True if the file was successfully read or false otherwise.</returns>
|
---|
58 | public bool Parse(Stream stream, bool valueAsLocation) {
|
---|
59 | Error = null;
|
---|
60 | try {
|
---|
61 | StreamReader reader = new StreamReader(stream);
|
---|
62 | char[] delim = new char[] { ' ', ',' }; // comma is added for nug30.sln which is the only file that separates the permutation with commas
|
---|
63 | string[] firstline = reader.ReadLine().Split(delim, StringSplitOptions.RemoveEmptyEntries);
|
---|
64 | Size = int.Parse(firstline[0]);
|
---|
65 | Quality = double.Parse(firstline[1], CultureInfo.InvariantCulture.NumberFormat);
|
---|
66 | Assignment = new int[Size];
|
---|
67 | int read = 0;
|
---|
68 | while (!reader.EndOfStream) {
|
---|
69 | string valLine = reader.ReadLine();
|
---|
70 | string[] vals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries);
|
---|
71 | for (int j = 0; j < vals.Length; j++) {
|
---|
72 | if (valueAsLocation)
|
---|
73 | Assignment[int.Parse(vals[j]) - 1] = read++;
|
---|
74 | else Assignment[read++] = int.Parse(vals[j]) - 1;
|
---|
75 | }
|
---|
76 | }
|
---|
77 | return true;
|
---|
78 | } catch (Exception e) {
|
---|
79 | Error = e;
|
---|
80 | return false;
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|
84 | }
|
---|