Skip to main content
Version: 🚧 Alpha 🚧

10. Headless Training with PPO

By Killian Trouillet


Starting GAMA Headless​

Training uses GAMA's headless server so the simulation runs without a graphical display.

Windows​

gama-headless.bat -socket 6868

Linux / MacOS​

./gama-headless.sh -socket 6868

Wait for the message indicating the server is ready before running the Python script.

Use the same port for the GAMA server and the Python environment. The GAMA-Gymnasium README demonstrates 6868.


Understanding PPO​

PPO (Proximal Policy Optimization) is a policy gradient algorithm. Unlike Q-Learning which learns values for state-action pairs, PPO directly learns a policy - a neural network that maps observations to actions.

PPO vs Q-Learning​

AspectQ-Learning (Part 1)PPO (Part 2)
What it learnsA table of Q-valuesA neural network policy
Action selectionPick max Q-valueSample from policy distribution
Explorationε-greedy (random with decay)Entropy bonus (natural noise)
State spaceFinite (needs a table)Infinite (network generalizes)
Action spaceDiscrete onlyDiscrete or continuous
Update ruleBellman equationGradient ascent on policy

Key PPO Hyperparameters​

ParameterValueMeaning
lr3e-4How fast the network updates
gamma0.99Discount factor (same concept as Part 1)
K_epochs10Iterate 10 times over the collected data
eps_clip0.2Half-width of the probability-ratio interval used by the clipped surrogate objective
ent_coef0.01Entropy bonus - encourages exploration

The ActorCritic Network​

We build a small neural network in PyTorch that outputs both an action (Actor) and a value estimate (Critic):

import torch
from torch import nn

class ActorCritic(nn.Module):
def __init__(self, state_dim=13, action_dim=2, hidden=64):
super().__init__()
self.shared = nn.Sequential(
nn.Linear(state_dim, hidden), nn.Tanh(),
nn.Linear(hidden, hidden), nn.Tanh(),
)
self.actor_mean = nn.Linear(hidden, action_dim)
self.actor_log_std = nn.Parameter(torch.full((action_dim,), -0.5))
self.critic = nn.Linear(hidden, 1)

# Small init - prevents tanh saturation early in training
nn.init.orthogonal_(self.actor_mean.weight, gain=0.01)
nn.init.zeros_(self.actor_mean.bias)

def forward(self, x):
h = self.shared(x)
mean = torch.tanh(self.actor_mean(h)) # actions in [-1, 1]
std = self.actor_log_std.exp().expand_as(mean)
return mean, std, self.critic(h)
  • Shared backbone: Two hidden layers (64 neurons, Tanh) process the observation
  • Actor head: Outputs a mean action vector bounded to [-1, 1] by tanh. Small orthogonal initialization keeps outputs near 0 early in training, preventing gradient saturation
  • Critic head: Outputs a single value estimate (how good is this state?)
  • Normal distribution: Actions are sampled from Normal(mean, std), giving smooth continuous control

The sampled Normal value is not bounded by the tanh applied to its mean. A PPO implementation for the [-1, 1] action space must constrain sampled actions and calculate the corresponding log probability consistently; that action-distribution code is not included here.


The PPO Update​

Each episode, we collect a trajectory (states, actions, rewards), then:

  1. Compute discounted returns: future reward from each step
  2. Compute advantages: returns - value estimates (how much better was reality vs prediction)
  3. Run K gradient epochs: update the network using the PPO clipped objective
ratio = exp(new_log_prob - old_log_prob)
surr1 = ratio * advantages
surr2 = clamp(ratio, 1 - eps, 1 + eps) * advantages
loss = -min(surr1, surr2) + vf_coef * value_loss - ent_coef * entropy

This sketch omits the tensor operations, value loss, entropy calculation and optimization steps required by a working implementation. The clipping limits the policy update measured by the probability ratio; PPO still needs the rest of the objective and update procedure.


The Training Script​

Connecting to GAMA​

import gymnasium as gym
import gama_gymnasium # registers the environment

env = gym.make(
"gama_gymnasium_env/GamaEnv-v0",
gaml_experiment_path="path/to/your_forager_model.gaml",
gaml_experiment_name="gym_env",
gama_ip_address="localhost",
gama_port=6868,
)

Training Loop​

agent = PPOAgent(state_dim=13, action_dim=2)
buffer = RolloutBuffer()
UPDATE_EVERY = 2048

total_steps = 0
for ep in range(1, NUM_EPISODES + 1):
obs, _ = env.reset()
done = False
step = 0

while not done and step < 300:
action, log_prob, value = agent.select_action(obs)
next_obs, reward, terminated, truncated, _ = env.step(action)
done = terminated or truncated

buffer.states.append(torch.FloatTensor(obs))
buffer.actions.append(torch.FloatTensor(action))
buffer.logprobs.append(torch.tensor(log_prob))
buffer.values.append(torch.tensor(value))
buffer.rewards.append(reward)
# Keep the two episode flags separate for return and value bootstrapping.
buffer.terminated.append(terminated)
buffer.truncated.append(truncated)
buffer.next_observations.append(torch.FloatTensor(next_obs))

obs = next_obs
step += 1
total_steps += 1

# PPO update every UPDATE_EVERY steps (accumulates data across episodes)
if total_steps >= UPDATE_EVERY:
agent.update(buffer)
buffer.clear()
total_steps = 0

if buffer.states:
agent.update(buffer)

agent.save("saved_models/ppo_forager.pth")
env.close()

done above controls when Python resets the episode. The omitted rollout and update implementations must keep the termination flags distinct: a terminal state does not bootstrap, while a time-limit truncation uses the final observation's value estimate. The buffer fields shown illustrate the information required; this page does not define a runnable PPOAgent or RolloutBuffer.


Running the Training​

Training results​

The training result depends on the chosen model, environment settings and random seed. Use the episode rewards to inspect whether the policy is learning; do not treat a particular reward trajectory as guaranteed.


The code on this page is illustrative: it does not define a complete PPOAgent, RolloutBuffer, the training constants or a runnable Forager PPO script. The GAML environment itself is listed in Step 9. For complete runnable examples of the package interface, see the GAMA-Gymnasium basic example and its CartPole DQN example. The CartPole example uses DQN and a different GAMA model; it is not the Forager PPO program shown here.

The illustrative PPO components have these roles:

ComponentPurpose
ActorCriticNeural network (shared backbone + actor/critic heads)
RolloutBufferStores trajectory data (states, actions, rewards, etc.)
PPOAgentWraps the network with action selection + PPO update