[8516] | 1 | using System;
|
---|
| 2 | using System.IO;
|
---|
[8520] | 3 | using HeuristicLab.Problems.RoutePlanning.Data.Core;
|
---|
[8516] | 4 | using HeuristicLab.Problems.RoutePlanning.Interfaces;
|
---|
| 5 | using HeuristicLab.Problems.RoutePlanning.RoutingGraph;
|
---|
| 6 | using ICSharpCode.SharpZipLib.GZip;
|
---|
| 7 |
|
---|
| 8 | namespace HeuristicLab.Problems.RoutePlanning.Data.DIMACS {
|
---|
| 9 | public class DIMACSDataSource : IDataSource {
|
---|
| 10 | FileInfo graphFile;
|
---|
| 11 | FileInfo coordinatesFile;
|
---|
| 12 |
|
---|
| 13 | IGraph graph;
|
---|
| 14 |
|
---|
| 15 | public DIMACSDataSource(string coordinates, string inputgraph) {
|
---|
| 16 | coordinatesFile = new FileInfo(coordinates);
|
---|
| 17 | graphFile = new FileInfo(inputgraph);
|
---|
| 18 | graph = new Graph();
|
---|
| 19 | ReadData();
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | #region IDataSource Members
|
---|
| 23 |
|
---|
| 24 | public IGraph GetRoutingGraph() {
|
---|
| 25 | return graph;
|
---|
| 26 | }
|
---|
| 27 |
|
---|
[8520] | 28 | public IGraph GetRoutingGraph(VehicleType vehicle) {
|
---|
| 29 | throw new NotImplementedException();
|
---|
| 30 | }
|
---|
| 31 |
|
---|
[8516] | 32 | #endregion
|
---|
| 33 |
|
---|
| 34 | #region Private Methods
|
---|
| 35 |
|
---|
| 36 | private void ReadData() {
|
---|
| 37 | using (FileStream fs = File.OpenRead(coordinatesFile.FullName))
|
---|
| 38 | using (GZipInputStream input = new GZipInputStream(fs)) {
|
---|
| 39 | string s = "";
|
---|
| 40 |
|
---|
| 41 | int i;
|
---|
| 42 |
|
---|
| 43 | while ((i = input.ReadByte()) != -1) {
|
---|
| 44 | char c = (char)i;
|
---|
| 45 | if (c == '\r' || c == '\n') {
|
---|
| 46 | if (s.StartsWith("c")) {
|
---|
| 47 | if (s.Length > 1) {
|
---|
| 48 | // comment
|
---|
| 49 | Console.WriteLine(s);
|
---|
| 50 | }
|
---|
| 51 | } else if (s.StartsWith("p")) {
|
---|
| 52 | string[] line = s.Split(' ');
|
---|
| 53 | if (line[1] == "aux" && line[2] == "sp" && line[3] == "co") {
|
---|
| 54 | int vertices = int.Parse(line[4]);
|
---|
| 55 | Console.WriteLine("vertices: {0}", vertices);
|
---|
| 56 | } else {
|
---|
| 57 | throw new ArgumentException("Wrong problem format");
|
---|
| 58 | }
|
---|
| 59 | } else if (s.StartsWith("v")) {
|
---|
| 60 | string[] line = s.Split(' ');
|
---|
| 61 |
|
---|
| 62 | long id = int.Parse(line[1]);
|
---|
| 63 | double lon = double.Parse(line[2]);
|
---|
| 64 | double lat = double.Parse(line[3]);
|
---|
| 65 |
|
---|
| 66 | Vertex v = new Vertex(id, lon, lat);
|
---|
| 67 | graph.AddVertex(v);
|
---|
| 68 | }
|
---|
| 69 | s = string.Empty;
|
---|
| 70 | } else {
|
---|
| 71 | s += c;
|
---|
| 72 | }
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 |
|
---|
| 77 | using (FileStream fs = File.OpenRead(graphFile.FullName))
|
---|
| 78 | using (GZipInputStream input = new GZipInputStream(fs)) {
|
---|
| 79 | string s = "";
|
---|
| 80 |
|
---|
| 81 | int i;
|
---|
| 82 |
|
---|
| 83 | while ((i = input.ReadByte()) != -1) {
|
---|
| 84 | char c = (char)i;
|
---|
| 85 | if (c == '\r' || c == '\n') {
|
---|
| 86 | if (s.StartsWith("c")) {
|
---|
| 87 | if (s.Length > 1) {
|
---|
| 88 | // comment
|
---|
| 89 | Console.WriteLine(s);
|
---|
| 90 | }
|
---|
| 91 | } else if (s.StartsWith("p")) {
|
---|
| 92 | string[] line = s.Split(' ');
|
---|
| 93 | if (line[1] == "sp") {
|
---|
| 94 | int vertices = int.Parse(line[2]);
|
---|
| 95 | int edges = int.Parse(line[3]);
|
---|
| 96 | Console.WriteLine("vertices: {0}; edges: {1}", vertices, edges);
|
---|
| 97 | } else {
|
---|
| 98 | throw new ArgumentException("Wrong problem format");
|
---|
| 99 | }
|
---|
| 100 | } else if (s.StartsWith("a")) {
|
---|
| 101 | string[] line = s.Split(' ');
|
---|
| 102 |
|
---|
| 103 | long src = int.Parse(line[1]);
|
---|
| 104 | long dst = int.Parse(line[2]);
|
---|
| 105 | int len = int.Parse(line[3]);
|
---|
| 106 |
|
---|
| 107 | Vertex vSrc = graph.GetVertex(src);
|
---|
| 108 | Vertex vDst = graph.GetVertex(dst);
|
---|
| 109 |
|
---|
[8520] | 110 | Edge<Vertex> edgeForward = new Edge<Vertex>(vSrc, vDst);
|
---|
| 111 | edgeForward.Weight = len;
|
---|
[8516] | 112 |
|
---|
[8520] | 113 | Edge<Vertex> edgeBackward = new Edge<Vertex>(vSrc, vDst);
|
---|
| 114 | edgeBackward.Weight = len;
|
---|
[8516] | 115 |
|
---|
[8520] | 116 | graph.AddEdge(edgeForward);
|
---|
| 117 | graph.AddEdge(edgeBackward);
|
---|
[8516] | 118 | }
|
---|
| 119 | s = string.Empty;
|
---|
| 120 | } else {
|
---|
| 121 | s += c;
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | #endregion
|
---|
| 128 | }
|
---|
| 129 | }
|
---|