Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.Instances.QAPLIB/3.3/QAPLIBSolutionParser.cs @ 10988

Last change on this file since 10988 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 5.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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
22using System;
23using System.Globalization;
24using System.IO;
25using System.Linq;
26
27namespace 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);
84        char[] delim = new char[] { ' ', '\t', ',' }; // comma is added for nug30.sln which is the only file that separates the permutation with commas
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);
93          int start = 1;
94          if (vals.Any(x => x == "0")) start = 0;
95          if (vals.Length == 0) continue;
96          for (int j = 0; j < vals.Length; j++) {
97            if (valueAsLocation)
98              Assignment[int.Parse(vals[j]) - start] = read++;
99            else Assignment[read++] = int.Parse(vals[j]) - start;
100          }
101        }
102        if (read < Size) Assignment = null;
103        return true;
104      } catch (Exception e) {
105        Error = e;
106        return false;
107      }
108    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.