From 2a1c7c8c5e78840279ee84bd36de52ef73c0f80c Mon Sep 17 00:00:00 2001 From: Brychan Dempsey Date: Thu, 21 Oct 2021 22:16:53 +1300 Subject: [PATCH] Fixed a potential issue with heuristics calculations --- ShortestTotalPath/Program.cs | 108 ++++++++++++++++++++--------------- 1 file changed, 62 insertions(+), 46 deletions(-) diff --git a/ShortestTotalPath/Program.cs b/ShortestTotalPath/Program.cs index 97ce6cd..25906f7 100644 --- a/ShortestTotalPath/Program.cs +++ b/ShortestTotalPath/Program.cs @@ -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 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 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