//Writing routes for the dataModel public static void Read_Data_Routes(string routes, DataModel dataModel) { var documentRoutes = new StreamReader(new FileStream(routes, FileMode.Open, FileAccess.Read)); string line; line = documentRoutes.ReadLine(); while ((line = documentRoutes.ReadLine()) != null) { string[] parametersRoute = line.Split(';'); DateTime start = DateTime.Today + new TimeSpan(int.Parse(parametersRoute[0]), int.Parse(parametersRoute[1]), 1); DateTime end = DateTime.Today + new TimeSpan(int.Parse(parametersRoute[2]), int.Parse(parametersRoute[3]), 1); int delay = int.Parse(parametersRoute[4]); int i = 5; while (i + 1 < parametersRoute.Length && parametersRoute[i + 1] != "") { Link link = DataModel.GetLink(int.Parse(parametersRoute[i]), int.Parse(parametersRoute[i + 1]), dataModel.links); DateTime tempTime = start; TimeSpan timeDelay = new TimeSpan(0, delay, 0); while (tempTime <= end) { try { link.times.Add(tempTime); link.times.Add(tempTime + new TimeSpan(1, 0, 0, 0)); link.times.Add(tempTime + new TimeSpan(2, 0, 0, 0)); tempTime = tempTime.Add(timeDelay); } catch { break; } } try { link.times = link.times.OrderBy(x => x.Day).ToList(); } catch {} i++; } } documentRoutes.Close(); }
/*DijkstraSearch searches for the fastest route through the network by arranging the nodes and links from fastest to slowest. * If DijkstaSearch finds the end, it will stop searching and the fastest route is found*/ private static void DijkstraSearch(Node start, Node end, DateTime time, DataModel dataModel) { start.minCostToStart = time; var prioQueue = new List <Node> { start }; while (prioQueue.Any()) { prioQueue = prioQueue.OrderBy(x => x.minCostToStart).ToList(); var node = prioQueue.First(); prioQueue.Remove(node); foreach (var link in node.connections.OrderBy(x => Link.GetDepartTime(DataModel.GetLink(x.start.number, x.end.number, dataModel.links), node.minCostToStart) + x.weight)) { DateTime tempCost = Link.GetDepartTime(DataModel.GetLink(link.start.number, link.end.number, dataModel.links), node.minCostToStart) + link.weight; var childNode = link.end; if (childNode.visited) { continue; } if (tempCost < childNode.minCostToStart) { childNode.minCostToStart = tempCost; childNode.nearestToStart = node; if (!prioQueue.Contains(childNode)) { prioQueue.Add(childNode); } } } node.visited = true; if (node == end) { return; } } }