package aima.agents; import java.io.*; import java.util.*; import java.awt.Point; public class VacuumEnvironment extends GridEnvironment { public VacuumEnvironment(AgentThing[] agents, int xSize, int ySize, float probDirt) { super(agents, xSize, ySize); this.addWalls(); this.addWithProbability(Dirt.class, probDirt); } /** Count 100 for each dirt found, 1 off for each time step, and 1000 off for * not returning home. */ public float performanceMeasure(Agent agentArg) { AgentThing agent = (AgentThing) agentArg; int result = 0; for (int i = 0; i < agent.contents.size(); i++) if (agent.contents.elementAt(i) instanceof Dirt) result += 100; if (agent.location.equals(start)) result += 1000; return result - step; } /** Handle the "Suck" and "Shut-Off" actions, or call super */ public void execute(Agent agent, Action action) { executeVacuumAction((AgentThing)agent, (AnAction)action); } void executeVacuumAction(AgentThing agent, AnAction action) { String name = action.getName(); if (name.equalsIgnoreCase("Suck")) { Thing dirt = findObjectOfType(Dirt.class, agent.location); if (dirt != null) { placeInContainer(dirt, agent); } } else if (name.equalsIgnoreCase("Shut-Off")) { agent.isAlive = false; } else { executeGridAction(agent, action); } } public Percept presentPercept(Agent agentArg) { AgentThing agent = (AgentThing) agentArg; Point loc = (Point) agent.location; return new VacuumPercept(agent.isBump, findObjectOfType(Dirt.class,loc) != null, loc.equals(start)); } public static VacuumEnvironment sample() { AgentThing a = new AgentThing(new AgentProgram() { public Action execute(Percept perceptArg) { VacuumPercept percept = (VacuumPercept)perceptArg; if (percept.isDirt) return new AnAction("suck"); if (percept.isBump || AIMA.random() < .2) { return new AnAction("turn", AIMA.randomChoice("right", "left")); } return new AnAction("forward"); }}); AgentThing agents[] = { a }; return new VacuumEnvironment(agents, 10, 10, 0.75f); } public static void main(String[] args) { System.out.println("Test: "+sample()); } }