- Timestamp:
- 08/22/12 19:14:33 (12 years ago)
- Location:
- branches/RoutePlanning/HeuristicLab.Problems.RoutePlanning/3.3
- Files:
-
- 2 added
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/RoutePlanning/HeuristicLab.Problems.RoutePlanning/3.3/Data.DIMACS/DIMACSDataSource.cs
r8516 r8520 1 1 using System; 2 2 using System.IO; 3 using HeuristicLab.Problems.RoutePlanning.Data.Core; 3 4 using HeuristicLab.Problems.RoutePlanning.Interfaces; 4 5 using HeuristicLab.Problems.RoutePlanning.RoutingGraph; … … 23 24 public IGraph GetRoutingGraph() { 24 25 return graph; 26 } 27 28 public IGraph GetRoutingGraph(VehicleType vehicle) { 29 throw new NotImplementedException(); 25 30 } 26 31 … … 103 108 Vertex vDst = graph.GetVertex(dst); 104 109 105 Edge<Vertex> edge = new Edge<Vertex>(vSrc, vDst);106 edge .Weight = len;110 Edge<Vertex> edgeForward = new Edge<Vertex>(vSrc, vDst); 111 edgeForward.Weight = len; 107 112 108 graph.AddEdge(edge); 113 Edge<Vertex> edgeBackward = new Edge<Vertex>(vSrc, vDst); 114 edgeBackward.Weight = len; 109 115 116 graph.AddEdge(edgeForward); 117 graph.AddEdge(edgeBackward); 110 118 } 111 119 s = string.Empty; -
branches/RoutePlanning/HeuristicLab.Problems.RoutePlanning/3.3/Data.Osm.Core/Way.cs
r8516 r8520 2 2 using System; 3 3 using System.Collections.Generic; 4 using HeuristicLab.Problems.RoutePlanning.Data.Core; 4 5 namespace HeuristicLab.Problems.RoutePlanning.Data.Osm.Core { 5 #region Highway Types6 7 // see: https://wiki.openstreetmap.org/wiki/Key:highway8 public enum HighwayType {9 null_type,10 motorway,11 motorway_link,12 trunk,13 trunk_link,14 primary,15 primary_link,16 secondary,17 secondary_link,18 tertiary,19 tertiary_link,20 living_street,21 pedestrian,22 residential,23 unclassified,24 service,25 track,26 bus_guideway,27 raceway,28 road,29 path,30 footway,31 cycleway,32 bridleway,33 steps34 }35 36 #endregion37 38 6 public class Way : OsmBase { 39 7 private List<Node> nodes; -
branches/RoutePlanning/HeuristicLab.Problems.RoutePlanning/3.3/Data.Osm/OsmDataSource.cs
r8516 r8520 3 3 using System.IO; 4 4 using System.Xml; 5 using HeuristicLab.Problems.RoutePlanning.Data.Core; 6 using HeuristicLab.Problems.RoutePlanning.Data.Osm.Core; 5 7 using HeuristicLab.Problems.RoutePlanning.Interfaces; 6 8 using HeuristicLab.Problems.RoutePlanning.RoutingGraph; 7 using HeuristicLab.Problems.RoutePlanning.Data.Osm.Core;8 9 9 10 namespace HeuristicLab.Problems.RoutePlanning.Data.Osm { 10 11 public class OsmDataSource : IDataSource { 11 12 private FileInfo file; 13 private ICostCalculator costCalculator; 14 private IGraph graph; 12 15 private Dictionary<long, Vertex> vertices; 13 private IGraph graph; 14 15 public OsmDataSource(string filename) { 16 17 public OsmDataSource(string filename) 18 : this(filename, new TravelTimeCostCalculator()) { 19 } 20 21 public OsmDataSource(string filename, ICostCalculator costCalc) { 16 22 file = new FileInfo(filename); 23 costCalculator = costCalc; 17 24 graph = new Graph(); 18 25 vertices = new Dictionary<long, Vertex>(); … … 25 32 public IGraph GetRoutingGraph() { 26 33 return graph; 34 } 35 36 public IGraph GetRoutingGraph(VehicleType vehicle) { 37 throw new NotImplementedException(); 27 38 } 28 39 … … 103 114 Vertex v2 = way[i + 1]; 104 115 graph.AddVertex(v2); 105 short vertexCategory = (short)category; 116 //short vertexCategory = (short)category; 117 float weight = costCalculator.CalculateCosts(v1, v2, category); 106 118 if (oneWayRoad) { 107 Edge<Vertex> edge = new Edge<Vertex>(v1, v2, vertexCategory);119 Edge<Vertex> edge = new Edge<Vertex>(v1, v2, weight); 108 120 graph.AddEdge(edge); 109 121 } else { 110 Edge<Vertex> edgeForward = new Edge<Vertex>(v1, v2, vertexCategory);122 Edge<Vertex> edgeForward = new Edge<Vertex>(v1, v2, weight); 111 123 graph.AddEdge(edgeForward); 112 124 113 Edge<Vertex> edgeBackward = new Edge<Vertex>(v2, v1, vertexCategory);125 Edge<Vertex> edgeBackward = new Edge<Vertex>(v2, v1, weight); 114 126 graph.AddEdge(edgeBackward); 115 127 } … … 201 213 Vertex v2 = way[i + 1]; 202 214 graph.AddVertex(v2); 203 short vertexCategory = (short)category; 215 //short vertexCategory = (short)category; 216 float weight = costCalculator.CalculateCosts(v1, v2, category); 204 217 if (oneWayRoad) { 205 Edge<Vertex> edge = new Edge<Vertex>(v1, v2, vertexCategory);218 Edge<Vertex> edge = new Edge<Vertex>(v1, v2, weight); 206 219 graph.AddEdge(edge); 207 220 } else { 208 Edge<Vertex> edgeForward = new Edge<Vertex>(v1, v2, vertexCategory);221 Edge<Vertex> edgeForward = new Edge<Vertex>(v1, v2, weight); 209 222 graph.AddEdge(edgeForward); 210 223 211 Edge<Vertex> edgeBackward = new Edge<Vertex>(v2, v1, vertexCategory);224 Edge<Vertex> edgeBackward = new Edge<Vertex>(v2, v1, weight); 212 225 graph.AddEdge(edgeBackward); 213 226 } … … 218 231 } 219 232 } 233 234 //private float CalculateWeight(Vertex s, Vertex t, HighwayType ht) { 235 // double distanceKms = Utils.LocationDistance(s.Logitude, s.Latitude, t.Logitude, t.Latitude); 236 // double weight = distanceKms; 237 // return (float)weight; 238 //} 239 240 //private float CalculateWeight(Vertex s, Vertex t, HighwayType ht) { 241 // double distanceKms = Utils.LocationDistance(s.Logitude, s.Latitude, t.Logitude, t.Latitude); 242 // int speedKmH = GetMaxSpeed(ht); 243 // double weight = (distanceKms / speedKmH) * 60; 244 // return (float)weight; 245 //} 220 246 } 221 247 } -
branches/RoutePlanning/HeuristicLab.Problems.RoutePlanning/3.3/HeuristicLab.Problems.RoutePlanning.csproj
r8516 r8520 57 57 </ItemGroup> 58 58 <ItemGroup> 59 <Compile Include="Data.Core\Enums.cs" /> 59 60 <Compile Include="Data.DIMACS\DIMACSDataSource.cs" /> 61 <Compile Include="Interfaces\ICostCalculator.cs" /> 60 62 <Compile Include="RoutingGraph\EarthDistanceCostCalculator.cs" /> 61 <Compile Include="Interfaces\ICostCalculator.cs" />62 63 <Compile Include="RoutingGraph\Edge.cs" /> 63 64 <Compile Include="RoutingGraph\Graph.cs" /> -
branches/RoutePlanning/HeuristicLab.Problems.RoutePlanning/3.3/Interfaces/ICostCalculator.cs
r8516 r8520 1 1 2 using HeuristicLab.Problems.RoutePlanning.Data.Core; 2 3 using HeuristicLab.Problems.RoutePlanning.RoutingGraph; 3 4 namespace HeuristicLab.Problems.RoutePlanning.Interfaces { 4 5 public interface ICostCalculator { 5 6 float Costs(Edge<Vertex> edge); 7 float CalculateCosts(Vertex source, Vertex destination, HighwayType category); 6 8 float EstimatedCosts(Vertex source, Vertex destination); 7 9 } -
branches/RoutePlanning/HeuristicLab.Problems.RoutePlanning/3.3/Interfaces/IDataSource.cs
r8516 r8520 1 1 2 using HeuristicLab.Problems.RoutePlanning.Data.Core; 2 3 namespace HeuristicLab.Problems.RoutePlanning.Interfaces { 3 4 public interface IDataSource { 4 5 IGraph GetRoutingGraph(); 6 IGraph GetRoutingGraph(VehicleType vehicle); 5 7 } 6 8 } -
branches/RoutePlanning/HeuristicLab.Problems.RoutePlanning/3.3/Interfaces/IGraph.cs
r8516 r8520 20 20 int GetInDegree(long id); 21 21 22 float EstimatedCosts(long sourceId, long targetId);22 //float EstimatedCosts(long sourceId, long targetId); 23 23 } 24 24 } -
branches/RoutePlanning/HeuristicLab.Problems.RoutePlanning/3.3/RoutingGraph/EarthDistanceCostCalculator.cs
r8516 r8520 1 using System;1 using HeuristicLab.Problems.RoutePlanning.Data.Core; 2 2 using HeuristicLab.Problems.RoutePlanning.Interfaces; 3 using HeuristicLab.Problems.RoutePlanning.Utilities; 3 4 4 5 namespace HeuristicLab.Problems.RoutePlanning.RoutingGraph { 5 6 public class EarthDistanceCostCalculator : ICostCalculator { 6 private const double earthMeanRadius = 6371.0 * 1000.0;7 8 7 #region ICostCalculator Members 9 8 … … 12 11 } 13 12 13 public float CalculateCosts(Vertex source, Vertex destination, HighwayType category) { 14 double distanceKms = Utils.LocationDistance(source.Logitude, source.Latitude, 15 destination.Logitude, destination.Latitude); 16 double weight = distanceKms; 17 return (float)weight; 18 } 19 14 20 public float EstimatedCosts(Vertex source, Vertex destination) { 15 return (float)EarthDistance(source.Logitude, source.Latitude, destination.Logitude, destination.Latitude); 21 double distanceKms = Utils.LocationDistance(source.Logitude, source.Latitude, 22 destination.Logitude, destination.Latitude); 23 double weight = distanceKms; 24 return (float)weight; 16 25 } 17 26 18 27 #endregion 19 20 private double DegToRad(double alpha) {21 return alpha * Math.PI / 180;22 }23 24 private double AngularDistance(double startLon, double startLat, double endLon, double endLat) {25 double lat1 = DegToRad(startLat);26 double lat2 = DegToRad(endLat);27 double long1 = DegToRad(startLon);28 double long2 = DegToRad(endLat);29 30 double x = Math.Sin(lat1) * Math.Sin(lat2) + Math.Cos(lat1) * Math.Cos(lat2) * Math.Cos(long1 - long2);31 x = Math.Min(1, Math.Max(0, x));32 33 return Math.Acos(x);34 }35 36 private double EarthDistance(double startLon, double startLat, double endLon, double endLat) {37 return earthMeanRadius * AngularDistance(startLon, startLat, endLon, endLat);38 }39 28 } 40 29 } -
branches/RoutePlanning/HeuristicLab.Problems.RoutePlanning/3.3/RoutingGraph/Edge.cs
r8516 r8520 13 13 } 14 14 15 private short category;16 public short Category {17 get { return category; }18 set { category = value; }19 }15 //private short category; 16 //public short Category { 17 // get { return category; } 18 // set { category = value; } 19 //} 20 20 21 21 private float weight; … … 30 30 } 31 31 32 public Edge(Vertex source, Vertex target, short category) {32 public Edge(Vertex source, Vertex target, float weight) { 33 33 this.source = source; 34 34 this.target = target; 35 this. category = category;35 this.weight = weight; 36 36 } 37 37 … … 51 51 return source.GetHashCode() ^ target.GetHashCode(); 52 52 } 53 54 public int GetMaxSpeed() {55 int speed = 0;56 57 //TODO:58 switch (category) {59 case 23:60 case 17:61 case 18:62 case 22:63 case 21:64 case 24:65 case 16:66 case 20:67 case 15:68 case 12:69 case 0:70 speed = 1;71 break;72 73 case 11:74 case 14:75 speed = 15;76 break;77 78 case 19:79 speed = 30;80 break;81 82 case 9:83 case 10:84 case 7:85 case 8:86 speed = 80;87 break;88 89 case 13:90 speed = 50;91 break;92 93 case 3:94 case 4:95 case 5:96 case 6:97 speed = 100;98 break;99 100 case 1:101 case 2:102 speed = 130;103 break;104 105 default:106 speed = 1;107 break;108 }109 return speed;110 }111 53 } 112 54 } -
branches/RoutePlanning/HeuristicLab.Problems.RoutePlanning/3.3/RoutingGraph/Graph.cs
r8516 r8520 3 3 using System.Collections.Generic; 4 4 using HeuristicLab.Problems.RoutePlanning.Interfaces; 5 using HeuristicLab.Problems.RoutePlanning.Utilities;6 5 namespace HeuristicLab.Problems.RoutePlanning.RoutingGraph { 7 6 public class Graph : IGraph { … … 126 125 foreach (Edge<Vertex> edge in edges) { 127 126 // TODO: check if edge can be traversed (eg. highway tag for car roads, ...) 128 float weight = GetWeight(edge); // TODO: edge.Weight? 129 neighbors[edge.Target.Id] = weight; 127 //float weight = GetWeight(edge); // TODO: edge.Weight? 128 //neighbors[edge.Target.Id] = weight; 129 neighbors[edge.Target.Id] = edge.Weight; 130 130 } 131 131 return neighbors; … … 136 136 foreach (Edge<Vertex> edge in edges) { 137 137 // TODO: check if edge can be traversed (eg. highway tag for car roads, ...) 138 float weight = GetWeight(edge); // TODO: edge.Weight? 139 neighbors[edge.Source.Id] = weight; 138 //float weight = GetWeight(edge); // TODO: edge.Weight? 139 //neighbors[edge.Source.Id] = weight; 140 neighbors[edge.Source.Id] = edge.Weight; 140 141 } 141 142 return neighbors; 142 143 } 143 144 144 public float GetWeight(Edge<Vertex> edge) {145 //double distance = Utils.Distance(edge.Source.Logitude, edge.Source.Latitude,146 // edge.Target.Logitude, edge.Target.Latitude);145 //public float GetWeight(Edge<Vertex> edge) { 146 // //double distance = Utils.Distance(edge.Source.Logitude, edge.Source.Latitude, 147 // // edge.Target.Logitude, edge.Target.Latitude); 147 148 148 PointD s = new PointD(edge.Source.Logitude, edge.Source.Latitude);149 PointD t = new PointD(edge.Target.Logitude, edge.Target.Latitude);150 double distance = Utils.LocationDistance(s, t);149 // PointD s = new PointD(edge.Source.Logitude, edge.Source.Latitude); 150 // PointD t = new PointD(edge.Target.Logitude, edge.Target.Latitude); 151 // double distance = Utils.LocationDistance(s, t); 151 152 152 int speed = edge.GetMaxSpeed(); 153 double weight = (distance / speed) * 60; 154 //double weight = (distance / speed) * 3.6; 155 //double weight = (distance / speed); 156 //double weight = distance; 157 return (float)weight; 158 } 153 // //int speed = edge.GetMaxSpeed(); 154 // //double weight = (distance / speed) * 60; 155 // //double weight = (distance / speed) * 3.6; 156 // //double weight = (distance / speed); 157 // //double weight = distance; 158 // //return (float)weight; 159 // return (float)1; 160 //} 159 161 160 public float EstimatedCosts(long sourceId, long targetId) {161 Vertex source = GetVertex(sourceId);162 Vertex target = GetVertex(targetId);163 return EstimatedCosts(source, target);164 }162 //public float EstimatedCosts(long sourceId, long targetId) { 163 // Vertex source = GetVertex(sourceId); 164 // Vertex target = GetVertex(targetId); 165 // return EstimatedCosts(source, target); 166 //} 165 167 166 public float EstimatedCosts(Vertex source, Vertex target) {167 //double distance = Utils.Distance(source.Logitude, source.Latitude,168 // target.Logitude, target.Latitude);168 //public float EstimatedCosts(Vertex source, Vertex target) { 169 // //double distance = Utils.Distance(source.Logitude, source.Latitude, 170 // // target.Logitude, target.Latitude); 169 171 170 PointD s = new PointD(source.Logitude, source.Latitude);171 PointD t = new PointD(target.Logitude, target.Latitude);172 double distance = Utils.LocationDistance(s, t);172 // PointD s = new PointD(source.Logitude, source.Latitude); 173 // PointD t = new PointD(target.Logitude, target.Latitude); 174 // double distance = Utils.LocationDistance(s, t); 173 175 174 //int speed = 130;175 //int speed = 10;176 int speed = 130;177 double costs = (distance / speed) * 60;178 //double costs = (distance / speed) * 3.6;179 //double costs = (distance / speed);180 //double costs = distance;181 return (float)costs;182 }176 // //int speed = 130; 177 // //int speed = 10; 178 // int speed = 130; 179 // double costs = (distance / speed) * 60; 180 // //double costs = (distance / speed) * 3.6; 181 // //double costs = (distance / speed); 182 // //double costs = distance; 183 // return (float)costs; 184 //} 183 185 } 184 186 } -
branches/RoutePlanning/HeuristicLab.Problems.RoutePlanning/3.3/RoutingGraph/TravelTimeCostCalculator.cs
r8516 r8520 1 1 2 using HeuristicLab.Problems.RoutePlanning.Data.Core; 2 3 using HeuristicLab.Problems.RoutePlanning.Interfaces; 3 4 using HeuristicLab.Problems.RoutePlanning.Utilities; … … 7 8 8 9 public float Costs(Edge<Vertex> edge) { 9 double distance = Utils.Distance(edge.Source.Logitude, edge.Source.Latitude,10 edge.Target.Logitude, edge.Target.Latitude);10 //double distance = Utils.Distance(edge.Source.Logitude, edge.Source.Latitude, 11 // edge.Target.Logitude, edge.Target.Latitude); 11 12 //double distance = Utils.LocationDistance(source.Node.Point, target.Node.Point); 12 int speed = edge.GetMaxSpeed();13 double weight = (distance / speed) * 3.6;13 //int speed = edge.GetMaxSpeed(); 14 //double weight = (distance / speed) * 3.6; 14 15 //double weight = (distance / speed); 16 //return (float)1; 17 return edge.Weight; 18 } 19 20 public float CalculateCosts(Vertex source, Vertex destination, HighwayType category) { 21 double distanceKms = Utils.Distance(source.Logitude, source.Latitude, 22 destination.Logitude, destination.Latitude); 23 int speedKmH = GetMaxSpeed(category); 24 double weight = (distanceKms / speedKmH) * 60; 15 25 return (float)weight; 16 26 } 17 27 18 28 public float EstimatedCosts(Vertex source, Vertex destination) { 19 double distance = Utils.Distance(source.Logitude, source.Latitude,20 destination.Logitude, destination.Latitude);29 //double distance = Utils.Distance(source.Logitude, source.Latitude, 30 // destination.Logitude, destination.Latitude); 21 31 //double distance = Utils.LocationDistance(source.Node.Point, target.Node.Point); 22 int speed = 130;23 double costs = (distance / speed) * 3.6;32 int speedKmH = 130; 33 //double costs = (distance / speed) * 3.6; 24 34 //double costs = (distance / speed); 25 return (float)costs; 35 //return (float)costs; 36 double distanceKms = Utils.Distance(source.Logitude, source.Latitude, 37 destination.Logitude, destination.Latitude); 38 double weight = (distanceKms / speedKmH) * 60; 39 return (float)weight; 40 } 41 42 private int GetMaxSpeed(short category) { 43 int speed = 0; 44 45 //TODO: 46 switch (category) { 47 case 23: 48 case 17: 49 case 18: 50 case 22: 51 case 21: 52 case 24: 53 case 16: 54 case 20: 55 case 15: 56 case 12: 57 case 0: 58 speed = 1; 59 break; 60 61 case 11: 62 case 14: 63 speed = 15; 64 break; 65 66 case 19: 67 speed = 30; 68 break; 69 70 case 9: 71 case 10: 72 case 7: 73 case 8: 74 speed = 80; 75 break; 76 77 case 13: 78 speed = 50; 79 break; 80 81 case 3: 82 case 4: 83 case 5: 84 case 6: 85 speed = 100; 86 break; 87 88 case 1: 89 case 2: 90 speed = 130; 91 break; 92 93 default: 94 speed = 1; 95 break; 96 } 97 return speed; 98 } 99 100 public int GetMaxSpeed(HighwayType ht) { 101 int speed = 0; 102 103 //TODO: 104 switch (ht) { 105 case HighwayType.bridleway: 106 case HighwayType.bus_guideway: 107 case HighwayType.raceway: 108 case HighwayType.cycleway: 109 case HighwayType.footway: 110 case HighwayType.steps: 111 case HighwayType.null_type: 112 speed = 1; 113 break; 114 115 case HighwayType.path: 116 case HighwayType.service: 117 case HighwayType.pedestrian: 118 case HighwayType.living_street: 119 speed = 15; 120 break; 121 122 case HighwayType.road: 123 case HighwayType.track: 124 speed = 30; 125 break; 126 127 case HighwayType.tertiary: 128 case HighwayType.tertiary_link: 129 case HighwayType.secondary: 130 case HighwayType.secondary_link: 131 speed = 80; 132 break; 133 134 case HighwayType.unclassified: 135 case HighwayType.residential: 136 speed = 50; 137 break; 138 139 case HighwayType.trunk: 140 case HighwayType.trunk_link: 141 case HighwayType.primary: 142 case HighwayType.primary_link: 143 speed = 100; 144 break; 145 case HighwayType.motorway: 146 case HighwayType.motorway_link: 147 speed = 130; 148 break; 149 150 default: 151 speed = 1; 152 break; 153 } 154 return speed; 26 155 } 27 156 -
branches/RoutePlanning/HeuristicLab.Problems.RoutePlanning/3.3/Utilities/Utils.cs
r8516 r8520 17 17 18 18 public static double LocationDistance(PointD p1, PointD p2) { 19 return LocationDistance(p1.X, p1.Y, p2.X, p2.Y); 20 } 21 22 // calculates the distance of the 2 given locations in kilometers 23 public static double LocationDistance(double startX, double startY, double endX, double endY) { 19 24 // The Haversine formula 20 25 /* dlon = lon2 - lon1 … … 25 30 */ 26 31 27 double lat1Rad = p1.Y * (Math.PI / 180.0);28 double long1Rad = p1.X * (Math.PI / 180.0);29 double lat2Rad = p2.Y * (Math.PI / 180.0);30 double long2Rad = p2.X * (Math.PI / 180.0);32 double lat1Rad = startY * (Math.PI / 180.0); 33 double long1Rad = startX * (Math.PI / 180.0); 34 double lat2Rad = endY * (Math.PI / 180.0); 35 double long2Rad = endX * (Math.PI / 180.0); 31 36 32 37 double longitude = long2Rad - long1Rad; … … 39 44 double c = 2.0 * Math.Asin(Math.Sqrt(a)); 40 45 41 const double earthRadiusKms = 63 76.5;46 const double earthRadiusKms = 6367.5; 42 47 double distance = earthRadiusKms * c; 43 48
Note: See TracChangeset
for help on using the changeset viewer.