001 package aima.basic.vaccum; 002 003 import java.util.Hashtable; 004 import java.util.Random; 005 006 import aima.basic.Agent; 007 import aima.basic.Environment; 008 import aima.basic.Percept; 009 010 /** 011 * @author Ravi Mohan 012 * 013 */ 014 public class TrivialVaccumEnvironment extends Environment { 015 016 String location1, location2; 017 018 String defaultLocation; 019 020 Hashtable<String, String> status; 021 022 public TrivialVaccumEnvironment() { 023 super(); 024 location1 = "A"; 025 location2 = "B"; 026 defaultLocation = location1; 027 status = new Hashtable<String, String>(); 028 Random r = new Random(); 029 int i = r.nextInt(2); 030 int j = r.nextInt(2); 031 if (i == 0) { 032 status.put(location1, "Clean"); 033 034 } else { 035 status.put(location1, "Dirty"); 036 } 037 038 if (j == 0) { 039 status.put(location2, "Clean"); 040 041 } else { 042 status.put(location2, "Dirty"); 043 } 044 } 045 046 public TrivialVaccumEnvironment(String loc1Status, String loc2Status) { 047 super(); 048 location1 = "A"; 049 location2 = "B"; 050 defaultLocation = location1; 051 status = new Hashtable<String, String>(); 052 status.put(location1, loc1Status); 053 status.put(location2, loc2Status); 054 055 } 056 057 @Override 058 public void executeAction(Agent a, String agentAction) { 059 060 if (agentAction.equals("Right")) { 061 setAgentLocation(a, location2); 062 setAgentPerformance(a, getAgentperformance(a) - 1); 063 } else if (agentAction.equals("Left")) { 064 setAgentLocation(a, location1); 065 setAgentPerformance(a, getAgentperformance(a) - 1); 066 } else if (agentAction.equals("Suck")) { 067 if (getLocationStatus(getAgentLocation(a)).equals("Dirty")) { 068 setLocationStatus(getAgentLocation(a), "Clean"); 069 setAgentPerformance(a, getAgentperformance(a) + 10); 070 } 071 072 } else if (agentAction.equals(Agent.NO_OP)) { 073 // this is a bit unfair, but it simplifies testing... 074 a.die(); 075 076 } 077 } 078 079 @Override 080 public Percept getPerceptSeenBy(Agent anAgent) { 081 Percept retval = new Percept(); 082 retval.setAttribute("location", anAgent.getAttribute("location")); 083 retval.setAttribute("status", status.get(anAgent 084 .getAttribute("location"))); 085 return retval; 086 087 } 088 089 @Override 090 public void addAgent(Agent a) { 091 addAgent(a, defaultLocation); 092 093 } 094 095 public void addAgent(Agent a, String location) { 096 setAgentLocation(a, location); 097 // agents.add(a); 098 super.addAgent(a); 099 setAgentPerformance(a, 0); 100 } 101 102 public String getLocation1Status() { 103 return status.get(location1); 104 } 105 106 public String getLocation2Status() { 107 return status.get(location2); 108 } 109 110 public String getLocationStatus(String location) { 111 return status.get(location); 112 } 113 114 public Hashtable getStatus() { 115 return status; 116 } 117 118 private void setAgentLocation(Agent a, String location) { 119 a.setAttribute("location", location); 120 } 121 122 public String getAgentLocation(Agent a) { 123 return (String) a.getAttribute("location"); 124 } 125 126 private void setAgentPerformance(Agent a, int i) { 127 a.setAttribute("performance", new Integer(i)); 128 } 129 130 public int getAgentperformance(Agent a) { 131 Integer i = (Integer) a.getAttribute("performance"); 132 return i.intValue(); 133 } 134 135 private void setLocationStatus(String aLocation, String aStatus) { 136 status.put(aLocation, aStatus); 137 } 138 }