Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5598 was 5563, checked in by abeham, 14 years ago

#1330

  • Added working version of QAP
File size: 2.7 KB
Line 
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) {
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) {
37      Error = null;
38      try {
39        StreamReader reader = new StreamReader(stream);
40        Size = int.Parse(reader.ReadLine());
41        Distances = new double[Size, Size];
42        Weights = new double[Size, Size];
43        reader.ReadLine();
44        char[] delim = new char[] { ' ' };
45        for (int i = 0; i < Size; i++) {
46          string valLine = reader.ReadLine();
47          string[] vals = new string[Size];
48          string[] partVals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries);
49          partVals.CopyTo(vals, 0);
50          int index = partVals.Length;
51          while (index < Size) {
52            valLine = reader.ReadLine();
53            partVals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries);
54            partVals.CopyTo(vals, index);
55            index += partVals.Length;
56          }
57          for (int j = 0; j < Size; j++) {
58            Distances[i, j] = double.Parse(vals[j]);
59          }
60        }
61        reader.ReadLine();
62        int read = 0;
63        int k = 0;
64        while (!reader.EndOfStream) {
65          string valLine = reader.ReadLine();
66          string[] vals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries);
67          for (int j = 0; j < vals.Length; j++) {
68            if (read + j == Size) {
69              read = 0;
70              k++;
71            }
72            Weights[k, read + j] = double.Parse(vals[j]);
73          }
74          read += vals.Length;
75        }
76        return true;
77      } catch (Exception e) {
78        Error = e;
79        return false;
80      }
81    }
82  }
83}
Note: See TracBrowser for help on using the repository browser.