/
indexing.py
69 lines (55 loc) · 1.38 KB
/
indexing.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
"""
create a game indexing
"""
def create(actions):
N = len(actions)
M = 1
offsets = [0]*N
for i in range(N-1,-1,-1):
assert actions[i] > 0
offsets[i] = M
M = M*actions[i]
return (M, actions, offsets)
"""
actions -> outcome
"""
def index(indexing, action):
(_,actions,_) = indexing
outcome = 0
for a,n in zip(action,actions):
assert a >= 0 and a < n
outcome = n*outcome + a
return outcome
"""
outcome -> actions
"""
def unindex(indexing, outcome):
(M,actions,_) = indexing
N = len(actions)
assert outcome >= 0 and outcome < M
action = [0]*N
for i in range(N-1,-1,-1):
action[i] = outcome%actions[i]
outcome = outcome/actions[i]
return action
"""
outcome -> player i's action
"""
def unindexi(indexing, outcome, i):
(M,actions,offsets) = indexing
N = len(actions)
assert outcome >= 0 and outcome < M
assert i >= 0 and i < N
return outcome/offsets[i]%actions[i]
"""
outcome -> outcome, switching i's action to y
"""
def reindex(indexing, outcome, i, y):
(M,actions,offsets) = indexing
N = len(actions)
assert outcome >= 0 and outcome < M
assert i >= 0 and i < N
assert y >= 0 and y < actions[i]
high = outcome/offsets[i]/actions[i]
low = outcome%offsets[i]
return (high*actions[i] + y)*offsets[i] + low