Ejemplo n.º 1
0
        /// <summary>
        ///小车移动函数,小车的横纵坐标每改变一次,窗体就执行重绘函数,就实现了小车的移动
        /// </summary>
        /// <param name="Elc"></param>
        public int SearchRoute(ElecMap Elc, int startX, int startY, int endX, int endY)//04022
        {
            AstarSearch astartSearch = new AstarSearch();

            Elc.mapnode[startX, startY].nodeCanUsed     = true; //小车自己所在的地方解除占用
            Elc.TempMapNode[startX, startY].nodeCanUsed = true; //小车自己所在的地方解除占用
            this.route = astartSearch.search(Elc, Elc.Width, Elc.Height, startX, startY, endX, endY);
            routeIndex = 0;
            if (this.route == null)
            {
                this.vehical_state = v_state.cannotToDestination;
            }
            for (int ii = 0; ii < this.route.Count; ii++)
            {
                this.cost = this.cost + 1;


                // Elc.mapnode[(int)this.route[ii].X, (int)this.route[ii].Y].vehiclePriority = new List<int>();
                // Elc.mapnode[(int)this.route[ii].Height, (int)this.route[ii].Width].vehiclePriority.Add(this.v_num);
            }
            //解除节点的占用
            for (int p = 0; p < Elc.heightNum; p++)
            {
                for (int q = 0; q < Elc.widthNum; q++)
                {
                    Elc.mapnode[p, q].nodeCanUsed     = true;
                    Elc.TempMapNode[p, q].nodeCanUsed = true;//小车自己所在的地方解除占用
                }
            }
            return(this.route.Count);
        }
Ejemplo n.º 2
0
    List <Vector3> RedTrace(Vector3 position)
    {
        AstarSearch    actions   = new AstarSearch(this, position);
        List <Vector3> getAction = actions.GetActions();

        return(getAction);
    }
        public static void DrawGride(SquareGrid grid, AstarSearch aStar)
        {
            for (int row = 0; row < grid.Rows; row++)
            {
                for (int column = 0; column < grid.Columns; column++)
                {
                    Location cell = new Location(column, row);

                    if (cell == aStar.Start)
                    {
                        Console.Write("S ");
                    }
                    else if (cell == aStar.Goal)
                    {
                        Console.Write("G ");
                    }
                    else if (grid.walls.Contains(cell))
                    {
                        Console.Write("# ");
                    }
                    else if (aStar.Path.Contains(cell))
                    {
                        Console.Write("* ");
                    }
                    else
                    {
                        Console.Write("  ");
                    }
                }
                Console.WriteLine();
            }
        }
Ejemplo n.º 4
0
 //size == dimension * dimension
 public RandomMaze(int dimension, int seed,float roadPercentage)
 {
     //atleast be half dimension apart
     startAndDestDist = dimension / 2;
     astar = new AstarSearch();
     this.gridNeedToFill = (int)(dimension * dimension * roadPercentage);
     this.dimension=dimension;
     this.roadPercentage = roadPercentage;
     this.seed = seed;
     random = new Random(seed);
     random.Next(Int32.MaxValue);
     maze = new float[dimension,dimension];
     astar.initailize2DArrayToValue(maze, WALL);
     setStartPointAndDestPoint(dimension);
 }
Ejemplo n.º 5
0
        public void TestSearchWithRoadMap()
        {
            var problem = new RoadMapProblem();
            var hOfn    = new StraightLineDistance();

            var testSubject = new AstarSearch(problem, hOfn);

            var testResult = testSubject.Search();

            Assert.AreEqual(418, testResult.PathCost);

            var destCity = testResult.State as City;

            Assert.IsNotNull(destCity);
            System.Diagnostics.Debug.WriteLine(testResult);
        }
Ejemplo n.º 6
0
 public void MazeTest()
 {
     AstarSearch astar = new AstarSearch();
     int dimension = 20;
     int dumbSmart = dimension / 10 * 2;
     int seed = 34534;
     List<Node> path = null;
     RandomMaze maze = new RandomMaze(dimension,seed,0.85f);
     path = maze.generateDumbPathToDestPoint(maze.destX,maze.destY,dumbSmart, dumbSmart);
     Console.WriteLine(" -- path count " + path.Count);
     maze.astar.printPath(path);
     maze.updateMazeRoad(path);
     maze.printMaze();
     Console.WriteLine("-----------------astar---------------------");
     astar.printPath(astar.FindPath(astar.Float2DtoInt(maze.maze),1,maze.startPoint.x,maze.startPoint.y,maze.destPoint.x,
         maze.destPoint.y));
 }
Ejemplo n.º 7
0
 public SearchManager()
 {
     Elc         = ElecMap.Instance;
     astarSearch = new AstarSearch();
 }
