-
Notifications
You must be signed in to change notification settings - Fork 20
/
run_experiment.py
executable file
·375 lines (328 loc) · 13.2 KB
/
run_experiment.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#!/usr/bin/env python2
from __future__ import print_function
# https://github.mit.edu/Learning-and-Intelligent-Systems/ltamp_pr2/blob/d1e6024c5c13df7edeab3a271b745e656a794b02/learn_tools/collect_simulation.py
# https://github.mit.edu/caelan/pddlstream-experiments/blob/master/run_experiment.py
import argparse
import numpy as np
import time
import datetime
import math
import numpy
import random
import os
import sys
import traceback
import resource
import copy
import psutil
PACKAGES = ['pddlstream', 'ss-pybullet']
def add_packages(packages):
sys.path.extend(os.path.abspath(os.path.join(os.getcwd(), d)) for d in packages)
add_packages(PACKAGES)
np.set_printoptions(precision=3, threshold=3, edgeitems=1, suppress=True) #, linewidth=1000)
import pddlstream.language.statistics
pddlstream.language.statistics.LOAD_STATISTICS = False
pddlstream.language.statistics.SAVE_STATISTICS = False
from pybullet_tools.utils import has_gui, elapsed_time, user_input, ensure_dir, \
wrap_numpy_seed, timeout, write_json, SEPARATOR, WorldSaver, \
get_random_seed, get_numpy_seed, set_random_seed, set_numpy_seed, wait_for_user, get_date, is_darwin, INF
from pddlstream.utils import str_from_object, safe_rm_dir, Verbose, KILOBYTES_PER_GIGABYTE, BYTES_PER_KILOBYTE
from pddlstream.algorithms.algorithm import reset_globals
from src.command import create_state, iterate_commands
from src.observe import observe_pybullet
from src.world import World
from src.policy import run_policy
from src.task import cook_block, TASKS_FNS
from run_pybullet import create_parser
from multiprocessing import Pool, TimeoutError, cpu_count
EXPERIMENTS_DIRECTORY = 'experiments/'
TEMP_DIRECTORY = 'temp_parallel/'
MAX_TIME = 10*60
TIME_BUFFER = 60
SERIAL = is_darwin()
VERBOSE = SERIAL
SERIALIZE_TASK = True
MEAN_TIME_PER_TRIAL = 300 # trial / sec
HOURS_TO_SECS = 60 * 60
N_TRIALS = 1 # 1
MAX_MEMORY = 3.5*KILOBYTES_PER_GIGABYTE
SPARE_CORES = 4
POLICIES = [
{'constrain': False, 'defer': False},
{'constrain': True, 'defer': False},
{'constrain': False, 'defer': True}, # Move actions grow immensely
{'constrain': True, 'defer': True},
# TODO: serialize
]
# 8Gb memory limit
# https://ipc2018-classical.bitbucket.io/
# Tasks
# 1) Inspect drawers
# 2) Swap drawers (uniform prior)
# 3) Object behind one of two objects
# 4) Cook meal
# 6) Object on drawer that needs to be moved
# 7) Crowded surface
# 8) Scaling to longer tasks (no point if serializing)
# 9) Packing into drawer
# 10) Fixed base manipulation
# 11) Regrasp using the cabinet
# 12) Irrelevant distractors that aren't picked up
TASK_NAMES = [
'inspect_drawer',
'sugar_drawer',
'swap_drawers',
'detect_block',
#'cook_meal',
#'regrasp_block',
#'hold_block',
#'cook_block',
#'stow_block',
]
# TODO: CPU usage at 300% due to TracIK or the visualizer?
# TODO: could check collisions only with real (non-observed) values
ERROR_OUTCOME = {
'error': True,
'achieved_goal': False,
'total_time': INF,
'plan_time': INF,
'num_iterations': 0,
'num_constrained': 0,
'num_unconstrained': 0,
'num_successes': 0,
'num_actions': INF,
'num_commands': INF,
'total_cost': INF,
}
# TODO: doesn't work on flakey
################################################################################
def map_parallel(fn, inputs, num_cores=None): #, timeout=None):
# Processes rather than threads (shared memory)
# TODO: with statement on Pool
if SERIAL:
for outputs in map(fn, inputs):
yield outputs
return
pool = Pool(processes=num_cores) #, initializer=mute)
generator = pool.imap_unordered(fn, inputs) #, chunksize=1)
# pool_result = pool.map_async(worker, args)
#return generator
while True:
# TODO: need to actually retrieve the info about which thread failed
try:
yield generator.next() # timeout=timeout)
except StopIteration:
break
#except MemoryError: # as e:
# traceback.print_exc()
# continue
#except TimeoutError: # as e:
# traceback.print_exc() # Kills all jobs
# continue
if pool is not None:
pool.close()
pool.terminate()
pool.join()
#import psutil
#if parallel:
# process = psutil.Process(os.getpid())
# print(process)
# print(process.get_memory_info())
################################################################################
def name_from_policy(policy):
return '_'.join('{}={:d}'.format(key, value) for key, value in sorted(policy.items()))
def set_memory_limits():
# ulimit -a
# soft, hard = resource.getrlimit(name) # resource.RLIM_INFINITY
# resource.setrlimit(resource.RLIMIT_AS, (soft, hard))
process = psutil.Process(os.getpid())
soft_memory = int(BYTES_PER_KILOBYTE*MAX_MEMORY)
hard_memory = soft_memory
process.rlimit(psutil.RLIMIT_AS, (soft_memory, hard_memory))
# TODO: AttributeError: 'Process' object has no attribute 'rlimit'
#soft_time = MAX_TIME + 2*60 # I think this kills the wrong things
#hard_time = soft_time
#process.rlimit(psutil.RLIMIT_CPU, (soft_time, hard_time))
################################################################################
def run_experiment(experiment):
problem = experiment['problem']
task_name = problem['task'].name if SERIALIZE_TASK else problem['task']
trial = problem['trial']
policy = experiment['policy']
set_memory_limits()
if not VERBOSE:
sys.stdout = open(os.devnull, 'w')
stdout = sys.stdout
if not SERIAL:
current_wd = os.getcwd()
# trial_wd = os.path.join(current_wd, TEMP_DIRECTORY, '{}/'.format(os.getpid()))
trial_wd = os.path.join(current_wd, TEMP_DIRECTORY, 't={}_n={}_{}/'.format(
task_name, trial, name_from_policy(policy)))
safe_rm_dir(trial_wd)
ensure_dir(trial_wd)
os.chdir(trial_wd)
parser = create_parser()
args = parser.parse_args()
task_fn_from_name = {fn.__name__: fn for fn in TASKS_FNS}
task_fn = task_fn_from_name[task_name]
world = World(use_gui=SERIAL)
if SERIALIZE_TASK:
task_fn(world, fixed=args.fixed)
task = problem['task']
world.task = task
task.world = world
else:
# TODO: assumes task_fn is deterministic wrt task
task_fn(world, fixed=args.fixed)
problem['saver'].restore()
world._update_initial()
problem['task'] = task_name # for serialization
del problem['saver']
random.seed(hash((0, task_name, trial, time.time())))
numpy.random.seed(hash((1, task_name, trial, time.time())) % (2**32))
#seed1, seed2 = problem['seeds'] # No point unless you maintain the same random state per generator
#set_random_seed(seed1)
#set_random_seed(seed2)
#random.setstate(state1)
#numpy.random.set_state(state2)
reset_globals()
real_state = create_state(world)
#start_time = time.time()
#if has_gui():
# wait_for_user()
observation_fn = lambda belief: observe_pybullet(world)
transition_fn = lambda belief, commands: iterate_commands(real_state, commands, time_step=0)
outcome = dict(ERROR_OUTCOME)
try:
with timeout(MAX_TIME + TIME_BUFFER):
outcome = run_policy(task, args, observation_fn, transition_fn, max_time=MAX_TIME, **policy)
outcome['error'] = False
except KeyboardInterrupt:
raise KeyboardInterrupt()
except:
traceback.print_exc()
#outcome = {'error': True}
world.destroy()
if not SERIAL:
os.chdir(current_wd)
safe_rm_dir(trial_wd)
if not VERBOSE:
sys.stdout.close()
sys.stdout = stdout
result = {
'experiment': experiment,
'outcome': outcome,
}
return result
################################################################################
def create_problems(args):
task_fn_from_name = {fn.__name__: fn for fn in TASKS_FNS}
problems = []
for num in range(N_TRIALS):
for task_name in TASK_NAMES:
print('Trial: {} / {} | Task: {}'.format(num, N_TRIALS, task_name))
random.seed(hash((0, task_name, num, time.time())))
numpy.random.seed(wrap_numpy_seed(hash((1, task_name, num, time.time()))))
world = World(use_gui=False) # SERIAL
task_fn = task_fn_from_name[task_name]
task = task_fn(world, fixed=args.fixed)
task.world = None
if not SERIALIZE_TASK:
task = task_name
saver = WorldSaver()
problems.append({
'task': task,
'trial': num,
'saver': saver,
#'seeds': [get_random_seed(), get_numpy_seed()],
#'seeds': [random.getstate(), numpy.random.get_state()],
})
#print(world.body_from_name) # TODO: does not remain the same
#wait_for_user()
#world.reset()
#if has_gui():
# wait_for_user()
world.destroy()
return problems
################################################################################
def main():
parser = create_parser()
args = parser.parse_args()
print(args)
# https://stackoverflow.com/questions/15314189/python-multiprocessing-pool-hangs-at-join
# https://stackoverflow.com/questions/39884898/large-amount-of-multiprocessing-process-causing-deadlock
# TODO: alternatively don't destroy the world
num_cores = max(1, cpu_count() - SPARE_CORES)
json_path = os.path.abspath(os.path.join(EXPERIMENTS_DIRECTORY, '{}.json'.format(get_date())))
#memory_per_core = float(MAX_RAM) / num_cores # gigabytes
#set_soft_limit(resource.RLIMIT_AS, int(BYTES_PER_GIGABYTE * memory_per_core)) # bytes
#set_soft_limit(resource.RLIMIT_CPU, 2*MAX_TIME) # seconds
# RLIMIT_MEMLOCK, RLIMIT_STACK, RLIMIT_DATA
print('Results:', json_path)
print('Num Cores:', num_cores)
#print('Memory per Core: {:.2f}'.format(memory_per_core))
print('Tasks: {} | {}'.format(len(TASK_NAMES), TASK_NAMES))
print('Policies: {} | {}'.format(len(POLICIES), POLICIES))
print('Num Trials:', N_TRIALS)
num_experiments = len(TASK_NAMES) * len(POLICIES) * N_TRIALS
print('Num Experiments:', num_experiments)
max_parallel = math.ceil(float(num_experiments) / num_cores)
print('Estimated duration: {:.2f} hours'.format(MEAN_TIME_PER_TRIAL * max_parallel / HOURS_TO_SECS))
user_input('Begin?')
print(SEPARATOR)
print('Creating problems')
start_time = time.time()
problems = create_problems(args)
experiments = [{'problem': copy.deepcopy(problem), 'policy': policy} #, 'args': args}
for problem in problems for policy in POLICIES]
print('Created {} problems and {} experiments in {:.3f} seconds'.format(
len(problems), len(experiments), elapsed_time(start_time)))
print(SEPARATOR)
ensure_dir(EXPERIMENTS_DIRECTORY)
safe_rm_dir(TEMP_DIRECTORY)
ensure_dir(TEMP_DIRECTORY)
start_time = time.time()
results = []
try:
for result in map_parallel(run_experiment, experiments, num_cores=num_cores):
results.append(result)
print('{}\nExperiments: {} / {} | Time: {:.3f}'.format(
SEPARATOR, len(results), len(experiments), elapsed_time(start_time)))
print('Experiment:', str_from_object(result['experiment']))
print('Outcome:', str_from_object(result['outcome']))
write_json(json_path, results)
#except BaseException as e:
# traceback.print_exc() # e
finally:
if results:
write_json(json_path, results)
print(SEPARATOR)
print('Saved:', json_path)
print('Results:', len(results))
print('Duration / experiment: {:.3f}'.format(num_cores*elapsed_time(start_time) / len(experiments)))
print('Duration: {:.2f} hours'.format(elapsed_time(start_time) / HOURS_TO_SECS))
safe_rm_dir(TEMP_DIRECTORY)
# TODO: dump results automatically?
return results
# ./run_experiment.py 2>&1 | tee log.txt
if __name__ == '__main__':
main()
"""
_memory: 1571984.0, plan_time: 210.628690958, total_cost: 2075, total_time: 211.089640856}
WARNING: overflow on h^add! Costs clamped to 100000000
Traceback (most recent call last):
File "./run_experiment.py", line 202, in run_experiment
max_time=MAX_TIME, **policy)
File "/home/caelan/Programs/srlstream/src/policy.py", line 113, in run_policy
max_cost=plan_cost, replan_actions=defer_actions)
File "/home/caelan/Programs/srlstream/src/policy.py", line 34, in random_restart
plan, plan_cost, certificate = solve_pddlstream(belief, problem, args, max_time=remaining_time, **kwargs)
File "/home/caelan/Programs/srlstream/src/planner.py", line 167, in solve_pddlstream
search_sample_ratio=search_sample_ratio)
File "/home/caelan/Programs/srlstream/pddlstream/pddlstream/algorithms/focused.py", line 134, in solve_focused
File "/home/caelan/Programs/srlstream/pddlstream/pddlstream/algorithms/reorder.py", line 167, in reorder_stream_plan
ordering = dynamic_programming(nodes, valid_combine, stats_fn, **kwargs)
File "/home/caelan/Programs/srlstream/pddlstream/pddlstream/algorithms/reorder.py", line 127, in dynamic_programming
new_subset = frozenset([v]) | subset
MemoryError
"""