def _run(record_pngs_dir=None, record_json_dir=None): '''Runs a game''' print("Starting the Game.") if record_pngs_dir and not os.path.isdir(record_pngs_dir): os.makedirs(record_pngs_dir) if record_json_dir and not os.path.isdir(record_json_dir): os.makedirs(record_json_dir) obs = env.reset() done = False while not done: if args.render: env.render(record_pngs_dir=record_pngs_dir, record_json_dir=record_json_dir, do_sleep=do_sleep) actions = env.act(obs) obs, reward, done, info = env.step(actions) print("Final Result: ", info) if args.render: env.render(record_pngs_dir=record_pngs_dir, record_json_dir=record_json_dir, do_sleep=do_sleep) if do_sleep: time.sleep(5) env.render(close=True) if record_json_dir: finished_at = datetime.now().isoformat() _agents = args.agents.split(',') utility.join_json_state(record_json_dir, _agents, finished_at, config, info) return info
def save2json(replay_path, n_player=4, agent_names=range(4)): with open(replay_path, "rb") as file: replay_data = pickle.load(file) from pommerman.utility import join_json_state path, name = os.path.split(replay_path) t = name.strip('.pkl')[-26:] print(name) finished_at = datetime.strptime(t, "%Y-%m-%d %H:%M:%S.%f").isoformat() record_json_dir = os.path.join(path, name.split('.')[0]) if not os.path.exists(record_json_dir): os.mkdir(record_json_dir) env = pommerman.make(replay_data['mode'], [agents.BaseAgent() for _ in range(n_player)]) env.reset() env._board = np.array(replay_data["board"]) env._items = replay_data["items"] reward = None for i in replay_data["actions"]: env.save_json(record_json_dir=record_json_dir) reward, done, info = env.step(i)[1:] if done: env.save_json(record_json_dir=record_json_dir) join_json_state(record_json_dir=record_json_dir, agents=[str(i) for i in agent_names], finished_at=finished_at, config=replay_data['mode'], info=info) time.sleep(1) break if reward != replay_data["reward"]: print(reward) raise Exception("The current reward doesn't match the expected reward") env.close()
def run(env, agent_names, config, render, do_sleep, record_pngs_dir=None, record_json_dir=None): '''Runs a game''' if record_pngs_dir and not os.path.isdir(record_pngs_dir): os.makedirs(record_pngs_dir) if record_json_dir and not os.path.isdir(record_json_dir): os.makedirs(record_json_dir) obs = env.reset() done = False observations = [] steps = 0 while not done: if render: env.render(record_pngs_dir=record_pngs_dir, record_json_dir=record_json_dir, do_sleep=do_sleep) if render is False and record_json_dir: env.save_json(record_json_dir) time.sleep(1.0 / env._render_fps) actions = env.act(obs) steps += 1 obs, reward, done, info = env.step(actions) if max(reward) > 0 and done is False: raise ValueError('Why?????????????????????') observations.append(obs) if render: env.render(record_pngs_dir=record_pngs_dir, record_json_dir=record_json_dir, do_sleep=do_sleep) print(f'game end: reward={reward}') if do_sleep: time.sleep(5) else: time.sleep(5) env.render(close=True) if render is False and record_json_dir: env.save_json(record_json_dir) time.sleep(1.0 / env._render_fps) if record_json_dir: finished_at = datetime.now().isoformat() utility.join_json_state(record_json_dir, agent_names, finished_at, config, info) return info, steps, observations, reward
def _run(seed, acting_agent_ids, record_pngs_dir=None, record_json_dir=None): obs = env.reset() steps = 0 done = False acting_agent_ids = acting_agent_ids or [] while not done: steps += 1 if args.render: env.render(record_pngs_dir=record_pngs_dir, record_json_dir=record_json_dir, mode=render_mode) actions = env.act(obs, acting_agent_ids=acting_agent_ids) for agent_id in acting_agent_ids: agent_obs = obs[agent_id] action = agents[agent_id].act(agent_obs, env.action_space) actions.insert(agent_id, action) obs, reward, done, info = env.step(actions) if type(done) == list: done = all(done) for agent in agents: agent.episode_end(reward[agent.agent_id]) print("Final Result: ", info) if args.render: env.render(record_pngs_dir=args.record_pngs_dir, record_json_dir=args.record_json_dir, mode=args.render_mode) time.sleep(5) env.render(close=True) if record_json_dir: finished_at = datetime.now().isoformat() _agents = args.agents.split(',') utility.join_json_state(record_json_dir, _agents, finished_at, config) return info
def _run(record_pngs_dir=None, record_json_dir=None): '''Runs a game''' print("Starting the Game.") if record_pngs_dir and not os.path.isdir(record_pngs_dir): os.makedirs(record_pngs_dir) if record_json_dir and not os.path.isdir(record_json_dir): os.makedirs(record_json_dir) obs = env.reset() done = False # send environment information to Message server url = 'http://localhost:{}/envinfo'.format(args.messaging_port) print("sending envinfo to {}".format(url)) envinfo = env.spec._kwargs for key, value in envinfo.items(): envinfo[key] = json.dumps(value, cls=utility.PommermanJSONEncoder) send_json(envinfo, url) # send the initial observations to human-remote-control agents env.notify_obs(obs, waiting=True) # send jsonified state to Messaging server url = 'http://localhost:{}/initial_obs'.format(args.messaging_port) print("sending jsonified state to {}".format(url)) jsonified_state = env.get_json_info() send_json(jsonified_state, url) while not done: if args.render: env.render(record_pngs_dir=record_pngs_dir, record_json_dir=record_json_dir, do_sleep=do_sleep) if args.render is False and record_json_dir: env.save_json(record_json_dir) time.sleep(1.0 / env._render_fps) # get actions from all agents env.notify_obs(obs, waiting=True) actions = env.act(obs) obs, reward, done, info = env.step(actions) # send jsonified state to Messaging server url = 'http://localhost:{}/step'.format(args.messaging_port) print("sending jsonified state to {}".format(url)) jsonified_state = env.get_json_info() jsonified_state['done'] = json.dumps( done, cls=utility.PommermanJSONEncoder) # add done flag send_json(jsonified_state, url) info_ = deepcopy(info) info_['result'] = str( info_['result']) # needs to stringify Result class send_json(json.dumps(info_, cls=utility.PommermanJSONEncoder), 'http://localhost:{}/final_info'.format(args.messaging_port)) # send the final observations to human-remote-control agents env._is_partially_observable = False # temporary make it fully observable final_obs = env.get_observations() env.notify_obs(final_obs, waiting=False) print("Final Result: ", info) if args.render: env.render(record_pngs_dir=record_pngs_dir, record_json_dir=record_json_dir, do_sleep=do_sleep) if do_sleep: time.sleep(5) env.render(close=True) if args.render is False and record_json_dir: env.save_json(record_json_dir) time.sleep(1.0 / env._render_fps) if record_json_dir: finished_at = datetime.now().isoformat() _agents = args.agents.split(',') utility.join_json_state(record_json_dir, _agents, finished_at, config, info) return info