def evaluate_single_episode(agent_ids, agent_paths): num_agents = agent_ids.size agents = [] for id_ in agent_ids: my_agent_file = environment_utils.read_file(agent_full_paths[id_]) agents.append(environment_utils.get_last_callable(my_agent_file)) halite_scores = evaluate_game( "halite", agents, num_episodes=1, configuration={"agentExec": "LOCAL", # "agentTimeout": 10000, # Uncomment while debugging # "actTimeout": 10000, # Uncomment while debugging })[0] halite_scores = [-1 if h is None else h for h in halite_scores] episode_rewards = np.zeros((num_agents)) for i in range(0, num_agents-1): for j in range(i+1, num_agents): if halite_scores[i] == halite_scores[j]: first_score = 0.5 else: first_score = int(halite_scores[i] > halite_scores[j]) episode_rewards[i] += first_score episode_rewards[j] += 1-first_score episode_rewards /= (num_agents-1) return episode_rewards, halite_scores
def sample_from_config_or_path(config_or_path, return_callable): if isinstance(config_or_path, str): agent_file = environment_utils.read_file(config_or_path) if return_callable: try: return kaggle_agent.get_last_callable(agent_file) except: return environment_utils.get_last_callable(agent_file) else: return agent_file else: return sample_from_config(config_or_path)
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()}
def main(): """ Makes sure that the `submission_standalone.py` file is valid. """ out = sys.stdout submission = utils.read_file("submission_standalone.py") agent = utils.get_last_callable(submission) sys.stdout = out env = make("halite", debug=True) env.run([agent, agent]) if not env.state[0].status == env.state[1].status == "DONE": raise ValidationError( "`submission_standalone.py` file is not vaild. ", f"agent #1 state: '{env.state[0].status}', ", f"agent #2 state: '{env.state[1].status}'", ) print("`submission_standalone.py` file is valid.")
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)
agent_extensions[j][:-3]) agents_paths_video_names.append((agent_paths, video_name)) if generate_self_play_videos: for i in range(num_agents): agent_path = agent_full_paths[i] agent_paths = [agent_path, agent_path, agent_path, agent_path] video_name = agent_extensions[i][:-3] + " ***self play***" agents_paths_video_names.append((agent_paths, video_name)) if agents_paths_video_names: # Load all agent callables once (not really that much more performant) agent_callables = {} for i in range(num_agents): agent_path = agent_full_paths[i] agent_file = environment_utils.read_file(agent_path) agent_callables[agent_path] = environment_utils.get_last_callable( agent_file) env = make_environment("halite", configuration={"agentExec": "LOCAL"}) for agent_paths, video_name in agents_paths_video_names: agents = [] for p in agent_paths: agents.append(agent_callables[p]) env.reset(num_agents=len(agents)) env.run(agents) # Save the HTML recording in the videos folder game_recording = env.render(mode="html", width=800, height=600) videos_folder = os.path.join(agents_folder, '../Videos')
f.write(" {}".format(line)) # f.write(f' state_dict = {state_dict}\n') # TODO: without loading the state dict the params are initialized randomly f.write( f' action_space = {action_space}\n' + f' observation_space = {observation_space}\n' + ' model = QNet(action_space, observation_space, batch_norm=True)\n' + # ' model.load_state_dict(state_dict)\n' + # TODO: see above ' model.eval()\n' + ' return int(model.get_action(observation))\n') print("model written to", save_file) if __name__ == '__main__': args = _parse() if args.write: env = {'connectx': ConnectX}[args.env]() file = os.path.join('submission', args.env, 'submission.py') load_path = os.path.join('out', args.name, 'models', args.name + '.pt') write_agent_to_file(env, file, QNet, load_path) out = sys.stdout submission = utils.read_file(file) agent = utils.get_last_callable(submission) sys.stdout = out env = make(args.env, debug=True) env.run([agent, agent]) print("Success!" if env.state[0].status == env.state[1]. status == "DONE" else "Failed...")
"My Agent vs Random Agent:", mean_reward(evaluate("connectx", [my_agent, "random"], num_episodes=1000))) print( "My Agent vs Negamax Agent:", mean_reward(evaluate("connectx", [my_agent, "negamax"], num_episodes=10))) import inspect import os os.chdir('E:\\Projects\\04_ConnectX') def write_agent_to_file(function, file): with open(file, "a" if os.path.exists(file) else "w") as f: f.write(inspect.getsource(function)) print(function, "written to", file) write_agent_to_file(my_agent, "submission.py") import sys out = sys.stdout submission = utils.read_file("submission2.py") agent = utils.get_last_callable(submission) sys.stdout = out env = make("connectx", debug=True) env.run([agent, agent]) print("Success!" if env.state[0].status == env.state[1].status == "DONE" else "Failed...")
from kaggle_environments import evaluate, make, utils import sys agent_file = 'submission.py' if len(sys.argv) >= 2: agent_file = sys.argv[1] out = sys.stdout submission = utils.read_file(agent_file) agent = utils.get_last_callable(submission) sys.stdout = out env = make("connectx") # passing timeout configuration to make doesn't construct env properly... # validate against 5s timeout like in kaggle leaderboard env.configuration.timeout = env.configuration.actTimeout = 5 print("Validating agent from {} - this may take a minute...".format(agent_file)) s = env.run([agent, agent]) print("Success!" if env.state[0].status == env.state[1].status == "DONE" else "Failed..."+"\n"+str(s))
import sys from kaggle_environments import evaluate, make, utils #from https://www.kaggle.com/matant/pytorch-dqn-connectx#Write-Submission-File out = sys.stdout infile_path = "submissions/submission_REINFORCE.py" try: submission = utils.read_file(infile_path) agent = utils.get_last_callable(submission) finally: sys.stdout = out env = make("connectx", debug=True) env.run([agent, agent]) print("Success!" if env.state[0].status == env.state[1].status == "DONE" else "Failed...")