Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1330

  • Worked on QAP
  • Added QAPLIB problems
File size: 2.1 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      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}
Note: See TracBrowser for help on using the repository browser.