1+ import agents as ag
2+ import envgui as gui
3+ import random
4+
5+ # ______________________________________________________________________________
6+
7+ loc_A , loc_B = (1 , 1 ), (2 , 1 ) # The two locations for the Vacuum world
8+
9+
10+ def RandomVacuumAgent ():
11+ "Randomly choose one of the actions from the vacuum environment."
12+ p = ag .RandomAgentProgram (['Right' , 'Left' , 'Up' , 'Down' , 'Suck' , 'NoOp' ])
13+ return ag .Agent (p )
14+
15+
16+ def TableDrivenVacuumAgent ():
17+ "[Figure 2.3]"
18+ table = {((loc_A , 'Clean' ),): 'Right' ,
19+ ((loc_A , 'Dirty' ),): 'Suck' ,
20+ ((loc_B , 'Clean' ),): 'Left' ,
21+ ((loc_B , 'Dirty' ),): 'Suck' ,
22+ ((loc_A , 'Clean' ), (loc_A , 'Clean' )): 'Right' ,
23+ ((loc_A , 'Clean' ), (loc_A , 'Dirty' )): 'Suck' ,
24+ # ...
25+ ((loc_A , 'Clean' ), (loc_A , 'Clean' ), (loc_A , 'Clean' )): 'Right' ,
26+ ((loc_A , 'Clean' ), (loc_A , 'Clean' ), (loc_A , 'Dirty' )): 'Suck' ,
27+ # ...
28+ }
29+ p = ag .TableDrivenAgentProgram (table )
30+ return ag .Agent ()
31+
32+
33+ def ReflexVacuumAgent ():
34+ "A reflex agent for the two-state vacuum environment. [Figure 2.8]"
35+ def program (percept ):
36+ location , status = percept
37+ if status == 'Dirty' :
38+ return 'Suck'
39+ elif location == loc_A :
40+ return 'Right'
41+ elif location == loc_B :
42+ return 'Left'
43+ return ag .Agent (program )
44+
45+
46+ def ModelBasedVacuumAgent () -> object :
47+ "An agent that keeps track of what locations are clean or dirty."
48+ model = {loc_A : None , loc_B : None }
49+
50+ def program (percept ):
51+ "Same as ReflexVacuumAgent, except if everything is clean, do NoOp."
52+ location , status = percept
53+ model [location ] = status # Update the model here
54+ if model [loc_A ] == model [loc_B ] == 'Clean' :
55+ return 'NoOp'
56+ elif status == 'Dirty' :
57+ return 'Suck'
58+ elif location == loc_A :
59+ return 'Right'
60+ elif location == loc_B :
61+ return 'Left'
62+ return ag .Agent (program )
63+
64+ # ______________________________________________________________________________
65+ # Vacuum environment
66+
67+ class Dirt (ag .Thing ):
68+ pass
69+
70+ # class Floor(ag.Thing):
71+ # pass
72+
73+
74+ class VacuumEnvironment (ag .XYEnvironment ):
75+
76+ """The environment of [Ex. 2.12]. Agent perceives dirty or clean,
77+ and bump (into obstacle) or not; 2D discrete world of unknown size;
78+ performance measure is 100 for each dirt cleaned, and -1 for
79+ each turn taken."""
80+
81+ def __init__ (self , width = 4 , height = 3 ):
82+ super (VacuumEnvironment , self ).__init__ (width , height )
83+ self .add_walls ()
84+
85+ def thing_classes (self ):
86+ return [ag .Wall , Dirt , ReflexVacuumAgent , RandomVacuumAgent ,
87+ TableDrivenVacuumAgent , ModelBasedVacuumAgent ]
88+
89+ def percept (self , agent ):
90+ """The percept is a tuple of ('Dirty' or 'Clean', 'Bump' or 'None').
91+ Unlike the TrivialVacuumEnvironment, location is NOT perceived."""
92+ status = ('Dirty' if self .some_things_at (
93+ agent .location , Dirt ) else 'Clean' )
94+ bump = ('Bump' if agent .bump else 'None' )
95+ return (bump , status )
96+
97+ def execute_action (self , agent , action ):
98+ if action == 'Suck' :
99+ dirt_list = self .list_things_at (agent .location , Dirt )
100+ if dirt_list != []:
101+ dirt = dirt_list [0 ]
102+ agent .performance += 100
103+ self .delete_thing (dirt )
104+ else :
105+ super (VacuumEnvironment , self ).execute_action (agent , action )
106+
107+ if action != 'NoOp' :
108+ agent .performance -= 1
109+
110+
111+ class TrivialVacuumEnvironment (VacuumEnvironment ):
112+
113+ """This environment has two locations, A and B. Each can be Dirty
114+ or Clean. The agent perceives its location and the location's
115+ status. This serves as an example of how to implement a simple
116+ Environment."""
117+
118+ def __init__ (self ):
119+ super (TrivialVacuumEnvironment , self ).__init__ ()
120+ choice = random .randint (0 , 3 )
121+ if choice % 2 : # 1 or 3
122+ self .add_thing (Dirt (), loc_A )
123+ if choice > 1 : # 2 or 3
124+ self .add_thing (Dirt (), loc_B )
125+
126+ def percept (self , agent ):
127+ "Returns the agent's location, and the location status (Dirty/Clean)."
128+ status = ('Dirty' if self .some_things_at (
129+ agent .location , Dirt ) else 'Clean' )
130+ return (agent .location , status )
131+ #
132+ # def execute_action(self, agent, action):
133+ # """Change agent's location and/or location's status; track performance.
134+ # Score 10 for each dirt cleaned; -1 for each move."""
135+ # if action == 'Right':
136+ # agent.location = loc_B
137+ # agent.performance -= 1
138+ # elif action == 'Left':
139+ # agent.location = loc_A
140+ # agent.performance -= 1
141+ # elif action == 'Suck':
142+ # if self.status[agent.location] == 'Dirty':
143+ # agent.performance += 10
144+ # self.status[agent.location] = 'Clean'
145+ #
146+ def add_agent (self , a ):
147+ "Agents start in either location at random."
148+ super ().add_thing (a , random .choice ([loc_A , loc_B ]))
149+
150+
151+ # _________________________________________________________________________
152+
153+ # >>> a = ReflexVacuumAgent()
154+ # >>> a.program((loc_A, 'Clean'))
155+ # 'Right'
156+ # >>> a.program((loc_B, 'Clean'))
157+ # 'Left'
158+ # >>> a.program((loc_A, 'Dirty'))
159+ # 'Suck'
160+ # >>> a.program((loc_A, 'Dirty'))
161+ # 'Suck'
162+ #
163+ # >>> e = TrivialVacuumEnvironment()
164+ # >>> e.add_thing(ModelBasedVacuumAgent())
165+ # >>> e.run(5)
166+
167+ # Produces text-based status output
168+ # v = TrivialVacuumEnvironment()
169+ # a = ModelBasedVacuumAgent()
170+ # a = ag.TraceAgent(a)
171+ # v.add_agent(a)
172+ # v.run(10)
173+
174+ # Launch GUI of Trivial Environment
175+ # v = TrivialVacuumEnvironment()
176+ # a = RandomVacuumAgent()
177+ # a = ag.TraceAgent(a)
178+ # v.add_agent(a)
179+ # g = gui.EnvGUI(v, 'Vaccuum')
180+ # c = g.getCanvas()
181+ # c.mapImageNames({
182+ # Dirt: 'images/dirt.png',
183+ # ag.Wall: 'images/wall.jpg',
184+ # # Floor: 'images/floor.png',
185+ # ag.Agent: 'images/vacuum.png',
186+ # })
187+ # c.update()
188+ # g.mainloop()
189+
190+ # Launch GUI of more complex environment
191+ v = VacuumEnvironment (5 , 4 )
192+ #a = ModelBasedVacuumAgent()
193+ a = RandomVacuumAgent ()
194+ a = ag .TraceAgent (a )
195+ loc = v .random_location_inbounds ()
196+ v .add_thing (a , location = loc )
197+ v .scatter_things (Dirt )
198+ g = gui .EnvGUI (v , 'Vaccuum' )
199+ c = g .getCanvas ()
200+ c .mapImageNames ({
201+ ag .Wall : 'submissions/Becker/wall.jpg' ,
202+ # Floor: 'images/floor.png',
203+ Dirt : 'images/dirt.png' ,
204+ ag .Agent : 'images/vacuum.png' ,
205+ })
206+ c .update ()
207+ g .mainloop ()
0 commit comments