-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathengine_tests.py
114 lines (80 loc) · 1.61 KB
/
engine_tests.py
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
"""
Some tests and usage examples for the engine module
"""
import numpy as np
from engine import RMG, AdvRw, SimpleCoin
# Now we perform some tests
batch_size = 1
max_steps = 100
# Reward matrix for the Iterated Prisoner's Dilemma
ipd_rewards = np.array([[-1., 0.], [-3., -2.]])
env = RMG(max_steps=max_steps, payouts=ipd_rewards, batch_size=batch_size)
env.reset()
# Both agents defect
a = ([1], [1])
s, r, d, _ = env.step(a)
print(s,r,d)
# Player 1 cooperates but her opponent defects
a = ([0], [1])
s, r, d, _ = env.step(a)
print(s,r,d)
batch_size = 2
max_steps = 100
env = RMG(max_steps=max_steps, payouts=ipd_rewards, batch_size=batch_size)
env.reset()
# Both agents defect in one simulation but in the other both cooperate
a = ([1,0], [1,0])
s, r, d, _ = env.step(a)
print(s,r,d)
# AdvRW tests
print('AdvRW tests')
print('friend env')
env = AdvRw()
env.step(0)
env.step(0)
env.step(0)
env.step(0)
env.step(0)
env.step(0)
env.step(0)
env.step(0)
env.step(1)
env.step(1)
env.step(1)
env.step(1)
print('adversary env')
env = AdvRw('adversary')
env.step(0)
env.step(0)
env.step(0)
env.step(0)
env.step(0)
env.step(0)
env.step(0)
env.step(0)
env.step(1)
env.step(1)
env.step(1)
env.step(1)
#############################################################
print('Simple Coin tests')
max_steps = 1000
eng = SimpleCoin(max_steps=max_steps)
eng.reset()
a = (1, 1)
s, r, d = eng.step(a)
print(r)
print(s)
s, r, d = eng.step(a)
print(r)
print(s)
a = (1, 0)
s, r, d = eng.step(a)
print(r)
total_r = np.zeros(2)
eng.reset()
for _ in range(max_steps):
a = (1, 1)
_, r, _ = eng.step(a)
total_r += r
print(total_r)