1 |
|
---|
2 | using System;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using HeuristicLab.Problems.RoutePlanning.Osm;
|
---|
5 | namespace HeuristicLab.Problems.RoutePlanning.Graph {
|
---|
6 | public class Graph : IGraph {
|
---|
7 | private IDictionary<long, Vertex> vertices;
|
---|
8 | private IDictionary<long, List<Edge<Vertex>>> adjacencyMap;
|
---|
9 | private IDictionary<long, List<Edge<Vertex>>> incidenceMap;
|
---|
10 |
|
---|
11 | public Graph() {
|
---|
12 | vertices = new Dictionary<long, Vertex>();
|
---|
13 | adjacencyMap = new Dictionary<long, List<Edge<Vertex>>>();
|
---|
14 | incidenceMap = new Dictionary<long, List<Edge<Vertex>>>();
|
---|
15 | }
|
---|
16 |
|
---|
17 | public void AddVertex(Vertex vertex) {
|
---|
18 | if (!vertices.ContainsKey(vertex.Id)) {
|
---|
19 | vertices.Add(vertex.Id, vertex);
|
---|
20 | }
|
---|
21 | }
|
---|
22 |
|
---|
23 | public Vertex GetVertex(long id) {
|
---|
24 | Vertex vertex = null;
|
---|
25 | vertices.TryGetValue(id, out vertex);
|
---|
26 | return vertex;
|
---|
27 | }
|
---|
28 |
|
---|
29 | public void AddEdge(long startId, long endId) {
|
---|
30 | Vertex start = GetVertex(startId);
|
---|
31 | if (start == null) throw new ArgumentException(string.Format("Invalid vertex id {0}", startId), "startId");
|
---|
32 | Vertex end = GetVertex(startId);
|
---|
33 | if (end == null) throw new ArgumentException(string.Format("Invalid vertex id {0}", endId), "endId");
|
---|
34 |
|
---|
35 | Edge<Vertex> edge = new Edge<Vertex>(start, end);
|
---|
36 | //edge.Category = -1
|
---|
37 |
|
---|
38 | AddEdge(edge);
|
---|
39 | }
|
---|
40 |
|
---|
41 | public void AddEdge(Edge<Vertex> edge) {
|
---|
42 | if (!adjacencyMap.ContainsKey(edge.Source.Id)) {
|
---|
43 | adjacencyMap.Add(edge.Source.Id, new List<Edge<Vertex>>());
|
---|
44 | }
|
---|
45 | adjacencyMap[edge.Source.Id].Add(edge);
|
---|
46 |
|
---|
47 | if (!incidenceMap.ContainsKey(edge.Target.Id)) {
|
---|
48 | incidenceMap.Add(edge.Target.Id, new List<Edge<Vertex>>());
|
---|
49 | }
|
---|
50 | incidenceMap[edge.Target.Id].Add(edge);
|
---|
51 | }
|
---|
52 |
|
---|
53 | public void RemoveEdge(long startId, long endId) {
|
---|
54 | Edge<Vertex> edge = GetEdge(startId, endId);
|
---|
55 | if (edge != null) {
|
---|
56 | adjacencyMap[startId].Remove(edge);
|
---|
57 | incidenceMap[endId].Remove(edge);
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | public bool HasEdge(long startId, long endId) {
|
---|
62 | Edge<Vertex> edge = GetEdge(startId, endId);
|
---|
63 | return (edge != null);
|
---|
64 | }
|
---|
65 |
|
---|
66 | public Edge<Vertex> GetEdge(long startId, long endId) {
|
---|
67 | List<Edge<Vertex>> edges = adjacencyMap[startId];
|
---|
68 | foreach (Edge<Vertex> edge in edges) {
|
---|
69 | if (edge.Target.Id == endId) {
|
---|
70 | return edge;
|
---|
71 | }
|
---|
72 | }
|
---|
73 | return null;
|
---|
74 | }
|
---|
75 |
|
---|
76 | public List<Edge<Vertex>> GetInEdges(long vertexId) {
|
---|
77 | List<Edge<Vertex>> edges;
|
---|
78 | if (incidenceMap.TryGetValue(vertexId, out edges)) {
|
---|
79 | return edges;
|
---|
80 | } else {
|
---|
81 | return new List<Edge<Vertex>>();
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | public int GetInDegree(long vertexId) {
|
---|
86 | return incidenceMap[vertexId].Count;
|
---|
87 | }
|
---|
88 |
|
---|
89 | public List<Edge<Vertex>> GetOutEdges(long vertexId) {
|
---|
90 | List<Edge<Vertex>> edges;
|
---|
91 | if (adjacencyMap.TryGetValue(vertexId, out edges)) {
|
---|
92 | return edges;
|
---|
93 | } else {
|
---|
94 | return new List<Edge<Vertex>>();
|
---|
95 | }
|
---|
96 |
|
---|
97 | }
|
---|
98 |
|
---|
99 | public int GetOutDegree(long vertexId) {
|
---|
100 | return adjacencyMap[vertexId].Count;
|
---|
101 | }
|
---|
102 |
|
---|
103 | public List<Edge<Vertex>> GetEdges(long vertexId) {
|
---|
104 | List<Edge<Vertex>> result = new List<Edge<Vertex>>();
|
---|
105 | foreach (Edge<Vertex> edge in GetInEdges(vertexId)) {
|
---|
106 | if (!result.Contains(edge)) {
|
---|
107 | result.Add(edge);
|
---|
108 | }
|
---|
109 | }
|
---|
110 | foreach (Edge<Vertex> edge in GetOutEdges(vertexId)) {
|
---|
111 | if (!result.Contains(edge)) {
|
---|
112 | result.Add(edge);
|
---|
113 | }
|
---|
114 | }
|
---|
115 | return result;
|
---|
116 | }
|
---|
117 |
|
---|
118 | public Dictionary<long, float> GetNeighborsWithWeight(long id) {
|
---|
119 | Dictionary<long, float> neighbors = new Dictionary<long, float>();
|
---|
120 | List<Edge<Vertex>> edges = GetOutEdges(id);
|
---|
121 | foreach (Edge<Vertex> edge in edges) {
|
---|
122 | // TODO: check if edge can be traversed (eg. highway tag for car roads, ...)
|
---|
123 | float weight = GetWeight(edge); // TODO: edge.Weight?
|
---|
124 | neighbors[edge.Target.Id] = weight;
|
---|
125 | }
|
---|
126 | return neighbors;
|
---|
127 | }
|
---|
128 | public Dictionary<long, float> GetNeighborsWithWeightReversed(long id) {
|
---|
129 | Dictionary<long, float> neighbors = new Dictionary<long, float>();
|
---|
130 | List<Edge<Vertex>> edges = GetInEdges(id);
|
---|
131 | foreach (Edge<Vertex> edge in edges) {
|
---|
132 | // TODO: check if edge can be traversed (eg. highway tag for car roads, ...)
|
---|
133 | float weight = GetWeight(edge); // TODO: edge.Weight?
|
---|
134 | neighbors[edge.Source.Id] = weight;
|
---|
135 | }
|
---|
136 | return neighbors;
|
---|
137 | }
|
---|
138 |
|
---|
139 | public float GetWeight(Edge<Vertex> edge) {
|
---|
140 | double distance = Utils.Distance(edge.Source.Logitude, edge.Source.Latitude,
|
---|
141 | edge.Target.Logitude, edge.Target.Latitude);
|
---|
142 | //double distance = Utils.LocationDistance(source.Node.Point, target.Node.Point);
|
---|
143 | int speed = edge.GetMaxSpeed();
|
---|
144 | double weight = (distance / speed) * 3.6;
|
---|
145 | //double weight = (distance / speed);
|
---|
146 | return (float)weight;
|
---|
147 | }
|
---|
148 |
|
---|
149 | public float EstimatedCosts(long sourceId, long targetId) {
|
---|
150 | Vertex source = GetVertex(sourceId);
|
---|
151 | Vertex target = GetVertex(targetId);
|
---|
152 | return EstimatedCosts(source, target);
|
---|
153 | }
|
---|
154 |
|
---|
155 | public float EstimatedCosts(Vertex source, Vertex target) {
|
---|
156 | double distance = Utils.Distance(source.Logitude, source.Latitude,
|
---|
157 | target.Logitude, target.Latitude);
|
---|
158 | //double distance = Utils.LocationDistance(source.Node.Point, target.Node.Point);
|
---|
159 | int speed = 130;
|
---|
160 | double costs = (distance / speed) * 3.6;
|
---|
161 | //double costs = (distance / speed);
|
---|
162 | return (float)costs;
|
---|
163 | }
|
---|
164 | }
|
---|
165 | }
|
---|