1
1
import random
2
- from agents import Direction
2
+
3
3
from agents import Agent
4
- from agents import ReflexVacuumAgent , ModelBasedVacuumAgent , TrivialVacuumEnvironment , compare_agents ,\
5
- RandomVacuumAgent , TableDrivenVacuumAgent , TableDrivenAgentProgram , RandomAgentProgram , \
6
- SimpleReflexAgentProgram , ModelBasedReflexAgentProgram , rule_match
4
+ from agents import Direction
5
+ from agents import ReflexVacuumAgent , ModelBasedVacuumAgent , TrivialVacuumEnvironment , compare_agents , \
6
+ RandomVacuumAgent , TableDrivenVacuumAgent , TableDrivenAgentProgram , RandomAgentProgram , \
7
+ SimpleReflexAgentProgram , ModelBasedReflexAgentProgram
7
8
from agents import Wall , Gold , Explorer , Thing , Bump , Glitter , WumpusEnvironment , Pit , \
8
- VacuumEnvironment , Dirt
9
-
9
+ VacuumEnvironment , Dirt
10
10
11
11
random .seed ("aima-python" )
12
12
@@ -58,12 +58,12 @@ def test_add():
58
58
assert l2 .direction == Direction .D
59
59
60
60
61
- def test_RandomAgentProgram () :
62
- #create a list of all the actions a vacuum cleaner can perform
61
+ def test_RandomAgentProgram ():
62
+ # create a list of all the actions a vacuum cleaner can perform
63
63
list = ['Right' , 'Left' , 'Suck' , 'NoOp' ]
64
64
# create a program and then an object of the RandomAgentProgram
65
65
program = RandomAgentProgram (list )
66
-
66
+
67
67
agent = Agent (program )
68
68
# create an object of TrivialVacuumEnvironment
69
69
environment = TrivialVacuumEnvironment ()
@@ -72,10 +72,10 @@ def test_RandomAgentProgram() :
72
72
# run the environment
73
73
environment .run ()
74
74
# check final status of the environment
75
- assert environment .status == {(1 , 0 ): 'Clean' , (0 , 0 ): 'Clean' }
75
+ assert environment .status == {(1 , 0 ): 'Clean' , (0 , 0 ): 'Clean' }
76
76
77
77
78
- def test_RandomVacuumAgent () :
78
+ def test_RandomVacuumAgent ():
79
79
# create an object of the RandomVacuumAgent
80
80
agent = RandomVacuumAgent ()
81
81
# create an object of TrivialVacuumEnvironment
@@ -85,7 +85,7 @@ def test_RandomVacuumAgent() :
85
85
# run the environment
86
86
environment .run ()
87
87
# check final status of the environment
88
- assert environment .status == {(1 ,0 ):'Clean' , (0 ,0 ) : 'Clean' }
88
+ assert environment .status == {(1 , 0 ): 'Clean' , (0 , 0 ) : 'Clean' }
89
89
90
90
91
91
def test_TableDrivenAgent ():
@@ -109,22 +109,22 @@ def test_TableDrivenAgent():
109
109
# create an object of TrivialVacuumEnvironment
110
110
environment = TrivialVacuumEnvironment ()
111
111
# initializing some environment status
112
- environment .status = {loc_A :'Dirty' , loc_B :'Dirty' }
112
+ environment .status = {loc_A : 'Dirty' , loc_B : 'Dirty' }
113
113
# add agent to the environment
114
114
environment .add_thing (agent )
115
115
116
116
# run the environment by single step everytime to check how environment evolves using TableDrivenAgentProgram
117
- environment .run (steps = 1 )
118
- assert environment .status == {(1 ,0 ): 'Clean' , (0 ,0 ): 'Dirty' }
117
+ environment .run (steps = 1 )
118
+ assert environment .status == {(1 , 0 ): 'Clean' , (0 , 0 ): 'Dirty' }
119
119
120
- environment .run (steps = 1 )
121
- assert environment .status == {(1 ,0 ): 'Clean' , (0 ,0 ): 'Dirty' }
120
+ environment .run (steps = 1 )
121
+ assert environment .status == {(1 , 0 ): 'Clean' , (0 , 0 ): 'Dirty' }
122
122
123
- environment .run (steps = 1 )
124
- assert environment .status == {(1 ,0 ): 'Clean' , (0 ,0 ): 'Clean' }
123
+ environment .run (steps = 1 )
124
+ assert environment .status == {(1 , 0 ): 'Clean' , (0 , 0 ): 'Clean' }
125
125
126
126
127
- def test_ReflexVacuumAgent () :
127
+ def test_ReflexVacuumAgent ():
128
128
# create an object of the ReflexVacuumAgent
129
129
agent = ReflexVacuumAgent ()
130
130
# create an object of TrivialVacuumEnvironment
@@ -134,31 +134,31 @@ def test_ReflexVacuumAgent() :
134
134
# run the environment
135
135
environment .run ()
136
136
# check final status of the environment
137
- assert environment .status == {(1 ,0 ):'Clean' , (0 ,0 ) : 'Clean' }
137
+ assert environment .status == {(1 , 0 ): 'Clean' , (0 , 0 ) : 'Clean' }
138
138
139
139
140
140
def test_SimpleReflexAgentProgram ():
141
141
class Rule :
142
-
142
+
143
143
def __init__ (self , state , action ):
144
144
self .__state = state
145
145
self .action = action
146
-
146
+
147
147
def matches (self , state ):
148
148
return self .__state == state
149
-
149
+
150
150
loc_A = (0 , 0 )
151
151
loc_B = (1 , 0 )
152
-
152
+
153
153
# create rules for a two state Vacuum Environment
154
154
rules = [Rule ((loc_A , "Dirty" ), "Suck" ), Rule ((loc_A , "Clean" ), "Right" ),
155
- Rule ((loc_B , "Dirty" ), "Suck" ), Rule ((loc_B , "Clean" ), "Left" )]
156
-
155
+ Rule ((loc_B , "Dirty" ), "Suck" ), Rule ((loc_B , "Clean" ), "Left" )]
156
+
157
157
def interpret_input (state ):
158
158
return state
159
-
159
+
160
160
# create a program and then an object of the SimpleReflexAgentProgram
161
- program = SimpleReflexAgentProgram (rules , interpret_input )
161
+ program = SimpleReflexAgentProgram (rules , interpret_input )
162
162
agent = Agent (program )
163
163
# create an object of TrivialVacuumEnvironment
164
164
environment = TrivialVacuumEnvironment ()
@@ -167,7 +167,7 @@ def interpret_input(state):
167
167
# run the environment
168
168
environment .run ()
169
169
# check final status of the environment
170
- assert environment .status == {(1 ,0 ):'Clean' , (0 ,0 ) : 'Clean' }
170
+ assert environment .status == {(1 , 0 ): 'Clean' , (0 , 0 ) : 'Clean' }
171
171
172
172
173
173
def test_ModelBasedReflexAgentProgram ():
@@ -185,7 +185,7 @@ def matches(self, state):
185
185
186
186
# create rules for a two-state vacuum environment
187
187
rules = [Rule ((loc_A , "Dirty" ), "Suck" ), Rule ((loc_A , "Clean" ), "Right" ),
188
- Rule ((loc_B , "Dirty" ), "Suck" ), Rule ((loc_B , "Clean" ), "Left" )]
188
+ Rule ((loc_B , "Dirty" ), "Suck" ), Rule ((loc_B , "Clean" ), "Left" )]
189
189
190
190
def update_state (state , action , percept , model ):
191
191
return percept
@@ -203,7 +203,7 @@ def update_state(state, action, percept, model):
203
203
assert environment .status == {(1 , 0 ): 'Clean' , (0 , 0 ): 'Clean' }
204
204
205
205
206
- def test_ModelBasedVacuumAgent () :
206
+ def test_ModelBasedVacuumAgent ():
207
207
# create an object of the ModelBasedVacuumAgent
208
208
agent = ModelBasedVacuumAgent ()
209
209
# create an object of TrivialVacuumEnvironment
@@ -213,10 +213,10 @@ def test_ModelBasedVacuumAgent() :
213
213
# run the environment
214
214
environment .run ()
215
215
# check final status of the environment
216
- assert environment .status == {(1 ,0 ):'Clean' , (0 ,0 ) : 'Clean' }
216
+ assert environment .status == {(1 , 0 ): 'Clean' , (0 , 0 ) : 'Clean' }
217
217
218
218
219
- def test_TableDrivenVacuumAgent () :
219
+ def test_TableDrivenVacuumAgent ():
220
220
# create an object of the TableDrivenVacuumAgent
221
221
agent = TableDrivenVacuumAgent ()
222
222
# create an object of the TrivialVacuumEnvironment
@@ -226,10 +226,10 @@ def test_TableDrivenVacuumAgent() :
226
226
# run the environment
227
227
environment .run ()
228
228
# check final status of the environment
229
- assert environment .status == {(1 , 0 ):'Clean' , (0 , 0 ):'Clean' }
229
+ assert environment .status == {(1 , 0 ): 'Clean' , (0 , 0 ): 'Clean' }
230
230
231
231
232
- def test_compare_agents () :
232
+ def test_compare_agents ():
233
233
environment = TrivialVacuumEnvironment
234
234
agents = [ModelBasedVacuumAgent , ReflexVacuumAgent ]
235
235
@@ -263,90 +263,96 @@ def test_TableDrivenAgentProgram():
263
263
def test_Agent ():
264
264
def constant_prog (percept ):
265
265
return percept
266
+
266
267
agent = Agent (constant_prog )
267
268
result = agent .program (5 )
268
269
assert result == 5
269
270
271
+
270
272
def test_VacuumEnvironment ():
271
273
# Initialize Vacuum Environment
272
- v = VacuumEnvironment (6 ,6 )
273
- #Get an agent
274
+ v = VacuumEnvironment (6 , 6 )
275
+ # Get an agent
274
276
agent = ModelBasedVacuumAgent ()
275
277
agent .direction = Direction (Direction .R )
276
278
v .add_thing (agent )
277
- v .add_thing (Dirt (), location = (2 ,1 ))
279
+ v .add_thing (Dirt (), location = (2 , 1 ))
278
280
279
281
# Check if things are added properly
280
282
assert len ([x for x in v .things if isinstance (x , Wall )]) == 20
281
283
assert len ([x for x in v .things if isinstance (x , Dirt )]) == 1
282
284
283
- #Let the action begin!
285
+ # Let the action begin!
284
286
assert v .percept (agent ) == ("Clean" , "None" )
285
287
v .execute_action (agent , "Forward" )
286
288
assert v .percept (agent ) == ("Dirty" , "None" )
287
289
v .execute_action (agent , "TurnLeft" )
288
290
v .execute_action (agent , "Forward" )
289
291
assert v .percept (agent ) == ("Dirty" , "Bump" )
290
292
v .execute_action (agent , "Suck" )
291
- assert v .percept (agent ) == ("Clean" , "None" )
293
+ assert v .percept (agent ) == ("Clean" , "None" )
292
294
old_performance = agent .performance
293
295
v .execute_action (agent , "NoOp" )
294
296
assert old_performance == agent .performance
295
297
298
+
296
299
def test_WumpusEnvironment ():
297
300
def constant_prog (percept ):
298
301
return percept
302
+
299
303
# Initialize Wumpus Environment
300
304
w = WumpusEnvironment (constant_prog )
301
305
302
- #Check if things are added properly
306
+ # Check if things are added properly
303
307
assert len ([x for x in w .things if isinstance (x , Wall )]) == 20
304
308
assert any (map (lambda x : isinstance (x , Gold ), w .things ))
305
309
assert any (map (lambda x : isinstance (x , Explorer ), w .things ))
306
- assert not any (map (lambda x : not isinstance (x ,Thing ), w .things ))
310
+ assert not any (map (lambda x : not isinstance (x , Thing ), w .things ))
307
311
308
- #Check that gold and wumpus are not present on (1,1)
309
- assert not any (map (lambda x : isinstance (x , Gold ) or isinstance (x ,WumpusEnvironment ),
310
- w .list_things_at ((1 , 1 ))))
312
+ # Check that gold and wumpus are not present on (1,1)
313
+ assert not any (map (lambda x : isinstance (x , Gold ) or isinstance (x , WumpusEnvironment ),
314
+ w .list_things_at ((1 , 1 ))))
311
315
312
- #Check if w.get_world() segments objects correctly
316
+ # Check if w.get_world() segments objects correctly
313
317
assert len (w .get_world ()) == 6
314
318
for row in w .get_world ():
315
319
assert len (row ) == 6
316
320
317
- #Start the game!
321
+ # Start the game!
318
322
agent = [x for x in w .things if isinstance (x , Explorer )][0 ]
319
323
gold = [x for x in w .things if isinstance (x , Gold )][0 ]
320
324
pit = [x for x in w .things if isinstance (x , Pit )][0 ]
321
325
322
- assert w .is_done ()== False
326
+ assert w .is_done () == False
323
327
324
- #Check Walls
328
+ # Check Walls
325
329
agent .location = (1 , 2 )
326
330
percepts = w .percept (agent )
327
331
assert len (percepts ) == 5
328
- assert any (map (lambda x : isinstance (x ,Bump ), percepts [0 ]))
332
+ assert any (map (lambda x : isinstance (x , Bump ), percepts [0 ]))
329
333
330
- #Check Gold
334
+ # Check Gold
331
335
agent .location = gold .location
332
336
percepts = w .percept (agent )
333
- assert any (map (lambda x : isinstance (x ,Glitter ), percepts [4 ]))
334
- agent .location = (gold .location [0 ], gold .location [1 ]+ 1 )
337
+ assert any (map (lambda x : isinstance (x , Glitter ), percepts [4 ]))
338
+ agent .location = (gold .location [0 ], gold .location [1 ] + 1 )
335
339
percepts = w .percept (agent )
336
- assert not any (map (lambda x : isinstance (x ,Glitter ), percepts [4 ]))
340
+ assert not any (map (lambda x : isinstance (x , Glitter ), percepts [4 ]))
337
341
338
- #Check agent death
342
+ # Check agent death
339
343
agent .location = pit .location
340
344
assert w .in_danger (agent ) == True
341
345
assert agent .alive == False
342
346
assert agent .killed_by == Pit .__name__
343
347
assert agent .performance == - 1000
344
348
345
- assert w .is_done ()== True
349
+ assert w .is_done () == True
350
+
346
351
347
352
def test_WumpusEnvironmentActions ():
348
353
def constant_prog (percept ):
349
354
return percept
355
+
350
356
# Initialize Wumpus Environment
351
357
w = WumpusEnvironment (constant_prog )
352
358
@@ -371,4 +377,4 @@ def constant_prog(percept):
371
377
w .execute_action (agent , 'Climb' )
372
378
assert not any (map (lambda x : isinstance (x , Explorer ), w .things ))
373
379
374
- assert w .is_done ()== True
380
+ assert w .is_done () == True
0 commit comments