Null pointer fixes
This commit is contained in:
parent
a4ebf05d7a
commit
6d338018b5
@ -6,9 +6,9 @@ namespace astar
|
|||||||
{
|
{
|
||||||
public class Astar
|
public class Astar
|
||||||
{
|
{
|
||||||
Dictionary<Node, float> timeRequired = new();
|
private Dictionary<Node, float> timeRequired = new();
|
||||||
Dictionary<Node, float> goalDistance = new();
|
private Dictionary<Node, float> goalDistance = new();
|
||||||
Dictionary<Node, Node> previousNode = new();
|
private Dictionary<Node, Node> previousNode = new();
|
||||||
|
|
||||||
public Route FindPath(Graph.Graph graph, Node start, Node goal, Logger? logger)
|
public Route FindPath(Graph.Graph graph, Node start, Node goal, Logger? logger)
|
||||||
{
|
{
|
||||||
@ -16,9 +16,9 @@ namespace astar
|
|||||||
List<Node> toVisit = new();
|
List<Node> toVisit = new();
|
||||||
toVisit.Add(start);
|
toVisit.Add(start);
|
||||||
Node currentNode = start;
|
Node currentNode = start;
|
||||||
timeRequired.Add(start, 0);
|
SetTimeRequiredToReach(start, 0);
|
||||||
goalDistance.Add(start, Convert.ToSingle(Utils.DistanceBetweenNodes(start, goal)));
|
SetDistanceToGoal(start, Convert.ToSingle(Utils.DistanceBetweenNodes(start, goal)));
|
||||||
while (toVisit.Count > 0 && timeRequired[toVisit[0]] < timeRequired[goal])
|
while (toVisit.Count > 0 && GetTimeRequiredToReach(toVisit[0]) < GetTimeRequiredToReach(goal))
|
||||||
{
|
{
|
||||||
if(currentNode == goal)
|
if(currentNode == goal)
|
||||||
{
|
{
|
||||||
@ -29,11 +29,11 @@ namespace astar
|
|||||||
//Check all neighbors of current node
|
//Check all neighbors of current node
|
||||||
foreach (Edge e in currentNode.edges)
|
foreach (Edge e in currentNode.edges)
|
||||||
{
|
{
|
||||||
if (timeRequired[e.neighbor] > timeRequired[currentNode] + e.time)
|
if (GetTimeRequiredToReach(e.neighbor) > GetTimeRequiredToReach(currentNode) + e.time)
|
||||||
{
|
{
|
||||||
goalDistance[e.neighbor] = Convert.ToSingle(Utils.DistanceBetweenNodes(e.neighbor, goal));
|
SetDistanceToGoal(e.neighbor, Convert.ToSingle(Utils.DistanceBetweenNodes(e.neighbor, goal)));
|
||||||
timeRequired[e.neighbor] = timeRequired[currentNode] + e.time;
|
SetTimeRequiredToReach(e.neighbor, GetTimeRequiredToReach(currentNode) + e.time);
|
||||||
previousNode[e.neighbor] = currentNode;
|
SetPreviousNodeOf(e.neighbor, currentNode);
|
||||||
toVisit.Add(e.neighbor);
|
toVisit.Add(e.neighbor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -42,7 +42,7 @@ namespace astar
|
|||||||
toVisit.Sort(CompareDistance);
|
toVisit.Sort(CompareDistance);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(previousNode[goal] != null)
|
if(GetPreviousNodeOf(goal) != null)
|
||||||
{
|
{
|
||||||
logger?.Log(LogLevel.INFO, "Way found, shortest option.");
|
logger?.Log(LogLevel.INFO, "Way found, shortest option.");
|
||||||
currentNode = goal;
|
currentNode = goal;
|
||||||
@ -57,10 +57,10 @@ namespace astar
|
|||||||
tempNodes.Add(goal);
|
tempNodes.Add(goal);
|
||||||
while(currentNode != start)
|
while(currentNode != start)
|
||||||
{
|
{
|
||||||
#pragma warning disable CS8604 // Route was found, so has to have a previous node
|
#pragma warning disable CS8604, CS8600 // Route was found, so has to have a previous node
|
||||||
tempNodes.Add(previousNode[currentNode]);
|
tempNodes.Add(GetPreviousNodeOf(currentNode));
|
||||||
#pragma warning restore CS8604
|
currentNode = GetPreviousNodeOf(currentNode);
|
||||||
currentNode = previousNode[currentNode];
|
#pragma warning restore CS8604, CS8600
|
||||||
}
|
}
|
||||||
tempNodes.Reverse();
|
tempNodes.Reverse();
|
||||||
|
|
||||||
@ -71,12 +71,12 @@ namespace astar
|
|||||||
{
|
{
|
||||||
#pragma warning disable CS8600, CS8604 // Route was found, so has to have an edge
|
#pragma warning disable CS8600, CS8604 // Route was found, so has to have an edge
|
||||||
Edge e = tempNodes[i].GetEdgeToNode(tempNodes[i + 1]);
|
Edge e = tempNodes[i].GetEdgeToNode(tempNodes[i + 1]);
|
||||||
steps.Add(new Step(tempNodes[i], e, timeRequired[tempNodes[i]], goalDistance[tempNodes[i]]));
|
steps.Add(new Step(tempNodes[i], e, GetTimeRequiredToReach(tempNodes[i]), GetDistanceToGoal(tempNodes[i])));
|
||||||
#pragma warning restore CS8600, CS8604
|
#pragma warning restore CS8600, CS8604
|
||||||
totalDistance += e.distance;
|
totalDistance += e.distance;
|
||||||
}
|
}
|
||||||
|
|
||||||
Route _route = new Route(steps, true, totalDistance, timeRequired[goal]);
|
Route _route = new Route(steps, true, totalDistance, GetTimeRequiredToReach(goal));
|
||||||
|
|
||||||
logger?.Log(LogLevel.INFO, "Path found");
|
logger?.Log(LogLevel.INFO, "Path found");
|
||||||
|
|
||||||
@ -111,9 +111,9 @@ namespace astar
|
|||||||
return 0;
|
return 0;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (goalDistance[n1] < goalDistance[n2])
|
if (GetDistanceToGoal(n1) < GetDistanceToGoal(n2))
|
||||||
return -1;
|
return -1;
|
||||||
else if (goalDistance[n1] > goalDistance[n2])
|
else if (GetDistanceToGoal(n1) > GetDistanceToGoal(n2))
|
||||||
return 1;
|
return 1;
|
||||||
else return 0;
|
else return 0;
|
||||||
}
|
}
|
||||||
@ -131,14 +131,66 @@ namespace astar
|
|||||||
return 0;
|
return 0;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (timeRequired[n1] < timeRequired[n2])
|
if (GetTimeRequiredToReach(n1) < GetTimeRequiredToReach(n2))
|
||||||
return -1;
|
return -1;
|
||||||
else if (timeRequired[n1] > timeRequired[n2])
|
else if (GetTimeRequiredToReach(n1) > GetTimeRequiredToReach(n2))
|
||||||
return 1;
|
return 1;
|
||||||
else return 0;
|
else return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private float GetTimeRequiredToReach(Node n)
|
||||||
|
{
|
||||||
|
if (timeRequired.TryGetValue(n, out float t))
|
||||||
|
{
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return float.MaxValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void SetTimeRequiredToReach(Node n, float t)
|
||||||
|
{
|
||||||
|
if (!timeRequired.TryAdd(n, t))
|
||||||
|
timeRequired[n] = t;
|
||||||
|
}
|
||||||
|
|
||||||
|
private float GetDistanceToGoal(Node n)
|
||||||
|
{
|
||||||
|
if (goalDistance.TryGetValue(n, out float t))
|
||||||
|
{
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return float.MaxValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void SetDistanceToGoal(Node n, float d)
|
||||||
|
{
|
||||||
|
if (!goalDistance.TryAdd(n, d))
|
||||||
|
goalDistance[n] = d;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Node? GetPreviousNodeOf(Node n)
|
||||||
|
{
|
||||||
|
if(previousNode.TryGetValue(n, out Node? t))
|
||||||
|
{
|
||||||
|
return t;
|
||||||
|
}else
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void SetPreviousNodeOf(Node n, Node p)
|
||||||
|
{
|
||||||
|
if (!previousNode.TryAdd(n, p))
|
||||||
|
previousNode[n] = p;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user