Fixed a potential issue with heuristics calculations
This commit is contained in:
parent
d40ebdabd2
commit
2a1c7c8c5e
@ -8,56 +8,62 @@ namespace ShortestTotalPath
|
|||||||
{
|
{
|
||||||
static void Main(string[] args)
|
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;
|
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();
|
||||||
// Create the nodes
|
// double[] doubles = new double[] { 1.54, 1.37, 3.78, 8.54, 4.656, 7.334, 6.4643, 3.342, 3.456, 4.567 };
|
||||||
for (int i = 0; i < V; i++)
|
// 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++)
|
|
||||||
{
|
{
|
||||||
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);
|
if (nodes[j].Children.TryGetValue(nodes[i], out double value))
|
||||||
}
|
{
|
||||||
else
|
nodes[i].Children.Add(nodes[j], value);
|
||||||
{
|
}
|
||||||
nodes[i].Children.Add(nodes[j], 0.5 + rand.NextDouble() * 10);
|
else
|
||||||
|
{
|
||||||
|
nodes[i].Children.Add(nodes[j], 0.5 + rand.NextDouble() * 10.0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
DateTime start = DateTime.Now;
|
||||||
// Take an approximate start time
|
Algorithm a = new Algorithm();
|
||||||
DateTime start = DateTime.Now;
|
Node n = a.Run(nodes[0], g);
|
||||||
Algorithm a = new Algorithm();
|
DateTime end = DateTime.Now;
|
||||||
Node n = a.Run(nodes[0], g);
|
Console.WriteLine((end - start).TotalSeconds.ToString("N3") + " seconds");
|
||||||
DateTime end = DateTime.Now;
|
Console.WriteLine(n.TotalTraversedLength);
|
||||||
Console.WriteLine((end - start).TotalSeconds.ToString("N3"));
|
Console.Write("Path: ");
|
||||||
Console.Write("Path: ");
|
for (int i = 0; i < n.TraversedNodes.Count; i++)
|
||||||
for (int i = 0; i < n.TraversedNodes.Count; i++)
|
|
||||||
{
|
|
||||||
Console.Write(n.TraversedNodes[i].Name);
|
|
||||||
if (i == n.TraversedNodes.Count - 1)
|
|
||||||
{
|
{
|
||||||
Console.WriteLine();
|
Console.Write(n.TraversedNodes[i].Name);
|
||||||
}
|
if (i == n.TraversedNodes.Count - 1)
|
||||||
else
|
{
|
||||||
{
|
Console.WriteLine();
|
||||||
Console.Write(" -> ");
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
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
|
||||||
foreach (Node n in queue.Keys)
|
if (queue.Keys.Count == 1)
|
||||||
{
|
{
|
||||||
// Compare Length plus heuristic to the current shortest length
|
foreach (Node n in queue.Keys)
|
||||||
if (shortest is null || n.TotalTraversedLength + n.LocalHeuristic < length)
|
|
||||||
{
|
{
|
||||||
// new shortest found, replace the current shortest values
|
|
||||||
shortest = n;
|
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
|
// Remove the node from the queue
|
||||||
|
Loading…
x
Reference in New Issue
Block a user