Ejemplo n.º 8
0
        public void TestMethod1()
        {
            AstarSearch astar = new AstarSearch();
            List<Node> path = null;
            int srcX=1;
            int srcY=4;
            int destX =4;
            int destY =4;
            int limit=1;
            int[,] world ={
                          {0,0,0,0,0},
                          {1,0,1,1,0},
                          {0,0,1,0,0},
                          {0,1,1,1,0},
                          {0,0,1,1,0}
                          };
            path = astar.FindPath(world,limit,srcX,srcY,destX,destY);

            astar.printPath(path);

            Assert.IsNotNull(path);
        }
        static void Main(string[] args)
        {
            /*
             * This is just a quick test of the implemetned A* in the Pathfinding project.
             * This test will create a 6 row x 10 column grid, as represented by the
             * drawing below. After creating the grid, we will use the AStarSearch class
             * to find the shorest path from the start to the goal.  Finally we'll draw
             * a representation of the grid with the shortest path to a console window.
             */


            /*
             *  The following is a representation of the grid that will be implemented
             *  S = Starting Point
             *  W = Wall
             *  G = Goal/Ending Point.
             *
             *         0     1     2     3     4     5     6     7     8     9
             *      |-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
             *      |     |     |     |     |     |     |     |     |     |     |
             *  0   |  S  |     |     |     |     |     |     |     |     |  W  |
             *      |     |     |     |     |     |     |     |     |     |     |
             *      |-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
             *      |     |     |     |     |     |     |     |     |     |     |
             *  1   |     |  W  |  W  |  W  |  W  |  W  |  W  |  W  |     |  W  |
             *      |     |     |     |     |     |     |     |     |     |     |
             *      |-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
             *      |     |     |     |     |     |     |     |     |     |     |
             *  2   |     |  W  |     |     |     |     |  W  |     |     |  W  |
             *      |     |     |     |     |     |     |     |     |     |     |
             *      |-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
             *      |     |     |     |     |     |     |     |     |     |     |
             *  3   |     |     |     |  W  |  W  |     |  W  |     |     |     |
             *      |     |     |     |     |     |     |     |     |     |     |
             *      |-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
             *      |     |     |     |     |     |     |     |     |     |     |
             *  4   |  W  |  W  |  W  |  W  |  W  |     |  W  |  W  |  W  |     |
             *      |     |     |     |     |     |     |     |     |     |     |
             *      |-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
             *      |     |     |     |     |     |     |     |     |     |     |
             *  5   |  W  |  W  |  W  |  W  |  W  |     |     |     |     |  G  |
             *      |     |     |     |     |     |     |     |     |     |     |
             *      |-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
             *
             */

            SquareGrid grid = new SquareGrid(6, 10);

            //  Row 1
            grid.walls.Add(new Location(10 - 1, 1 - 1));

            //  Row 2 walls
            grid.walls.Add(new Location(2 - 1, 2 - 1));
            grid.walls.Add(new Location(3 - 1, 2 - 1));
            grid.walls.Add(new Location(4 - 1, 2 - 1));
            grid.walls.Add(new Location(5 - 1, 2 - 1));
            grid.walls.Add(new Location(6 - 1, 2 - 1));
            grid.walls.Add(new Location(7 - 1, 2 - 1));
            grid.walls.Add(new Location(8 - 1, 2 - 1));
            grid.walls.Add(new Location(10 - 1, 2 - 1));

            //  Row 3 walls
            grid.walls.Add(new Location(2 - 1, 3 - 1));
            grid.walls.Add(new Location(7 - 1, 3 - 1));
            grid.walls.Add(new Location(10 - 1, 3 - 1));

            //  Row 4 walls
            grid.walls.Add(new Location(4 - 1, 4 - 1));
            grid.walls.Add(new Location(5 - 1, 4 - 1));
            grid.walls.Add(new Location(7 - 1, 4 - 1));

            //  Row 5 walls
            grid.walls.Add(new Location(1 - 1, 5 - 1));
            grid.walls.Add(new Location(2 - 1, 5 - 1));
            grid.walls.Add(new Location(3 - 1, 5 - 1));
            grid.walls.Add(new Location(4 - 1, 5 - 1));
            grid.walls.Add(new Location(5 - 1, 5 - 1));
            grid.walls.Add(new Location(7 - 1, 5 - 1));
            grid.walls.Add(new Location(8 - 1, 5 - 1));
            grid.walls.Add(new Location(9 - 1, 5 - 1));

            //  Row 6 walls
            grid.walls.Add(new Location(1 - 1, 6 - 1));
            grid.walls.Add(new Location(2 - 1, 6 - 1));
            grid.walls.Add(new Location(3 - 1, 6 - 1));
            grid.walls.Add(new Location(4 - 1, 6 - 1));
            grid.walls.Add(new Location(5 - 1, 6 - 1));

            //  Start at (1, 1)
            Location startingLocation = new Location(0, 0);

            //  End at (10, 6)
            Location endingLocation = new Location(9, 5);

            //  Calculate the path
            AstarSearch aStar = new AstarSearch(grid);

            aStar.CalculatePath(startingLocation, endingLocation);

            //  Draw the grid to the screen so we can visually see it.
            DrawGride(grid, aStar);

            Console.ReadLine();
        }