Fixed a potential issue with heuristics calculations
This commit is contained in:
parent
d40ebdabd2
commit
2a1c7c8c5e
@ -7,13 +7,17 @@ namespace ShortestTotalPath
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
// 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);
|
||||
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++)
|
||||
{
|
||||
@ -34,17 +38,17 @@ namespace ShortestTotalPath
|
||||
}
|
||||
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;
|
||||
Algorithm a = new Algorithm();
|
||||
Node n = a.Run(nodes[0], g);
|
||||
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: ");
|
||||
for (int i = 0; i < n.TraversedNodes.Count; i++)
|
||||
{
|
||||
@ -58,6 +62,8 @@ namespace ShortestTotalPath
|
||||
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
|
||||
if (queue.Keys.Count == 1)
|
||||
{
|
||||
foreach (Node n in queue.Keys)
|
||||
{
|
||||
shortest = n;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (Node n in queue.Keys)
|
||||
{
|
||||
// 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
|
||||
shortest = n;
|
||||
length = n.TotalTraversedLength;
|
||||
length = n.TotalTraversedLength + n.LocalHeuristic;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Remove the node from the queue
|
||||
|
Loading…
x
Reference in New Issue
Block a user