Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.QuadraticAssignment/3.3/Parsers/QAPLIBParser.cs @ 6319

Last change on this file since 6319 was 5947, checked in by abeham, 14 years ago

#1330

  • simplified parser
  • correctly reading first matrix as weights (accessed by index of permutation) and second matrix as distances (accessed by value of permutation)
File size: 3.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.IO;
24
25namespace HeuristicLab.Problems.QuadraticAssignment {
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    public Exception Error { get; private set; }
31
32    public QAPLIBParser() {
33      Reset();
34    }
35
36    public void Reset() {
37      Size = 0;
38      Distances = null;
39      Weights = null;
40      Error = null;
41    }
42
43    public bool Parse(string file) {
44      using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read)) {
45        return Parse(stream);
46      }
47    }
48
49    /// <summary>
50    /// Reads from the given stream data which is expected to be in the QAPLIB format.
51    /// </summary>
52    /// <remarks>
53    /// The stream is not closed or disposed. The caller has to take care of that.
54    /// </remarks>
55    /// <param name="stream">The stream to read data from.</param>
56    /// <returns>True if the file was successfully read or false otherwise.</returns>
57    public bool Parse(Stream stream) {
58      Error = null;
59      try {
60        StreamReader reader = new StreamReader(stream);
61        Size = int.Parse(reader.ReadLine());
62        Distances = new double[Size, Size];
63        Weights = new double[Size, Size];
64        string valLine = null;
65        char[] delim = new char[] { ' ' };
66        int read = 0;
67        int k = 0;
68        while (k < Size) {
69          if (reader.EndOfStream) throw new InvalidDataException("Reached end of stream while reading first matrix.");
70          valLine = reader.ReadLine();
71          while (String.IsNullOrWhiteSpace(valLine)) valLine = reader.ReadLine();
72          string[] vals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries);
73          foreach (string val in vals) {
74            Weights[k, read++] = double.Parse(val);
75            if (read == Size) {
76              read = 0;
77              k++;
78            }
79          }
80        }
81
82        read = 0;
83        k = 0;
84
85        while (k < Size) {
86          if (reader.EndOfStream) throw new InvalidDataException("Reached end of stream while reading second matrix.");
87          valLine = reader.ReadLine();
88          while (String.IsNullOrWhiteSpace(valLine)) valLine = reader.ReadLine();
89          string[] vals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries);
90          foreach (string val in vals) {
91            Distances[k, read++] = double.Parse(val);
92            if (read == Size) {
93              read = 0;
94              k++;
95            }
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.