forked from NextCenturyCorporation/mcs-scene-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent_hypercubes.py
341 lines (290 loc) · 11 KB
/
agent_hypercubes.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import copy
import glob
import json
import random
import os
from typing import Any, Callable, Dict, List
from agent_scene_pair_json_converter import EXPECTED, UNEXPECTED, \
convert_scene_pair
import exceptions
import hypercubes
import tags
AGENT_GOAL_TEMPLATE = {
'category': tags.tag_to_label(tags.SCENE.AGENTS),
'description': '',
'domainsInfo': {
'agents': [
tags.DOMAINS.AGENTS_2,
tags.DOMAINS.AGENTS_3,
tags.DOMAINS.AGENTS_7
],
'objects': [],
'places': []
},
'sceneInfo': {},
'metadata': {
'choose': [EXPECTED, UNEXPECTED]
}
}
AGENT_GOAL_TEMPLATE['sceneInfo'][tags.SCENE.PRIMARY] = (
tags.tag_to_label(tags.SCENE.PASSIVE)
)
AGENT_GOAL_TEMPLATE['sceneInfo'][tags.SCENE.SECONDARY] = (
tags.tag_to_label(tags.SCENE.AGENTS)
)
AGENT_GOAL_TEMPLATE['sceneInfo'][tags.SCENE.QUATERNARY] = (
tags.tag_to_label(tags.SCENE.ACTION_NONE)
)
class AgentHypercube(hypercubes.Hypercube):
def __init__(
self,
filename_prefix: str,
body_template: Dict[str, Any],
goal_template: Dict[str, Any],
role_to_type: Dict[str, str],
training=False,
untrained=False
) -> None:
self._filename_prefix = filename_prefix
self._role_to_type = role_to_type
self._untrained = untrained
super().__init__(
goal_template['sceneInfo'][tags.SCENE.TERTIARY],
body_template,
goal_template,
training=training
)
# Override
def _create_scenes(
self,
body_template: Dict[str, Any],
goal_template: Dict[str, Any]
) -> List[Dict[str, Any]]:
# Each JSON filename will have a suffix of either 'e.json' or 'u.json'
json_filename = {
'expected': self._filename_prefix + 'e.json',
'unexpected': self._filename_prefix + 'u.json'
}
json_data = {
'expected': None,
'unexpected': None
}
category_list = ['expected']
if not self._training:
category_list.append('unexpected')
for category in category_list:
# Ensure the JSON scene file exists.
if not os.path.exists(json_filename[category]):
raise ValueError(f'Agent hypercube cannot find {category} '
f'scene JSON file: {json_filename[category]}')
# Read the data from the JSON scene file.
with open(json_filename[category]) as json_file:
print(f'Reading {category} agent scene JSON file: '
f'{json_filename[category]}')
json_data[category] = json.load(json_file)
# Create the pair of MCS scenes from the JSON data, which should be a
# list of trials that each have a list of frames.
scenes = convert_scene_pair(
body_template,
goal_template,
json_data['expected'],
json_data['unexpected'],
self._filename_prefix,
self._role_to_type,
self._untrained
)
# Remember a training hypercube will only have its expected scene.
scenes[0]['goal']['sceneInfo'][tags.SCENE.ID] = [os.path.splitext(
os.path.basename(json_filename['expected'])
)[0]]
if len(scenes) > 1:
scenes[1]['goal']['sceneInfo'][tags.SCENE.ID] = [os.path.splitext(
os.path.basename(json_filename['unexpected'])
)[0]]
return scenes
# Override
def _get_training_scenes(self) -> List[Dict[str, Any]]:
# Each AgentHypercubeFactory will handle training flag validation.
return self._scenes
class AgentHypercubeFactory(hypercubes.HypercubeFactory):
def __init__(
self,
name: str,
folder_name: str,
goal_template: str,
training: bool
) -> None:
super().__init__(name, training)
self._folder_name = folder_name
self._goal_template = goal_template
self._untrained = False
# Override
def _build(self, body_template: Dict[str, Any]) -> hypercubes.Hypercube:
return AgentHypercube(
self._filename_prefix,
body_template,
self._goal_template,
self.training,
# Must use only untrained shapes in 50% of scenes.
untrained=self._untrained
)
# Override
def build(
self,
total: str,
body_template_function: Callable[[], Dict[str, Any]],
throw_error=False
) -> List[hypercubes.Hypercube]:
# Return one hypercube per pair of expected/unexpected JSON scene files
# in the folder associated with this factory, or up to the given total.
hypercubes = []
prefix_to_number = {}
# Each JSON filename will have a suffix of either 'e.json' or 'u.json'
for suffix in ['e.json', 'u.json']:
for json_filename in glob.glob(self._folder_name + '/*' + suffix):
# Remove the filename suffix.
prefix_to_number[json_filename[:-6]] = (
prefix_to_number.get(json_filename[:-6], 0) + 1
)
print(f'Agent hypercube factory found {len(prefix_to_number.items())} '
f'pairs of scene JSON files in {self._folder_name}')
# Randomize the order of the files.
randomized_prefix_to_number = list(prefix_to_number.items())
random.shuffle(randomized_prefix_to_number)
# Generate one hypercube per valid pair of files.
count = 0
failed_filename_list = []
for prefix, number in randomized_prefix_to_number:
if (not self.training) and (number < 2):
print(f'[NOTE] Agent hypercube factory found only one scene '
f'JSON file in {self._folder_name} but expected two '
f'named {prefix + "e.json"} and {prefix + "u.json"}')
continue
count += 1
print(f'Generating agent hypercube {count} / {total}')
self._filename_prefix = prefix
try:
hypercube = self._build(body_template_function())
hypercubes.append(hypercube)
# Every other scene pair should have untrained objects.
self._untrained = (not self._untrained)
except (
RuntimeError,
ZeroDivisionError,
TypeError,
exceptions.SceneException,
ValueError
) as e:
if throw_error:
raise
print(f'[ERROR] Failed to create {self.name} hypercube')
print(e)
failed_filename_list.append(prefix)
if count == total:
break
if count < total:
print(f'[NOTE] Agent hypercube factory found only '
f'{count} valid pairs of scene JSON files in '
f'{self._folder_name} but {total} were required '
f'via command line argument.')
if len(failed_filename_list) > 0:
print('[NOTE] The following agent scene files failed:')
for filename in failed_filename_list:
print(f'{filename}')
return hypercubes
class AgentSingleObjectTrainingHypercubeFactory(AgentHypercubeFactory):
GOAL_TEMPLATE = copy.deepcopy(AGENT_GOAL_TEMPLATE)
GOAL_TEMPLATE['sceneInfo'][tags.SCENE.TERTIARY] = (
tags.TYPES.AGENT_BACKGROUND_SINGLE_OBJECT
)
def __init__(self) -> None:
super().__init__(
'AgentSingleObjectTraining',
'agents_background_single_object',
self.GOAL_TEMPLATE,
training=True
)
class AgentObjectPreferenceTrainingHypercubeFactory(AgentHypercubeFactory):
GOAL_TEMPLATE = copy.deepcopy(AGENT_GOAL_TEMPLATE)
GOAL_TEMPLATE['sceneInfo'][tags.SCENE.TERTIARY] = (
tags.TYPES.AGENT_BACKGROUND_OBJECT_PREFERENCE
)
def __init__(self) -> None:
super().__init__(
'AgentObjectPreferenceTraining',
'agents_background_object_preference',
self.GOAL_TEMPLATE,
training=True
)
class AgentObjectPreferenceEvaluationHypercubeFactory(AgentHypercubeFactory):
GOAL_TEMPLATE = copy.deepcopy(AGENT_GOAL_TEMPLATE)
GOAL_TEMPLATE['sceneInfo'][tags.SCENE.TERTIARY] = (
tags.TYPES.AGENT_EVALUATION_OBJECT_PREFERENCE
)
def __init__(self) -> None:
super().__init__(
'AgentObjectPreferenceEvaluation',
'agents_evaluation_object_preference',
self.GOAL_TEMPLATE,
training=False
)
class AgentEfficientActionAEvaluationHypercubeFactory(AgentHypercubeFactory):
GOAL_TEMPLATE = copy.deepcopy(AGENT_GOAL_TEMPLATE)
GOAL_TEMPLATE['sceneInfo'][tags.SCENE.TERTIARY] = (
tags.TYPES.AGENT_EVALUATION_EFFICIENT_ACTION_A
)
def __init__(self) -> None:
super().__init__(
'AgentEfficientActionAEvaluation',
'agents_evaluation_efficient_action_a',
self.GOAL_TEMPLATE,
training=False
)
class AgentEfficientActionBEvaluationHypercubeFactory(AgentHypercubeFactory):
GOAL_TEMPLATE = copy.deepcopy(AGENT_GOAL_TEMPLATE)
GOAL_TEMPLATE['sceneInfo'][tags.SCENE.TERTIARY] = (
tags.TYPES.AGENT_EVALUATION_EFFICIENT_ACTION_B
)
def __init__(self) -> None:
super().__init__(
'AgentEfficientActionBEvaluation',
'agents_evaluation_efficient_action_b',
self.GOAL_TEMPLATE,
training=False
)
class AgentExamplesTrainingHypercubeFactory(AgentHypercubeFactory):
GOAL_TEMPLATE = copy.deepcopy(AGENT_GOAL_TEMPLATE)
GOAL_TEMPLATE['sceneInfo'][tags.SCENE.TERTIARY] = 'agents examples'
def __init__(self) -> None:
super().__init__(
'AgentExamplesTraining',
'agents_examples',
self.GOAL_TEMPLATE,
training=True
)
class AgentExamplesEvaluationHypercubeFactory(AgentHypercubeFactory):
GOAL_TEMPLATE = copy.deepcopy(AGENT_GOAL_TEMPLATE)
GOAL_TEMPLATE['sceneInfo'][tags.SCENE.TERTIARY] = 'agents examples'
def __init__(self) -> None:
super().__init__(
'AgentExamplesEvaluation',
'agents_examples',
self.GOAL_TEMPLATE,
training=False
)
AGENT_TRAINING_HYPERCUBE_LIST = [
AgentSingleObjectTrainingHypercubeFactory(),
AgentObjectPreferenceTrainingHypercubeFactory(),
AgentEfficientActionAEvaluationHypercubeFactory(),
AgentEfficientActionBEvaluationHypercubeFactory(),
AgentObjectPreferenceEvaluationHypercubeFactory(),
AgentExamplesTrainingHypercubeFactory()
]
AGENT_EVALUATION_HYPERCUBE_LIST = [
AgentSingleObjectTrainingHypercubeFactory(),
AgentObjectPreferenceTrainingHypercubeFactory(),
AgentEfficientActionAEvaluationHypercubeFactory(),
AgentEfficientActionBEvaluationHypercubeFactory(),
AgentObjectPreferenceEvaluationHypercubeFactory(),
AgentExamplesEvaluationHypercubeFactory()
]