Infrastructure for AI experiments (RL, ML, planning, BDI, multi-agent scenarios, ...). It relies on the multi-agent library Mesa to design gym environments, to be embedded eg. in custom environments for gymnasium.
The infrastructure consists of different modules:
- the
mesa
part is about programming the environment and possibly hard-coding the agents: actions (and disabilities), what happens at performance, etc. When executed it maintains state, produces changes, and returns relevant events; - custom
gymnasium
environments collect the states and compute the rewards (typically from events); - training methods work on top of the
gymnasium
environments.
Structure of mesa-gym
project:
/gyms
contains various gyms built on top of MESA/grid
contains grid-based symbolic worlds/market
contains communication-centered (eg. markets) worlds
/trainers
contains reusable agent learning modules/common
contains helpers, eg. for data visualization
Conventionally, each mesa-gym
world contains:
world.py
: agent and environmental models that run onmesa
. they can be executed standaloneenv.py
: custom environments forgymnasium
building upon the world modelscript.py
: simple script that runs the world, possibly reusing trained modelstraining.py
: learning script, creating behavioural models
To start, you can run, from within a directory in gyms
, eg. grid/zzt_basic
- to execute a multi-agent world, using only
mesa
:
python world.py
- to execute a multi-agent world, using a
gymnasium
custom environment on top ofmesa
, and possibly pre-trained models
python script.py
- to train agents within
gymnasium
python training.py
- to visualize (meta-)data produced during training
python data_viz.py
The map is used to create the computational model of the envinroment, placing elements in the correct position. A view
class can be used to generate a visual representation to follow the execution. The execution is discrete, and follows the convention of Mesa:
model = create_world(map)
view = WorldView(model)
view.init()
while True:
end = model.step()
view.show()
if end:
break
To facilitate problem decomposition, agents may be defined as body (dealing with mechanicistic interventions on and by the environment) and mind (dealing with interpretation/decision mechanisms). As a good practice, given a certain domain, intervention of modelers should be only at mental level, that is, there is a body eg. AgentBody(mesa.Agent)
class that comes with the environment, which is then extended by a mental element.
For instance, the agent may take the perceptions (only a limited portion of the environment), identify the positions by the relative coordinates coming from the perception, and move randomly in any of those:
class RandomWalkingAgent(AgentBody):
def mental_init(self):
pass
def mental_step(self):
percepts = self.get_percepts()
potential_positions = list(percepts.keys())
self.move(self.random.choice(potential_positions))
In principle, one can use this framework to:
- setup reinforcement learning algorithms for creating policies automatically
- setup classificatory algorithms for creating abstractions of perceptual data
- setup hard-coded rules to regulate behaviour
- setup a full-fledged BDI decision-making cycle,
- ...
- create interfaces towards pettingzoo..
The fastest way to install all dependencies is to install mesa_gym
as a package:
pip install -e .
If you want to install packages manually, for the worlds to run:
pip install mesa
To use gymnasium for RL:
pip install gymnasium
For worlds requiring graphics:
pip install pygame
If you require to visualize diagrams
pip install matplotlib