[8438] | 1 |
|
---|
| 2 | using System;
|
---|
| 3 | using System.Collections.Generic;
|
---|
[8479] | 4 | using HeuristicLab.Problems.RoutePlanning.Osm;
|
---|
[8438] | 5 | namespace HeuristicLab.Problems.RoutePlanning.Graph {
|
---|
[8461] | 6 | public class Graph : IGraph {
|
---|
[8438] | 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>>>();
|
---|
[8461] | 14 | incidenceMap = new Dictionary<long, List<Edge<Vertex>>>();
|
---|
[8438] | 15 | }
|
---|
| 16 |
|
---|
| 17 | public void AddVertex(Vertex vertex) {
|
---|
[8461] | 18 | if (!vertices.ContainsKey(vertex.Id)) {
|
---|
| 19 | vertices.Add(vertex.Id, vertex);
|
---|
| 20 | }
|
---|
[8438] | 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 |
|
---|
[8461] | 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>>());
|
---|
[8438] | 44 | }
|
---|
[8461] | 45 | adjacencyMap[edge.Source.Id].Add(edge);
|
---|
[8438] | 46 |
|
---|
[8461] | 47 | if (!incidenceMap.ContainsKey(edge.Target.Id)) {
|
---|
| 48 | incidenceMap.Add(edge.Target.Id, new List<Edge<Vertex>>());
|
---|
[8438] | 49 | }
|
---|
[8461] | 50 | incidenceMap[edge.Target.Id].Add(edge);
|
---|
[8438] | 51 | }
|
---|
| 52 |
|
---|
[8461] | 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 |
|
---|
[8438] | 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) {
|
---|
[8479] | 77 | List<Edge<Vertex>> edges;
|
---|
| 78 | if (incidenceMap.TryGetValue(vertexId, out edges)) {
|
---|
| 79 | return edges;
|
---|
| 80 | } else {
|
---|
| 81 | return new List<Edge<Vertex>>();
|
---|
| 82 | }
|
---|
[8461] | 83 | }
|
---|
[8438] | 84 |
|
---|
[8461] | 85 | public int GetInDegree(long vertexId) {
|
---|
| 86 | return incidenceMap[vertexId].Count;
|
---|
[8438] | 87 | }
|
---|
| 88 |
|
---|
| 89 | public List<Edge<Vertex>> GetOutEdges(long vertexId) {
|
---|
[8479] | 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 |
|
---|
[8438] | 97 | }
|
---|
[8461] | 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, ...)
|
---|
[8479] | 123 | float weight = GetWeight(edge); // TODO: edge.Weight?
|
---|
[8461] | 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, ...)
|
---|
[8479] | 133 | float weight = GetWeight(edge); // TODO: edge.Weight?
|
---|
[8461] | 134 | neighbors[edge.Source.Id] = weight;
|
---|
| 135 | }
|
---|
| 136 | return neighbors;
|
---|
| 137 | }
|
---|
[8479] | 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 | }
|
---|
[8438] | 164 | }
|
---|
| 165 | }
|
---|