Package: posggym
POSGGymPOSGGym is an open source Python library providing implementations of Partially Observable Stochastic Game (POSG) environments coupled with dynamic models of each environment, all under a unified API. While there are a number of amazing open-source implementations for POSG environments, very few have support for dynamic models that can be used for planning, especially for continuous domains. The aim of this library is to fill this gap.A key goal of POSGGym is to provide easy to use and understand open-source implementations for many of the environments commonly used in the partially observable multi-agent planning literature.POSGGym also provides a collection of reference agents for it's various environments and a unified Agents API for using these agents. These agents can be used to evaluate algorithms, and includes both hand-coded bots and agents trained using reinforcement learning.POSGGym is directly inspired by and adapted from theGymnasiumandPettingZoolibraries for reinforcement learning. The key additions in POSGGym are support for environment models which can be used for planning and reference agents. POSGGym's API aims to stay as close to the Gymnasium and PettingZoo Parallel APIs as possible while incorporating models and agents into the mix.Any POSGGym environment can be converted into a PettingZoo environment using a simple wrapper (posggym.wrappers.petting_zoo.PettingZoo). This allows for easy integration with the ecosystem of libraries that support PettingZoo.DocumentationThe documentation for the project is available atposggym.readthedocs.io/.For a guide to building the documentation locally seedocs/README.md.InstallationPOSGGym supports and tests forPython>=3.8. We recommend using a virtual environment to install POSGGym (e.g.conda,venv).Using pipThe latest release version of POSGGym can be installed usingpipby running:pipinstallposggymThis will install the base dependencies for running the main environments and download the agent models (so may take a few minutes), but but in order to minimise the number of unused dependencies installed may not include all dependencies for all environments or for rendering some environments, and will not include dependencies for running many posggym agents.You can install all dependencies for a family of environments likepip install posggym[grid-world]andpip install posggym[continuous]or dependencies for all environments usingpip install posggym[envs-all].You can install dependencies for POSGGym agents usingpip install posggym[agents]or to install dependencies for all environments and agents usepip install posggym[all].Installing from sourceTo install POSGGym from source, first clone the repository then run:cdposggym
pipinstall-e.This will install the base dependencies and download the agent models (so may take a few minutes). You can optionally install extras as described above. E.g. to install all dependencies for all environments and agents use:pipinstall-e.[all]To run tests, install the test dependencies and then run the tests:pipinstall-e.[test]pytestOr alternatively you can run one of the examples from theexamplesdirectory:pythonexamples/run_random_agents.py--env_idDriving-v0--num_episodes10--render_modehumanEnvironmentsPOSGGym includes the following families of environments (for a full list of environments and their descriptsion see thedocumentation).Classic- These are classic POSG problems from the literature.Grid-World- These environments are all based in a 2D Gridworld.Continuous- 2D environments with continuous state, actions, and observations.Environment APIPOSGGym models each environment as a pythonenvclass. Creating environment instances and interacting with them is very simple, and flows almost identically to the Gymnasium user flow. Here's an example using thePredatorPrey-v0environment:importposggymenv=posggym.make("PredatorPrey-v0")observations,infos=env.reset(seed=42)fortinrange(100):env.render()actions={i:env.action_spaces[i].sample()foriinenv.agents}observations,rewards,terminations,truncations,all_done,infos=env.step(actions)ifall_done:observations,infos=env.reset()env.close()Model APIEvery environment provides access to a model of the environment in the form of a pythonmodelclass. Each model implements a generative model, which can be used for planning, along with functions for sampling initial states. Some environments also implement a full POSG model including the transition, joint observation and joint reward functions.The following is an example of accessing and using the environment model:importposggymenv=posggym.make("PredatorPrey-v0")model=env.modelmodel.seed(seed=42)state=model.sample_initial_state()observations=model.sample_initial_obs(state)fortinrange(100):actions={i:model.action_spaces[i].sample()foriinmodel.get_agents(state)}state,observations,rewards,terminations,truncations,all_done,infos=model.step(state,actions)ifall_done:state=model.sample_initial_state()observations=model.sample_initial_obs(state)The base model API is very similar to the environment API. The key difference that all methods are stateless so can be used repeatedly for planning. Indeed theenvclass for the built-in environments are mainly just a wrappers over the underlyingmodelclass that manage the state and add support for rendering.Note that unlike for theenvclass, for convenience the output of themodel.step()method is adataclassinstance and so it's components can be accessed as attributes. For example:timestep=model.step(state,actions)observations=timestep.observationsinfos=timestep.infosBoth theenvandmodelclasses support a number of other methods, please see the documentationposggym.readthedocs.io/for more details.Agents APIPOSGGym.Agents models each agent as a pythonpolicyclass, which at it's simplest accepts an observation and returns the next action. Here's an example using one of the K-Level Reasoning policies in thePursuitEvasion-v0environment:importposggymimportposggym.agentsaspgaenv=posggym.make("PursuitEvasion-v0",grid="16x16")policies={'0':pga.make("PursuitEvasion-v0/grid=16x16/klr_k1_seed0_i0-v0",env.model,'0'),'1':pga.make("PursuitEvasion-v0/grid=16x16/klr_k1_seed0_i1-v0",env.model,'1')}obs,info=env.reset(seed=42)fori,policyinpolicies.items():policy.reset(seed=7)fortinrange(100):actions={i:policies[i].step(obs[i])foriinenv.agents}obs,rewards,termination,truncated,all_done,info=env.step(actions)ifall_done:obs,info=env.reset()fori,policyinpolicies.items():policy.reset()env.close()forpolicyinpolicies.values():policy.close()For a full explanation of the agent API please see thePOSGGym Agents Getting Started documentation. A full list of implemented agents is also available in the documentation.CitationYou can cite POSGGym as:@misc{schwartzPOSGGym2023,title={POSGGym},urldate={2023-08-08},author={Schwartz, Jonathon and Newbury, Rhys and Kurniawati, Hanna},year={2023},}