Search in sources :

Example 1 with EnvironmentObject

use of aima.core.agent.EnvironmentObject in project aima-java by aimacode.

the class XYEnvironmentState method getObjectsNear.

public Set<EnvironmentObject> getObjectsNear(Agent agent, int radius) {
    Set<EnvironmentObject> objsNear = new LinkedHashSet<EnvironmentObject>();
    XYLocation agentLocation = getCurrentLocationFor(agent);
    for (XYLocation loc : objsAtLocation.keySet()) {
        if (withinRadius(radius, agentLocation, loc)) {
            objsNear.addAll(objsAtLocation.get(loc));
        }
    }
    // Ensure the 'agent' is not included in the Set of
    // objects near
    objsNear.remove(agent);
    return objsNear;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) EnvironmentObject(aima.core.agent.EnvironmentObject) XYLocation(aima.core.util.datastructure.XYLocation)

Example 2 with EnvironmentObject

use of aima.core.agent.EnvironmentObject in project aima-java by aimacode.

the class XYEnvironmentTest method testGetObjectsNear.

@Test
public void testGetObjectsNear() {
    XYLocation loc = new XYLocation(5, 5);
    env.moveObjectToAbsoluteLocation(a, loc);
    AbstractAgent b = new MockAgent();
    AbstractAgent c = new MockAgent();
    Wall w1 = new Wall();
    env.addObjectToLocation(b, new XYLocation(7, 4));
    env.addObjectToLocation(c, new XYLocation(5, 7));
    env.addObjectToLocation(w1, new XYLocation(3, 10));
    // at this point agent A should be able to see B and C but not the wall
    // with a "vision radius" of 3
    Set<EnvironmentObject> visibleToA = env.getObjectsNear(a, 3);
    Assert.assertEquals(2, visibleToA.size());
    // agent B should be able to see A only
    Set<EnvironmentObject> visibleToB = env.getObjectsNear(b, 3);
    Assert.assertEquals(1, visibleToB.size());
    // move B South
    env.moveObject(b, XYLocation.Direction.South);
    // at this point both a and c should be visible to b
    visibleToB = env.getObjectsNear(b, 3);
    Assert.assertEquals(2, visibleToB.size());
    // move c near the wall
    env.moveObjectToAbsoluteLocation(c, new XYLocation(3, 11));
    // only the wall should be visible
    Set<EnvironmentObject> visibleToC = env.getObjectsNear(c, 3);
    Assert.assertEquals(1, visibleToC.size());
}
Also used : MockAgent(aima.test.core.unit.agent.impl.MockAgent) EnvironmentObject(aima.core.agent.EnvironmentObject) Wall(aima.core.environment.xyenv.Wall) XYLocation(aima.core.util.datastructure.XYLocation) AbstractAgent(aima.core.agent.impl.AbstractAgent) Test(org.junit.Test)

Aggregations

EnvironmentObject (aima.core.agent.EnvironmentObject)2 XYLocation (aima.core.util.datastructure.XYLocation)2 AbstractAgent (aima.core.agent.impl.AbstractAgent)1 Wall (aima.core.environment.xyenv.Wall)1 MockAgent (aima.test.core.unit.agent.impl.MockAgent)1 LinkedHashSet (java.util.LinkedHashSet)1 Test (org.junit.Test)1