[7445] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14185] | 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[7445] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
[14471] | 23 | using System.Collections.Generic;
|
---|
[7445] | 24 | using System.IO;
|
---|
| 25 |
|
---|
[14471] | 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;
|
---|
[7445] | 32 |
|
---|
[14471] | 33 | public Parser() {
|
---|
[7445] | 34 | Reset();
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | public void Reset() {
|
---|
[14471] | 38 | Nodes = 0;
|
---|
| 39 | Edges = 0;
|
---|
| 40 | edges = new HashSet<Tuple<int, int>>();
|
---|
[7445] | 41 | }
|
---|
| 42 |
|
---|
[7538] | 43 | public void Parse(string file) {
|
---|
[7445] | 44 | using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read)) {
|
---|
[7538] | 45 | Parse(stream);
|
---|
[7445] | 46 | }
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | /// <summary>
|
---|
| 50 | /// Reads from the given stream data which is expected to be in the QAPLIB format.
|
---|
| 51 | /// </summary>
|
---|
| 52 | /// <remarks>
|
---|
| 53 | /// The stream is not closed or disposed. The caller has to take care of that.
|
---|
| 54 | /// </remarks>
|
---|
| 55 | /// <param name="stream">The stream to read data from.</param>
|
---|
| 56 | /// <returns>True if the file was successfully read or false otherwise.</returns>
|
---|
[7538] | 57 | public void Parse(Stream stream) {
|
---|
[14471] | 58 | char[] delim = new char[] { ' ', '\t' };
|
---|
[7538] | 59 | var reader = new StreamReader(stream);
|
---|
[14471] | 60 | var line = reader.ReadLine().Trim();
|
---|
| 61 | // skip comments
|
---|
| 62 | while (line.StartsWith("c", StringComparison.InvariantCultureIgnoreCase)) line = reader.ReadLine().Trim();
|
---|
[7445] | 63 |
|
---|
[14471] | 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);
|
---|
[7538] | 80 | }
|
---|
[7445] | 81 | }
|
---|
| 82 | }
|
---|