Ejemplo n.º 1
0
        public void testAIMA3eFigure3_24()
        {
            Map romaniaMap = new SimplifiedRoadMapOfPartOfRomania();
            IProblem <string, MoveToAction> problem = new GeneralProblem <string, MoveToAction>(
                SimplifiedRoadMapOfPartOfRomania.ARAD,
                MapFunctions.createActionsFunction(romaniaMap),
                MapFunctions.createResultFunction(),
                SimplifiedRoadMapOfPartOfRomania.BUCHAREST.Equals,
                MapFunctions.createDistanceStepCostFunction(romaniaMap));

            ISearchForActions <string, MoveToAction> search = new AStarSearch <string, MoveToAction>(new TreeSearch <string, MoveToAction>(),
                                                                                                     MapFunctions.createSLDHeuristicFunction(SimplifiedRoadMapOfPartOfRomania.BUCHAREST, romaniaMap));
            SearchAgent <string, MoveToAction> agent = new SearchAgent <string, MoveToAction>(problem, search);

            Assert.AreEqual(
                "[Action[name==moveTo, location==Sibiu], Action[name==moveTo, location==RimnicuVilcea], Action[name==moveTo, location==Pitesti], Action[name==moveTo, location==Bucharest]]",
                agent.getActions().ToString());
            Assert.AreEqual(4, agent.getActions().Size());
            Assert.AreEqual("5",
                            agent.getInstrumentation().getProperty("nodesExpanded"));
            Assert.AreEqual("10",
                            agent.getInstrumentation().getProperty("queueSize"));
            Assert.AreEqual("11",
                            agent.getInstrumentation().getProperty("maxQueueSize"));
        }
Ejemplo n.º 2
0
        public void testAIMA3eFigure3_15()
        {
            Map romaniaMap = new SimplifiedRoadMapOfPartOfRomania();
            IProblem <string, MoveToAction> problem = new GeneralProblem <string, MoveToAction>(
                SimplifiedRoadMapOfPartOfRomania.SIBIU,
                MapFunctions.createActionsFunction(romaniaMap),
                MapFunctions.createResultFunction(),
                SimplifiedRoadMapOfPartOfRomania.BUCHAREST.Equals,
                MapFunctions.createDistanceStepCostFunction(romaniaMap));

            ISearchForActions <string, MoveToAction> search
                = new AStarSearch <string, MoveToAction>(
                      new GraphSearch <string, MoveToAction>(),
                      MapFunctions.createSLDHeuristicFunction(
                          SimplifiedRoadMapOfPartOfRomania.BUCHAREST,
                          romaniaMap));
            SearchAgent <string, MoveToAction> agent = new SearchAgent <string, MoveToAction>(problem, search);

            ICollection <MoveToAction> actions = agent.getActions();

            Assert.AreEqual(
                "[Action[name==moveTo, location==RimnicuVilcea], Action[name==moveTo, location==Pitesti], Action[name==moveTo, location==Bucharest]]",
                actions.ToString());
            Assert.AreEqual("278",
                            search.getMetrics().get(QueueSearch <string, MoveToAction> .METRIC_PATH_COST));
        }
Ejemplo n.º 3
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);
        }