-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimulator.py
More file actions
238 lines (180 loc) · 7.28 KB
/
simulator.py
File metadata and controls
238 lines (180 loc) · 7.28 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
from http.server import BaseHTTPRequestHandler, HTTPServer
import json
import yaml
import alfworld
import alfworld.agents.environment
import os
from urllib.parse import urlparse, parse_qs
#!/opt/conda/bin/python
class Simulator():
MAX_STEPS = 10
MAX_RESETS = 134
alfworld_root = os.environ['ALFWORLD_ROOT']
alfworld_data = os.environ['ALFWORLD_DATA']
react_root = os.environ['REACT_ROOT']
split = "eval_out_of_distribution"
total_steps = 0
total_resets = 0
finished = False
env = None
example_prompts = None
with open(f'{react_root}base_config.yaml') as reader:
config = yaml.safe_load(reader)
env = getattr(alfworld.agents.environment, config["env"]["type"])(config, train_eval=split)
env = env.init_env(batch_size=1)
folder = f'{react_root}prompts/'
prompt_file = 'alfworld_3prompts.json'
with open(folder + prompt_file, 'r') as f:
example_prompts = json.load(f)
prefixes = {
'pick_and_place': 'put',
'pick_clean_then_place': 'clean',
'pick_heat_then_place': 'heat',
'pick_cool_then_place': 'cool',
'look_at_obj': 'examine',
'pick_two_obj': 'puttwo'
}
cnts = [0] * 6
rs = [0] * 6
def __init__(self):
if Simulator.env is None:
self.init_env()
if Simulator.example_prompts is None:
self.init_example_prompts()
def init_env(self):
Simulator.total_resets = 0
Simulator.finished = False
with open(f'{Simulator.react_root}base_config.yaml') as reader:
config = yaml.safe_load(reader)
_env = getattr(alfworld.agents.environment, config["env"]["type"])(config, train_eval=Simulator.split)
_env = _env.init_env(batch_size=1)
Simulator.env = _env
def init_example_prompts(self):
folder = f'{Simulator.react_root}prompts/'
prompt_file = 'alfworld_3prompts.json'
with open(folder + prompt_file, 'r') as f:
_example_prompts = json.load(f)
Simulator.example_prompts = _example_prompts
def process_ob(self, ob):
if ob.startswith('You arrive at loc '):
ob = ob[ob.find('. ')+2:]
return ob
def reset(self, max_steps):
if Simulator.total_resets >= Simulator.MAX_RESETS:
self.init_env()
Simulator.total_steps = 0
Simulator.total_resets += 1
Simulator.finished = False
Simulator.MAX_STEPS = max_steps
ob, info = Simulator.env.reset()
ob = '\n'.join(ob[0].split('\n\n')[1:])
name = '/'.join(info['extra.gamefile'][0].split('/')[-3:-1])
examples = []
for _, (k, v) in enumerate(Simulator.prefixes.items()):
if name.startswith(k):
examples = [
Simulator.example_prompts[f'react_{v}_1'],
Simulator.example_prompts[f'react_{v}_0']
]
break
return examples, ob
def step(self, action):
Simulator.total_steps += 1
observation, reward, done, info = Simulator.env.step([action])
observation, reward, done = self.process_ob(observation[0]), info['won'][0], done[0]
# TODO might want to handle "Think: ..." actions
if Simulator.total_steps >= Simulator.MAX_STEPS:
done = True
if done:
Simulator.finished = True
return observation, reward, done
def is_finished(self):
return Simulator.finished
class Handler(BaseHTTPRequestHandler):
# State variable
simulator = Simulator()
def do_GET(self):
parsed_url = urlparse(self.path)
query_params = parse_qs(parsed_url.query)
if parsed_url.path == '/get_next_task':
max_steps = int(query_params.get('max_steps', [10])[0])
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
examples, task = Handler.simulator.reset(max_steps=max_steps)
response_data = {
"examples": examples,
"task": task,
"task_index": Handler.simulator.total_resets
}
response_data = json.dumps(response_data)
self.wfile.write(response_data.encode('utf-8'))
print(f'Task: {task}')
elif parsed_url.path == '/reset_tasks':
Handler.simulator.init_env()
print('Reset tasks!')
self.send_response(200)
self.send_header('Content-type', 'text/plain')
self.end_headers()
else:
self.send_response(404)
self.send_header('Content-type', 'text/plain')
self.end_headers()
response = '404 Not Found'
self.wfile.write(response.encode('utf-8'))
def do_POST(self):
if self.path == '/take_action':
if Handler.simulator.is_finished():
self.send_response(400)
self.send_header('Content-type', 'application/json')
self.end_headers()
response_data = {
'observation': None,
'reward': None,
'done': None,
'message': 'You have exceeded the maximum number of allowed steps. Please try a new task.',
}
response_data = json.dumps(response_data)
self.wfile.write(response_data.encode('utf-8'))
else:
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
data = json.loads(post_data.decode('utf-8'))
action = data.get('action', None)
if action is not None:
observation, reward, done = Handler.simulator.step(action)
response_data = {
'observation': observation,
'reward': reward,
'done': done,
'message': None,
}
response_data = json.dumps(response_data)
if done and not reward:
self.send_response(400)
else:
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(response_data.encode('utf-8'))
print(f'Action: {action}')
print(f'Observation: {observation}')
else:
self.send_response(400)
self.send_header('Content-type', 'text/plain')
self.end_headers()
response = '400 Bad Request: Missing "action" parameter'
self.wfile.write(response.encode('utf-8'))
else:
self.send_response(404)
self.send_header('Content-type', 'text/plain')
self.end_headers()
response = '404 Not Found'
self.wfile.write(response.encode('utf-8'))
def run_server(port=8000):
server_address = ('', port)
httpd = HTTPServer(server_address, Handler)
print(f'Starting server on port {port}...')
httpd.serve_forever()
if __name__ == '__main__':
run_server()