예제 #1
0
    private void benchmark(object parameter)
    {
        var benchmarkParameter = (BenchmarkParameter)parameter;
        var grid = benchmarkParameter.grid;

        float astarTime = 0;
        float jpsTime   = 0;

        var stopwatch = new System.Diagnostics.Stopwatch();
        var astar     = new AStarSearch();

        astar.showDebug = false;
        var jps = new JumpPointSearch();

        jps.showDebug = false;
        var r    = new System.Random(100);
        var size = benchmarkParameter.grid.GetSize();

        for (var i = 0; i < benchmarkParameter.iterations; i++)
        {
            Vector3Int start;
            Vector3Int end;
            do
            {
                start = new Vector3Int(r.Next(0, size.x), r.Next(0, size.y), 0);
                end   = new Vector3Int(r.Next(0, size.x), r.Next(0, size.y), 0);
            } while (!grid.IsWalkable(start.x, start.y) || !grid.IsWalkable(end.x, end.y));

            // Run A* Search
            stopwatch.Restart();
            if (astar.GetPath(grid, new Vector2Int(start.x, start.y), new Vector2Int(end.x, end.y)) == null)
            {
                Debug.Log("Astar didn't find path");
            }
            astarTime += stopwatch.ElapsedMilliseconds;

            // Run Jump Point Search
            stopwatch.Restart();
            if (jps.GetPath(grid, new Vector2Int(start.x, start.y), new Vector2Int(end.x, end.y)) == null)
            {
                Debug.Log("JPS didn't find path");
            }
            jpsTime += stopwatch.ElapsedMilliseconds;

            if (i % 100 == 0)
            {
                Debug.Log("Processed " + i + " iterations");
            }
        }

        Debug.Log("Benchmark result (" + benchmarkParameter.iterations + " iterations): AStar took " + astarTime + "ms and JPS took " + jpsTime + "ms");
    }
예제 #2
0
    // Update is called once per frame
    void Update()
    {
        if (Input.inputString == "f")
        {
            Tile tempStart = nwg.GetTileAtIndex((int)startSearch.x, (int)startSearch.y);
            Tile tempEnd   = nwg.GetTileAtIndex((int)endSearch.x, (int)endSearch.y);

            JumpPointSearch.aStarSearch(tempStart, tempEnd);
        }
        //movement();
        debugRays();
        //randomRotation();
        drawPath();
    }
예제 #3
0
    public List <Vector2> SetNewPath(Vector3 newDest)
    {
        List <Vector2> p;
        Vector3        newDestCenterPos = CenterPosition(newDest);
        Vector3        currCenterPos    = CenterPosition(transform.position);

        currDestCenterPosition = newDestCenterPos;

        ClearPathMarker();
        objPosPair = new Dictionary <Vector2, GameObject>();

        p = JumpPointSearch.SearchPath(currCenterPos, newDestCenterPos);
        DrawPath(p);
        return(p);
    }
예제 #4
0
    void Start()
    {
        Vector3 sourceCenterPosition = CenterPosition(transform.position);

        currDestCenterPosition = CenterPosition(bush.transform.position);

        if (path == null || path.Count == 0)
        {
            path = JumpPointSearch.SearchPath(sourceCenterPosition, currDestCenterPosition);

            DrawPath(path);
        }

        lastDestCenterPosition = currDestCenterPosition;
    }
예제 #5
0
    public void FindPath(bool astar)
    {
        DebugDrawer.Clear();

        // Convert visible grid to memory grid
        var grid     = GridController.Ground;
        var pathGrid = GridController.Path;

        var sizeX = GridController.active.size.x;
        var sizeY = GridController.active.size.y;

        var memoryGrid = createGraph();
        // Set grid weights
        var start = new Vector2Int(-1, -1);
        var end   = new Vector2Int(-1, -1);

        for (var x = 0; x < sizeX; x++)
        {
            for (var y = 0; y < sizeY; y++)
            {
                var pathTile = pathGrid.GetTile(new Vector3Int(x, y, 0));
                if (pathTile != null)
                {
                    if (pathTile == GridController.active.start)
                    {
                        start = new Vector2Int(x, y);
                    }
                    else if (pathTile == GridController.active.end)
                    {
                        end = new Vector2Int(x, y);
                    }
                }
            }
        }

        if (start.x == -1 || end.x == -1)
        {
            Debug.Log("Couldn't find any start or end position");
            return;
        }

        List <Node> path;

        System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
        if (astar)
        {
            var asearch = new AStarSearch();
            sw.Start();
            path = asearch.GetPath(memoryGrid, start, end);
            UnityEngine.Debug.Log("A* Path - Path" + (path == null ? " not " : " ") + "found in : " + sw.ElapsedMilliseconds + " ms");
        }
        else
        {
            var jpsearch = new JumpPointSearch();
            sw.Start();
            path = jpsearch.GetPath(memoryGrid, start, end);
            UnityEngine.Debug.Log("JPS Path - Path" + (path == null ? " not " : " ") + "found in : " + sw.ElapsedMilliseconds + " ms");
        }

        if (path != null)
        {
            foreach (var pathTile in path)
            {
                if (pathTile.x == start.x && pathTile.y == start.y)
                {
                    continue;
                }
                if (pathTile.x == end.x && pathTile.y == end.y)
                {
                    continue;
                }

                if (pathTile.parent != null)
                {
                    DebugDrawer.Draw(new Vector2Int(pathTile.parent.x, pathTile.parent.y), new Vector2Int(pathTile.x, pathTile.y), Color.blue);
                }
                DebugDrawer.DrawCube(new Vector2Int(pathTile.x, pathTile.y), Vector2Int.one, Color.blue);
            }
        }
    }
 public JumpPointSearchOnTheCommand(JumpPointSearch search, Node start, Node end)
 {
     Start       = start;
     End         = end;
     MapAnalizer = search;
 }
