forked from google-research/football
/
football_action_set.py
291 lines (250 loc) · 10.7 KB
/
football_action_set.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# 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.
"""List of core actions / action sets supported by the gfootball environment."""
# ***** List of core actions *****
# Only add new ones, do not reorder so the numbering do not change.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from gfootball_engine import e_BackendAction
import numpy
from six.moves import range
class CoreAction(object):
def __init__(self, backend_action, name, sticky=False, directional=False,
release=False):
self._backend_action = backend_action
self._name = name
self._sticky = sticky
self._directional = directional
self._release = release
def __eq__(self, other):
assert set(other.__dict__) == set(self.__dict__)
return self._name == other._name
def __ne__(self, other):
return not self.__eq__(other)
def __lt__(self, other):
assert set(other.__dict__) == set(self.__dict__)
return self._backend_action < other._backend_action
def __le__(self, other):
assert set(other.__dict__) == set(self.__dict__)
return self._backend_action <= other._backend_action
def __gt__(self, other):
assert set(other.__dict__) == set(self.__dict__)
return self._backend_action > other._backend_action
def __ge__(self, other):
assert set(other.__dict__) == set(self.__dict__)
return self._backend_action >= other._backend_action
def __hash__(self):
return self._backend_action
def __repr__(self):
return self._name
action_idle = CoreAction(e_BackendAction.idle, "idle")
action_left = CoreAction(
e_BackendAction.left, "left", sticky=True, directional=True)
action_top_left = CoreAction(
e_BackendAction.top_left, "top_left", sticky=True, directional=True)
action_top = CoreAction(
e_BackendAction.top, "top", sticky=True, directional=True)
action_top_right = CoreAction(
e_BackendAction.top_right, "top_right", sticky=True, directional=True)
action_right = CoreAction(
e_BackendAction.right, "right", sticky=True, directional=True)
action_bottom_right = CoreAction(
e_BackendAction.bottom_right, "bottom_right", sticky=True, directional=True)
action_bottom = CoreAction(
e_BackendAction.bottom, "bottom", sticky=True, directional=True)
action_bottom_left = CoreAction(
e_BackendAction.bottom_left, "bottom_left", sticky=True, directional=True)
action_long_pass = CoreAction(e_BackendAction.long_pass, "long_pass")
action_high_pass = CoreAction(e_BackendAction.high_pass, "high_pass")
action_short_pass = CoreAction(e_BackendAction.short_pass, "short_pass")
action_shot = CoreAction(e_BackendAction.shot, "shot")
action_keeper_rush = CoreAction(
e_BackendAction.keeper_rush, "keeper_rush", sticky=True)
action_sliding = CoreAction(e_BackendAction.sliding, "sliding")
action_pressure = CoreAction(
e_BackendAction.pressure, "pressure", sticky=True)
action_team_pressure = CoreAction(
e_BackendAction.team_pressure, "team_pressure", sticky=True)
action_switch = CoreAction(e_BackendAction.switch, "switch")
action_sprint = CoreAction(e_BackendAction.sprint, "sprint", sticky=True)
action_dribble = CoreAction(
e_BackendAction.dribble, "dribble", sticky=True)
action_release_direction = CoreAction(
e_BackendAction.release_direction, "release_direction", directional=True,
release=True)
action_release_long_pass = CoreAction(e_BackendAction.release_long_pass,
"release_long_pass", release=True)
action_release_high_pass = CoreAction(e_BackendAction.release_high_pass,
"release_high_pass", release=True)
action_release_short_pass = CoreAction(e_BackendAction.release_short_pass,
"release_short_pass", release=True)
action_release_shot = CoreAction(e_BackendAction.release_shot,
"release_shot", release=True)
action_release_keeper_rush = CoreAction(
e_BackendAction.release_keeper_rush, "release_keeper_rush", release=True)
action_release_sliding = CoreAction(e_BackendAction.release_sliding,
"release_sliding", release=True)
action_release_pressure = CoreAction(e_BackendAction.release_pressure,
"release_pressure", release=True)
action_release_team_pressure = CoreAction(
e_BackendAction.release_team_pressure, "release_team_pressure",
release=True)
action_release_switch = CoreAction(e_BackendAction.release_switch,
"release_switch", release=True)
action_release_sprint = CoreAction(e_BackendAction.release_sprint,
"release_sprint", release=True)
action_release_dribble = CoreAction(e_BackendAction.release_dribble,
"release_dribble", release=True)
# ***** Define some action sets *****
# Special action set that includes all the core actions in the same order.
full_action_set = [
action_idle, action_left, action_top_left, action_top,
action_top_right, action_right, action_bottom_right,
action_bottom, action_bottom_left, action_long_pass,
action_high_pass, action_short_pass, action_shot,
action_keeper_rush, action_sliding, action_pressure,
action_team_pressure, action_switch, action_sprint,
action_dribble, action_release_direction,
action_release_long_pass, action_release_high_pass,
action_release_short_pass, action_release_shot,
action_release_keeper_rush, action_release_sliding,
action_release_pressure, action_release_team_pressure,
action_release_switch, action_release_sprint,
action_release_dribble
]
action_set_dict = {
# "full" action set is needed by the play_game script.
# Don't use it for training models.
"full":
full_action_set,
"default": [
action_idle,
action_left,
action_top_left,
action_top,
action_top_right,
action_right,
action_bottom_right,
action_bottom,
action_bottom_left,
action_long_pass,
action_high_pass,
action_short_pass,
action_shot,
action_sprint,
action_release_direction,
action_release_sprint,
action_keeper_rush,
action_release_keeper_rush,
action_sliding,
action_dribble,
action_release_dribble,
],
}
reverse_action_mapping = {
action_long_pass: action_release_long_pass,
action_high_pass: action_release_high_pass,
action_short_pass: action_release_short_pass,
action_shot: action_release_shot,
action_keeper_rush: action_release_keeper_rush,
action_sliding: action_release_sliding,
action_pressure: action_release_pressure,
action_team_pressure: action_release_team_pressure,
action_switch: action_release_switch,
action_sprint: action_release_sprint,
action_dribble: action_release_dribble,
action_release_long_pass: action_long_pass,
action_release_high_pass: action_high_pass,
action_release_short_pass: action_short_pass,
action_release_shot: action_shot,
action_release_keeper_rush: action_keeper_rush,
action_release_sliding: action_sliding,
action_release_pressure: action_pressure,
action_release_team_pressure: action_team_pressure,
action_release_switch: action_switch,
action_release_sprint: action_sprint,
action_release_dribble: action_dribble
}
# Returns action set specified by the config.
def get_action_set(config):
action_set_name = config["action_set"]
return action_set_dict[action_set_name]
def get_sticky_actions(config):
"""Returns list of sticky actions for the currently used action set."""
sticky_actions = []
for a in get_action_set(config):
if a._sticky:
sticky_actions.append(a)
return sticky_actions
# Converts different action representation to an action from a given action set.
def named_action_from_action_set(action_set, action):
if (hasattr(action, "__dict__") and action_set and
set(action.__dict__) == set(action_set[0].__dict__)):
return action
if (isinstance(action, numpy.int32) or isinstance(action, numpy.int64) or
isinstance(action, int)):
# The action can be given as a numpy.int32 which cannot be
# serialized. First convert it to a proper python integer.
action = int(action)
assert action < len(action_set), "Action outside of action set"
return action_set[action]
assert False, "Action {} not found in action set".format(action)
def disable_action(action):
assert set(action.__dict__) == set(action_left.__dict__)
if action._directional:
return action_release_direction
return reverse_action_mapping[action]
class StickyWrapper(object):
def __init__(self, config, controller):
self._to_execute = None
self._controller = controller
self._sticky_actions = get_sticky_actions(config)
self._active_sticky_actions = {}
for a in full_action_set:
if a._sticky:
self._active_sticky_actions[a] = False
def post_initialize(self):
self._controller.post_initialize()
def active_sticky_actions(self):
result = []
for a in self._sticky_actions:
result.append(self._active_sticky_actions[a])
return numpy.uint8(result)
def perform_action(self, action):
"""Performs a given CoreAction and updates sticky action states.
Disables sticky actions which are disjoint with the action being performed.
Args:
action: a CoreAction
"""
assert set(action.__dict__) == set(action_left.__dict__)
if self._to_execute:
self._controller.perform_action(self._to_execute._backend_action)
self._to_execute = None
if action == action_idle:
return
if action._directional:
# Direction action disables all other direction actions.
for a in full_action_set:
if a._directional and a._sticky:
self._active_sticky_actions[a] = False
else:
reverse = reverse_action_mapping[action]
if reverse._sticky:
self._active_sticky_actions[reverse] = False
self._controller.perform_action(action._backend_action)
if action._sticky:
self._active_sticky_actions[action] = True
elif not action._directional and not action._release:
self._to_execute = reverse_action_mapping[action]