[7445] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 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.Globalization;
|
---|
| 24 | using System.IO;
|
---|
[8909] | 25 | using System.Linq;
|
---|
[7445] | 26 |
|
---|
| 27 | namespace HeuristicLab.Problems.Instances.QAPLIB {
|
---|
| 28 | public class QAPLIBSolutionParser {
|
---|
| 29 | public int Size { get; private set; }
|
---|
| 30 | public int[] Assignment { get; private set; }
|
---|
| 31 | public double Quality { get; private set; }
|
---|
| 32 | public Exception Error { get; private set; }
|
---|
| 33 |
|
---|
| 34 | public QAPLIBSolutionParser() {
|
---|
| 35 | Reset();
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | public void Reset() {
|
---|
| 39 | Size = 0;
|
---|
| 40 | Assignment = null;
|
---|
| 41 | Quality = double.NaN;
|
---|
| 42 | Error = null;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | /// <summary>
|
---|
| 46 | /// Reads from the given stream data which is expected to be in the QAPLIB solution format.
|
---|
| 47 | ///
|
---|
| 48 | /// The QAPLIB solution file format (.sln) is as follows:
|
---|
| 49 | /// 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
|
---|
| 50 | /// Remaining lines: The values of the permutation separated by blanks. Values must lie in the range [1;size of the permutation].
|
---|
| 51 | /// </summary>
|
---|
| 52 | /// <param name="file">The file 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></returns>
|
---|
| 58 | public bool Parse(string file, bool valueAsLocation) {
|
---|
| 59 | using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read)) {
|
---|
| 60 | return Parse(stream, valueAsLocation);
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | /// <summary>
|
---|
| 65 | /// Reads from the given stream data which is expected to be in the QAPLIB solution format.
|
---|
| 66 | ///
|
---|
| 67 | /// The QAPLIB solution file format (.sln) is as follows:
|
---|
| 68 | /// 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
|
---|
| 69 | /// Remaining lines: The values of the permutation separated by blanks. Values must lie in the range [1;size of the permutation].
|
---|
| 70 | /// </summary>
|
---|
| 71 | /// <remarks>
|
---|
| 72 | /// The stream is not closed or disposed. The caller has to take care of that.
|
---|
| 73 | /// </remarks>
|
---|
| 74 | /// <param name="stream">The stream to read data from.</param>
|
---|
| 75 | /// <param name="valueAsLocation">The numbers can be interpreted either as facilities or locations.
|
---|
| 76 | /// HeuristicLab always encodes the permutation such that the index denotes the facility and the value the location.
|
---|
| 77 | /// If this parameter is true, then the permutation is expected to follow this encoding.
|
---|
| 78 | /// If this parameter is false, the meaning of value and index will be swapped when reading the permutation.</param>
|
---|
| 79 | /// <returns>True if the file was successfully read or false otherwise.</returns>
|
---|
| 80 | public bool Parse(Stream stream, bool valueAsLocation) {
|
---|
| 81 | Error = null;
|
---|
| 82 | try {
|
---|
| 83 | StreamReader reader = new StreamReader(stream);
|
---|
[8909] | 84 | char[] delim = new char[] { ' ', '\t', ',' }; // comma is added for nug30.sln which is the only file that separates the permutation with commas
|
---|
[7445] | 85 | string[] firstline = reader.ReadLine().Split(delim, StringSplitOptions.RemoveEmptyEntries);
|
---|
| 86 | Size = int.Parse(firstline[0]);
|
---|
| 87 | Quality = double.Parse(firstline[1], CultureInfo.InvariantCulture.NumberFormat);
|
---|
| 88 | Assignment = new int[Size];
|
---|
| 89 | int read = 0;
|
---|
| 90 | while (!reader.EndOfStream) {
|
---|
| 91 | string valLine = reader.ReadLine();
|
---|
| 92 | string[] vals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries);
|
---|
[8909] | 93 | int start = 1;
|
---|
| 94 | if (vals.Any(x => x == "0")) start = 0;
|
---|
[8553] | 95 | if (vals.Length == 0) continue;
|
---|
[7445] | 96 | for (int j = 0; j < vals.Length; j++) {
|
---|
| 97 | if (valueAsLocation)
|
---|
[8909] | 98 | Assignment[int.Parse(vals[j]) - start] = read++;
|
---|
| 99 | else Assignment[read++] = int.Parse(vals[j]) - start;
|
---|
[7445] | 100 | }
|
---|
| 101 | }
|
---|
[8553] | 102 | if (read < Size) Assignment = null;
|
---|
[7445] | 103 | return true;
|
---|
| 104 | } catch (Exception e) {
|
---|
| 105 | Error = e;
|
---|
| 106 | return false;
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 | }
|
---|
| 110 | }
|
---|