using System; using System.IO; using HeuristicLab.Problems.RoutePlanning.Data.Core; using HeuristicLab.Problems.RoutePlanning.Interfaces; using HeuristicLab.Problems.RoutePlanning.RoutingGraph; using ICSharpCode.SharpZipLib.GZip; namespace HeuristicLab.Problems.RoutePlanning.Data.DIMACS { public class DIMACSDataSource : IDataSource { FileInfo graphFile; FileInfo coordinatesFile; IGraph graph; public DIMACSDataSource(string coordinates, string inputgraph) { coordinatesFile = new FileInfo(coordinates); graphFile = new FileInfo(inputgraph); graph = new Graph(); ReadData(); } #region IDataSource Members public IGraph GetRoutingGraph() { return graph; } public IGraph GetRoutingGraph(VehicleType vehicle) { throw new NotImplementedException(); } #endregion #region Private Methods private void ReadData() { using (FileStream fs = File.OpenRead(coordinatesFile.FullName)) using (GZipInputStream input = new GZipInputStream(fs)) { string s = ""; int i; while ((i = input.ReadByte()) != -1) { char c = (char)i; if (c == '\r' || c == '\n') { if (s.StartsWith("c")) { if (s.Length > 1) { // comment Console.WriteLine(s); } } else if (s.StartsWith("p")) { string[] line = s.Split(' '); if (line[1] == "aux" && line[2] == "sp" && line[3] == "co") { int vertices = int.Parse(line[4]); Console.WriteLine("vertices: {0}", vertices); } else { throw new ArgumentException("Wrong problem format"); } } else if (s.StartsWith("v")) { string[] line = s.Split(' '); long id = int.Parse(line[1]); double lon = double.Parse(line[2]); double lat = double.Parse(line[3]); Vertex v = new Vertex(id, lon, lat); graph.AddVertex(v); } s = string.Empty; } else { s += c; } } } using (FileStream fs = File.OpenRead(graphFile.FullName)) using (GZipInputStream input = new GZipInputStream(fs)) { string s = ""; int i; while ((i = input.ReadByte()) != -1) { char c = (char)i; if (c == '\r' || c == '\n') { if (s.StartsWith("c")) { if (s.Length > 1) { // comment Console.WriteLine(s); } } else if (s.StartsWith("p")) { string[] line = s.Split(' '); if (line[1] == "sp") { int vertices = int.Parse(line[2]); int edges = int.Parse(line[3]); Console.WriteLine("vertices: {0}; edges: {1}", vertices, edges); } else { throw new ArgumentException("Wrong problem format"); } } else if (s.StartsWith("a")) { string[] line = s.Split(' '); long src = int.Parse(line[1]); long dst = int.Parse(line[2]); int len = int.Parse(line[3]); Vertex vSrc = graph.GetVertex(src); Vertex vDst = graph.GetVertex(dst); Edge edgeForward = new Edge(vSrc, vDst); edgeForward.Weight = len; Edge edgeBackward = new Edge(vSrc, vDst); edgeBackward.Weight = len; graph.AddEdge(edgeForward); graph.AddEdge(edgeBackward); } s = string.Empty; } else { s += c; } } } } #endregion } }