//------------------------------------------------------------------------------------------------------------------------------------------ public void Dispose() { uniqueAirports.Clear(); openList.Clear(); targetNode = null; }
//------------------------------------------------------------------------------------------------------------------------------------------ private static int CompareNodes(AStarNode a, AStarNode b) { int diff = b.totalCost - a.totalCost; return(diff); }
//-------------------------------------------------------------------------------------------------------------------------------- private int EstimateCost(AStarNode node, IList <Route> routes) { return(CountOpenRoutes(node, routes) * CostScale); }
//-------------------------------------------------------------------------------------------------------------------------------- private bool IsPlanValid(AStarNode node) { return(node.loaded == allRoutesFlag && node.unloaded == allRoutesFlag); }
//-------------------------------------------------------------------------------------------------------------------------------- private int CalculateCost(AStarNode node) { Debug.Assert(node.parent != null); return(node.parent.costFromStart + node.parent.airportData.DistanceTo(node.airportData)); }
//-------------------------------------------------------------------------------------------------------------------------------- private void Search() { workerThreads = new Thread[uniqueAirports.Count - 1]; threadData = new ThreadData[workerThreads.Length]; lockThreadData = new object[workerThreads.Length]; for (int i = 0; i < workerThreads.Length; ++i) { workerThreads[i] = new Thread(EvaluateAirportThread); threadData[i] = new ThreadData(); lockThreadData[i] = new object(); workerThreads[i].Start(threadData[i]); } while (ContinueSearch) { AStarNode node = PopFromOpenList(); if (node == null) { break; } RoutesComplete = 100.0f * CountCompleteRoutes(node) / routes.Count; if (Verbose) { Debug.WriteLine(node.ToString(routes.Count)); } if (IsPlanValid(node)) { targetNode = node; break; } int index = 0; foreach (var kv in uniqueAirports) { AirportData airportData = kv.Value; if (node.airportData == airportData) { continue; } lock (lockThreadData[index]) { threadData[index].airportData = airportData; threadData[index].node = node; } ++index; } bool isDone = false; while (!isDone) { isDone = true; for (int i = 0; i < threadData.Length; ++i) { if (!threadData[i].isDone) { isDone = false; break; } } if (!isDone) { Thread.Sleep(0); } } openList.Sort(CompareNodes); for (int i = 0; i < threadData.Length; ++i) { lock (lockThreadData[i]) { threadData[i].airportData = null; threadData[i].node = null; threadData[i].isDone = false; } } Thread.Sleep(0); } ContinueSearch = false; if (Verbose) { Debug.WriteLine("------------------------------------------------------------"); } }