1
1
import random
2
-
3
- from agents import Agent
4
2
from agents import Direction
5
- from agents import ReflexVacuumAgent , ModelBasedVacuumAgent , TrivialVacuumEnvironment , compare_agents , \
6
- RandomVacuumAgent , TableDrivenVacuumAgent , TableDrivenAgentProgram , RandomAgentProgram , \
7
- SimpleReflexAgentProgram , ModelBasedReflexAgentProgram
3
+ from agents import Agent
4
+ from agents import ReflexVacuumAgent , ModelBasedVacuumAgent , TrivialVacuumEnvironment , compare_agents ,\
5
+ RandomVacuumAgent , TableDrivenVacuumAgent , TableDrivenAgentProgram , RandomAgentProgram , \
6
+ SimpleReflexAgentProgram , ModelBasedReflexAgentProgram , rule_match
8
7
from agents import Wall , Gold , Explorer , Thing , Bump , Glitter , WumpusEnvironment , Pit , \
9
- VacuumEnvironment , Dirt
8
+ VacuumEnvironment , Dirt
9
+
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,96 +263,90 @@ def test_TableDrivenAgentProgram():
263
263
def test_Agent ():
264
264
def constant_prog (percept ):
265
265
return percept
266
-
267
266
agent = Agent (constant_prog )
268
267
result = agent .program (5 )
269
268
assert result == 5
270
269
271
-
272
270
def test_VacuumEnvironment ():
273
271
# Initialize Vacuum Environment
274
- v = VacuumEnvironment (6 , 6 )
275
- # Get an agent
272
+ v = VacuumEnvironment (6 ,6 )
273
+ #Get an agent
276
274
agent = ModelBasedVacuumAgent ()
277
275
agent .direction = Direction (Direction .R )
278
276
v .add_thing (agent )
279
- v .add_thing (Dirt (), location = (2 , 1 ))
277
+ v .add_thing (Dirt (), location = (2 ,1 ))
280
278
281
279
# Check if things are added properly
282
280
assert len ([x for x in v .things if isinstance (x , Wall )]) == 20
283
281
assert len ([x for x in v .things if isinstance (x , Dirt )]) == 1
284
282
285
- # Let the action begin!
283
+ #Let the action begin!
286
284
assert v .percept (agent ) == ("Clean" , "None" )
287
285
v .execute_action (agent , "Forward" )
288
286
assert v .percept (agent ) == ("Dirty" , "None" )
289
287
v .execute_action (agent , "TurnLeft" )
290
288
v .execute_action (agent , "Forward" )
291
289
assert v .percept (agent ) == ("Dirty" , "Bump" )
292
290
v .execute_action (agent , "Suck" )
293
- assert v .percept (agent ) == ("Clean" , "None" )
291
+ assert v .percept (agent ) == ("Clean" , "None" )
294
292
old_performance = agent .performance
295
293
v .execute_action (agent , "NoOp" )
296
294
assert old_performance == agent .performance
297
295
298
-
299
296
def test_WumpusEnvironment ():
300
297
def constant_prog (percept ):
301
298
return percept
302
-
303
299
# Initialize Wumpus Environment
304
300
w = WumpusEnvironment (constant_prog )
305
301
306
- # Check if things are added properly
302
+ #Check if things are added properly
307
303
assert len ([x for x in w .things if isinstance (x , Wall )]) == 20
308
304
assert any (map (lambda x : isinstance (x , Gold ), w .things ))
309
305
assert any (map (lambda x : isinstance (x , Explorer ), w .things ))
310
- assert not any (map (lambda x : not isinstance (x , Thing ), w .things ))
306
+ assert not any (map (lambda x : not isinstance (x ,Thing ), w .things ))
311
307
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 ))))
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 ))))
315
311
316
- # Check if w.get_world() segments objects correctly
312
+ #Check if w.get_world() segments objects correctly
317
313
assert len (w .get_world ()) == 6
318
314
for row in w .get_world ():
319
315
assert len (row ) == 6
320
316
321
- # Start the game!
317
+ #Start the game!
322
318
agent = [x for x in w .things if isinstance (x , Explorer )][0 ]
323
319
gold = [x for x in w .things if isinstance (x , Gold )][0 ]
324
320
pit = [x for x in w .things if isinstance (x , Pit )][0 ]
325
321
326
- assert w .is_done () == False
322
+ assert w .is_done ()== False
327
323
328
- # Check Walls
324
+ #Check Walls
329
325
agent .location = (1 , 2 )
330
326
percepts = w .percept (agent )
331
327
assert len (percepts ) == 5
332
- assert any (map (lambda x : isinstance (x , Bump ), percepts [0 ]))
328
+ assert any (map (lambda x : isinstance (x ,Bump ), percepts [0 ]))
333
329
334
- # Check Gold
330
+ #Check Gold
335
331
agent .location = gold .location
336
332
percepts = w .percept (agent )
337
- assert any (map (lambda x : isinstance (x , Glitter ), percepts [4 ]))
338
- agent .location = (gold .location [0 ], gold .location [1 ] + 1 )
333
+ assert any (map (lambda x : isinstance (x ,Glitter ), percepts [4 ]))
334
+ agent .location = (gold .location [0 ], gold .location [1 ]+ 1 )
339
335
percepts = w .percept (agent )
340
- assert not any (map (lambda x : isinstance (x , Glitter ), percepts [4 ]))
336
+ assert not any (map (lambda x : isinstance (x ,Glitter ), percepts [4 ]))
341
337
342
- # Check agent death
338
+ #Check agent death
343
339
agent .location = pit .location
344
340
assert w .in_danger (agent ) == True
345
341
assert agent .alive == False
346
342
assert agent .killed_by == Pit .__name__
347
343
assert agent .performance == - 1000
348
344
349
- assert w .is_done () == True
350
-
345
+ assert w .is_done ()== True
351
346
352
347
def test_WumpusEnvironmentActions ():
353
348
def constant_prog (percept ):
354
349
return percept
355
-
356
350
# Initialize Wumpus Environment
357
351
w = WumpusEnvironment (constant_prog )
358
352
@@ -377,4 +371,4 @@ def constant_prog(percept):
377
371
w .execute_action (agent , 'Climb' )
378
372
assert not any (map (lambda x : isinstance (x , Explorer ), w .things ))
379
373
380
- assert w .is_done () == True
374
+ assert w .is_done ()== True
0 commit comments