Fixed AStar Weight-assignment

OSMEdge.cs:
renamed speedped, speedcar
This commit is contained in:
C9Glax 2023-02-08 19:05:13 +01:00
parent d18e4f5abf
commit f15171a9f1
2 changed files with 38 additions and 30 deletions

View File

@ -44,7 +44,7 @@ public class OsmEdge
highway, oneway, footway, sidewalk, cycleway, busway, forward, maxspeed, name, surface, lanes, access, tracktype, id, EMPTY highway, oneway, footway, sidewalk, cycleway, busway, forward, maxspeed, name, surface, lanes, access, tracktype, id, EMPTY
} }
public static Dictionary<wayType, byte> speedcar = new() { private static readonly Dictionary<wayType, byte> defaultSpeedCar = new() {
{ wayType.NONE, 0 }, { wayType.NONE, 0 },
{ wayType.motorway, 110 }, { wayType.motorway, 110 },
{ wayType.trunk, 100 }, { wayType.trunk, 100 },
@ -76,7 +76,7 @@ public class OsmEdge
{ wayType.construction, 0 } { wayType.construction, 0 }
}; };
public static Dictionary<wayType, byte> speedped = new() { private static readonly Dictionary<wayType, byte> defaultSpeedPedestrian = new() {
{ wayType.NONE, 0 }, { wayType.NONE, 0 },
{ wayType.motorway, 0 }, { wayType.motorway, 0 },
{ wayType.trunk, 0 }, { wayType.trunk, 0 },
@ -115,7 +115,7 @@ public class OsmEdge
{ {
KeyValuePair<tagType, object> tag = ConvertToTag(key, value); KeyValuePair<tagType, object> tag = ConvertToTag(key, value);
if(tag.Key != tagType.EMPTY) if(tag.Key != tagType.EMPTY)
this.tags.Add(tag.Key, tag.Value); tags.Add(tag.Key, tag.Value);
} }
public static KeyValuePair<tagType, object> ConvertToTag(string key, string value) public static KeyValuePair<tagType, object> ConvertToTag(string key, string value)
@ -165,32 +165,34 @@ public class OsmEdge
public ulong GetId() public ulong GetId()
{ {
return this.tags.ContainsKey(tagType.id) ? (ulong)this.tags[tagType.id] : 0; return tags.ContainsKey(tagType.id) ? (ulong)tags[tagType.id] : 0;
} }
public wayType GetHighwayType() public wayType GetHighwayType()
{ {
return this.tags.ContainsKey(tagType.highway) ? (wayType)this.tags[tagType.highway] : wayType.NONE; if (!tags.ContainsKey(tagType.highway))
throw new Exception("Not a road?");
return (wayType)tags[tagType.highway];
} }
public bool IsOneWay() public bool IsOneWay()
{ {
return this.tags.ContainsKey(tagType.oneway) && (bool)this.tags[tagType.oneway]; return tags.ContainsKey(tagType.oneway) && (bool)tags[tagType.oneway];
} }
public byte? GetMaxSpeed(speedType type) public byte GetMaxSpeed(speedType type)
{ {
if(type == speedType.road) switch (type)
{ {
return this.tags.ContainsKey(tagType.maxspeed) ? (byte)this.tags[tagType.maxspeed] : null; case speedType.road:
return tags.ContainsKey(tagType.maxspeed) ? (byte)tags[tagType.maxspeed] : (byte)0;
case speedType.car:
return tags.ContainsKey(tagType.maxspeed) ? (byte)tags[tagType.maxspeed] : defaultSpeedCar[GetHighwayType()];
case speedType.pedestrian:
return defaultSpeedPedestrian[GetHighwayType()];
default:
return 0;
} }
if(type == speedType.car)
{
return this.tags.ContainsKey(tagType.maxspeed)
? (byte)this.tags[tagType.maxspeed]
: speedcar[this.GetHighwayType()];
}
return speedped[this.GetHighwayType()];
} }
public bool IsForward() public bool IsForward()
@ -201,11 +203,11 @@ public class OsmEdge
public double GetWeight(OsmNode parentNode, speedType vehicle) public double GetWeight(OsmNode parentNode, speedType vehicle)
{ {
double distance = Utils.DistanceBetween(parentNode, neighborCoordinates); double distance = Utils.DistanceBetween(parentNode, neighborCoordinates);
byte? speedByte = GetMaxSpeed(vehicle); byte speedByte = GetMaxSpeed(vehicle);
if (speedByte != null && speedByte > 0) if (speedByte > 0)
{ {
double speed = Convert.ToDouble(speedByte); double speed = Convert.ToDouble(speedByte);
return distance / (speed * 1000 / 360); return distance / speed;
} }
else else
{ {

View File

@ -6,7 +6,7 @@ namespace Pathfinding;
public class Pathfinder public class Pathfinder
{ {
public static List<OsmNode> CustomAStar(string workingDir, Coordinates start, Coordinates goal) public static List<OsmNode> CustomAStar(string workingDir, Coordinates start, Coordinates goal, OsmEdge.speedType vehicle)
{ {
RegionManager regionManager = new RegionManager(workingDir); RegionManager regionManager = new RegionManager(workingDir);
Region startRegion, goalRegion; Region startRegion, goalRegion;
@ -32,6 +32,7 @@ public class Pathfinder
while (toVisit.Count > 0 && !stop) while (toVisit.Count > 0 && !stop)
{ {
Console.WriteLine("toVisit-length: {0}", toVisit.Count);
closestNodeToGoal = toVisit.First(); closestNodeToGoal = toVisit.First();
foreach (OsmNode node in toVisit) foreach (OsmNode node in toVisit)
{ {
@ -48,16 +49,20 @@ public class Pathfinder
foreach (OsmEdge edge in closestNodeToGoal.edges) foreach (OsmEdge edge in closestNodeToGoal.edges)
{ {
OsmNode? neighbor = regionManager.GetNode(edge.neighborCoordinates); OsmNode? neighbor = regionManager.GetNode(edge.neighborCoordinates);
if (neighbor != null && neighbor.currentPathWeight > closestNodeToGoal.currentPathWeight + Utils.DistanceBetween(closestNodeToGoal, neighbor)) if (neighbor != null)
{ {
neighbor.previousPathNode = closestNodeToGoal; double newPotentialWeight =
neighbor.currentPathWeight = closestNodeToGoal.currentPathWeight + closestNodeToGoal.currentPathWeight + edge.GetWeight(closestNodeToGoal, vehicle);
Utils.DistanceBetween(closestNodeToGoal, neighbor); if (neighbor.currentPathWeight > newPotentialWeight)
{
neighbor.previousPathNode = closestNodeToGoal;
neighbor.currentPathWeight = newPotentialWeight;
if (neighbor.Equals(goalNode)) if (neighbor.Equals(goalNode))
stop = true; stop = true;
else else
toVisit.Add(neighbor); toVisit.Add(neighbor);
}
} }
} }
@ -72,7 +77,8 @@ public class Pathfinder
currentNode = currentNode.previousPathNode; currentNode = currentNode.previousPathNode;
} }
path.Add(startNode); path.Add(startNode);
path.Reverse();
return path; return path;
} }