예제 #7
0
    private BaseSearchAlgo GetAlgorithm()
    {
        BaseSearchAlgo algo = null;

        switch (m_searchAlgo)
        {
        case SearchAlgo.A_Star:
            algo = new AStar(m_startNode, m_goalNode, m_nodes, m_weight, m_showTime);
            break;

        case SearchAlgo.Theta_Star:
            algo = new ThetaStar(m_startNode, m_goalNode, m_nodes, m_weight, m_showTime);
            break;

        case SearchAlgo.LazyTheta_Star:
            algo = new LazyThetaStar(m_startNode, m_goalNode, m_nodes, m_weight, m_showTime);
            break;

        case SearchAlgo.BestFirstSearch:
            algo = new BestFirstSearch(m_startNode, m_goalNode, m_nodes, m_weight, m_showTime);
            break;

        case SearchAlgo.BreadthFirstSearch:
            algo = new BreadthFirstSearch(m_startNode, m_goalNode, m_nodes, m_weight, m_showTime);
            break;

        case SearchAlgo.DijkstraSearch:
            algo = new DijkstraSearch(m_startNode, m_goalNode, m_nodes, m_weight, m_showTime);
            break;

        case SearchAlgo.JPS:
            algo = new JumpPointSearch(m_startNode, m_goalNode, m_nodes, m_weight, m_showTime);
            break;

        case SearchAlgo.JPSPlus:
            algo = new JPSPlus(m_startNode, m_goalNode, m_nodes, m_weight, m_showTime);
            break;

        case SearchAlgo.BiA_Star:
            algo = new BiAStar(m_startNode, m_goalNode, m_nodes, m_weight, m_showTime);
            break;

            #region Incremental
        case SearchAlgo.D_Star:
            algo = new DStar(m_startNode, m_goalNode, m_nodes, m_showTime);
            break;

        case SearchAlgo.FocussedD_Star:
            algo = new FocussedDStar(m_startNode, m_goalNode, m_nodes, m_showTime);
            break;

        case SearchAlgo.LPA_Star:
            algo = new LPAStar(m_startNode, m_goalNode, m_nodes, m_showTime);
            //algo = new LPAStar_Optimized(m_startNode, m_endNode, m_nodes, m_showTime);
            break;

        case SearchAlgo.DstarLite:
            algo = new DStarLite(m_startNode, m_goalNode, m_nodes, m_showTime);
            break;

        case SearchAlgo.Path_AA_Star:
            algo = new Path_AAStar(m_startNode, m_goalNode, m_nodes, m_showTime);
            break;

        case SearchAlgo.Tree_AA_Star:
            algo = new Tree_AAStar(m_startNode, m_goalNode, m_nodes, m_showTime);
            break;
            #endregion

            #region Moving Target
        case SearchAlgo.GAA_Star:
            algo = new GAAStar(m_startNode, m_goalNode, m_nodes, m_showTime);
            break;

        case SearchAlgo.GFRA_Star:
            algo = new GFRAStar(m_startNode, m_goalNode, m_nodes, m_showTime);
            break;

        case SearchAlgo.MT_DstarLite:
            algo = new MT_DStarLite(m_startNode, m_goalNode, m_nodes, m_showTime);
            break;
            #endregion

        case SearchAlgo.AnnotatedA_Star:
            algo = new AnnotatedAStar(m_startNode, m_goalNode, m_nodes, m_weight, m_showTime, m_unitSize);
            break;

        default:
            Debug.LogError($"No code for SearchAlgo={m_searchAlgo}");
            break;
        }

        return(algo);
    }