-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv_manager.py
More file actions
728 lines (619 loc) · 30.3 KB
/
env_manager.py
File metadata and controls
728 lines (619 loc) · 30.3 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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
from typing import List, Tuple, Dict, Union, Any
from collections import defaultdict
import torch
import numpy as np
from functools import partial
import os
from copy import deepcopy
from agent_system.environments.prompts import *
from agent_system.environments.base import EnvironmentManagerBase, to_numpy
from agent_system.memory import SimpleMemory
def parse_gamefile(infos):
gamefile = []
for info in infos:
if 'extra.gamefile' in info:
gamefile.append(info['extra.gamefile'])
else:
gamefile.append(None)
return gamefile
def set_gamefile(infos, gamefile):
for i in range(len(infos)):
if 'extra.gamefile' in infos[i]:
infos[i]['extra.gamefile'] = gamefile[i]
else:
infos[i]['extra.gamefile'] = None
return infos
class AlfWorldEnvironmentManager(EnvironmentManagerBase):
def __init__(self, envs, projection_f, config):
self.memory = SimpleMemory()
super().__init__(envs, projection_f, config)
def reset(self):
text_obs, image_obs, infos = self.envs.reset()
self.gamefile = parse_gamefile(infos)
# initialize the history buffer
self.memory.reset(batch_size = len(text_obs))
self.tasks = []
self.pre_text_obs = text_obs
self.extract_task(text_obs)
full_text_obs = self.build_text_obs(text_obs, self.envs.get_admissible_commands, init=True)
return {'text': full_text_obs, 'image': image_obs, 'anchor': text_obs}, infos
def step(self, text_actions: List[str]):
actions, valids = self.projection_f(text_actions, self.envs.get_admissible_commands)
text_obs, image_obs, rewards, dones, infos = self.envs.step(actions)
self.memory.store({'text_obs': self.pre_text_obs, 'action': actions})
self.pre_text_obs = text_obs
full_text_obs = self.build_text_obs(text_obs, self.envs.get_admissible_commands)
if infos[0].get("extra.gamefile") is None:
infos = set_gamefile(infos, self.gamefile)
# add action_valid to infos
for i, info in enumerate(infos):
info['is_action_valid'] = to_numpy(valids[i])
next_observations = {'text': full_text_obs, 'image': image_obs, 'anchor': text_obs}
rewards = to_numpy(rewards)
dones = to_numpy(dones)
return next_observations, rewards, dones, infos
def extract_task(self, text_obs: List[str]):
for obs in text_obs:
task_start = obs.find('Your task is to: ')
if task_start != -1:
self.tasks.append(obs[task_start + len('Your task is to: '):].strip())
else:
raise ValueError("Task description not found in text observation.")
def build_text_obs(self, text_obs: List[str], admissible_actions: List[List[str]], init: bool = False) -> List[str]:
"""
This function builds the text observation for the agent.
"""
postprocess_text_obs = []
if not init and self.config.env.history_length > 0:
memory_contexts, valid_lens = self.memory.fetch(
self.config.env.history_length,
obs_key="text_obs",
action_key="action")
for i in range(len(text_obs)):
# exclude 'help' in admissible_actions[i]
reformatted_admissible_actions = "\n ".join(f"'{s}'" for s in admissible_actions[i] if s != 'help')
if init or self.config.env.history_length <= 0:
obs = ALFWORLD_TEMPLATE_NO_HIS.format(
current_observation=text_obs[i],
admissible_actions=reformatted_admissible_actions
)
else:
obs = ALFWORLD_TEMPLATE.format(
task_description=self.tasks[i],
step_count=len(self.memory[i]),
history_length=valid_lens[i],
action_history=memory_contexts[i],
current_step=len(self.memory[i]) + 1,
current_observation=text_obs[i],
admissible_actions=reformatted_admissible_actions
)
postprocess_text_obs.append(obs)
return postprocess_text_obs
def _process_batch(self, batch_idx, total_batch_list, total_infos, success):
# Find the last entry with active masks
for i in reversed(range(len(total_batch_list[batch_idx]))):
batch_item = total_batch_list[batch_idx][i]
if batch_item['active_masks']:
info = total_infos[batch_idx][i]
won_value = float(info['won'])
success['success_rate'].append(won_value)
# Process game file if it exists
gamefile = info.get("extra.gamefile")
if gamefile:
self._process_gamefile(gamefile, won_value, success)
return # Exit after finding the first active mask
def _process_gamefile(self, gamefile, won_value, success):
tasks = [
"pick_and_place",
"pick_two_obj_and_place",
"look_at_obj_in_light",
"pick_heat_then_place_in_recep",
"pick_cool_then_place_in_recep",
"pick_clean_then_place_in_recep",
]
for task in tasks:
if task in gamefile:
success[f"{task}_success_rate"].append(won_value)
break
class SokobanEnvironmentManager(EnvironmentManagerBase):
ACTION_LOOKUP = {
0: "Still",
1: "Up",
2: "Down",
3: "Left",
4: "Right",
}
def __init__(self, envs, projection_f, config):
self.is_multi_modal = envs.mode == 'rgb_array'
self.memory = SimpleMemory()
super().__init__(envs, projection_f, config)
def reset(self):
obs, infos = self.envs.reset()
if self.is_multi_modal:
obs = np.array(obs, obs[0].dtype)
self.pre_text_obs = self.envs.render(mode='tiny_rgb_array')
observations = {
'text': self.build_text_obs(infos, init=True),
'image': obs,
'anchor': obs
}
else:
self.pre_text_obs = obs
observations = {
'text': self.build_text_obs(infos, obs, init=True),
'image': None,
'anchor': obs
}
self.memory.reset(batch_size = len(infos))
return observations, infos
def step(self, text_actions: List[str]):
actions, valids = self.projection_f(text_actions)
next_obs, rewards, dones, infos = self.envs.step(actions)
for i, info in enumerate(infos):
info['is_action_valid'] = to_numpy(valids[i])
self.memory.store({'text_obs': self.pre_text_obs, 'action': [self.ACTION_LOOKUP[act] for act in actions]})
if self.is_multi_modal:
next_obs = np.array(next_obs, next_obs[0].dtype)
self.pre_text_obs = self.envs.render(mode='tiny_rgb_array')
next_observations = {
'text': self.build_text_obs(infos),
'image': next_obs,
'anchor': next_obs
}
else:
self.pre_text_obs = next_obs
next_observations = {
'text': self.build_text_obs(infos, next_obs),
'image': None,
'anchor': next_obs
}
rewards = to_numpy(rewards)
dones = to_numpy(dones)
return next_observations, rewards, dones, infos
def build_text_obs(self, infos, text_obs: List[str]=None, init: bool = False) -> List[str]:
"""
This function builds the text observation for the agent.
"""
postprocess_text_obs = []
if not init and self.config.env.history_length > 0:
memory_contexts, valid_lens = self.memory.fetch(
self.config.env.history_length,
obs_key="text_obs",
action_key="action")
for i in range(len(infos)):
if init or self.config.env.history_length <= 0:
obs = SOKOBAN_VISUAL_TEMPLATE if self.is_multi_modal \
else SOKOBAN_TEMPLATE_NO_HIS.format(
current_observation=text_obs[i],
)
else:
if self.is_multi_modal:
obs = SOKOBAN_VISUAL_TEMPLATE
else:
obs = SOKOBAN_TEMPLATE.format(
step_count=len(self.memory[i]),
history_length=valid_lens[i],
action_history=memory_contexts[i],
current_step=len(self.memory[i]) + 1,
current_observation=text_obs[i],
)
postprocess_text_obs.append(obs)
return postprocess_text_obs
class GymCardEnvironmentManager(EnvironmentManagerBase):
def __init__(self, envs, projection_f, config):
super().__init__(envs, projection_f, config)
def reset(self) -> Dict[str, Any]:
obs, infos = self.envs.reset()
# infos = [None] * self.envs.num_envs
observations = {'text': self.build_text_obs(infos), 'image': obs, 'anchor': obs.copy()}
return observations, infos
def step(self, text_actions: List[str]):
next_observations, rewards, dones, infos = super().step(text_actions)
# add text observation to next_observations
next_observations['text'] = self.build_text_obs(infos)
next_observations['anchor'] = next_observations['image'].copy()
return next_observations, rewards, dones, infos
def build_text_obs(self, infos: Tuple[Dict]=None) -> List[str]:
"""
This function builds the text observation for the agent.
"""
postprocess_text_obs = []
for i in range(len(infos)):
if 'ezpoints' in self.config.env.env_name.lower():
text_formula = ''.join(str(element) for element in infos[i]['Formula']) if infos[i] is not None else ''
obs = GYM_CARDS_EZPOINTS_TEMPLATE.format(text_formula=text_formula)
elif 'points24' in self.config.env.env_name.lower():
text_formula = ''.join(str(element) for element in infos[i]['Formula']) if infos[i] is not None else ''
obs = GYM_CARDS_POINTS24_TEMPLATE.format(text_formula=text_formula)
elif 'numberline' in self.config.env.env_name.lower():
obs = GYM_CARDS_NUMBERLINE_TEMPLATE
elif "blackjack" in self.config.env.env_name.lower():
obs = GYM_CARDS_BLACKJACK_TEMPLATE
else:
raise ValueError(f"Unsupported environment: {self.config.env.env_name}")
postprocess_text_obs.append(obs)
return postprocess_text_obs
class WebshopEnvironmentManager(EnvironmentManagerBase):
def __init__(self, envs, projection_f, config):
self.memory = SimpleMemory()
super().__init__(envs, projection_f, config)
def reset(self) -> Dict[str, Any]:
obs, infos = self.envs.reset()
self.tasks = self.extract_task(obs)
obs = self.format_obs(obs)
# infos = [None] * self.envs.num_envs
observations = {'text': self.build_text_obs(obs, infos, init=True),
'image': None,
'anchor': obs.copy()
}
self.pre_text_obs = obs
self.memory.reset(batch_size = len(infos))
return observations, infos
def step(self, text_actions: List[str]):
actions, valids = self.projection_f(text_actions)
next_obs, rewards, dones, infos = self.envs.step(actions)
next_obs = self.format_obs(next_obs)
self.memory.store({'text_obs': self.pre_text_obs, 'action': actions})
self.pre_text_obs = next_obs
next_observations = {
'text': self.build_text_obs(next_obs, infos),
'image': None,
'anchor': next_obs.copy()
}
# add action_valid to infos
for i, info in enumerate(infos):
info['is_action_valid'] = to_numpy(valids[i])
rewards = to_numpy(rewards)
dones = to_numpy(dones)
return next_observations, rewards, dones, infos
def extract_task(self, text_obs: List[str]):
tasks = []
for obs in text_obs:
parts = obs.split(" [SEP] ")
assert parts[1]=='Instruction:'
tasks.append(parts[2])
return tasks
def format_obs(self, text_obs):
postprocess_text_obs = []
for i in range(len(text_obs)):
parts = text_obs[i].split(" [SEP] ")
# the index of self.tasks[i] in parts
try:
index = parts.index(self.tasks[i])
reformatted_obs = " [SEP] ".join(f"'{p}'" for p in parts[index+1:])
except:
reformatted_obs = text_obs[i]
postprocess_text_obs.append(reformatted_obs)
return postprocess_text_obs
def format_avail_actions(self, avail):
actions = []
for key in avail.keys():
if key not in ["has_search_bar", "clickables"]:
raise ValueError(f"Unknown key in available actions: {key}")
if avail["has_search_bar"]:
actions.append("search[<your query>]")
for txt in avail["clickables"]:
actions.append(f"click[{txt}]")
return actions
def build_text_obs(self, text_obs: List[str], infos: List[List[str]], init: bool = False) -> List[str]:
"""
This function builds the text observation for the agent.
"""
postprocess_text_obs = []
if not init and self.config.env.history_length > 0:
memory_contexts, valid_lens = self.memory.fetch(
self.config.env.history_length,
obs_key="text_obs",
action_key="action")
for i in range(len(text_obs)):
available_actions = self.format_avail_actions(infos[i]['available_actions'])
reformatted_available_actions = "\n".join(f"'{s}'," for s in available_actions)
if init or self.config.env.history_length <= 0:
obs = WEBSHOP_TEMPLATE_NO_HIS.format(
task_description=self.tasks[i],
current_observation=text_obs[i],
available_actions=reformatted_available_actions
)
else:
obs = WEBSHOP_TEMPLATE.format(
task_description=self.tasks[i],
step_count=len(self.memory[i]),
history_length=valid_lens[i],
action_history=memory_contexts[i],
current_step=len(self.memory[i]) + 1,
current_observation=text_obs[i],
available_actions=reformatted_available_actions
)
if len(obs) > 13000:
print(f"Warning len(obs)={len(obs)} is too long")
obs = WEBSHOP_TEMPLATE_NO_HIS.format(
task_description=self.tasks[i],
current_observation=text_obs[i],
available_actions=reformatted_available_actions
)
postprocess_text_obs.append(obs)
return postprocess_text_obs
def _process_batch(self, batch_idx, total_batch_list, total_infos, success):
for i in reversed(range(len(total_batch_list[batch_idx]))):
batch_item = total_batch_list[batch_idx][i]
if batch_item['active_masks']:
info = total_infos[batch_idx][i]
won_value = float(info['won'])
score_value = float(info['task_score'])
success['success_rate'].append(won_value)
success['webshop_task_score (not success_rate)'].append(score_value)
return
class AppWorldEnvironmentManager(EnvironmentManagerBase):
def __init__(self, envs, projection_f, config):
self.memory = SimpleMemory()
super().__init__(envs, projection_f, config)
def reset(self):
text_obs, infos = self.envs.reset()
self.supervisors = [info['supervisor'] for info in infos]
self.memory.reset(batch_size = len(text_obs))
self.tasks = text_obs.copy()
self.pre_text_obs = text_obs
full_text_obs = self.build_text_obs(text_obs, init=True)
return {'text': full_text_obs, 'image': None, 'anchor': text_obs}, infos
def step(self, text_actions: List[str]):
actions, valids = self.projection_f(text_actions)
text_obs, rewards, dones, infos = self.envs.step(actions)
self.memory.store({'text_obs': text_obs, 'action': actions})
self.pre_text_obs = text_obs
full_text_obs = self.build_text_obs(text_obs)
# add action_valid to infos
for i, info in enumerate(infos):
info['is_action_valid'] = to_numpy(valids[i])
next_observations = {'text': full_text_obs, 'image': None, 'anchor': text_obs}
rewards = to_numpy(rewards)
dones = to_numpy(dones)
return next_observations, rewards, dones, infos
def build_text_obs(self, text_obs: List[str], init: bool = False) -> List[str]:
"""
This function builds the text observation for the agent.
"""
postprocess_text_obs = []
if init and self.supervisors is not None:
for i in range(len(text_obs)):
obs = APPWORLD_TEMPLATE_NO_HIS.format(
supervisor_first_name=self.supervisors[i]['first_name'],
supervisor_last_name=self.supervisors[i]['last_name'],
supervisor_email=self.supervisors[i]['email'],
supervisor_phone_number=self.supervisors[i]['phone_number'],
task_description=self.tasks[i],
)
postprocess_text_obs.append(obs)
else:
for i in range(len(text_obs)):
# Get last `history_length` steps
recent_history = self.memory[i][-self.config.env.history_length:]
valid_history_length = len(recent_history)
start_index = len(self.memory[i]) - valid_history_length
action_history = ""
for j, record in enumerate(recent_history):
step_number = start_index + j + 1
action = record["action"]
env_obs = record["text_obs"]
action_history += f"\nCode {step_number}: \n{action}\n\nResult {step_number}: \n{env_obs}\n"
if len(action_history) > 10000:
action_history = "... " + action_history[-10000:]
obs = APPWORLD_TEMPLATE.format(
supervisor_first_name=self.supervisors[i]['first_name'],
supervisor_last_name=self.supervisors[i]['last_name'],
supervisor_email=self.supervisors[i]['email'],
supervisor_phone_number=self.supervisors[i]['phone_number'],
task_description=self.tasks[i],
step_count=len(self.memory[i]),
history_length=valid_history_length,
action_history=action_history.strip(),
current_step=len(self.memory[i]) + 1,
current_observation=text_obs[i],
)
postprocess_text_obs.append(obs)
return postprocess_text_obs
class TravelPlannerEnvironmentManager(EnvironmentManagerBase):
DAY_PLAN_TEMPLATE = {
"days": None,
"current_city": "-",
"transportation": "-",
"breakfast": "-",
"attraction": "-",
"lunch": "-",
"dinner": "-",
"accommodation": "-",
}
def __init__(self, envs, projection_f, config):
self.memory = SimpleMemory()
self.queries = None
super().__init__(envs, projection_f, config)
def _generate_empty_plan(self, day_count: int) -> str:
"""
Generate an empty plan for the given number of days.
"""
res = []
for day in range(1, day_count + 1):
day_plan = deepcopy(self.DAY_PLAN_TEMPLATE)
day_plan["days"] = day
res.append(day_plan)
return str(res).replace("'", '"') # Convert to JSON-like string format
def reset(self):
obs, infos = self.envs.reset()
self.queries = [info["info"] for info in infos]
self.pre_text_obs = obs
self.memory.reset(batch_size=len(obs))
full_text_obs = self.build_text_obs(
cur_obs=obs,
plans=[self._generate_empty_plan(query["days"]) for query in self.queries],
init=True,
)
return {"text": full_text_obs, "image": None, "anchor": obs}, infos
def step(self, text_actions: List[str]):
valids, actions, thoughts, plans = self.projection_f(text_actions)
obs, rewards, dones, infos = self.envs.step(text_actions)
actions = ["<action>" + action + "</action>" for action in actions]
self.memory.store({"text_obs": self.pre_text_obs, "action": actions})
self.pre_text_obs = obs
print(f"extract plan[0]: {plans[0]}")
full_text_obs = self.build_text_obs(cur_obs=obs, plans=plans, init=False)
for i, info in enumerate(infos):
info["format_reward"] = valids[i]
next_observations = {"text": full_text_obs, "image": None, "anchor": obs}
rewards = to_numpy(rewards)
rewards = rewards + to_numpy(valids) # add 1 for valid text actions
dones = to_numpy(dones)
return next_observations, rewards, dones, infos
def build_text_obs(
self,
cur_obs: List[str],
plans: List[str],
init: bool = False,
) -> List[str]:
if init:
postprocess_text_obs = [
TRAVELPLANNER_ZEROSHOT_REACT_INSTRUCTION.format(
query=self.queries[i]["query"],
plan=plans[i],
history="",
observation="",
)
for i in range(len(self.queries))
]
else:
histories, _ = self.memory.fetch(
0,
obs_key="text_obs",
action_key="action",
)
postprocess_text_obs = []
for i in range(len(cur_obs)):
obs = TRAVELPLANNER_ZEROSHOT_REACT_INSTRUCTION.format(
query=self.queries[i]["query"],
plan=plans[i],
history=histories[i],
observation=cur_obs[i],
)
postprocess_text_obs.append(obs)
# print(f"postprocess_text_obs[0]: {postprocess_text_obs[0]}")
return postprocess_text_obs
def _process_batch(self, batch_idx, total_batch_list, total_infos, success):
for i in reversed(range(len(total_batch_list[batch_idx]))):
batch_item = total_batch_list[batch_idx][i]
if batch_item["active_masks"]:
info = total_infos[batch_idx][i]
won_value = float(info["won"])
valid_action_ratio = float(info["valid_action_ratio"])
success["success_rate"].append(won_value)
success["valid_action_ratio (not score or success_rate)"].append(
valid_action_ratio
)
success["plan_reward (not score or success_rate)"].append(
float(info["plan_reward"])
)
success["format_reward (not score or success_rate)"].append(
float(info["format_reward"])
)
return
def make_envs(config):
"""
Create enviroments
"""
# check if config.env.rollout.n is an integer
if not isinstance(config.env.rollout.n, int):
raise ValueError("config.env.rollout.n should be an integer")
group_n = config.env.rollout.n if config.env.rollout.n > 0 else 1
if "gym_cards" in config.env.env_name.lower():
from agent_system.environments.env_package.gym_cards import build_gymcards_envs, gym_projection
_envs = build_gymcards_envs(env_name=config.env.env_name, seed=config.env.seed, env_num=config.data.train_batch_size, group_n=group_n, is_train=True)
_val_envs = build_gymcards_envs(env_name=config.env.env_name, seed=config.env.seed + 1000, env_num=config.data.val_batch_size, group_n=1, is_train=False)
projection_f = partial(gym_projection, env_name=config.env.env_name)
envs = GymCardEnvironmentManager(_envs, projection_f, config)
val_envs = GymCardEnvironmentManager(_val_envs, projection_f, config)
return envs, val_envs
elif "alfworld" in config.env.env_name.lower():
from agent_system.environments.env_package.alfworld import build_alfworld_envs, alfworld_projection
if config.env.env_name == 'alfworld/AlfredThorEnv':
alf_config_path = os.path.join(os.path.dirname(__file__), 'env_package/alfworld/configs/config_tw.yaml')
elif config.env.env_name == 'alfworld/AlfredTWEnv':
alf_config_path = os.path.join(os.path.dirname(__file__), 'env_package/alfworld/configs/config_tw.yaml')
else:
raise ValueError(f"Unsupported environment: {config.env.env_name}")
env_kwargs = {
'eval_dataset': 'eval_in_distribution', # 'eval_in_distribution' or 'eval_out_of_distribution'
}
_envs = build_alfworld_envs(alf_config_path, config.env.seed, config.data.train_batch_size, group_n, is_train=True, env_kwargs=env_kwargs)
_val_envs = build_alfworld_envs(alf_config_path, config.env.seed + 1000, config.data.val_batch_size, 1, is_train=False, env_kwargs=env_kwargs)
projection_f = partial(alfworld_projection)
envs = AlfWorldEnvironmentManager(_envs, projection_f, config)
val_envs = AlfWorldEnvironmentManager(_val_envs, projection_f, config)
return envs, val_envs
elif "sokoban" in config.env.env_name.lower():
from agent_system.environments.env_package.sokoban import build_sokoban_envs, sokoban_projection
env_kwargs = {
'dim_room': config.env.sokoban.dim_room,
'num_boxes': config.env.sokoban.num_boxes,
'max_steps': config.env.max_steps,
'search_depth': config.env.sokoban.search_depth
}
_envs = build_sokoban_envs(config.env.seed, config.data.train_batch_size, group_n, mode=config.env.sokoban.mode, is_train=True, env_kwargs=env_kwargs)
_val_envs = build_sokoban_envs(config.env.seed + 1000, config.data.val_batch_size, 1, mode=config.env.sokoban.mode, is_train=False, env_kwargs=env_kwargs)
projection_f = partial(sokoban_projection)
envs = SokobanEnvironmentManager(_envs, projection_f, config)
val_envs = SokobanEnvironmentManager(_val_envs, projection_f, config)
return envs, val_envs
elif "webshop" in config.env.env_name.lower():
from agent_system.environments.env_package.webshop import build_webshop_envs, webshop_projection
if config.env.webshop.use_small:
file_path = os.path.join(os.path.dirname(__file__), 'env_package/webshop/webshop/data/items_shuffle_1000.json')
attr_path = os.path.join(os.path.dirname(__file__), 'env_package/webshop/webshop/data/items_ins_v2_1000.json')
else:
file_path = os.path.join(os.path.dirname(__file__), 'env_package/webshop/webshop/data/items_shuffle.json')
attr_path = os.path.join(os.path.dirname(__file__), 'env_package/webshop/webshop/data/items_ins_v2.json')
env_kwargs = {
'observation_mode': 'text',
'num_products': None,
'human_goals': config.env.webshop.human_goals,
'file_path': file_path,
'attr_path': attr_path
}
_envs = build_webshop_envs(seed=config.env.seed, env_num=config.data.train_batch_size, group_n=group_n, is_train=True, env_kwargs=env_kwargs)
_val_envs = build_webshop_envs(seed=config.env.seed + 1000, env_num=config.data.val_batch_size, group_n=1, is_train=False, env_kwargs=env_kwargs)
projection_f = partial(webshop_projection)
envs = WebshopEnvironmentManager(_envs, projection_f, config)
val_envs = WebshopEnvironmentManager(_val_envs, projection_f, config)
import time
time.sleep((config.data.train_batch_size * group_n + config.data.val_batch_size) * 0.1) # wait for the envs to be ready
return envs, val_envs
elif "appworld" in config.env.env_name.lower():
from agent_system.environments.env_package.appworld import build_appworld_envs, appworld_projection
_envs = build_appworld_envs(dataset_name='train', seed=config.env.seed, env_num=config.data.train_batch_size, group_n=group_n, start_server_id=0)
_val_envs = build_appworld_envs(dataset_name='test_normal', seed=config.env.seed + 1000, env_num=config.data.val_batch_size, group_n=1, start_server_id=config.data.train_batch_size*group_n)
projection_f = partial(appworld_projection)
envs = AppWorldEnvironmentManager(_envs, projection_f, config)
val_envs = AppWorldEnvironmentManager(_val_envs, projection_f, config)
return envs, val_envs
elif "travelplanner" in config.env.env_name.lower():
from agent_system.environments.env_package.travelplanner import (
build_travelplanner_envs,
travelplanner_projection,
)
_envs = build_travelplanner_envs(
seed=config.env.seed,
env_num=config.data.train_batch_size,
group_n=group_n,
is_train=True,
env_kwargs={"split": "train"},
)
_val_envs = build_travelplanner_envs(
seed=config.env.seed + 1000,
env_num=config.data.val_batch_size,
group_n=1,
is_train=False,
env_kwargs={"split": "validation"},
)
projection_f = partial(travelplanner_projection)
envs = TravelPlannerEnvironmentManager(_envs, projection_f, config)
val_envs = TravelPlannerEnvironmentManager(_val_envs, projection_f, config)
return envs, val_envs
else:
print("Environment not supported")
exit(1)