Free cookie consent management tool by TermsFeed Policy Generator

source: branches/QAP/HeuristicLab.Problems.QuadraticAssignment/3.3/Parsers/QAPLIBParser.cs @ 5648

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

#1330

  • Unified QAP visualization in solution and problem view
  • Fixed bug in gradient-descent gradient calculation when performing multidimensional scaling
  • Extended QAPLIB parsers to cover some file format variations
  • Added unit tests to check if all QAPLIB instances import without error
  • Changed BestKnownSolution to be an OptionalValueParameter
File size: 2.8 KB
RevLine 
[5562]1using System;
2using System.IO;
3
4namespace HeuristicLab.Problems.QuadraticAssignment {
5  public class QAPLIBParser {
6    public int Size { get; private set; }
7    public double[,] Distances { get; private set; }
8    public double[,] Weights { get; private set; }
9    public Exception Error { get; private set; }
10
11    public QAPLIBParser() {
12      Reset();
13    }
14
15    public void Reset() {
16      Size = 0;
17      Distances = null;
18      Weights = null;
19      Error = null;
20    }
21
22    public bool Parse(string file) {
[5563]23      using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read)) {
24        return Parse(stream);
25      }
26    }
27
28    /// <summary>
29    /// Reads from the given stream data which is expected to be in the QAPLIB format.
30    /// </summary>
31    /// <remarks>
32    /// The stream is not closed or disposed. The caller has to take care of that.
33    /// </remarks>
34    /// <param name="stream">The stream to read data from.</param>
35    /// <returns>True if the file was successfully read or false otherwise.</returns>
36    public bool Parse(Stream stream) {
[5562]37      Error = null;
38      try {
[5563]39        StreamReader reader = new StreamReader(stream);
[5562]40        Size = int.Parse(reader.ReadLine());
41        Distances = new double[Size, Size];
42        Weights = new double[Size, Size];
[5648]43        string valLine = reader.ReadLine();
[5562]44        char[] delim = new char[] { ' ' };
45        for (int i = 0; i < Size; i++) {
[5648]46          if (i > 0 || String.IsNullOrWhiteSpace(valLine))
47            valLine = reader.ReadLine();
[5562]48          string[] vals = new string[Size];
49          string[] partVals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries);
50          partVals.CopyTo(vals, 0);
51          int index = partVals.Length;
52          while (index < Size) {
53            valLine = reader.ReadLine();
54            partVals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries);
55            partVals.CopyTo(vals, index);
56            index += partVals.Length;
57          }
58          for (int j = 0; j < Size; j++) {
59            Distances[i, j] = double.Parse(vals[j]);
60          }
61        }
[5648]62        valLine = reader.ReadLine();
[5562]63        int read = 0;
64        int k = 0;
65        while (!reader.EndOfStream) {
[5648]66          if (read > 0 || String.IsNullOrWhiteSpace(valLine))
67            valLine = reader.ReadLine();
[5562]68          string[] vals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries);
69          for (int j = 0; j < vals.Length; j++) {
70            if (read + j == Size) {
71              read = 0;
72              k++;
73            }
74            Weights[k, read + j] = double.Parse(vals[j]);
75          }
76          read += vals.Length;
77        }
78        return true;
79      } catch (Exception e) {
80        Error = e;
81        return false;
82      }
83    }
84  }
85}
Note: See TracBrowser for help on using the repository browser.