Ejemplo n.º 1
0
def get_many(client: Client) -> List[Dict]:
    """Gets agents belonging to authenticated user.

    Parameters:
        client (Client): Authenticated client.
    
    Returns:
        List of agents.

    """
    response = client.get(f'{AGENTS_PREFIX}/')
    response_raise_error_if_any(response)
    return response.json()
Ejemplo n.º 2
0
def create(client: Client, league_create: LeagueCreate) -> Dict:
    """Creates an league with specified configuration.

    Parameters:
        client (Client): Authenticated client.
        league_create (LeagueCreate): League create instance.
    
    Returns:
        Details of an league.

    """
    response = client.post(f'{LEAGUE_PREFIX}/', data=asdict(league_create))
    response_raise_error_if_any(response)
    return response.json()
Ejemplo n.º 3
0
def delete(client: Client, agent_name: str) -> bool:
    """Deletes specified agent.

    Parameters:
        client (Client): Authenticated client.
        agent_name (str): Name of agent to delete.

    Returns:
        Whether agent was delete. True if an agent was delete, False if there was no such agent.

    """
    response = client.delete(f'{AGENTS_PREFIX}/{agent_name}')
    response_raise_error_if_any(response)
    return response.status_code == 202
Ejemplo n.º 4
0
def get_loss(client: Client, agent_name: str) -> Dict:
    """Recent loss metrics.

    Parameters:
        client (Client): Authenticated client.
        agent_name (str): Name of agent.
    
    Returns:
        Loss metrics in a dictionary.

    """
    response = client.get(f'{AGENTS_PREFIX}/{agent_name}/loss')
    response_raise_error_if_any(response)
    return response.json()
Ejemplo n.º 5
0
def get(client: Client, league_name: str) -> Dict:
    """Get indepth information about a specific league.

    Parameters:
        client (Client): Authenticated client.
        league_name (str): Name of league.
    
    Returns:
        Details of an league.

    """
    response = client.get(f'{LEAGUE_PREFIX}/{league_name}')
    response_raise_error_if_any(response)
    return response.json()
Ejemplo n.º 6
0
def reset(client: Client, env_name: str) -> List[float]:
    """Resets the environment to starting position.

    Parameters:
        client (Client): Authenticated client.
        env_name (str): Name of the environment.
    
    Returns:
        Environment state in the starting position.

    """
    response = client.post(f"{ENV_PREFIX}/{env_name}/reset")
    response_raise_error_if_any(response)
    return response.json()
Ejemplo n.º 7
0
def delete(client: Client, env_name: str) -> bool:
    """Deletes specified environment.

    Parameters:
        client (Client): Authenticated client.
        env_name (str): Name of the environment.

    Returns:
        Whether environment was delete. True if an environment was delete, False if there was no such environment.

    """
    response = client.delete(f'{ENV_PREFIX}/{env_name}')
    response_raise_error_if_any(response)
    return response.status_code == 202
Ejemplo n.º 8
0
def get(client: Client, env_name: str) -> Dict:
    """Get indepth information about a specific environment.

    Parameters:
        client (Client): Authenticated client.
        env_name (str): Name of environment.
    
    Returns:
        Details of an environment.

    """
    response = client.get(f'{ENV_PREFIX}/{env_name}')
    response_raise_error_if_any(response)
    return response.json()
Ejemplo n.º 9
0
def get(client: Client, agent_name: str) -> Dict:
    """Get indepth information about a specific agent.

    Parameters:
        client (Client): Authenticated client.
        agent_name (str): Name of agent.
    
    Returns:
        Details of an agent.

    """
    response = client.get(f'{AGENTS_PREFIX}/{agent_name}')
    response_raise_error_if_any(response)
    return response.json()
Ejemplo n.º 10
0
def delete(client: Client, league_name: str) -> bool:
    """Deletes specified league.

    Parameters:
        client (Client): Authenticated client.
        league_name (str): Name of the league.

    Returns:
        Whether league was delete. True if an league was delete, False otherwise.

    """
    response = client.delete(f'{LEAGUE_PREFIX}/{league_name}')
    response_raise_error_if_any(response)
    return response.status_code == 202
Ejemplo n.º 11
0
def start(client: Client, league_name: str, config: Optional[Dict] = None) -> str:
    """Starts league, i.e. creates experiments with provided agents and environments.

    Parameters:
        client (Client): Authenticated client.
        league_name (str): Name of the league.
    
    Returns:
        Information about started league.
    
    """
    config = config or {}
    response = client.post(f"{LEAGUE_PREFIX}/{league_name}/start", data=config)
    response_raise_error_if_any(response)
    return "Started successfully" if response.ok else "Failed to start"
Ejemplo n.º 12
0
def step(client: Client, env_name: str, step) -> Dict[str, Any]:
    """Steps the environment based on provided data.

    Parameters:
        client (Client): Authenticated client.
        env_name (str): Name of the environment.
        step (EnvStep): Step information for the environment. Consists of action details and whether to commit.
    
    Returns:
        Environment state after taking provided actions. Consists of "observation", "reward", "done" and "info".

    """
    response = client.post(f"{ENV_PREFIX}/{env_name}/step", data=step)
    response_raise_error_if_any(response)
    return response.json()
