Search in sources :

Example 1 with Shoot

use of aima.core.environment.wumpusworld.action.Shoot in project aima-java by aimacode.

the class HybridWumpusAgentTest method testPlanShot.

@SuppressWarnings("serial")
@Test
public void testPlanShot() {
    HybridWumpusAgent hwa = new HybridWumpusAgent(4);
    // Should be just shoot as are facing the Wumpus
    Assert.assertEquals(Arrays.asList(new Shoot()), hwa.planShot(new AgentPosition(1, 1, AgentPosition.Orientation.FACING_EAST), new LinkedHashSet<Room>() {

        {
            add(new Room(3, 1));
        }
    }, allRooms(4)));
    Assert.assertEquals(Arrays.asList(new TurnLeft(AgentPosition.Orientation.FACING_EAST), new Shoot()), hwa.planShot(new AgentPosition(1, 1, AgentPosition.Orientation.FACING_EAST), new LinkedHashSet<Room>() {

        {
            add(new Room(1, 2));
        }
    }, allRooms(4)));
    Assert.assertEquals(Arrays.asList(new Forward(new AgentPosition(1, 1, AgentPosition.Orientation.FACING_EAST)), new TurnLeft(AgentPosition.Orientation.FACING_EAST), new Shoot()), hwa.planShot(new AgentPosition(1, 1, AgentPosition.Orientation.FACING_EAST), new LinkedHashSet<Room>() {

        {
            add(new Room(2, 2));
        }
    }, allRooms(4)));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HybridWumpusAgent(aima.core.environment.wumpusworld.HybridWumpusAgent) TurnLeft(aima.core.environment.wumpusworld.action.TurnLeft) AgentPosition(aima.core.environment.wumpusworld.AgentPosition) Forward(aima.core.environment.wumpusworld.action.Forward) Shoot(aima.core.environment.wumpusworld.action.Shoot) Room(aima.core.environment.wumpusworld.Room) Test(org.junit.Test)

Example 2 with Shoot

use of aima.core.environment.wumpusworld.action.Shoot in project aima-java by aimacode.

the class HybridWumpusAgent method planShot.

/**
	 * 
	 * @param current
	 *            the agent's current position
	 * @param possibleWumpus
	 *            a set of squares where we don't know that there isn't the
	 *            wumpus.
	 * @param allowed
	 *            a set of squares that can form part of the route
	 * 
	 * @return the sequence of actions to reach the nearest square that is in
	 *         line with a possible wumpus position. The last action is a shot.
	 */
public List<Action> planShot(AgentPosition current, Set<Room> possibleWumpus, Set<Room> allowed) {
    Set<AgentPosition> shootingPositions = new LinkedHashSet<>();
    for (Room p : possibleWumpus) {
        int x = p.getX();
        int y = p.getY();
        for (int i = 1; i <= kb.getCaveXDimension(); i++) {
            if (i < x) {
                shootingPositions.add(new AgentPosition(i, y, AgentPosition.Orientation.FACING_EAST));
            }
            if (i > x) {
                shootingPositions.add(new AgentPosition(i, y, AgentPosition.Orientation.FACING_WEST));
            }
            if (i < y) {
                shootingPositions.add(new AgentPosition(x, i, AgentPosition.Orientation.FACING_NORTH));
            }
            if (i > y) {
                shootingPositions.add(new AgentPosition(x, i, AgentPosition.Orientation.FACING_SOUTH));
            }
        }
    }
    // reside
    for (Room p : possibleWumpus) {
        for (AgentPosition.Orientation orientation : AgentPosition.Orientation.values()) {
            shootingPositions.remove(new AgentPosition(p.getX(), p.getY(), orientation));
        }
    }
    Iterator<AgentPosition> it = shootingPositions.iterator();
    Set<Room> shootingPositionsArray = new LinkedHashSet<>();
    while (it.hasNext()) {
        AgentPosition tmp = it.next();
        shootingPositionsArray.add(new Room(tmp.getX(), tmp.getY()));
    }
    List<Action> actions = planRoute(current, shootingPositionsArray, allowed);
    AgentPosition newPos = current;
    if (actions.size() > 0) {
        newPos = ((Forward) actions.get(actions.size() - 1)).getToPosition();
    }
    while (!shootingPositions.contains(newPos)) {
        TurnLeft tLeft = new TurnLeft(newPos.getOrientation());
        newPos = new AgentPosition(newPos.getX(), newPos.getY(), tLeft.getToOrientation());
        actions.add(tLeft);
    }
    actions.add(new Shoot());
    return actions;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Action(aima.core.agent.Action) TurnLeft(aima.core.environment.wumpusworld.action.TurnLeft) Shoot(aima.core.environment.wumpusworld.action.Shoot)

Aggregations

Shoot (aima.core.environment.wumpusworld.action.Shoot)2 TurnLeft (aima.core.environment.wumpusworld.action.TurnLeft)2 LinkedHashSet (java.util.LinkedHashSet)2 Action (aima.core.agent.Action)1 AgentPosition (aima.core.environment.wumpusworld.AgentPosition)1 HybridWumpusAgent (aima.core.environment.wumpusworld.HybridWumpusAgent)1 Room (aima.core.environment.wumpusworld.Room)1 Forward (aima.core.environment.wumpusworld.action.Forward)1 Test (org.junit.Test)1