Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RoutePlanning/HeuristicLab.Problems.RoutePlanning/3.3/Data.DIMACS/DIMACSDataSource.cs @ 8516

Last change on this file since 8516 was 8516, checked in by spimming, 12 years ago

#1894:

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