forked from b06b01073/go_thesis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoutils.py
66 lines (43 loc) · 1.54 KB
/
goutils.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
import govars
import numpy as np
from seed import set_seed
set_seed()
def move_encode(move):
'''
Args:
move (string): a string of the form COLOR[CR], where COLOR is 'B' or 'W', C is the column, R is the row. If CR are empty, then it represents a pass move
'''
left_bracket_index = move.index('[')
right_bracket_index = move.index(']')
# CR is empty
if left_bracket_index + 1 == right_bracket_index:
return govars.PASS
action2d = move[left_bracket_index + 1: right_bracket_index]
return action2d_to_action1d(action2d)
def action1d_to_action2d(action1d):
'''
convert action in 1d(0 - 361) to 2d coordinate
Args:
action1d(int): 0-361
Returns:
tuple: (row, col)
'''
return action1d // govars.SIZE, action1d % govars.SIZE
def readable_action1d(acion1d):
'''
convert action in 1d(0 -361) to 2d coordinate in human readable format([a-z a-z])
'''
r, c = action1d_to_action2d(acion1d)
return chr(ord('a') + c) + chr(ord('a') + r)
def action2d_to_action1d(action2d):
'''
convert action in 2d to action in 1d
Args:
action2d: 2d action in [CR] format
'''
action1d = (ord(action2d[0]) - ord('a')) + (ord(action2d[1]) - ord('a')) * 19 # action1d = col + row * 19, so that action2d = (row, col), see gogame.py(next_state function)
return action1d
def action1d_to_onehot(action1d):
one_hot = np.zeros((govars.ACTION_SPACE))
one_hot[action1d] = 1
return one_hot