forked from google-research/football
/
script_helpers.py
119 lines (107 loc) · 4.09 KB
/
script_helpers.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
# coding=utf-8
# Copyright 2019 Google LLC
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Set of functions used by command line scripts."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from gfootball.env import config
from gfootball.env import football_action_set
from gfootball.env import football_env
from gfootball.env import observation_processor
import copy
import six.moves.cPickle
import os
import tempfile
import tensorflow as tf
class ScriptHelpers(object):
"""Set of methods used by command line scripts."""
def __init__(self):
pass
def __modify_trace(self, replay, fps):
"""Adopt replay to the new framerate and add additional steps at the end."""
trace = []
min_fps = replay[0]['debug']['config']['physics_steps_per_frame']
assert fps % min_fps == 0, (
'Trace has to be rendered in framerate being multiple of {}'.formmat(
min_fps))
assert fps <= 100, ('Framerate of up to 100 is supported')
empty_steps = int(fps / min_fps) - 1
for f in replay:
trace.append(f)
idle_step = copy.deepcopy(f)
idle_step['debug']['action'] = [football_action_set.action_idle
] * len(f['debug']['action'])
for _ in range(empty_steps):
trace.append(idle_step)
# Add some empty steps at the end, so that we can record videos.
for _ in range(10):
trace.append(idle_step)
return trace
def __build_players(self, dump_file, spec):
players = []
for player in spec:
players.extend(['replay:path={},left_players=1'.format(
dump_file)] * config.count_left_players(player))
players.extend(['replay:path={},right_players=1'.format(
dump_file)] * config.count_right_players(player))
return players
def dump_to_txt(self, dump_file, output, include_debug):
with open(dump_file, 'rb') as f:
replay = six.moves.cPickle.load(f)
if not include_debug:
for s in replay:
if 'debug' in s:
del s['debug']
with open(output, 'w') as f:
f.write(str(replay))
def dump_to_video(self, dump_file):
with open(dump_file, 'rb') as f:
dump = six.moves.cPickle.load(f)
cfg = config.Config(dump[0]['debug']['config'])
cfg['dump_full_episodes'] = True
cfg['write_video'] = True
cfg['display_game_stats'] = True
processor = observation_processor.ObservationProcessor(cfg)
for frame in dump:
processor.update(frame)
processor.write_dump('episode_done')
def replay(self, dump, fps=10, config_update={}, directory=None):
with open(dump, 'rb') as f:
replay = six.moves.cPickle.load(f)
trace = self.__modify_trace(replay, fps)
fd, temp_path = tempfile.mkstemp(suffix='.dump')
with open(temp_path, 'wb') as f:
six.moves.cPickle.dump(trace, f)
assert replay[0]['debug']['frame_cnt'] == 1, (
'Trace does not start from the beginning of the episode, can not replay')
cfg = config.Config(replay[0]['debug']['config'])
cfg['players'] = self.__build_players(temp_path, cfg['players'])
config_update['physics_steps_per_frame'] = int(100 / fps)
config_update['real_time'] = False
if 'render' not in config_update:
config_update['render'] = True
if directory:
config_update['tracesdir'] = directory
config_update['write_video'] = True
cfg.update(config_update)
env = football_env.FootballEnv(cfg)
env.reset()
done = False
try:
while not done:
_, _, done, _ = env.step(None)
except KeyboardInterrupt:
env.write_dump('shutdown')
exit(1)
os.close(fd)