Ejemplo n.º 13
0
def create(client: Client, env_create: EnvironmentCreate) -> Dict:
    """Creates an environment with specified configuration.

    Parameters:
        client (Client): Authenticated client.
        config (dict): Configuration of an environment.
    
    Returns:
        Details of an environment.

    """
    env_create_dict = asdict(env_create)
    response = client.post(f'{ENV_PREFIX}/', data=env_create_dict)
    response_raise_error_if_any(response)
    return response.json()
Ejemplo n.º 14
0
def reset(client: Client, league_name: str) -> str:
    """Resets the league to starting position.

    Doesn't affect Agent nor Environment. Only resets values related to the League.

    Parameters:
        client (Client): Authenticated client.
        league_name (str): Name of the league.
    
    Returns:
        Confirmation on reset league.

    """
    response = client.post(f"{LEAGUE_PREFIX}/{league_name}/reset")
    response_raise_error_if_any(response)
    return response.json()
Ejemplo n.º 15
0
def step(client, agent_name: str, step: Dict) -> None:
    """Steps forward in agents learning mechanism.

    This method is used to provide learning data, like recent observations and rewards,
    and trigger learning mechanism. *Note* that majority of agents perform `step` after
    every environment iteration (`step`).

    Parameters:
        client (Client): Authenticated client.
        agent_name (str): Name of agent.
        step (Dict): Data required for the agent to use in learning.
            Likely that's `observation`, `next_observation`, `reward`, `action` values and `done` flag.
    
    """
    response = client.post(f"{AGENTS_PREFIX}/{agent_name}/step", data=step)
    response_raise_error_if_any(response)
    return
Ejemplo n.º 16
0
def commit(client: Client, env_name: str) -> Dict[str, Any]:
    """Commits last provided data. Must be proceeded by environment `step`.

    Useful when environment requires many agents or when agent is allowed to make mistakes.

    Parameters:
        client (Client): Authenticated client.
        env_name (str): Name of the environment
    
    Returns:
        Data about the state the environment has transtioned into.
        This should be the same as when using `step` with `commit=True`.
    
    """
    response = client.post(f"{ENV_PREFIX}/{env_name}/commit")
    response_raise_error_if_any(response)
    return response.json()
Ejemplo n.º 17
0
def create(client: Client, agent_create: AgentCreate) -> Dict:
    """Creates an agent with specified configuration.

    Parameters:
        client (Client): Authenticated client.
        config (dict): Configuration of an agent.
    
    Returns:
        Details of an agent.

    """
    agent_create_dict = asdict(
        agent_create,
        dict_factory=lambda x: {k: v
                                for (k, v) in x if v is not None})
    response = client.post(f'{AGENTS_PREFIX}/', data=agent_create_dict)
    response_raise_error_if_any(response)
    return response.json()
Ejemplo n.º 18
0
def act(client,
        agent_name: str,
        obs: Dict,
        params: Optional[Dict] = None) -> Dict:
    """Asks agent about its action on provided observation.

    Parameters:
        client (Client): Authenticated client.
        agent_name (str): Name of agent.
        obs (Dict): Observation from current environment state.
        params (Optional dict): Anything useful for the agent to learn, e.g. epsilon greedy value.
    
    Returns:
        Dictionary container actions.
    
    """
    response = client.post(f"{AGENTS_PREFIX}/{agent_name}/act",
                           obs,
                           params=params)
    response_raise_error_if_any(response)
    return response.json()
Ejemplo n.º 19
0
def metrics(
    client: Client, league_name: str, metric_names: Optional[List[str]] = None, limit: int = 1,
) -> Dict[str, List[Tuple[int, float]]]:
    """Gets metrics obtained while running an league.

    Parameters:
        client (Client): Authenticated client.
        league_name (str): Name of the league.
        metric_names (Optional list of strings): List of metrics you are intrested in seeing.
            If None then it'll return all available. Defaults to None.
        limit (int): Number of last samples to return. Defaults to only the most recent metrics.
    
    Returns:
        Dictionary with keys being metric names and values in a list consisting of an index and value (tuple).
        For example:
            {
                "episode/score": [(10, -10), (9, -7), (8, 3)],
                "loss/actor": [(10, 1.5), (9, 4.1), (8, 0.99)]
            }
    
    """
    response = client.post(f"{LEAGUE_PREFIX}/{league_name}/metrics", data=metric_names, params=dict(limit=limit))
    response_raise_error_if_any(response)
    return response.json()
Ejemplo n.º 20
0
def info(client: Client, env_name: str) -> Dict[str, Any]:
    response = client.get(f"{ENV_PREFIX}/{env_name}/info")
    response_raise_error_if_any(response)
    return response.json()