1 | using System;
|
---|
2 | using System.IO;
|
---|
3 |
|
---|
4 | namespace 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 | Error = null;
|
---|
24 | try {
|
---|
25 | StreamReader reader = new StreamReader(file);
|
---|
26 | Size = int.Parse(reader.ReadLine());
|
---|
27 | Distances = new double[Size, Size];
|
---|
28 | Weights = new double[Size, Size];
|
---|
29 | reader.ReadLine();
|
---|
30 | char[] delim = new char[] { ' ' };
|
---|
31 | for (int i = 0; i < Size; i++) {
|
---|
32 | string valLine = reader.ReadLine();
|
---|
33 | string[] vals = new string[Size];
|
---|
34 | string[] partVals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries);
|
---|
35 | partVals.CopyTo(vals, 0);
|
---|
36 | int index = partVals.Length;
|
---|
37 | while (index < Size) {
|
---|
38 | valLine = reader.ReadLine();
|
---|
39 | partVals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries);
|
---|
40 | partVals.CopyTo(vals, index);
|
---|
41 | index += partVals.Length;
|
---|
42 | }
|
---|
43 | for (int j = 0; j < Size; j++) {
|
---|
44 | Distances[i, j] = double.Parse(vals[j]);
|
---|
45 | }
|
---|
46 | }
|
---|
47 | reader.ReadLine();
|
---|
48 | int read = 0;
|
---|
49 | int k = 0;
|
---|
50 | while (!reader.EndOfStream) {
|
---|
51 | string valLine = reader.ReadLine();
|
---|
52 | string[] vals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries);
|
---|
53 | for (int j = 0; j < vals.Length; j++) {
|
---|
54 | if (read + j == Size) {
|
---|
55 | read = 0;
|
---|
56 | k++;
|
---|
57 | }
|
---|
58 | Weights[k, read + j] = double.Parse(vals[j]);
|
---|
59 | }
|
---|
60 | read += vals.Length;
|
---|
61 | }
|
---|
62 | return true;
|
---|
63 | } catch (Exception e) {
|
---|
64 | Error = e;
|
---|
65 | return false;
|
---|
66 | }
|
---|
67 |
|
---|
68 | }
|
---|
69 | }
|
---|
70 | }
|
---|