001 package aima.search.map; 002 003 import java.util.ArrayList; 004 import java.util.List; 005 006 import aima.basic.Agent; 007 import aima.basic.Environment; 008 import aima.basic.Percept; 009 import aima.util.Util; 010 011 /** 012 * Represents the environment a MapAgent can navigate. 013 */ 014 015 /** 016 * @author Ciaran O'Reilly 017 * 018 */ 019 020 public class MapEnvironment extends Environment { 021 022 private Map aMap = null; 023 024 public MapEnvironment(Map aMap) { 025 this.aMap = aMap; 026 } 027 028 public void addAgent(Agent a, String startLocation) { 029 super.addAgent(a); 030 a.setAttribute(DynAttributeNames.AGENT_LOCATION, startLocation); 031 a.setAttribute(DynAttributeNames.AGENT_TRAVEL_DISTANCE, 0.0); 032 } 033 034 @Override 035 public void executeAction(Agent a, String act) { 036 String currLoc = (String) a 037 .getAttribute(DynAttributeNames.AGENT_LOCATION); 038 Double distance = aMap.getDistance(currLoc, act); 039 if (distance != null) { 040 double currTD = (Double) a 041 .getAttribute(DynAttributeNames.AGENT_TRAVEL_DISTANCE); 042 a.setAttribute(DynAttributeNames.AGENT_TRAVEL_DISTANCE, currTD 043 + distance); 044 a.setAttribute(DynAttributeNames.AGENT_LOCATION, act); 045 } 046 } 047 048 @Override 049 public Percept getPerceptSeenBy(Agent anAgent) { 050 String currLoc = (String) anAgent.getAttribute( 051 DynAttributeNames.AGENT_LOCATION); 052 List<Object> possibleActions = new ArrayList<Object>(); 053 for (String a : aMap.getLocationsLinkedTo(currLoc)) { 054 possibleActions.add(a); 055 possibleActions.add(new Double(aMap.getDistance(currLoc, a))); 056 } 057 return new Percept(DynAttributeNames.PERCEPT_IN, currLoc, 058 DynAttributeNames.PERCEPT_POSSIBLE_ACTIONS, possibleActions); 059 } 060 061 public Map getMap() { 062 return aMap; 063 } 064 }