-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_data_extractor.py
158 lines (139 loc) · 5.42 KB
/
log_data_extractor.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import os
import re
from functools import reduce
from multiprocessing import Pool
from utils.create_ai_data import *
def extract_states(state_list):
'''
1. state
2. corners
3. edges
4. hexes
5-7. players
'''
def extract_state(state_set):
# print(state_set, '\n')
def transform_corner(settlement, city):
if settlement != 'none':
out1 = settlement
out2 = 1
elif city != 'none':
out1 = city
out2 = 2
else:
out1 = 'none'
out2 = 0
return out1, out2
try:
corners = reduce(list.__add__, json.loads(state_set[1].replace('None', '"none"').replace('\'', '"')))
# print(corners)
for i in range(len(corners)//2):
temp1, temp2 = transform_corner(corners[2*i], corners[2*i+1])
corners[2*i] = temp1
corners[2*i+1] = temp2
# print(corners)
return json.loads(state_set[0].replace('\'', '"').replace('(', '[').replace(')', ']')) + \
corners + \
json.loads(state_set[2].replace('None', '"none"').replace('\'', '"')) + \
[json.loads(state_set[3].replace('False', '0').replace('True', '1')).index(1)] + \
json.loads(state_set[4]) + \
json.loads(state_set[5]) + \
json.loads(state_set[6])
except:
return state_set
i = 0
states = []
while 7 * i + 3 < len(state_list):
states.append(extract_state(state_list[7 * i + 3: 7 * i + 10]))
i += 1
return states[:-1]
def extract_actions(action_list):
def extract_action(action):
# if 'setup1' in action:
if 'buy' in action:
return 'buy', action.split(' ')[-1]
elif 'setup_1' in action or 'setup_2' in action:
return action
elif 'used' in action:
return 'used', action.split(' ')[-1]
elif 'plenty' in action:
return 'plenty', action.split(' ')[-3]
elif 'takes' in action:
return 'monopoly', action.split(' ')[-3]
elif 'robber' in action:
return 'robber', re.findall('\(\d*, \d*\)', action)[0]
elif 'settlement' in action:
return 'settlement', re.findall('\(\d*, \d*\)', action)[0]
elif 'city' in action:
return 'city', re.findall('\(\d*, \d*\)', action)[0]
elif 'road' in action:
return 'road', re.findall('\(\d*, \d*\)', action)[0]
elif 'development' in action:
pass
elif 'exchanged' in action:
return 'trade', action.split(' ')[-4], action.split(' ')[-1]
elif 'rolled' in action:
pass
elif 'steals' in action:
pass
elif 'collects' in action:
pass
elif 'discards' in action:
pass
elif len(action) == 0:
pass
else:
return action
return list(filter(lambda x: x is not None, map(extract_action, action_list)))
def extract_options(option_list):
def extract_option(option):
return json.loads(
option.replace('(', '[').replace(')', ']').replace('\'', '"').replace('False', '0').replace('True', '1'))
length = len(option_list) // 2
return [list(map(extract_option, option_list))[2 * i + 1] for i in range(length)]
def extract_all(action_file):
def pips(number):
return (6 - abs(7 - number)) * (number != 0)
ii = int(action_file.split('_')[1].split('.')[0])
with open(f'ai_logs/road_ai_logs/action_logs/action_{ii}.txt', 'r') as f:
action_log = f.read()
with open(f'ai_logs/road_ai_logs/state_logs/state_{ii}.txt', 'r') as f:
state_log = f.read()
with open(f'ai_logs/road_ai_logs/option_logs/option_{ii}.txt', 'r') as f:
option_log = f.read()
harbors = json.loads(state_log[7:].split('\nstate: ')[0].replace('\'', '"'))
resources = json.loads(state_log[7:].split('\nstate: ')[1].replace('\'', '"'))
numbers = list(map(pips, json.loads(state_log[7:].split('\nstate: ')[2])))
winner = state_log.split(' ')[-1][:-1]
try:
assert winner in ['white', 'blue', 'orange']
except:
return None
static = harbors + resources + numbers
states = extract_states(state_log[7:].split('\nstate: '))
try:
assert len(states) < 1215
except:
return None
actions = extract_actions(action_log[8:].split('\naction: '))
options = extract_options(option_log[8:].split('\noption: '))
actions.pop(0)
all_actions = []
while len(actions) > 0:
turn_actions = []
# print(action)
action = actions.pop(0)
while isinstance(action, tuple):
turn_actions.append(list(action))
action = actions.pop(0)
all_actions.append(turn_actions)
road_ai_data(ii, winner, static, states, all_actions, options)
# trade_ai_data(ii, winner, static, states, all_actions, options)
# build_ai_data(ii, winner, static, states, all_actions, options)
if __name__ == '__main__':
action_files = os.listdir('ai_logs/road_ai_logs/action_logs')
action_files = list(filter(lambda x: int(x.split('.')[0].split('_')[1])<=96000, action_files))
# extract_all(action_files[0])
with Pool(os.cpu_count() * 1 // 4) as p:
p.map(extract_all, action_files)
# extract_all(1)