Пример #1
0
 public WumpusActionsFunction(WumpusCave cave)
 {
     this.cave = cave;
 }
Пример #2
0
        /**
         * Returns a sequence of actions using A* Search.
         *
         * @param current
         *            the agent's current position
         * @param goals
         *            a set of squares; try to plan a route to one of them
         * @param allowed
         *            a set of squares that can form part of the route
         *
         * @return the best sequence of actions that the agent have to do to reach a
         *         goal from the current position.
         */
        public ICollection <IAction> planRoute(AgentPosition current, ISet <Room> goals, ISet <Room> allowed)
        {
            // Every square represent 4 possible positions for the agent, it could
            // be in different orientations. For every square in allowed and goals
            // sets we add 4 squares.
            ISet <AgentPosition> allowedPositions = CollectionFactory.CreateSet <AgentPosition>();

            foreach (Room allowedRoom in allowed)
            {
                int x = allowedRoom.getX();
                int y = allowedRoom.getY();

                allowedPositions.Add(new AgentPosition(x, y, AgentPosition.Orientation.FACING_WEST));
                allowedPositions.Add(new AgentPosition(x, y, AgentPosition.Orientation.FACING_EAST));
                allowedPositions.Add(new AgentPosition(x, y, AgentPosition.Orientation.FACING_NORTH));
                allowedPositions.Add(new AgentPosition(x, y, AgentPosition.Orientation.FACING_SOUTH));
            }
            ISet <AgentPosition> goalPositions = CollectionFactory.CreateSet <AgentPosition>();

            foreach (Room goalRoom in goals)
            {
                int x = goalRoom.getX();
                int y = goalRoom.getY();

                goalPositions.Add(new AgentPosition(x, y, AgentPosition.Orientation.FACING_WEST));
                goalPositions.Add(new AgentPosition(x, y, AgentPosition.Orientation.FACING_EAST));
                goalPositions.Add(new AgentPosition(x, y, AgentPosition.Orientation.FACING_NORTH));
                goalPositions.Add(new AgentPosition(x, y, AgentPosition.Orientation.FACING_SOUTH));
            }

            WumpusCave cave = new WumpusCave(kb.getCaveXDimension(), kb.getCaveYDimension(), allowedPositions);

            GoalTest <AgentPosition> goalTest = goalPositions.Contains;

            IProblem <AgentPosition, IAction> problem = new GeneralProblem <AgentPosition, IAction>(current,
                                                                                                    WumpusFunctionFunctions.createActionsFunction(cave),
                                                                                                    WumpusFunctionFunctions.createResultFunction(), goalTest);

            IToDoubleFunction <Node <AgentPosition, IAction> > h = new ManhattanHeuristicFunction(goals);

            ISearchForActions <AgentPosition, IAction> search = new AStarSearch <AgentPosition, IAction>(
                new GraphSearch <AgentPosition, IAction>(), h);
            SearchAgent <AgentPosition, IAction> agent;
            ICollection <IAction> actions = null;

            try
            {
                agent   = new SearchAgent <AgentPosition, IAction>(problem, search);
                actions = agent.getActions();
                // Search agent can return a NoOp if already at goal,
                // in the context of this agent we will just return
                // no actions.
                if (actions.Size() == 1 && actions.Get(0).IsNoOp())
                {
                    actions = CollectionFactory.CreateQueue <IAction>();
                }
            }
            catch (Exception e)
            {
                throw e;
            }

            return(actions);
        }
Пример #3
0
 public static IActionsFunction <AgentPosition, IAction> createActionsFunction(WumpusCave cave)
 {
     return(new WumpusActionsFunction(cave));
 }