-
Notifications
You must be signed in to change notification settings - Fork 2
/
problem_generator.py
133 lines (87 loc) · 3.69 KB
/
problem_generator.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
import random
import os
LOC_BASE_TYPE = "loc_base"
LOC_OBJ_TYPE = "loc_object"
OBJ_TYPE = "block"
OBJ_LOC_NAME = "table"
BASE_LOC_NAME = "table_anchor"
OBJ_NAME = "ball"
class ProblemGenerator(object):
def __init__(self, template_file):
self.template_file_ = template_file
def generate(self, n_object, n_samples=None, output_dir=None):
if n_samples is None:
n_samples = 1000
if output_dir is None:
output_dir = "problems"
os.system("mkdir -p {}".format(output_dir))
for i in range(n_samples):
filename = "{}/o{}_p{}.pddl".format(output_dir, n_object, i)
template = self.load_problem_template_()
objects_def, objects, object_locs, base_locs = self.gen_object_def_(n_object, n_object + 1)
init_config = self.gen_init_config_(objects, object_locs, base_locs)
goal_config = self.gen_goal_config_(objects, object_locs)
problem_def = template.format(objects_def, init_config, goal_config)
self.dump_(problem_def, filename)
def gen_object_def_(self, n_objects, n_locations):
objects_def = ""
objects = []
object_locs = []
base_locs = []
# object locations
for i in range(n_locations):
name = "{}_{}".format(OBJ_LOC_NAME, i)
object_locs.append(name)
objects_def += name + " "
objects_def += "- {}\n".format(LOC_OBJ_TYPE)
# robot locations
for i in range(n_locations):
name = "{}_{}".format(BASE_LOC_NAME, i)
base_locs.append(name)
objects_def += name + " "
objects_def += "- {}\n".format(LOC_BASE_TYPE)
# objects
for i in range(n_objects):
name = "{}_{}".format(OBJ_NAME, i)
objects.append(name)
objects_def += name + " "
objects_def += "- {}".format(OBJ_TYPE)
return objects_def, objects, object_locs, base_locs
def gen_init_config_(self, objects, object_locs, base_locs):
config = ""
objects_shuffled = self.shuffle_(objects)
obj_locs_shuffled = self.shuffle_(object_locs)
# set robot init location
config += "(at_base {})\n".format(random.choice(base_locs))
# set object initial locations
for i in range(len(objects)):
config += "(at_object {} {})\n".format(objects_shuffled[i], obj_locs_shuffled[i])
config += "(occupied {})\n".format(obj_locs_shuffled[i])
# set object location and anchor location pair
for i in range(len(object_locs)):
config += "(reachable {} {})\n".format(object_locs[i], base_locs[i])
return config
def gen_goal_config_(self, objects, object_locs):
config = ""
objects_shuffled = self.shuffle_(objects)
obj_locs_shuffled = self.shuffle_(object_locs)
for i in range(len(objects)):
config += "(at_object {} {})\n".format(objects_shuffled[i], obj_locs_shuffled[i])
return config
def load_problem_template_(self):
template = ""
with open(self.template_file_, "r") as fin:
for line in fin:
template += line
return template
def dump_(self, prob_def, filename):
with open(filename, "w") as fout:
fout.write(prob_def)
print("Problem defintion was saved at: `{}`".format(filename))
def shuffle_(self, target):
t_shuffled = target[:]
random.shuffle(t_shuffled)
return t_shuffled
if __name__ == "__main__":
pg = ProblemGenerator("problem_template.pddl")
pg.generate(10, 1)