Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a method add_agents() to all multiagents domain + fix paper-rock-scissors multiagent domain #283

Merged
merged 4 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions skdecide/builders/domain/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

from __future__ import annotations

from typing import Union
from typing import Set, Union

from skdecide.core import StrDict
from skdecide.core import SINGLE_AGENT_ID, StrDict

__all__ = ["MultiAgent", "SingleAgent"]

Expand All @@ -19,8 +19,21 @@ class MultiAgent:

T_agent = StrDict

def get_agents(self) -> Set[str]:
"""Return the set of available agents ids."""
return set(self.get_observation_space())


class SingleAgent(MultiAgent):
"""A domain must inherit this class if it is single-agent (i.e hosting only one agent)."""

T_agent = Union

def get_agents(self) -> Set[str]:
"""Return a singleton for single agent domains.

We must be here consistent with `skdecide.core.autocast()` which transforms a single agent domain
into a multi agents domain whose only agent has the id "agent".

"""
return {SINGLE_AGENT_ID}
6 changes: 5 additions & 1 deletion skdecide/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,8 @@ def cast_evaluate_function(memory, action, next_state):
)


SINGLE_AGENT_ID = "agent"

# (auto)cast-related objects/functions
cast_dict = {
(Memory, Union): lambda obj, src, dst: cast(obj[0], src[0], dst[0]),
Expand All @@ -686,7 +688,9 @@ def cast_evaluate_function(memory, action, next_state):
(StrDict, Union): lambda obj, src, dst: cast(
next(iter(obj.values())), src[0], dst[0]
),
(Union, StrDict): lambda obj, src, dst: {"agent": cast(obj, src[0], dst[0])},
(Union, StrDict): lambda obj, src, dst: {
SINGLE_AGENT_ID: cast(obj, src[0], dst[0])
},
(StrDict, StrDict): lambda obj, src, dst: {
k: cast(v, src[0], dst[0]) for k, v in obj.items()
},
Expand Down
17 changes: 4 additions & 13 deletions skdecide/hub/domain/rock_paper_scissors/rock_paper_scissors.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ class RockPaperScissors(D):
def __init__(self, max_moves: int = 10):
self._max_moves = max_moves

def get_agents(self):
return {"player1", "player2"}

def _state_step(
self, action: D.T_agent[D.T_concurrency[D.T_event]]
) -> TransitionOutcome[
Expand Down Expand Up @@ -79,7 +82,7 @@ def _state_step(
return TransitionOutcome(
state=State(num_move=num_move),
value={"player1": Value(reward=r1), "player2": Value(reward=r2)},
termination=(num_move >= self._max_moves),
termination={k: (num_move >= self._max_moves) for k in self.get_agents()},
)

def _get_action_space_(self) -> D.T_agent[Space[D.T_event]]:
Expand All @@ -100,15 +103,3 @@ def _get_observation(

def _get_observation_space_(self) -> D.T_agent[Space[D.T_observation]]:
return {"player1": EnumSpace(Move), "player2": EnumSpace(Move)}


if __name__ == "__main__":
from skdecide.utils import rollout

domain = RockPaperScissors()
rollout(
domain,
action_formatter=lambda a: str({k: v.name for k, v in a.items()}),
outcome_formatter=lambda o: f"{ {k: v.name for k, v in o.observation.items()} }"
f" - rewards: { {k: v.reward for k, v in o.value.items()} }",
)
20 changes: 20 additions & 0 deletions tests/autocast/test_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from skdecide import Domain, RLDomain
from skdecide.core import autocast_all


def test_get_agents_with_autocast():
class MyDomain(RLDomain):
"""Partially defined single agent domain."""

def _get_observation_space_(self):
return {1, 2, 3}

# single agent domain
domain = MyDomain()

# up-cast as multi-agent domain
upcast_domain = MyDomain()
autocast_all(upcast_domain, RLDomain, Domain)

assert domain.get_agents() == Domain.get_agents(upcast_domain)
assert domain.get_agents() != Domain.get_agents(domain)
14 changes: 14 additions & 0 deletions tests/domains/test_rock_paper_scissors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from skdecide.hub.domain.rock_paper_scissors.rock_paper_scissors import (
RockPaperScissors,
)
from skdecide.utils import rollout


def test_rock_paper_scissors():
domain = RockPaperScissors()
rollout(
domain,
action_formatter=lambda a: str({k: v.name for k, v in a.items()}),
outcome_formatter=lambda o: f"{ {k: v.name for k, v in o.observation.items()} }"
f" - rewards: { {k: v.reward for k, v in o.value.items()} }",
)