-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_scrubed_problems.py
216 lines (185 loc) · 7.22 KB
/
generate_scrubed_problems.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
"""
Use SCRUB to generate reduced 3D scene graph problem files for use with planning problems.
"""
import argparse
import json
import os
import pickle
import time
import warnings
import pddlgym
import torch
from torch.utils.data import DataLoader
from tqdm import tqdm, trange
from ploi.argparsers import get_ploi_argument_parser
from ploi.datautils import (
collect_training_data,
create_graph_dataset,
create_graph_dataset_hierarchical,
GraphDictDataset,
)
from ploi.guiders import SceneGraphGuidance
from ploi.modelutils import GraphNetwork
from ploi.planning import FD
# from ploi.planning.incremental_hierarchical_planner import IncrementalHierarchicalPlanner
from ploi.planning.scenegraph_planner import SceneGraphPlanner
from ploi.planning.scrubber import SCRUBber
from ploi.traineval import test_planner
def _create_planner(planner_name):
if planner_name == "fd-lama-first":
return FD(alias_flag="--alias lama-first")
if planner_name == "fd-opt-lmcut":
return FD(alias_flag="--alias seq-opt-lmcut")
raise ValueError(f"Uncrecognized planner name {planner_name}")
def scrub_problems_in_domain(
planner,
domain_name,
num_problems,
timeout,
savedir,
mode="test",
debug_mode=False,
all_problems=False,
no_scrub=False,
write_domain_file=False,
):
print(f"Writing scrubbed domains with prune = {not no_scrub}")
# In debug mode, use train problems for testing too (False by default)
env = None
if mode == "test":
env = pddlgym.make("PDDLEnv{}Test-v0".format(domain_name))
else:
env = pddlgym.make("PDDLEnv{}-v0".format(domain_name))
if debug_mode:
warnings.warn(
"WARNING: Running in debug mode (i.e., testing on train problems)"
)
env = pddlgym.make("PDDLEnv{}-v0".format(domain_name))
num_problems = min(num_problems, len(env.problems))
# If `all_problems` is set to True, override num_problems
if all_problems:
num_problems = len(env.problems)
if write_domain_file:
domain_file_name = env.domain.domain_fname
new_domain_name = env.domain.domain_name.lower() + "scrub"
print(domain_file_name, new_domain_name)
fp = open(domain_file_name, "r")
lines = fp.readlines()
try:
lines[1] = lines[1].replace(env.domain.domain_name.lower(), new_domain_name)
except Exception as e:
pass
fp.close()
parent_of_savedir = os.path.dirname(savedir)
fp = open(os.path.join(parent_of_savedir, f"{new_domain_name}.pddl"), "w")
for l in lines:
fp.write(l)
fp.close()
stats_to_log = ["num_node_expansions", "plan_length", "search_time", "total_time"]
num_timeouts = 0
num_failures = 0
num_invalidated_plans = 0
run_stats = []
for problem_idx in trange(num_problems):
env.fix_problem_index(problem_idx)
state, _ = env.reset()
domain_file = env.domain.domain_fname
problem_file = env.problems[problem_idx].problem_fname
problem_unique_name = env.problems[problem_idx].problem_name
planner(
env.domain,
state,
timeout,
problem_file,
savedir,
domain_file_global=env._domain_file,
no_scrub=no_scrub,
problem_unique_name=problem_unique_name,
)
def _scrub_domain(args):
# Seed RNG
torch.manual_seed(args.seed)
# Create dir to log files to
args.expdir = os.path.join(args.logdir, args.expid)
if not os.path.exists(args.expdir):
os.makedirs(args.expdir, exist_ok=True)
# Directory to write the domain files to
args.savedir = f"{args.logdir}/scrubbed_domains/{args.domain.lower()}scrub"
if args.mode == "test":
args.savedir = args.savedir + "_test"
if not os.path.exists(args.savedir):
os.makedirs(args.savedir, exist_ok=True)
print(f"Writing scrubbed problems to {args.savedir}")
# Capitalize the first letter of the domain name
args.domain = args.domain.capitalize()
# This datafile is the same for ploi and hierarchical variants
args.datafile = os.path.join(args.logdir, f"ploi_{args.domain}.pkl")
print(f"Domain: {args.domain}")
print(f"Train planner: {args.train_planner_name}")
print(f"Test planner: {args.eval_planner_name}")
eval_planner = _create_planner(args.eval_planner_name)
is_strips_domain = True
train_planner = _create_planner(args.train_planner_name)
training_data = None
print("Collecting training data")
if not os.path.exists(args.datafile) or args.force_collect_data:
training_data = collect_training_data(
args.domain, train_planner, num_train_problems=args.num_train_problems
)
with open(args.datafile, "wb") as f:
pickle.dump(training_data, f)
else:
print("Training data already found on disk")
with open(args.datafile, "rb") as f:
print("Loading training data from file")
training_data = pickle.load(f)
graphs_inp, graphs_tgt, graph_metadata = create_graph_dataset(training_data)
scenegraph_guidance = SceneGraphGuidance(graph_metadata)
planner_to_eval = SCRUBber(
is_strips_domain=is_strips_domain,
base_planner=eval_planner,
guidance=scenegraph_guidance,
)
no_scrub = False
write_domain_file = False
if args.mode == "train":
no_scrub = True # Do not prune in train mode (we need to retain extraneous objects to train PLOI variants)
write_domain_file = True
else:
write_domain_file = False
no_scrub = False
scrub_problems_in_domain(
planner_to_eval,
args.domain,
args.num_test_problems,
args.timeout,
args.savedir,
args.mode,
all_problems=args.all_problems,
no_scrub=no_scrub,
write_domain_file=write_domain_file,
)
if __name__ == "__main__":
parser = get_ploi_argument_parser()
parser.add_argument(
"--all-problems", action="store_true", help="SCRUB all problems in domain"
)
args = parser.parse_args()
domains_to_scrub = [
"taskographyv2tiny1", # 'taskographyv2medium1', 'taskographyv2tiny2', 'taskographyv2medium2',
# 'taskographyv2tiny10', 'taskographyv2medium10', 'taskographyv3tiny10bagslots10', 'taskographyv3medium10bagslots10',
# 'taskographyv3tiny10bagslots3', 'taskographyv3medium10bagslots3', 'taskographyv3tiny10bagslots5', 'taskographyv3medium10bagslots5',
# 'taskographyv3tiny10bagslots7', 'taskographyv3medium10bagslots7',
# 'taskographyv4tiny5', 'taskographyv4medium5', 'taskographyv5tiny5bagslots5', 'taskographyv5medium5bagslots5'
]
# (Un)comment this to generate scrubbed domains for all entries in domains_to_scrub
for d in domains_to_scrub:
args.domain = d
for mode in ["train", "test"]:
args.mode = mode
_scrub_domain(args)
# (Un)comment this to generate entries for __init__.py in pddlgym (to register these envs)
for d in domains_to_scrub:
name = d.lower() + "scrub"
PDDL_GYM_STRING = f"(\n \"{name}\",\n {{\n 'operators_as_actions': True,\n 'dynamic_action_space':True,\n }}\n),"
print(PDDL_GYM_STRING)