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.")
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') Path(videos_folder).mkdir(parents=True, exist_ok=True) video_path = os.path.join(videos_folder, video_name+'.html')
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...")