forked from xavierpuigf/online_watch_and_help
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
215 lines (187 loc) · 8.43 KB
/
main.py
File metadata and controls
215 lines (187 loc) · 8.43 KB
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
import pickle
from pathlib import Path
from agents.GnP_agent import GnP_agent
from agents.Human_agent import Human_agent
from agents.MCTS_agent import MCTS_agent
from arguments import get_args
from envs.arena import Arena
from envs.unity_environment import UnityEnvironment
from utils.utils_exception import check_unity_error, handle
from utils.utils_graph import fix_graph, fix_multiple_location
from utils.utils_logging import Saver
class Runner:
def __init__(self, args):
self.args = args
self._get_env_task_set()
self._get_saver()
self._get_agents()
self._get_env()
self.arena = Arena(self.env, self.agents, self.saver)
def _get_saver(self):
if self.args.num_agents == 1:
method = "single"
else:
if self.args.helper_class == "MCTS":
if self.args.helper_goal_type == "unknown":
raise ValueError("MCTS helper cannot infer goals")
elif self.args.helper_goal_type == "gt":
method_suffix = "oracle_goal"
elif self.args.helper_goal_type == "random":
method_suffix = "random_goal"
else:
raise ValueError(f"{self.args.helper_goal_type = }")
elif self.args.helper_class == "GnP":
proposer_name = self.args.autotom_proposer_name.split("/")[-1]
if self.args.autotom_method == "autotom":
estimator_name = self.args.autotom_estimator_name.split("/")[-1]
method_suffix = f"autotom_Q={proposer_name}_P={estimator_name}"
elif self.args.autotom_method == "llm":
method_suffix = proposer_name
else:
raise ValueError(f"{self.args.autotom_method = }")
elif self.args.helper_class == "Human":
method_suffix = "human"
else:
raise ValueError(f"{self.args.helper_class = }")
method = f"{self.args.helper_class}_{method_suffix}"
self.args.record_dir = (
Path(self.args.record_dir) / self.args.dataset_path.stem / method
)
self.args.record_dir.mkdir(parents=True, exist_ok=True)
self.saver = Saver(
logger_name=self.args.logger_name,
record_dir=self.args.record_dir,
save_img=dict(
camera_views=self.args.save_camera_views,
image_width=self.args.image_width,
image_height=self.args.image_height,
),
save_belief=False,
process_id=self.args.process_id,
)
def _get_env_task_set(self):
self.args.dataset_path = Path(self.args.dataset_path)
with self.args.dataset_path.open("rb") as f:
env_task_set = pickle.load(f)
env_task_set = fix_graph(env_task_set)
env_task_set = fix_multiple_location(env_task_set, verbose=False, drop_env=True)
self.env_task_set = env_task_set
def _get_agents(self):
args_agent_common = dict(
recursive=False,
max_episode_length=20, # MCTS:expand()
num_simulation=20,
max_rollout_steps=5,
c_init=0.1,
c_base=100,
num_samples=1,
logging=True,
logging_graphs=True,
agent_params=dict(
open_cost=0,
should_close=False,
walk_cost=0.05,
belief=dict(
forget_rate=0,
belief_type="uniform",
),
),
)
self.agents = []
for i in range(self.args.num_agents):
args_agent = dict(agent_id=i + 1, char_index=i, **args_agent_common)
args_agent["agent_params"]["obs_type"] = self.args.obs_type[i]
if self.args.debug:
args_agent["num_particles"] = (
1 if self.args.obs_type[i] == "full" else 3
)
args_agent["num_processes"] = 0
else:
num_particles = (
1 if self.args.obs_type[i] == "full" else self.args.num_particles
)
args_agent["num_particles"] = num_particles
args_agent["num_processes"] = num_particles
if i == 0 or self.args.helper_class == "MCTS":
self.agents.append(MCTS_agent(**args_agent))
else:
match self.args.helper_class:
case "GnP":
args_agent["autotom_args"] = dict(
filter_thres=self.args.autotom_thres_filter,
num_particles=self.args.autotom_num_particles,
proposer_name=self.args.autotom_proposer_name,
estimator_name=self.args.autotom_estimator_name,
method=self.args.autotom_method,
hide_helper_history=self.args.autotom_hide_helper_history,
disable_estimation=self.args.autotom_disable_estimation,
)
args_agent["agent_args"] = dict(
thres_grab=self.args.gnp_thres_grab,
thres_put=self.args.gnp_thres_put,
start_at_put=self.args.gnp_start_at_put,
)
self.agents.append(GnP_agent(**args_agent))
case "Human":
self.agents.append(Human_agent(**args_agent))
case _:
raise ValueError(f"Invalid config: {self.args.helper_class}")
def _get_env(self):
self.env = UnityEnvironment(
num_agents=len(self.agents),
max_episode_length=self.args.max_steps,
port_id=0,
convert_goal=True,
env_task_set=self.env_task_set,
observation_types=self.args.obs_type,
use_editor=self.args.use_editor,
executable_args=dict(
file_name=self.args.executable_file,
x_display=self.args.display,
no_graphics=False,
timeout_wait=30,
),
base_port=self.args.base_port,
)
def run(self):
if self.args.episode_ids is None:
self.args.episode_ids = list(range(len(self.env_task_set)))
if self.args.debug_len is not None:
self.args.episode_ids = self.args.episode_ids[: self.args.debug_len]
episode_ids = self.args.episode_ids
with self.saver.pbar as pbar:
pbar_run = pbar.add_task("run", total=self.args.num_runs)
for ith_run in range(self.args.num_runs):
self.saver.reset_run(ith_run)
if self.saver.run_path.exists():
pbar.update(pbar_run, advance=1)
continue
pbar_episode = pbar.add_task("episode", total=len(episode_ids))
for episode_id in episode_ids:
self.saver.reset_episode(episode_id, self.env_task_set[episode_id])
if not self.saver.episode_path.exists():
for ith_retry in range(self.args.num_retries):
if ith_retry != 0:
msg = f"retry {ith_retry}: {self.saver.current_episode}"
self.saver.warning(msg)
try:
self.arena.reset(
episode_id=episode_id,
helper_goal_type=self.args.helper_goal_type,
seed=len(self.agents) * ith_run * ith_retry,
)
success = self.arena.run()
if success:
break
except Exception as e:
e = check_unity_error(e)
handle(e, self.saver, exc_info=True)
self.saver.remove_pbar_task("step")
self.saver.save_episode()
pbar.update(pbar_episode, advance=1)
self.saver.save_run()
pbar.update(pbar_run, advance=1)
pbar.remove_task(pbar_episode)
if __name__ == "__main__":
runner = Runner(args=get_args())
runner.run()