-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvs.py
More file actions
240 lines (191 loc) · 8.03 KB
/
envs.py
File metadata and controls
240 lines (191 loc) · 8.03 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
import ray
import gym
import numpy as np
# -----------------------------------------------------------------------------
# Ray remote worker actor -----------------------------------------------------
# -----------------------------------------------------------------------------
@ray.remote(num_cpus=0.2)
class WebshopWorker:
"""Ray remote actor that replaces the worker function.
Each actor hosts a *WebAgentTextEnv* instance.
"""
def __init__(self, seed, env_kwargs):
# Lazy import avoids CUDA initialisation issues
import sys
import os
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), 'webshop'))
sys.path.append(project_root)
from web_agent_site.envs import WebAgentTextEnv # noqa: WPS433 (runtime import)
env_kwargs['seed'] = seed
self.env = gym.make('WebAgentTextEnv-v0', **env_kwargs)
def step(self, action):
"""Execute a step in the environment"""
obs, reward, done, info = self.env.step(action)
info = dict(info or {}) # make a *copy* so we can mutate safely
info['available_actions'] = self.env.get_available_actions()
info['task_score'] = reward
# Redefine reward. We only use rule-based reward - win for 10, lose for 0.
if done and reward == 1.0:
info['won'] = True
reward = 10.0
else:
info['won'] = False
reward = 0
return obs, reward, done, info
def reset(self, idx):
"""Reset the environment with given session index"""
obs, info = self.env.reset(session=idx)
info = dict(info or {})
info['available_actions'] = self.env.get_available_actions()
info['won'] = False
return obs, info
def render(self, mode_for_render):
"""Render the environment"""
rendered = self.env.render(mode=mode_for_render)
return rendered
def get_available_actions(self):
"""Get available actions"""
return self.env.get_available_actions()
def get_goals(self):
"""Get environment goals"""
return self.env.server.goals
def close(self):
"""Close the environment"""
self.env.close()
# -----------------------------------------------------------------------------
# Vectorised Ray environment --------------------------------------------------
# -----------------------------------------------------------------------------
class WebshopMultiProcessEnv(gym.Env):
"""A vectorised, Ray-based wrapper around *WebAgentTextEnv*.
``info`` dictionaries returned by :py:meth:`step` **and** :py:meth:`reset`
automatically contain the key ``'available_actions'`` so downstream RL code
can obtain the *legal* action set without extra IPC overhead.
"""
def __init__(
self,
seed: int = 0,
env_num: int = 1,
group_n: int = 1,
is_train: bool = True,
env_kwargs: dict = None,
) -> None:
super().__init__()
# Initialize Ray if not already initialized
if not ray.is_initialized():
ray.init()
self.group_n = group_n
self.env_num = env_num
self.num_processes = env_num * group_n
self.is_train = is_train
if not is_train: assert group_n == 1
self._rng = np.random.RandomState(seed)
self._env_kwargs = env_kwargs if env_kwargs is not None else {'observation_mode': 'text', 'num_products': None}
# -------------------------- Ray actors setup --------------------------
self._workers = []
for i in range(self.num_processes):
worker = WebshopWorker.remote(seed + (i // self.group_n), self._env_kwargs)
self._workers.append(worker)
# Get goals from the first worker
goals_future = self._workers[0].get_goals.remote()
goals = ray.get(goals_future)
# ------- original ----------#
# if args.num is None:
# if split == 'test':
# self.goal_idxs = range(500)
# elif split == 'eval':
# self.goal_idxs = range(500, 1500)
# elif split == 'train':
# self.goal_idxs = range(1500, len(self.env.server.goals))
# else:
# self.goal_idxs = range(len(self.env.server.goals))
if not self.is_train:
self.goal_idxs = range(500)
else:
self.goal_idxs = range(500, len(goals))
print(self.goal_idxs)
# ------------------------------------------------------------------
# Base API ----------------------------------------------------------
# ------------------------------------------------------------------
def step(self, actions: list[str]):
if len(actions) != self.num_processes:
raise ValueError(
f'Expected {self.num_processes} actions, got {len(actions)}',
)
# Send step commands to all workers
futures = []
for worker, action in zip(self._workers, actions):
future = worker.step.remote(action)
futures.append(future)
# Collect results
results = ray.get(futures)
obs_list, reward_list, done_list, info_list = [], [], [], []
for obs, reward, done, info in results:
obs_list.append(obs)
reward_list.append(reward)
done_list.append(done)
info_list.append(info)
return obs_list, reward_list, done_list, info_list
def reset(self):
idx = self._rng.choice(self.goal_idxs, size=self.env_num, replace=False)
idx = np.repeat(idx, self.group_n).tolist()
# Send reset commands to all workers
futures = []
for worker, i in zip(self._workers, idx):
future = worker.reset.remote(i)
futures.append(future)
# Collect results
results = ray.get(futures)
obs_list, info_list = [], []
for obs, info in results:
obs_list.append(obs)
info_list.append(info)
return obs_list, info_list
# ------------------------------------------------------------------
# Convenience helpers ----------------------------------------------
# ------------------------------------------------------------------
def render(self, mode: str = 'text', env_idx: int = None):
if env_idx is not None:
future = self._workers[env_idx].render.remote(mode)
return ray.get(future)
futures = []
for worker in self._workers:
future = worker.render.remote(mode)
futures.append(future)
return ray.get(futures)
# ------------------------------------------------------------------
# Clean‑up ----------------------------------------------------------
# ------------------------------------------------------------------
def close(self):
if getattr(self, '_closed', False):
return
# Close all workers and kill Ray actors
close_futures = []
for worker in self._workers:
future = worker.close.remote()
close_futures.append(future)
# Wait for all workers to close
ray.get(close_futures)
# Kill all Ray actors
for worker in self._workers:
ray.kill(worker)
self._closed = True
def __del__(self): # noqa: D401
self.close()
# -----------------------------------------------------------------------------
# Factory helper --------------------------------------------------------------
# -----------------------------------------------------------------------------
def build_webshop_envs(
seed: int = 0,
env_num: int = 1,
group_n: int = 1,
is_train: bool = True,
env_kwargs: dict = None,
):
"""Mirror *build_sokoban_envs* so higher‑level code can swap seamlessly."""
return WebshopMultiProcessEnv(
seed=seed,
env_num=env_num,
group_n=group_n,
is_train=is_train,
env_kwargs=env_kwargs,
)