forked from aimacode/aima-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_reinforcement_learning4e.py
More file actions
69 lines (51 loc) · 2.38 KB
/
test_reinforcement_learning4e.py
File metadata and controls
69 lines (51 loc) · 2.38 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
import pytest
from mdp4e import sequential_decision_environment
from reinforcement_learning4e import *
random.seed("aima-python")
north = (0, 1)
south = (0, -1)
west = (-1, 0)
east = (1, 0)
policy = {(0, 2): east, (1, 2): east, (2, 2): east, (3, 2): None,
(0, 1): north, (2, 1): north, (3, 1): None,
(0, 0): north, (1, 0): west, (2, 0): west, (3, 0): west}
def test_PassiveDUEAgent():
agent = PassiveDUEAgent(policy, sequential_decision_environment)
for i in range(200):
run_single_trial(agent, sequential_decision_environment)
agent.estimate_U()
# Agent does not always produce same results.
# Check if results are good enough.
# print(agent.U[(0, 0)], agent.U[(0,1)], agent.U[(1,0)])
assert agent.U[(0, 0)] > 0.15 # In reality around 0.3
assert agent.U[(0, 1)] > 0.15 # In reality around 0.4
assert agent.U[(1, 0)] > 0 # In reality around 0.2
def test_PassiveADPAgent():
agent = PassiveADPAgent(policy, sequential_decision_environment)
for i in range(100):
run_single_trial(agent, sequential_decision_environment)
# Agent does not always produce same results.
# Check if results are good enough.
# print(agent.U[(0, 0)], agent.U[(0,1)], agent.U[(1,0)])
assert agent.U[(0, 0)] > 0.15 # In reality around 0.3
assert agent.U[(0, 1)] > 0.15 # In reality around 0.4
assert agent.U[(1, 0)] > 0 # In reality around 0.2
def test_PassiveTDAgent():
agent = PassiveTDAgent(policy, sequential_decision_environment, alpha=lambda n: 60. / (59 + n))
for i in range(200):
run_single_trial(agent, sequential_decision_environment)
# Agent does not always produce same results.
# Check if results are good enough.
assert agent.U[(0, 0)] > 0.15 # In reality around 0.3
assert agent.U[(0, 1)] > 0.15 # In reality around 0.35
assert agent.U[(1, 0)] > 0.15 # In reality around 0.25
def test_QLearning():
q_agent = QLearningAgent(sequential_decision_environment, Ne=5, Rplus=2, alpha=lambda n: 60. / (59 + n))
for i in range(200):
run_single_trial(q_agent, sequential_decision_environment)
# Agent does not always produce same results.
# Check if results are good enough.
assert q_agent.Q[((0, 1), (0, 1))] >= -0.5 # In reality around 0.1
assert q_agent.Q[((1, 0), (0, -1))] <= 0.5 # In reality around -0.1
if __name__ == '__main__':
pytest.main()