为什么修饰符“public”对这个项目无效,而它对其他 3 个类似类型的实现有效?

问题描述 投票:0回答:0

这是 4 种搜索算法 BFS、DFS、GBFS 和 A* 的基本实现,其中代理(机器人)在带有墙壁的 5 x 11 网格中导航,以到达两个目标位置之一。我实现了 BFS、DFS、GBFS 没有任何问题,但在 A* 上我收到错误消息 CS0106 The modifier 'public' is not valid for this item.


public string Astar()
{
    //Return solution if initial position is goal
    if ((position.X == goal_position.X) && (position.Y == goal_position.Y))
    {
        return "Goal is at Starting Position";
    }
    else
    {
        //Initialize data structure for open nodes and visited nodes
        List<point2D> vacant = new List<point2D>();
        List<point2D> visited = new List<point2D>();

        //Initialize expanding node
        point2D visited_node;

        //Put the initial position in the open list
        vacant.Add(position);

        //Initial stationary cost
        position.GScore = 0;

        while (vacant.Count != 0)
        {
            //Sort the open list order by f(n)
            vacant = vacant.OrderBy(s => s.FScore).ToList();

            //Expand the first node of the priority list
            visited_node = vacant.First();
            vacant.Remove(vacant.First());

            //Add the expanded node to the visisted list
            visited.Add(visited_node);

            //Initialize UI
            ui.Draw(position, goal_position, agent_world.WallList, visited_node, agent_world.Width, agent_world.Length);
            Thread.Sleep(100);

            foreach (grid g in agent_world.Grids)
            {
                //Verify the expanding grid is within the map
                if ((visited_node.X == g.Position.X) && (visited_node.Y == g.Position.Y))
                {
                    //Verify if adjacent nodes are available
                    if (g.Paths.Count != 0)
                    {
                        foreach (Path p in g.Paths)
                        {
                            //Repeated state checking
                            if ((!visited.Any(x => x.X == p.Location.Position.X && x.Y == p.Location.Position.Y)) && !vacant.Any(x => x.X == p.Location.Position.X && x.Y == p.Location.Position.Y))
                            {
                                p.Location.Position.ParentNode = new point2D(visited_node);
                                //Calculate g(n) as the cost so far from the start to the current node
                                p.Location.Position.GScore = visited_node.GScore + 1;

                                //Calculate f(n) value
                                p.Location.Position.FScore = p.Location.Position.GScore + Math.Sqrt(Math.Pow(goal_position.X - p.Location.Position.X, 2) + Math.Pow(goal_position.Y - p.Location.Position.Y, 2));
                                //p.Location.Pos.FScore = p.Location.Pos.GScore + Math.Abs(goalPos.X - p.Location.Pos.X) + Math.Abs(goalPos.Y - p.Location.Pos.Y);

                                //Add adjacent nodes to the open list
                                vacant.Add(p.Location.Position);
                            }
                        }
                    }

                    //If solution is found
                    if ((visited_node.X == goal_position.X) && (visited_node.Y == goal_position.Y))
                    {
                        return solution("A*", Position, goal_position, visited);
                    }
                }
            }
        }

        //If no solution is found
        return "No solution";
    }
}

此函数与我没有得到错误的其他函数非常相似,用于比较的 DFS 函数


public string DFS()
{
    if ((position.X == goal_position.X) && (position.Y == goal_position.Y))
    {
        return "Goal is at Starting Position";
    }
    else
    {
        // Nodes that are vacant
        Stack<point2D> vacant = new Stack<point2D>();

        //Nodes that are visited
        List<point2D> visited = new List<point2D>();
        //Initializes the expanding node
        point2D visited_node;
        //Pushes the initial position to the open stack
        vacant.Push(position);

        while (vacant.Count != 0)
        {
            //Visiting a node causes it to be popped out of the stack
            visited_node = vacant.Pop();
            //Visiting a node adds the node to the visible list
            visited.Add(visited_node);

            Debug.WriteLine("Expand: " + visited_node.Coordinate);

            //Initialize the UI
            ui.Draw(position, goal_position, agent_world.WallList, visited_node, agent_world.Width, agent_world.Length);
            Thread.Sleep(100);

            foreach (grid g in agent_world.Grids)
            {
                //Checks if the grid is within the map or world
                if ((visited_node.X == g.Position.X) && (visited_node.Y == g.Position.Y))
                {
                    //Verifies if nearby adjacent nodes are available
                    if (g.Paths.Count != 0)
                    {
                        foreach (Path p in g.Paths)
                        {
                            //Checks the state repeatedly
                            if((!visited.Any(x => x.X == p.Location.Position.X && x.Y == p.Location.Position.Y)) && !vacant.Any(x => x.X == p.Location.Position.X && x.Y == p.Location.Position.Y))
                            {
                                p.Location.Position.ParentNode = new point2D(visited_node);

                                Debug.WriteLine(p.Location.Position.Coordinate);
                                //push nearby nodes to the open
                                vacant.Push(p.Location.Position);
                            }
                        }
                    }
                    //If solution is found
                    if ((visited_node.X == goal_position.X) && (visited_node.Y == goal_position.Y))
                        return solution("DFS", Position, goal_position, visited);
                }
            }
        }
        //If no solution found
        return "No Solution";
    }
}

我试着让函数成为自己的类,但没有成功。

c# search public a-star heuristics
© www.soinside.com 2019 - 2024. All rights reserved.