Grabbed more metadata from gym.env and disabled logging of HTTP requests

This commit is contained in:
Brandon Rozek 2019-06-06 21:49:12 -04:00
parent a650d911ea
commit 147d682c1c
2 changed files with 70 additions and 5 deletions

View file

@ -10,6 +10,7 @@ class Environment:
self.port = port self.port = port
protocol = "https://" if ssl else "http://" protocol = "https://" if ssl else "http://"
self.server = protocol + address + ":" + str(port) self.server = protocol + address + ":" + str(port)
self.observation_space, self.action_space, self.reward_range, self.metadata, self.action_meanings = self.get_initial_metadata()
## ##
# Helper Functions # Helper Functions
@ -32,6 +33,25 @@ class Environment:
def get_info(self): def get_info(self):
r = requests.get(self.server + "/info") r = requests.get(self.server + "/info")
return r.json() return r.json()
def get_observation_space(self):
r = requests.get(self.server + '/observation_space')
return pickle.loads(r.content)
def get_action_space(self):
r = requests.get(self.server + '/action_space')
return pickle.loads(r.content)
def get_reward_range(self):
r = requests.get(self.server + '/reward_range')
return pickle.loads(r.content)
def get_metadata(self):
r = requests.get(self.server + '/metadata')
return pickle.loads(r.content)
def get_action_meanings(self):
r = requests.get(self.server + '/action_meanings')
return pickle.loads(r.content)
def get_initial_metadata(self):
r = requests.get(self.server + '/gym?observation_space&action_space&reward_range&metadata&action_meanings')
content = pickle.loads(r.content)
return content['observation_space'], content['action_space'], content['reward_range'], content['metadata'], content['action_meanings']
## ##
# Common API # Common API
@ -41,7 +61,7 @@ class Environment:
return pickle.loads(r.content) return pickle.loads(r.content)
def step(self, action): def step(self, action):
r = requests.post(self.server + "/action", data={'id': action}) r = requests.post(self.server + "/action", data={'id': action})
content = r.json() content = pickle.loads(r.content)
return self.get_state(), float(content['reward']), content['done'] == "True", content['info'] return content['state'], content['reward'], content['done'], content['info']
# env = Environment("127.0.0.1", 5000) # env = Environment("127.0.0.1", 5000)

View file

@ -5,11 +5,14 @@ from flask import request
import pickle import pickle
import json import json
# Make it so that it doesn't log every HTTP request
import logging
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
## ##
# OpenAI Gym State # OpenAI Gym State
## ##
# environment_name = sys.argv[1]
# environment_name = "Acrobot-v1" # environment_name = "Acrobot-v1"
environment_name = "Pong-v0" environment_name = "Pong-v0"
env = gym.make(environment_name) env = gym.make(environment_name)
@ -46,6 +49,47 @@ def get_env():
return json.dumps(shape) return json.dumps(shape)
return environment_name return environment_name
@app.route('/gym', methods=['GET'])
def get_extra_data():
global env
data = {}
if request.args.get('action_space') is not None:
data['action_space'] = env.action_space
if request.args.get('observation_space') is not None:
data['observation_space'] = env.observation_space
if request.args.get('reward_range') is not None:
data['reward_range'] = env.reward_range
if request.args.get('metadata') is not None:
data['metadata'] = env.metadata
if request.args.get('action_meanings') is not None:
data['action_meanings'] = env.unwrapped.get_action_meanings()
return pickle.dumps(data)
@app.route('/action_space', methods=['GET'])
def get_action_space():
global env
return pickle.dumps(env.action_space)
@app.route('/observation_space', methods=['GET'])
def get_observation_space():
global env
return pickle.dumps(env.observation_space)
@app.route('/reward_range', methods=['GET'])
def get_reward_range():
global env
return pickle.dumps(env.reward_range)
@app.route('/metadata', methods=['GET'])
def get_metadata():
global env
return pickle.dumps(env.metadata)
@app.route('/action_meanings', methods=['GET'])
def get_action_meanings():
global env
return pickle.dumps(env.unwrapped.get_action_meanings())
@app.route('/state', methods=['GET']) @app.route('/state', methods=['GET'])
def get_state(): def get_state():
return pickle_state() return pickle_state()
@ -78,10 +122,11 @@ def perform_action():
score += reward score += reward
content = {} content = {}
content['state'] = state
content['reward'] = reward content['reward'] = reward
content['done'] = done content['done'] = done
content['info'] = info content['info'] = info
return json.dumps(content) return pickle.dumps(content)
@app.route('/reset') @app.route('/reset')
def reset_env(): def reset_env():