-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgym_http_server.py
537 lines (441 loc) · 17.4 KB
/
gym_http_server.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
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
#!/usr/bin/env python3
import sys
import json
import uuid
import argparse
import logging
import base64
import zlib
from flask import Flask, request, jsonify
import gymnasium as gym
import numpy as np
logger = logging.getLogger("werkzeug")
def _mapper(obj):
ret = obj
if isinstance(ret, tuple):
ret = ret[0]
if isinstance(ret, int):
ret = np.array([ret])
if isinstance(ret, np.float32):
ret = np.array([ret])
return ret
def _compress_b64_encode(byte_arr):
return base64.b64encode(zlib.compress(byte_arr)).decode("utf-8")
def _observation_to_jsonable(obs):
obs = _mapper(obs)
if isinstance(obs, np.ndarray):
obs = obs.flatten()
if obs.dtype == np.float32:
obs = obs.astype(np.float64)
if obs.dtype in (np.int32, np.uint8):
obs = obs.astype(np.int64)
if sys.byteorder != "little":
byte_arr = obs.byteswap().tobytes()
else:
byte_arr = obs.tobytes()
jsonable = {
"type": f"{obs.dtype}",
"data": _compress_b64_encode(byte_arr),
}
return jsonable
def _replace_inf(num):
if np.isneginf(num):
return np.finfo(np.float64).min
if np.isposinf(num):
return np.finfo(np.float64).max
return num.item()
def _normalize_infs(nums):
return [_replace_inf(x) for x in np.array(nums).flatten()]
########## Container for environments ##########
class Envs:
"""
Container and manager for the environments instantiated
on this server.
When a new environment is created, such as with
envs.create('CartPole-v1'), it is stored under a short
identifier (such as '3c657dbc'). Future API calls make
use of this instance_id to identify which environment
should be manipulated.
"""
def __init__(self):
self.envs = {}
self.id_len = 8
def _lookup_env(self, instance_id):
try:
return self.envs[instance_id]
except KeyError as e:
raise InvalidUsage(f"Instance_id {instance_id} unknown") from e
def _remove_env(self, instance_id):
try:
del self.envs[instance_id]
except KeyError as e:
raise InvalidUsage(f"Instance_id {instance_id} unknown") from e
def _add_alpha_channel(self, rf):
if isinstance(rf, np.ndarray) and rf.dtype == np.uint8:
return np.dstack((rf, np.full((rf.shape[0], rf.shape[1]), 255, dtype=np.uint8)))
return rf
def _render_frame_jsonable(self, rf):
jsonable = None
if isinstance(rf, str):
jsonable = rf
elif isinstance(rf, np.ndarray) and rf.dtype == np.uint8:
# Refer: https://stackoverflow.com/questions/53548127/post-numpy-array-with-json-to-flask-app-with-requests
jsonable = {
"rows": rf.shape[0],
"cols": rf.shape[1],
"data": _compress_b64_encode(rf.tobytes()),
}
else:
jsonable = rf
return jsonable
def create(self, env_id, max_episode_steps, auto_reset, disable_env_checker, kwargs):
try:
env = gym.make(env_id, max_episode_steps, auto_reset, False, disable_env_checker, **kwargs)
except gym.error.Error as e:
raise InvalidUsage(f"Attempted to look up malformed environment ID '{env_id}'") from e
instance_id = str(uuid.uuid4().hex)[: self.id_len]
self.envs[instance_id] = env
return instance_id
def list_all(self):
return {instance_id: env.spec.id for (instance_id, env) in self.envs.items()}
def reset(self, instance_id, seed):
env = self._lookup_env(instance_id)
seed = int(seed) if seed is not None else None
obs = env.reset(seed=seed)
jsonable = _observation_to_jsonable(obs)
return jsonable
def get_id(self, instance_id):
_id = self._lookup_env(instance_id).spec.id
return _id
def render(self, instance_id):
env = self._lookup_env(instance_id)
render_frame = env.render()
render_frame = self._add_alpha_channel(render_frame)
return self._render_frame_jsonable(render_frame)
def step(self, instance_id, action):
env = self._lookup_env(instance_id)
if isinstance(action, int):
nice_action = action
else:
nice_action = np.array(action)
observation, reward, terminated, truncated, info = env.step(nice_action)
obs_jsonable = _observation_to_jsonable(observation)
return [obs_jsonable, reward, terminated, truncated, info]
def get_action_space_contains(self, instance_id, x):
env = self._lookup_env(instance_id)
return env.action_space.contains(int(x))
def get_action_space_info(self, instance_id):
env = self._lookup_env(instance_id)
return self._get_space_properties(env.action_space)
def get_action_space_sample(self, instance_id):
env = self._lookup_env(instance_id)
action = env.action_space.sample()
if isinstance(action, (list, tuple)) or ("numpy" in str(type(action))):
try:
action = action.tolist()
except TypeError:
print(type(action))
print("TypeError")
return action
def get_observation_space_contains(self, instance_id, j):
env = self._lookup_env(instance_id)
info = self._get_space_properties(env.observation_space)
for key, value in j.items():
# Convert both values to json for compaibility
if json.dumps(info[key]) != json.dumps(value):
print(f'Values for "{key}" do not match. Passed "{value}", Observed "{info[key]}".')
return False
return True
def get_observation_space_info(self, instance_id):
env = self._lookup_env(instance_id)
return self._get_space_properties(env.observation_space)
def get_transitions(self, instance_id):
env = self._lookup_env(instance_id)
return env.unwrapped.P
def get_episode_samples(self, instance_id, seed, count):
seed = int(seed) if seed is not None else None
count = int(count)
eps = []
for _ in range(count):
obs = self.reset(instance_id, seed)
ep = [{"s": obs, "r": 0.0}]
eps.append(ep)
while True:
a = self.get_action_space_sample(instance_id)
si = self.step(instance_id, a)
ep.append({"s": si[0], "r": si[1]})
if si[2]:
break
return eps
def _get_space_properties(self, space):
info = {}
info["name"] = space.__class__.__name__
if info["name"] == "Discrete":
info["n"] = space.n.item()
elif info["name"] == "Box":
info["shape"] = space.shape
# It's not JSON compliant to have Infinity, -Infinity, NaN.
# Many newer JSON parsers allow it, but many don't. Notably python json
# module can read and write such floats. So we only here fix "export version",
# also make it flat.
info["low"] = _normalize_infs(space.low)
info["high"] = _normalize_infs(space.high)
elif info["name"] == "HighLow":
info["num_rows"] = space.num_rows
info["matrix"] = _normalize_infs(space.matrix)
return info
def env_close(self, instance_id):
env = self._lookup_env(instance_id)
env.close()
self._remove_env(instance_id)
########## App setup ##########
app = Flask(__name__)
app.config["JSONIFY_PRETTYPRINT_REGULAR"] = False
envs = Envs()
########## Error handling ##########
class InvalidUsage(Exception):
status_code = 400
def __init__(self, message, status_code=None, payload=None):
Exception.__init__(self)
self.message = message
if status_code is not None:
self.status_code = status_code
self.payload = payload
def to_dict(self):
rv = dict(self.payload or ())
rv["message"] = self.message
return rv
def get_required_param(json_, param):
if json_ is None:
logger.info("Request is not a valid json")
raise InvalidUsage("Request is not a valid json")
value = json_.get(param, None)
if (value is None) or (value == "") or (value == []):
logger.info("A required request parameter '%s' had value %s", param, value)
raise InvalidUsage(f"A required request parameter '{param}' was not provided")
return value
def get_optional_param(json_, param, default):
if json_ is None:
logger.info("Request is not a valid json")
raise InvalidUsage("Request is not a valid json")
value = json_.get(param, None)
if (value is None) or (value == "") or (value == []):
logger.info(
"An optional request parameter '%s' had value %s and was replaced with default value %s",
param,
value,
default,
)
value = default
return value
@app.errorhandler(InvalidUsage)
def handle_invalid_usage(error):
response = jsonify(error.to_dict())
response.status_code = error.status_code
return response
########## API route definitions ##########
@app.route("/v1/envs/", methods=["POST"])
def env_create():
"""
Create an instance of the specified environment
Parameters:
- Refer: https://gymnasium.farama.org/api/registry/#gymnasium.make
Returns:
- instance_id: a short identifier (such as '3c657dbc')
for the created environment instance. The instance_id is
used in future API calls to identify the environment to be
manipulated
"""
print(f"#### {request.get_json()}")
env_id = get_required_param(request.get_json(), "env_id")
max_episode_steps = get_optional_param(request.get_json(), "max_episode_steps", None)
auto_reset = get_optional_param(request.get_json(), "auto_reset", None)
disable_env_checker = get_optional_param(request.get_json(), "disable_env_checker", None)
kwargs = get_optional_param(request.get_json(), "kwargs", {})
instance_id = envs.create(env_id, max_episode_steps, auto_reset, disable_env_checker, kwargs)
return jsonify(instance_id=instance_id)
@app.route("/v1/envs/", methods=["GET"])
def env_list_all():
"""
List all environments running on the server
Returns:
- envs: dict mapping instance_id to env_id
(e.g. {'3c657dbc': 'CartPole-v1'}) for every env
on the server
"""
all_envs = envs.list_all()
return jsonify(all_envs=all_envs)
@app.route("/v1/envs/<instance_id>/", methods=["GET"])
def env_get_id(instance_id):
"""
Get the env_id.
Parameters:
- instance_id: a short identifier (such as '3c657dbc')
for the environment instance
Returns:
- env_id: the id of the environment
"""
_id = envs.get_id(instance_id)
return jsonify(id=_id)
@app.route("/v1/envs/<instance_id>/reset/", methods=["POST"])
def env_reset(instance_id):
"""
Reset the state of the environment and return an initial
observation.
Parameters:
- instance_id: a short identifier (such as '3c657dbc')
for the environment instance
- seed: set the seed for this env's random number generator(s).
Returns:
- observation: the initial observation of the space
"""
seed = get_optional_param(request.get_json(), "seed", None)
observation = envs.reset(instance_id, seed)
return jsonify(observation=observation)
@app.route("/v1/envs/<instance_id>/render/", methods=["GET"])
def env_render(instance_id):
"""
Compute the render frames as specified by render_mode during the initialization
of the environment.
Returns:
- render_frame: the computed render_frame.
"""
render_frame = envs.render(instance_id)
return jsonify(render_frame=render_frame)
@app.route("/v1/envs/<instance_id>/step/", methods=["POST"])
def env_step(instance_id):
"""
Run one timestep of the environment's dynamics.
Parameters:
- instance_id: a short identifier (such as '3c657dbc')
for the environment instance
- action: an action to take in the environment
Returns:
- observation: agent's observation of the current
environment
- reward: amount of reward returned after previous action
- done: whether the episode has ended
- info: a dict containing auxiliary diagnostic information
"""
json_ = request.get_json()
action = get_required_param(json_, "action")
[obs_jsonable, reward, terminated, truncated, info] = envs.step(instance_id, action)
return jsonify(observation=obs_jsonable, reward=reward, terminated=terminated, truncated=truncated, info=info)
@app.route("/v1/envs/<instance_id>/action_space/", methods=["GET"])
def env_action_space_info(instance_id):
"""
Get information (name and dimensions/bounds) of the env's
action_space
Parameters:
- instance_id: a short identifier (such as '3c657dbc')
for the environment instance
Returns:
- info: a dict containing 'name' (such as 'Discrete'), and
additional dimensional info (such as 'n') which varies from
space to space
"""
info = envs.get_action_space_info(instance_id)
return jsonify(info=info)
@app.route("/v1/envs/<instance_id>/action_space/sample/", methods=["GET"])
def env_action_space_sample(instance_id):
"""
Get a sample from the env's action_space
Parameters:
- instance_id: a short identifier (such as '3c657dbc')
for the environment instance
Returns:
- action: a randomly sampled element belonging to the action_space
"""
action = envs.get_action_space_sample(instance_id)
return jsonify(action=action)
@app.route("/v1/envs/<instance_id>/action_space/contains/<action>/", methods=["GET"])
def env_action_space_contains(instance_id, action):
"""
Assess that value is a member of the env's action_space
Parameters:
- instance_id: a short identifier (such as '3c657dbc')
for the environment instance
- x: the value to be checked as member
Returns:
- member: whether the value passed as parameter belongs to the action_space
"""
member = envs.get_action_space_contains(instance_id, action)
return jsonify(member=member)
@app.route("/v1/envs/<instance_id>/observation_space/contains/", methods=["POST"])
def env_observation_space_contains(instance_id):
"""
Assess that the parameters are members of the env's observation_space
Parameters:
- instance_id: a short identifier (such as '3c657dbc')
for the environment instance
Returns:
- member: whether all the values passed belong to the observation_space
"""
j = request.get_json()
member = envs.get_observation_space_contains(instance_id, j)
return jsonify(member=member)
@app.route("/v1/envs/<instance_id>/observation_space/", methods=["GET"])
def env_observation_space_info(instance_id):
"""
Get information (name and dimensions/bounds) of the env's
observation_space
Parameters:
- instance_id: a short identifier (such as '3c657dbc')
for the environment instance
Returns:
- info: a dict containing 'name' (such as 'Discrete'),
and additional dimensional info (such as 'n') which
varies from space to space
"""
info = envs.get_observation_space_info(instance_id)
return jsonify(info=info)
@app.route("/v1/envs/<instance_id>/transitions/", methods=["GET"])
def env_get_transitions(instance_id):
"""
Get transition probability from state given action.
Parameters:
- instance_id: a short identifier (such as '3c657dbc')
for the environment instance
Returns:
- transition: all transitions as tuple (probability of transition, next state, reward, done)
"""
probs = envs.get_transitions(instance_id)
return jsonify(transitions=probs)
@app.route("/v1/envs/<instance_id>/episodes/", methods=["POST"])
def env_episode_samples(instance_id):
"""
Generates a given number of episodes.
Parameters:
- instance_id: a short identifier (such as '3c657dbc')
for the environment instance
- seed: set the seed for this env's random number generator(s).
- count: number of episodes to generate
Returns:
- episodes: generated episodes as list of { s: state, r: reward }
"""
json_ = request.get_json()
count = get_required_param(json_, "count")
seed = get_optional_param(json_, "seed", None)
episodes = envs.get_episode_samples(instance_id, seed, count)
return jsonify(episodes=episodes)
@app.route("/v1/envs/<instance_id>/", methods=["DELETE"])
def env_close(instance_id):
"""
Manually close an environment
Parameters:
- instance_id: a short identifier (such as '3c657dbc')
for the environment instance
"""
envs.env_close(instance_id)
return ("", 200)
def run_main():
parser = argparse.ArgumentParser(description="Start a Gym HTTP API server")
parser.add_argument("-l", "--listen", help="interface to listen to", default="127.0.0.1")
parser.add_argument("-p", "--port", default=40004, type=int, help="port to bind to")
parser.add_argument("-g", "--log_level", default="ERROR", type=str, help="server log level")
args = parser.parse_args()
print(f"Server starting at: http://{args.listen}:{args.port}. Loglevel: {args.log_level}.")
logger.setLevel(args.log_level)
app.run(host=args.listen, port=args.port)
if __name__ == "__main__":
run_main()