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

@ -8,56 +8,62 @@ namespace ShortestTotalPath
{
static void Main(string[] args)
{
Graph g = new();
// Take an approximate start time
for (int k = 0; k < 10; k++)
{
Graph g = new();
const int V = 10;
List<Node> nodes = new();
// Use random numbers, but with a controlled seed (tests are repeatable)
Random rand = new(2);
// Create the nodes
for (int i = 0; i < V; i++)
{
Node newNode = new(i.ToString());
g.Nodes.Add(newNode);
nodes.Add(newNode);
}
// Create the links between nodes
for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
const int V = 10;
List<Node> nodes = new();
// Use random numbers, but with a controlled seed (tests are repeatable)
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
for (int i = 0; i < V; i++)
{
if (i != j)
Node newNode = new(i.ToString());
g.Nodes.Add(newNode);
nodes.Add(newNode);
}
// Create the links between nodes
for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
{
if (nodes[j].Children.TryGetValue(nodes[i], out double value))
if (i != j)
{
nodes[i].Children.Add(nodes[j], value);
}
else
{
nodes[i].Children.Add(nodes[j], 0.5 + rand.NextDouble() * 10);
if (nodes[j].Children.TryGetValue(nodes[i], out double value))
{
nodes[i].Children.Add(nodes[j], value);
}
else
{
nodes[i].Children.Add(nodes[j], 0.5 + rand.NextDouble() * 10.0);
}
}
}
}
}
// Take an approximate start time
DateTime start = DateTime.Now;
Algorithm a = new Algorithm();
Node n = a.Run(nodes[0], g);
DateTime end = DateTime.Now;
Console.WriteLine((end - start).TotalSeconds.ToString("N3"));
Console.Write("Path: ");
for (int i = 0; i < n.TraversedNodes.Count; i++)
{
Console.Write(n.TraversedNodes[i].Name);
if (i == n.TraversedNodes.Count - 1)
DateTime start = DateTime.Now;
Algorithm a = new Algorithm();
Node n = a.Run(nodes[0], g);
DateTime end = DateTime.Now;
Console.WriteLine((end - start).TotalSeconds.ToString("N3") + " seconds");
Console.WriteLine(n.TotalTraversedLength);
Console.Write("Path: ");
for (int i = 0; i < n.TraversedNodes.Count; i++)
{
Console.WriteLine();
}
else
{
Console.Write(" -> ");
Console.Write(n.TraversedNodes[i].Name);
if (i == n.TraversedNodes.Count - 1)
{
Console.WriteLine();
}
else
{
Console.Write(" -> ");
}
}
}
Console.ReadLine();
}
@ -92,7 +98,7 @@ namespace ShortestTotalPath
{
// See if the node's traversed nodes contains all nodes in the graph
bool trueForAll = true;
foreach (var item in g.Nodes)
foreach (Node item in g.Nodes)
{
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
// 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
foreach (Node n in queue.Keys)
if (queue.Keys.Count == 1)
{
// Compare Length plus heuristic to the current shortest length
if (shortest is null || n.TotalTraversedLength + n.LocalHeuristic < length)
foreach (Node n in queue.Keys)
{
// new shortest found, replace the current shortest values
shortest = n;
length = n.TotalTraversedLength;
}
}
else
{
foreach (Node n in queue.Keys)
{
// Compare Length plus heuristic to the current shortest length
if (n.TotalTraversedLength + n.LocalHeuristic < length)
{
// new shortest found, replace the current shortest values
shortest = n;
length = n.TotalTraversedLength + n.LocalHeuristic;
}
}
}
// Remove the node from the queue