using System;
using System.IO;
namespace HeuristicLab.Problems.QuadraticAssignment {
public class QAPLIBParser {
public int Size { get; private set; }
public double[,] Distances { get; private set; }
public double[,] Weights { get; private set; }
public Exception Error { get; private set; }
public QAPLIBParser() {
Reset();
}
public void Reset() {
Size = 0;
Distances = null;
Weights = null;
Error = null;
}
public bool Parse(string file) {
using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read)) {
return Parse(stream);
}
}
///
/// Reads from the given stream data which is expected to be in the QAPLIB format.
///
///
/// The stream is not closed or disposed. The caller has to take care of that.
///
/// The stream to read data from.
/// True if the file was successfully read or false otherwise.
public bool Parse(Stream stream) {
Error = null;
try {
StreamReader reader = new StreamReader(stream);
Size = int.Parse(reader.ReadLine());
Distances = new double[Size, Size];
Weights = new double[Size, Size];
reader.ReadLine();
char[] delim = new char[] { ' ' };
for (int i = 0; i < Size; i++) {
string valLine = reader.ReadLine();
string[] vals = new string[Size];
string[] partVals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries);
partVals.CopyTo(vals, 0);
int index = partVals.Length;
while (index < Size) {
valLine = reader.ReadLine();
partVals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries);
partVals.CopyTo(vals, index);
index += partVals.Length;
}
for (int j = 0; j < Size; j++) {
Distances[i, j] = double.Parse(vals[j]);
}
}
reader.ReadLine();
int read = 0;
int k = 0;
while (!reader.EndOfStream) {
string valLine = reader.ReadLine();
string[] vals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries);
for (int j = 0; j < vals.Length; j++) {
if (read + j == Size) {
read = 0;
k++;
}
Weights[k, read + j] = double.Parse(vals[j]);
}
read += vals.Length;
}
return true;
} catch (Exception e) {
Error = e;
return false;
}
}
}
}