예제 #1
0
    void Update()
    {
        // Dijkstra
        if (Input.GetKeyDown(KeyCode.Space) && currentAlgorithm == "bFSearch")
        {
            currentAlgorithm = null;

            control.snapshotStatus.text = "Running Breadth-First-Search pathfinding algorithm...";
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            List <Node> path = bFSearch.FindPath(startX, startY, startZ, goalX, goalY, goalZ);
            stopwatch.Stop();
            control.snapshotStatus.text = "Press ENTER to move to next algorithm\nPathfinding complete!";

            long timeToComplete     = stopwatch.ElapsedMilliseconds;
            int  pathedNodes        = path.Count;
            int  openListSize       = bFSearch.openList.Count;
            int  closedListSize     = bFSearch.closedList.Count;
            int  totalNodesSearched = openListSize + closedListSize;
            int  totalMoveCost      = path[path.Count - 1].g;

            UnityEngine.Debug.Log($"Time taken to complete: {timeToComplete}ms");
            UnityEngine.Debug.Log($"Number of pathed nodes: {pathedNodes}");
            UnityEngine.Debug.Log($"Total nodes searched: {totalNodesSearched}");
            UnityEngine.Debug.Log($"Open list size: {openListSize}");
            UnityEngine.Debug.Log($"Closed list size: {closedListSize}");
            UnityEngine.Debug.Log($"Total move cost of path (g): {totalMoveCost}");

            control.consoleText.text = "Breadth-First-Search results\n\n"
                                       + "Time to complete: " + timeToComplete + "ms\n"
                                       + "Number of pathed nodes: " + pathedNodes + "\n"
                                       + "Total nodes searched: " + totalNodesSearched + "\n"
                                       + "Open list size: " + openListSize + "\n"
                                       + "Closed list size: " + closedListSize + "\n"
                                       + "Total move cost of path (g): " + totalMoveCost;

            // Write test results to file
            writer.WriteLine($"BFSearch, {timeToComplete}, {pathedNodes}, {totalNodesSearched}, {openListSize}, {closedListSize}, {totalMoveCost}");
            writer.Close();

            UnityEngine.Debug.Log($"Results written to: {pathString}");

            currentAlgorithm = "bFSearchDone";
        }

        if (Input.GetKeyDown(KeyCode.Return) && currentAlgorithm == "bFSearchDone")
        {
            currentAlgorithm = null;

            SceneManager.LoadScene("Level 1 Dijkstra");
        }
    }
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            control.snapshotStatus.text = "Running Best-First-Search pathfinding algorithm...";
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            List <Node> path = bFSearch.FindPath(startX, startY, startZ, goalX, goalY, goalZ);
            stopwatch.Stop();
            control.snapshotStatus.text = "Pathfinding complete!";

            long timeToComplete     = stopwatch.ElapsedMilliseconds;
            int  pathedNodes        = path.Count;
            int  openListSize       = bFSearch.openList.Count;
            int  closedListSize     = bFSearch.closedList.Count;
            int  totalNodesSearched = openListSize + closedListSize;
            int  totalMoveCost      = path[path.Count - 1].g;

            UnityEngine.Debug.Log($"Time taken to complete: {timeToComplete}ms");
            UnityEngine.Debug.Log($"Number of pathed nodes: {pathedNodes}");
            UnityEngine.Debug.Log($"Total nodes searched: {totalNodesSearched}");
            UnityEngine.Debug.Log($"Open list size: {openListSize}");
            UnityEngine.Debug.Log($"Closed list size: {closedListSize}");
            UnityEngine.Debug.Log($"Total move cost of path (g): {totalMoveCost}");

            control.consoleText.text = "RESULTS\n\n"
                                       + "Time to complete: " + timeToComplete + "ms\n"
                                       + "Number of pathed nodes: " + pathedNodes + "\n"
                                       + "Total nodes searched: " + totalNodesSearched + "\n"
                                       + "Open list size: " + openListSize + "\n"
                                       + "Closed list size: " + closedListSize + "\n"
                                       + "Total move cost of path (g): " + totalMoveCost;

            // Create file at "Results/path_name/DDMMYYYY"
            string       scene      = SceneManager.GetActiveScene().name;
            string       time       = System.DateTimeOffset.Now.ToString("yyyyMMdd");
            string       pathString = $"Results/{scene}/{time}.csv";
            StreamWriter writer     = new StreamWriter(pathString, true);

            // Write test results to file
            writer.WriteLine("'algorithm', 'Time to complete (ms)', 'Number of pathed nodes', 'Total nodes searched', 'Open list size', 'Closed list size', 'Total move cost (g)'");
            writer.WriteLine($"BFSearch, {timeToComplete}, {pathedNodes}, {totalNodesSearched}, {openListSize}, {closedListSize}, {totalMoveCost}");
            writer.Close();
            UnityEngine.Debug.Log($"Results written to: {pathString}");
        }
    }