Ejemplo n.º 1
0
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"]),
        "timeout":
        utils.get(args, int, 10, ["timeout"]),
        "host":
        utils.get(args, str, "127.0.0.1", ["host"]),
        "port":
        utils.get(args, int, 8000, ["port"]),
        "use_subprocess":
        utils.get(args, bool, True, ["subprocess-agents"])
    })
Ejemplo n.º 2
0
def load_latest_ship_attr(mainattr='ships', attr='halite'):
    match, config = load_latest()
    record = {}
    for pid in range(4):
        record[pid] = {}
    for step in range(400):
        state = match['steps'][step][0]  # list of length 1 for each step
        obs = state['observation']  # these are observations at this step
        for pid in range(4):
            qps = get_quadrant_points(pid)
            obs['player'] = pid  # change the player to the one we want to inspect
            precord = record[pid]
            obs = structify(
                obs)  # turn the dict's into structures with attributes
            b = Board(obs, config)
            iterable = getattr(
                b, mainattr).values() if mainattr == 'cells' else b.me
            keyattr = 'position' if mainattr == 'cells' else 'id'
            for s in iterable:
                if attr == 'position' and s.halite > 0:
                    continue
                if mainattr == 'cells' and s.halite == 0 or s.position not in qps:
                    continue
                key = getattr(s, keyattr)
                if key not in precord:
                    precord[key] = {}
                    precord[key][step] = getattr(s, attr)
                else:
                    precord[key][step] = getattr(s, attr)
    return record
Ejemplo n.º 3
0
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}
Ejemplo n.º 4
0
def replay_match(path, step=0):
    """Replay game to a specific step - necessary for recreating stateful values."""
    with open(path,encoding='ascii') as f:
        match = json.load(f)
    env = make("halite", configuration=match['configuration'], steps=match['steps'])
    myid = [pid for pid, name in enumerate(match['info']['TeamNames']) if name == "Ready Salted"][0]
    config = env.configuration
    # env already done - can write out full replay
    t = env.render(mode='html', return_obj=True, width=800, height=800, header=False, controls=True)
    write_html(t, 'replay.html')
    # If agent carries state across turns, need to run through all steps, or can directly index into a step otherwise
    # check that we are correct player
    # print('My Id: ', board.current_player_id, board.current_player)
    print(f'Running for:\n\t{path}\n\t{agent.__module__}\n\tID = {myid}\n')
    actTime = 0
    for step in range(400):
        state = match['steps'][step][0]  # list of length 1 for each step
        obs = state['observation']  # these are observations at this step
        obs['player'] = myid  # change the player to the one we want to inspect
        obs = structify(obs)  # turn the dict's into structures with attributes
        icon = '\|/-'[(obs.step+1) % 4]
        t0 = time.time()
        ret = agent(obs, config)
        actTime = time.time() - t0
        print(f'{icon} step+1: {obs.step +1} StepTime:{round(actTime,2)}')#, end="\r", flush=True)
def test_can_step_through_agents():
    before_each()
    while not env.done:
        action1 = env.agents.random(env.state[0].observation)
        action2 = env.agents.reaction(
            utils.structify({"board": env.state[0].observation.board, "mark": 2}))
        env.step([action1, action2])
    assert env.state[0].reward + env.state[1].reward == 0
Ejemplo n.º 6
0
    def test_football_env_run_30_steps(self):
        env = KaggleFootballMultiAgentEnv()

        # use the built-in agents in the kaggle environment
        run_right_agent = env.kaggle_env.agents["run_right"]
        do_nothing_agent = env.kaggle_env.agents["do_nothing"]

        obs = env.reset()
        self.assertEqual(list(obs.keys()), ["agent0", "agent1"])
        done = {"__all__": False}
        num_steps_completed = 0
        while not done["__all__"] and num_steps_completed <= 30:
            action0 = run_right_agent(structify(obs["agent0"]))[0]
            action1 = do_nothing_agent(structify(obs["agent1"]))[0]
            action_dict = {"agent0": action0, "agent1": action1}
            obs, _, done, _ = env.step(action_dict)
            num_steps_completed += 1
Ejemplo n.º 7
0
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()}
Ejemplo n.º 8
0
def interpreter(state, env):
    obs = state[0].observation
    config = env.configuration

    # Initialize the board (place cell halite and starting ships).
    if env.done:
        return populate_board(state, env)

    # Interpreter invoked here
    actions = [agent.action for agent in state]
    board = Board(obs, config, actions)
    board = board.next()
    state[0].observation = obs = utils.structify(board.observation)

    # Remove players with invalid status or insufficient potential.
    for index, agent in enumerate(state):
        player_halite, shipyards, ships = obs.players[index]
        if agent.status == "ACTIVE" and len(ships) == 0 and (
                len(shipyards) == 0 or player_halite < config.spawnCost):
            # Agent can no longer gather any halite
            agent.status = "DONE"
            agent.reward = board.step - board.configuration.episode_steps - 1
        if agent.status != "ACTIVE" and agent.status != "DONE":
            obs.players[index] = [0, {}, {}]

    # Check if done (< 2 players and num_agents > 1)
    if len(state) > 1 and sum(
            1 for agent in state if agent.status == "ACTIVE") < 2:
        for agent in state:
            if agent.status == "ACTIVE":
                agent.status = "DONE"

    # Update Rewards.
    for index, agent in enumerate(state):
        if agent.status == "ACTIVE":
            agent.reward = obs.players[index][0]
        elif agent.status != "DONE":
            agent.reward = 0

    return state