(players, queue_subproc, mode)
Handles running of the match loop
Handles running of the match loop
Handles running of the match loop
def thread(players, queue_subproc, mode):
    """Handles running of the match loop"""
    uuid_ = unique_uuid("matches")
    base_agent = pommerman.agents.BaseAgent
    env = pommerman.make(
        mode,
        [base_agent(), base_agent(),
         base_agent(), base_agent()])
    net, net_end = multiprocessing.Pipe()
    queue_subproc.put([net_end, players, uuid_])
    obs = env.reset()
    record = {
        "board": numpy.array(env._board, copy=True).tolist(),
        "actions": [],
        "mode": str(mode)
    }
    done = False
    while not done:
        obs_res = resolve_classes(obs.copy())
        turn_id = str(uuid.uuid4())[:5]
        try:
            obs_bytes = []
            for key, value in enumerate(obs_res):
                if 10 + key in obs[0]["alive"]:
                    obs_bytes.append(
                        gzip.compress(
                            bytes(
                                rapidjson.dumps({
                                    "o": value,  # o = obs
                                    "i": turn_id,  # i = Turn ID
                                    "d": False  # d = Dead
                                }),
                                "utf8")))
                else:
                    obs_bytes.append(
                        gzip.compress(
                            bytes(
                                rapidjson.dumps({
                                    "d": True  # d = Dead
                                }),
                                "utf8")))
            net.send([
                constants.SubprocessCommands.match_next.value, turn_id,
                obs_bytes,
                len(obs[0]["alive"])
            ])
            act = net.recv()
        except:
            act = [0, 0, 0, 0]
        record["actions"].append(numpy.array(act, copy=True).tolist())
        obs, rew, done = env.step(act)[:3]
    record["reward"] = rew
    env.close()
    with open("./matches/" + uuid_ + ".json", "w") as file:
        rapidjson.dump(record, file)
    net.send([constants.SubprocessCommands.match_end.value, rew])
    net.recv()
    exit(0)
def thread ( players , queue_subproc , mode ) : uuid_ = unique_uuid ( "matches" ) base_agent = pommerman . agents . BaseAgent env = pommerman . make ( mode , [ base_agent ( ) , base_agent ( ) , base_agent ( ) , base_agent ( ) ] ) net , net_end = multiprocessing . Pipe ( ) queue_subproc . put ( [ net_end , players , uuid_ ] ) obs = env . reset ( ) record = { "board" : numpy . array ( env . _board , copy = True ) . tolist ( ) , "actions" : [ ] , "mode" : str ( mode ) } done = False while not done : obs_res = resolve_classes ( obs . copy ( ) ) turn_id = str ( uuid . uuid4 ( ) ) [ : 5 ] try : obs_bytes = [ ] for key , value in enumerate ( obs_res ) : if 10 + key in obs [ 0 ] [ "alive" ] : obs_bytes . append ( gzip . compress ( bytes ( rapidjson . dumps ( { "o" : value , # o = obs "i" : turn_id , # i = Turn ID "d" : False # d = Dead } ) , "utf8" ) ) ) else : obs_bytes . append ( gzip . compress ( bytes ( rapidjson . dumps ( { "d" : True # d = Dead } ) , "utf8" ) ) ) net . send ( [ constants . SubprocessCommands . match_next . value , turn_id , obs_bytes , len ( obs [ 0 ] [ "alive" ] ) ] ) act = net . recv ( ) except : act = [ 0 , 0 , 0 , 0 ] record [ "actions" ] . append ( numpy . array ( act , copy = True ) . tolist ( ) ) obs , rew , done = env . step ( act ) [ : 3 ] record [ "reward" ] = rew env . close ( ) with open ( "./matches/" + uuid_ + ".json" , "w" ) as file : rapidjson . dump ( record , file ) net . send ( [ constants . SubprocessCommands . match_end . value , rew ] ) net . recv ( ) exit ( 0 )