-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_agents.py
More file actions
119 lines (94 loc) · 3.53 KB
/
Copy pathtest_agents.py
File metadata and controls
119 lines (94 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import random
from def_agents import Direction
from def_agents import Agent
from def_agents import (ReflexVacuumAgent, ModelBasedVacuumAgent, TrivialVacuumEnvironment, compare_agents,
RandomVacuumAgent)
random.seed("aima-python")
def test_move_forward():
d = Direction("up")
l1 = d.move_forward((0, 0))
assert l1 == (0, -1)
d = Direction(Direction.R)
l1 = d.move_forward((0, 0))
assert l1 == (1, 0)
d = Direction(Direction.D)
l1 = d.move_forward((0, 0))
assert l1 == (0, 1)
d = Direction("left")
l1 = d.move_forward((0, 0))
assert l1 == (-1, 0)
l2 = d.move_forward((1, 0))
assert l2 == (0, 0)
def test_add():
d = Direction(Direction.U)
l1 = d + "right"
l2 = d + "left"
assert l1.direction == Direction.R
assert l2.direction == Direction.L
d = Direction("right")
l1 = d.__add__(Direction.L)
l2 = d.__add__(Direction.R)
assert l1.direction == "up"
assert l2.direction == "down"
d = Direction("down")
l1 = d.__add__("right")
l2 = d.__add__("left")
assert l1.direction == Direction.L
assert l2.direction == Direction.R
d = Direction(Direction.L)
l1 = d + Direction.R
l2 = d + Direction.L
assert l1.direction == Direction.U
assert l2.direction == Direction.D
def test_RandomVacuumAgent() :
# create an object of the RandomVacuumAgent
agent = RandomVacuumAgent()
# create an object of TrivialVacuumEnvironment
environment = TrivialVacuumEnvironment()
# add agent to the environment
environment.add_thing(agent)
# run the environment
environment.run()
# check final status of the environment
assert environment.status == {(1,0):'Clean' , (0,0) : 'Clean'}
def test_ReflexVacuumAgent() :
# create an object of the ReflexVacuumAgent
agent = ReflexVacuumAgent()
# create an object of TrivialVacuumEnvironment
environment = TrivialVacuumEnvironment()
# add agent to the environment
environment.add_thing(agent)
# run the environment
environment.run()
# check final status of the environment
assert environment.status == {(1,0):'Clean' , (0,0) : 'Clean'}
def test_ModelBasedVacuumAgent() :
# create an object of the ModelBasedVacuumAgent
agent = ModelBasedVacuumAgent()
# create an object of TrivialVacuumEnvironment
environment = TrivialVacuumEnvironment()
# add agent to the environment
environment.add_thing(agent)
# run the environment
environment.run()
# check final status of the environment
assert environment.status == {(1,0):'Clean' , (0,0) : 'Clean'}
def test_compare_agents() :
environment = TrivialVacuumEnvironment
agents = [ModelBasedVacuumAgent, ReflexVacuumAgent]
result = compare_agents(environment, agents)
performance_ModelBasedVacummAgent = result[0][1]
performance_ReflexVacummAgent = result[1][1]
# The performance of ModelBasedVacuumAgent will be at least as good as that of
# ReflexVacuumAgent, since ModelBasedVacuumAgent can identify when it has
# reached the terminal state (both locations being clean) and will perform
# NoOp leading to 0 performance change, whereas ReflexVacuumAgent cannot
# identify the terminal state and thus will keep moving, leading to worse
# performance compared to ModelBasedVacuumAgent.
assert performance_ReflexVacummAgent <= performance_ModelBasedVacummAgent
def test_Agent():
def constant_prog(percept):
return percept
agent = Agent(constant_prog)
result = agent.program(5)
assert result == 5