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)));
}
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;
}
Aggregations