2019-06-05 21:56:01 -04:00
|
|
|
import sys
|
|
|
|
import gym
|
|
|
|
from flask import Flask
|
|
|
|
from flask import request
|
|
|
|
import pickle
|
|
|
|
import json
|
|
|
|
|
2019-06-06 21:49:12 -04:00
|
|
|
# Make it so that it doesn't log every HTTP request
|
|
|
|
import logging
|
|
|
|
log = logging.getLogger('werkzeug')
|
|
|
|
log.setLevel(logging.ERROR)
|
2019-06-05 21:56:01 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# OpenAI Gym State
|
|
|
|
##
|
2019-06-08 14:52:56 -04:00
|
|
|
class Environment:
|
|
|
|
def __init__(self, environment_name):
|
|
|
|
self.sim = gym.make(environment_name)
|
|
|
|
self.environment_name = environment_name
|
|
|
|
self.reset()
|
|
|
|
def step(self, action):
|
|
|
|
# [TODO] Check to see if 'action' is valid
|
|
|
|
self.state, self.reward, self.done, self.info = self.sim.step(action)
|
|
|
|
self.score += self.reward
|
|
|
|
def reset(self):
|
|
|
|
self.state = self.sim.reset()
|
|
|
|
self.reward = 0
|
|
|
|
self.score = 0
|
|
|
|
self.done = False
|
|
|
|
self.info = {}
|
|
|
|
def preprocess(self, state):
|
|
|
|
raise NotImplementedError
|
|
|
|
def get_state(self, preprocess = False, pickleobj = False):
|
|
|
|
state = self.state
|
|
|
|
if preprocess:
|
|
|
|
state = self.preprocess(state)
|
|
|
|
if pickleobj:
|
|
|
|
state = pickle.dumps(state)
|
|
|
|
return state
|
2019-06-05 21:56:01 -04:00
|
|
|
|
|
|
|
|
|
|
|
##
|
2019-06-08 14:52:56 -04:00
|
|
|
# Pong Specific Environment Information
|
2019-06-05 21:56:01 -04:00
|
|
|
##
|
2019-06-08 14:52:56 -04:00
|
|
|
import cv2
|
|
|
|
class PongEnv(Environment):
|
|
|
|
def __init__(self):
|
|
|
|
super(PongEnv, self).__init__("PongNoFrameskip-v4")
|
|
|
|
def preprocess(self, state):
|
|
|
|
# Grayscale
|
|
|
|
frame = cv2.cvtColor(state, cv2.COLOR_RGB2GRAY)
|
|
|
|
# Crop irrelevant parts
|
|
|
|
frame = frame[34:194, 15:145] # Crops to shape (160, 130)
|
|
|
|
# Downsample
|
|
|
|
frame = cv2.resize(frame, (80, 80), interpolation=cv2.INTER_AREA)
|
|
|
|
# Normalize
|
|
|
|
frame = frame / 255
|
|
|
|
return frame
|
2019-06-05 21:56:01 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2019-06-08 14:52:56 -04:00
|
|
|
env = PongEnv()
|
|
|
|
|
2019-06-05 21:56:01 -04:00
|
|
|
##
|
|
|
|
# Flask Environment
|
|
|
|
##
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
|
|
@app.route('/environment', methods=['GET'])
|
|
|
|
def get_env():
|
2019-06-08 14:52:56 -04:00
|
|
|
global env
|
2019-06-05 21:56:01 -04:00
|
|
|
if request.args.get('shape') is not None:
|
|
|
|
shape = {}
|
2019-06-08 14:52:56 -04:00
|
|
|
shape['observation'] = env.sim.observation_space.shape
|
|
|
|
shape['action'] = env.sim.action_space.n
|
2019-06-05 21:56:01 -04:00
|
|
|
return json.dumps(shape)
|
2019-06-08 14:52:56 -04:00
|
|
|
return env.environment_name
|
2019-06-05 21:56:01 -04:00
|
|
|
|
2019-06-06 21:49:12 -04:00
|
|
|
@app.route('/gym', methods=['GET'])
|
|
|
|
def get_extra_data():
|
|
|
|
global env
|
|
|
|
data = {}
|
|
|
|
if request.args.get('action_space') is not None:
|
2019-06-08 14:52:56 -04:00
|
|
|
data['action_space'] = env.sim.action_space
|
2019-06-06 21:49:12 -04:00
|
|
|
if request.args.get('observation_space') is not None:
|
2019-06-08 14:52:56 -04:00
|
|
|
data['observation_space'] = env.sim.observation_space
|
2019-06-06 21:49:12 -04:00
|
|
|
if request.args.get('reward_range') is not None:
|
2019-06-08 14:52:56 -04:00
|
|
|
data['reward_range'] = env.sim.reward_range
|
2019-06-06 21:49:12 -04:00
|
|
|
if request.args.get('metadata') is not None:
|
2019-06-08 14:52:56 -04:00
|
|
|
data['metadata'] = env.sim.metadata
|
2019-06-06 21:49:12 -04:00
|
|
|
if request.args.get('action_meanings') is not None:
|
2019-06-08 14:52:56 -04:00
|
|
|
data['action_meanings'] = env.sim.unwrapped.get_action_meanings()
|
2019-06-06 21:49:12 -04:00
|
|
|
return pickle.dumps(data)
|
|
|
|
|
|
|
|
@app.route('/action_space', methods=['GET'])
|
|
|
|
def get_action_space():
|
|
|
|
global env
|
2019-06-08 14:52:56 -04:00
|
|
|
return pickle.dumps(env.sim.action_space)
|
2019-06-06 21:49:12 -04:00
|
|
|
|
|
|
|
@app.route('/observation_space', methods=['GET'])
|
|
|
|
def get_observation_space():
|
|
|
|
global env
|
2019-06-08 14:52:56 -04:00
|
|
|
return pickle.dumps(env.sim.observation_space)
|
2019-06-06 21:49:12 -04:00
|
|
|
|
|
|
|
@app.route('/reward_range', methods=['GET'])
|
|
|
|
def get_reward_range():
|
|
|
|
global env
|
2019-06-08 14:52:56 -04:00
|
|
|
return pickle.dumps(env.sim.reward_range)
|
2019-06-06 21:49:12 -04:00
|
|
|
|
|
|
|
@app.route('/metadata', methods=['GET'])
|
|
|
|
def get_metadata():
|
|
|
|
global env
|
2019-06-08 14:52:56 -04:00
|
|
|
return pickle.dumps(env.sim.metadata)
|
2019-06-06 21:49:12 -04:00
|
|
|
|
|
|
|
@app.route('/action_meanings', methods=['GET'])
|
|
|
|
def get_action_meanings():
|
|
|
|
global env
|
2019-06-08 14:52:56 -04:00
|
|
|
return pickle.dumps(env.sim.unwrapped.get_action_meanings())
|
2019-06-06 21:49:12 -04:00
|
|
|
|
2019-06-05 21:56:01 -04:00
|
|
|
@app.route('/state', methods=['GET'])
|
|
|
|
def get_state():
|
2019-06-08 14:52:56 -04:00
|
|
|
return env.get_state(pickleobj = True, preprocess = request.args.get('preprocess') is not None)
|
2019-06-05 21:56:01 -04:00
|
|
|
|
|
|
|
@app.route('/reward', methods=['GET'])
|
|
|
|
def get_reward():
|
2019-06-08 14:52:56 -04:00
|
|
|
global env
|
2019-06-05 21:56:01 -04:00
|
|
|
if request.args.get('all') is not None:
|
2019-06-08 14:52:56 -04:00
|
|
|
return str(env.score)
|
2019-06-05 21:56:01 -04:00
|
|
|
else:
|
2019-06-08 14:52:56 -04:00
|
|
|
return str(env.reward)
|
2019-06-05 21:56:01 -04:00
|
|
|
|
|
|
|
@app.route('/done', methods=['GET'])
|
|
|
|
def is_done():
|
2019-06-08 14:52:56 -04:00
|
|
|
global env
|
|
|
|
return str(env.done)
|
2019-06-05 21:56:01 -04:00
|
|
|
|
|
|
|
@app.route('/info', methods=['GET'])
|
|
|
|
def get_info():
|
2019-06-08 14:52:56 -04:00
|
|
|
global env
|
|
|
|
return json.dumps(env.info)
|
2019-06-05 21:56:01 -04:00
|
|
|
|
|
|
|
@app.route('/action', methods=['POST'])
|
|
|
|
def perform_action():
|
2019-06-08 14:52:56 -04:00
|
|
|
global env
|
2019-06-05 21:56:01 -04:00
|
|
|
action = int(request.form['id'])
|
2019-06-08 14:52:56 -04:00
|
|
|
env.step(action)
|
2019-06-05 21:56:01 -04:00
|
|
|
|
|
|
|
content = {}
|
2019-06-08 14:52:56 -04:00
|
|
|
content['state'] = env.get_state(preprocess = request.args.get('preprocess') is not None)
|
|
|
|
content['reward'] = env.reward
|
|
|
|
content['done'] = env.done
|
|
|
|
content['info'] = env.info
|
2019-06-06 21:49:12 -04:00
|
|
|
return pickle.dumps(content)
|
2019-06-05 21:56:01 -04:00
|
|
|
|
|
|
|
@app.route('/reset')
|
|
|
|
def reset_env():
|
2019-06-08 14:52:56 -04:00
|
|
|
global env
|
|
|
|
env.reset()
|
|
|
|
return env.get_state(pickleobj = True, preprocess = request.args.get('preprocess') is not None)
|
2019-06-05 21:56:01 -04:00
|
|
|
|