8. The GymAgent bridge
The gama-gymnasium package connects a GAMA model to the Gymnasium API. The GAMA model exposes its action space, observation space and transition results through an agent species named GymAgent.
Define GymAgent​
The package's model interface uses these fields and the update_data action:
species GymAgent {
map<string, unknown> action_space;
map<string, unknown> observation_space;
unknown state;
float reward;
bool terminated;
bool truncated;
map<string, unknown> info;
unknown next_action;
map<string, unknown> data;
action update_data {
data <- [
"State"::state,
"Reward"::reward,
"Terminated"::terminated,
"Truncated"::truncated,
"Info"::info
];
}
}
Set action_space and observation_space when creating the bridge agent. Their descriptions must match the actions and observations used by the model. This tutorial's continuous forager uses a two-value action and a 13-value observation; the next step defines how the forager calculates them.
After the model updates the state, reward and episode flags, call update_data so the bridge can return those values to Python. Keep the data keys and capitalization shown in the model interface.
Run the bridge​
Install gama-gymnasium and its dependencies, then start GAMA's headless server on a port chosen for the environment. The package README currently demonstrates port 6868:
gama-headless.sh -socket 6868
On Windows, use gama-headless.bat -socket 6868. The Python environment connects to the same port. See the package README for its current Python call and requirements.
Complete Model​
This listing carries the continuous world from step 7 forward and adds a GymAgent bridge. It initializes the action and observation spaces and publishes the initial observation. Step 9 completes the forager behavior and updates the bridge after each simulation step.
/**
* Name: SmartForagerGym - Step 8: The GymAgent Bridge
* Description: Continuous forager world with the Gymnasium bridge interface.
* Tags: reinforcement-learning, gymnasium, continuous, tutorial
*/
model SmartForagerGym
global {
float world_size <- 100.0;
point food_location <- {95.0, 95.0};
float food_radius <- 5.0;
list<geometry> obstacles <- [];
int max_steps <- 300;
int current_step <- 0;
init {
obstacles << square(10) at_location {25.0, 25.0};
obstacles << square(10) at_location {35.0, 25.0};
obstacles << square(10) at_location {25.0, 35.0};
obstacles << square(10) at_location {65.0, 45.0};
obstacles << square(10) at_location {75.0, 45.0};
obstacles << square(10) at_location {75.0, 55.0};
create GymAgent {
action_space <- [
"type"::"Box",
"low"::[-1.0, -1.0],
"high"::[1.0, 1.0],
"dtype"::"float"
];
observation_space <- [
"type"::"Box",
"low"::list_with(13, 0.0),
"high"::list_with(13, 1.0),
"dtype"::"float"
];
state <- list_with(13, 0.0);
}
create forager number: 1 {
location <- {5.0, 5.0};
}
ask GymAgent[0] {
do update_data();
}
}
}
species GymAgent {
map<string, unknown> action_space;
map<string, unknown> observation_space;
unknown state;
float reward <- 0.0;
bool terminated <- false;
bool truncated <- false;
map<string, unknown> info <- [];
unknown next_action;
map<string, unknown> data <- [];
action update_data {
data <- [
"State"::state,
"Reward"::reward,
"Terminated"::terminated,
"Truncated"::truncated,
"Info"::info
];
}
}
species forager {
aspect default {
draw circle(3) color: #blue;
}
}
experiment gym_env {
output {
display Continuous_World title: "Continuous World" renderer: 2d {
graphics "background" {
draw rectangle(world_size, world_size)
at: {world_size / 2, world_size / 2}
color: rgb(240, 240, 240);
}
graphics "obstacles" {
loop obs over: obstacles {
draw obs color: rgb(80, 80, 80);
}
}
graphics "food" {
draw circle(food_radius) at: food_location color: rgb(50, 180, 50);
}
species forager;
}
}
}