- Timestamp:
- 12/09/16 15:56:22 (7 years ago)
- Location:
- branches/MemPRAlgorithm/HeuristicLab.Problems.Instances.DIMACS
- Files:
-
- 2 added
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/MemPRAlgorithm/HeuristicLab.Problems.Instances.DIMACS/3.3/Parser.cs
r14429 r14471 21 21 22 22 using System; 23 using System.Collections.Generic; 23 24 using System.IO; 24 25 25 namespace HeuristicLab.Problems.Instances.QAPLIB { 26 public class QAPLIBParser { 27 public int Size { get; private set; } 28 public double[,] Distances { get; private set; } 29 public double[,] Weights { get; private set; } 26 namespace HeuristicLab.Problems.Instances.DIMACS { 27 public class Parser { 28 public int Nodes { get; private set; } 29 public int Edges { get; private set; } 30 public ICollection<Tuple<int, int>> AdjacencyList { get { return edges; } } 31 private HashSet<Tuple<int, int>> edges; 30 32 31 public QAPLIBParser() {33 public Parser() { 32 34 Reset(); 33 35 } 34 36 35 37 public void Reset() { 36 Size= 0;37 Distances = null;38 Weights = null;38 Nodes = 0; 39 Edges = 0; 40 edges = new HashSet<Tuple<int, int>>(); 39 41 } 40 42 … … 54 56 /// <returns>True if the file was successfully read or false otherwise.</returns> 55 57 public void Parse(Stream stream) { 58 char[] delim = new char[] { ' ', '\t' }; 56 59 var reader = new StreamReader(stream); 57 Size = int.Parse(reader.ReadLine()); 58 Distances = new double[Size, Size]; 59 Weights = new double[Size, Size]; 60 char[] delim = new char[] { ' ', '\t' }; 60 var line = reader.ReadLine().Trim(); 61 // skip comments 62 while (line.StartsWith("c", StringComparison.InvariantCultureIgnoreCase)) line = reader.ReadLine().Trim(); 61 63 62 Weights = ParseMatrix(reader, delim); 63 Distances = ParseMatrix(reader, delim); 64 } 65 66 private double[,] ParseMatrix(StreamReader reader, char[] delim) { 67 int read = 0, k = 0; 68 double[,] result = new double[Size, Size]; 69 while (k < Size) { 70 if (reader.EndOfStream) throw new InvalidDataException("Reached end of stream while reading second matrix."); 71 string valLine = reader.ReadLine(); 72 while (String.IsNullOrWhiteSpace(valLine)) valLine = reader.ReadLine(); 73 string[] vals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries); 74 foreach (string val in vals) { 75 result[k, read++] = double.Parse(val); 76 if (read == Size) { 77 read = 0; 78 k++; 79 } 80 } 81 } 82 return result; 64 // p edge NODES EDGES 65 var split = line.Split(delim, StringSplitOptions.RemoveEmptyEntries); 66 Nodes = int.Parse(split[2]); 67 do { 68 line = reader.ReadLine(); 69 if (string.IsNullOrEmpty(line)) break; 70 // e XX YY 71 split = line.Split(delim, StringSplitOptions.RemoveEmptyEntries); 72 var src = int.Parse(split[1]); 73 var tgt = int.Parse(split[2]); 74 Tuple<int, int> e = null; 75 if (src < tgt) e = Tuple.Create(src, tgt); 76 else if (src > tgt) e = Tuple.Create(tgt, src); 77 else continue; // src == tgt 78 if (edges.Add(e)) Edges++; 79 } while (!reader.EndOfStream); 83 80 } 84 81 }
Note: See TracChangeset
for help on using the changeset viewer.