Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 8153 was 7445, checked in by abeham, 13 years ago

#1614

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