11. Evaluate the trained policy
After training, load the saved policy and evaluate it in a new episode. Use the GAMA server and port from step 10; the current GAMA-Gymnasium README documents the headless server workflow.
Connect to the environment​
Create the environment with the same model path and bridge settings used during training:
import gymnasium as gym
import gama_gymnasium
agent = PPOAgent(state_dim=13, action_dim=2)
agent.load("saved_models/ppo_forager.pth")
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,
)
Run an evaluation episode​
Assume agent has been initialized from the trained checkpoint using the same PPO implementation as the training program. In this example, select_action(obs, test=True) uses the policy's mean action instead of sampling an exploratory action. Run until the environment reports either termination or truncation:
obs, info = env.reset()
total_reward = 0.0
done = False
while not done:
action, _, _ = agent.select_action(obs, test=True)
obs, reward, terminated, truncated, info = env.step(action)
total_reward += reward
done = terminated or truncated
print(f"Episode reward: {total_reward:.1f}")
env.close()
The reward and number of steps can vary with the trained weights and environment settings. This evaluation uses the headless server setup documented by the package; it does not display the model in the GAMA interface. PPOAgent and its checkpoint are dependencies supplied by a training implementation; they are not defined on this page. The package's CartPole DQN example includes a complete model and training code, but uses DQN rather than PPO and does not validate the Forager.
Continue with the multi-agent forager, which replaces the Gymnasium bridge with the PettingZoo interface.