def action_act(args): global cached_agent if len(args.agents) != 1: return {"error": "One agent must be provided."} raw = args.agents[0] # Process the configuration. # (additional environment specificproperties come along without being checked). err, config = utils.structify( utils.process_schema(utils.schemas["configuration"], args.configuration)) if err: return {"error": err} timeout = config.actTimeout if cached_agent == None or cached_agent.id != raw: if cached_agent != None: cached_agent.destroy() cached_agent = Agent(raw, config, raw) timeout = config.agentTimeout state = { "observation": utils.get(args.state, dict, {}, ["observation"]), "reward": args.get("reward", None), "info": utils.get(args.state, dict, {}, ["info"]) } action = cached_agent.act(state, timeout) if isinstance(action, errors.DeadlineExceeded): action = "DeadlineExceeded" elif isinstance(action, BaseException): action = "BaseException" return {"action": action}
def parse_args(args): return utils.structify({ "action": utils.get(args, str, "list", ["action"]), "agents": utils.get(args, list, [], ["agents"]), "configuration": utils.get(args, dict, {}, ["configuration"]), "environment": args.get("environment", None), "episodes": utils.get(args, int, 1, ["episodes"]), "state": utils.get(args, dict, {}, ["state"]), "steps": utils.get(args, list, [], ["steps"]), "render": utils.get(args, dict, {"mode": "json"}, ["render"]), "debug": utils.get(args, bool, False, ["debug"]), "host": utils.get(args, str, "127.0.0.1", ["host"]), "port": utils.get(args, int, 8000, ["port"]) })
def action_act(args): global cached_agent if len(args.agents) != 1: return {"error": "One agent must be provided."} raw = args.agents[0] if cached_agent == None or cached_agent.id != raw: if cached_agent != None: cached_agent.destroy() cached_agent = Agent(raw, args.configuration, args.use_subprocess, raw) state = { "observation": utils.get(args.state, dict, {}, ["observation"]), "reward": args.get("reward", None), "info": utils.get(args.state, dict, {}, ["info"]) } return {"action": cached_agent.act(state, args.timeout)}
def render(args, env): mode = utils.get(args.render, str, "json", path=["mode"]) if mode == "human" or mode == "ansi": args.render["mode"] = "ansi" elif mode == "ipython" or mode == "html": args.render["mode"] = "html" else: args.render["mode"] = "json" return env.render(**args.render)
def action_http(args): from flask import Flask, request middleware = {"request": None, "response": None} if args.middleware != None: try: raw = utils.read_file(args.middleware) local = utils.get_exec(raw) middleware["request"] = utils.get(local, path=["request"], is_callable=True) middleware["response"] = utils.get(local, path=["response"], is_callable=True) except Exception as e: return {"error": str(e), "trace": traceback.format_exc()} app = Flask(__name__, static_url_path="", static_folder="") app.route("/", methods=["GET", "POST"])(lambda: http_request(request, middleware)) app.run(args.host, args.port, debug=True)
def action_handler(args): args = utils.structify({ "action": utils.get(args, str, "list", ["action"]), "agents": utils.get(args, list, [], ["agents"]), "configuration": utils.get(args, dict, {}, ["configuration"]), "environment": args.get("environment", None), "episodes": utils.get(args, int, 1, ["episodes"]), "steps": utils.get(args, list, [], ["steps"]), "render": utils.get(args, dict, {"mode": "json"}, ["render"]), "debug": utils.get(args, bool, False, ["debug"]) }) for index, agent in enumerate(args.agents): agent = utils.read_file(agent, agent) args.agents[index] = utils.get_last_callable(agent, agent) if args.action == "list": return action_list(args) if args.environment == None: return {"error": "Environment required."} try: if args.action == "http-server": return {"error": "Already running a http server."} elif args.action == "evaluate": return action_evaluate(args) elif args.action == "step": return action_step(args) elif args.action == "run": return action_run(args) elif args.action == "load": return action_load(args) else: return {"error": "Unknown Action"} except Exception as e: return {"error": str(e), "trace": traceback.format_exc()}