Fixed a potential issue with heuristics calculations

This commit is contained in:
Brychan Dempsey 2021-10-21 22:16:53 +13:00
parent d40ebdabd2
commit 2a1c7c8c5e

View File

@ -7,13 +7,17 @@ namespace ShortestTotalPath
internal class Program internal class Program
{ {
static void Main(string[] args) static void Main(string[] args)
{
// Take an approximate start time
for (int k = 0; k < 10; k++)
{ {
Graph g = new(); Graph g = new();
const int V = 10; const int V = 10;
List<Node> nodes = new(); List<Node> nodes = new();
// Use random numbers, but with a controlled seed (tests are repeatable) // Use random numbers, but with a controlled seed (tests are repeatable)
Random rand = new(2); Random rand = new();
// double[] doubles = new double[] { 1.54, 1.37, 3.78, 8.54, 4.656, 7.334, 6.4643, 3.342, 3.456, 4.567 };
// Create the nodes // Create the nodes
for (int i = 0; i < V; i++) for (int i = 0; i < V; i++)
{ {
@ -34,17 +38,17 @@ namespace ShortestTotalPath
} }
else else
{ {
nodes[i].Children.Add(nodes[j], 0.5 + rand.NextDouble() * 10); nodes[i].Children.Add(nodes[j], 0.5 + rand.NextDouble() * 10.0);
} }
} }
} }
} }
// Take an approximate start time
DateTime start = DateTime.Now; DateTime start = DateTime.Now;
Algorithm a = new Algorithm(); Algorithm a = new Algorithm();
Node n = a.Run(nodes[0], g); Node n = a.Run(nodes[0], g);
DateTime end = DateTime.Now; DateTime end = DateTime.Now;
Console.WriteLine((end - start).TotalSeconds.ToString("N3")); Console.WriteLine((end - start).TotalSeconds.ToString("N3") + " seconds");
Console.WriteLine(n.TotalTraversedLength);
Console.Write("Path: "); Console.Write("Path: ");
for (int i = 0; i < n.TraversedNodes.Count; i++) for (int i = 0; i < n.TraversedNodes.Count; i++)
{ {
@ -58,6 +62,8 @@ namespace ShortestTotalPath
Console.Write(" -> "); Console.Write(" -> ");
} }
} }
}
Console.ReadLine(); Console.ReadLine();
} }
@ -92,7 +98,7 @@ namespace ShortestTotalPath
{ {
// See if the node's traversed nodes contains all nodes in the graph // See if the node's traversed nodes contains all nodes in the graph
bool trueForAll = true; bool trueForAll = true;
foreach (var item in g.Nodes) foreach (Node item in g.Nodes)
{ {
if (!poppedNode.TraversedNodes.Contains(item)) if (!poppedNode.TraversedNodes.Contains(item))
{ {
@ -151,14 +157,24 @@ namespace ShortestTotalPath
// Another approach is to run simultaneous comparisons on chunks of data, i.e. break the queue into two-or-more // Another approach is to run simultaneous comparisons on chunks of data, i.e. break the queue into two-or-more
// sections and find the lowest of each section, then grab the overall lowest, but this requires additional // sections and find the lowest of each section, then grab the overall lowest, but this requires additional
// time to establish the required threads. For graphs > rank of 10, that may be a better approach // time to establish the required threads. For graphs > rank of 10, that may be a better approach
if (queue.Keys.Count == 1)
{
foreach (Node n in queue.Keys)
{
shortest = n;
}
}
else
{
foreach (Node n in queue.Keys) foreach (Node n in queue.Keys)
{ {
// Compare Length plus heuristic to the current shortest length // Compare Length plus heuristic to the current shortest length
if (shortest is null || n.TotalTraversedLength + n.LocalHeuristic < length) if (n.TotalTraversedLength + n.LocalHeuristic < length)
{ {
// new shortest found, replace the current shortest values // new shortest found, replace the current shortest values
shortest = n; shortest = n;
length = n.TotalTraversedLength; length = n.TotalTraversedLength + n.LocalHeuristic;
}
} }
} }
// Remove the node from the queue // Remove the node from the queue