2023-11-22 20:22:34 +00:00
|
|
|
{
|
|
|
|
|
"cells": [
|
|
|
|
|
{
|
2023-12-01 15:36:07 +00:00
|
|
|
"cell_type": "markdown",
|
2023-11-22 20:22:34 +00:00
|
|
|
"metadata": {},
|
|
|
|
|
"source": [
|
2023-12-01 15:36:07 +00:00
|
|
|
"## Train a Multi agent system using RLLIB\n",
|
2023-11-22 20:22:34 +00:00
|
|
|
"\n",
|
2023-12-01 15:36:07 +00:00
|
|
|
"This notebook will demonstrate how to use the `PrimaiteRayMARLEnv` to train a very basic system with two PPO agents."
|
2023-11-22 20:22:34 +00:00
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
2023-12-01 15:36:07 +00:00
|
|
|
"cell_type": "markdown",
|
2023-11-22 20:22:34 +00:00
|
|
|
"metadata": {},
|
|
|
|
|
"source": [
|
2023-12-01 15:36:07 +00:00
|
|
|
"#### First, Import packages and read our config file."
|
2023-11-22 20:22:34 +00:00
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
|
|
|
|
"execution_count": null,
|
|
|
|
|
"metadata": {},
|
|
|
|
|
"outputs": [],
|
|
|
|
|
"source": [
|
2023-12-01 15:36:07 +00:00
|
|
|
"from primaite.game.game import PrimaiteGame\n",
|
|
|
|
|
"import yaml\n",
|
|
|
|
|
"\n",
|
|
|
|
|
"from primaite.session.environment import PrimaiteRayEnv\n",
|
|
|
|
|
"from primaite import PRIMAITE_PATHS\n",
|
|
|
|
|
"\n",
|
2023-11-22 20:22:34 +00:00
|
|
|
"import ray\n",
|
|
|
|
|
"from ray import air, tune\n",
|
2023-12-01 15:36:07 +00:00
|
|
|
"from ray.rllib.algorithms.ppo import PPOConfig\n",
|
2024-05-31 12:12:35 +01:00
|
|
|
"from primaite.session.ray_envs import PrimaiteRayMARLEnv\n",
|
2023-12-01 15:36:07 +00:00
|
|
|
"\n",
|
|
|
|
|
"# If you get an error saying this config file doesn't exist, you may need to run `primaite setup` in your command line\n",
|
|
|
|
|
"# to copy the files to your user data path.\n",
|
2024-03-07 14:33:21 +00:00
|
|
|
"with open(PRIMAITE_PATHS.user_config_path / 'example_config/data_manipulation_marl.yaml', 'r') as f:\n",
|
2023-12-01 15:36:07 +00:00
|
|
|
" cfg = yaml.safe_load(f)\n",
|
|
|
|
|
"\n",
|
|
|
|
|
"ray.init(local_mode=True)"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "markdown",
|
|
|
|
|
"metadata": {},
|
|
|
|
|
"source": [
|
|
|
|
|
"#### Create a Ray algorithm config which accepts our two agents"
|
2023-11-22 20:22:34 +00:00
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
|
|
|
|
"execution_count": null,
|
|
|
|
|
"metadata": {},
|
|
|
|
|
"outputs": [],
|
|
|
|
|
"source": [
|
|
|
|
|
"config = (\n",
|
|
|
|
|
" PPOConfig()\n",
|
|
|
|
|
" .multi_agent(\n",
|
2023-12-01 15:36:07 +00:00
|
|
|
" policies={'defender_1','defender_2'}, # These names are the same as the agents defined in the example config.\n",
|
2023-11-22 20:22:34 +00:00
|
|
|
" policy_mapping_fn=lambda agent_id, episode, worker, **kw: agent_id,\n",
|
|
|
|
|
" )\n",
|
2024-02-26 10:26:28 +00:00
|
|
|
" .environment(env=PrimaiteRayMARLEnv, env_config=cfg)#, disable_env_checking=True)\n",
|
2023-12-01 14:58:34 +00:00
|
|
|
" .rollouts(num_rollout_workers=0)\n",
|
2023-11-22 20:22:34 +00:00
|
|
|
" .training(train_batch_size=128)\n",
|
|
|
|
|
" )\n"
|
|
|
|
|
]
|
|
|
|
|
},
|
2023-12-01 15:36:07 +00:00
|
|
|
{
|
|
|
|
|
"cell_type": "markdown",
|
|
|
|
|
"metadata": {},
|
|
|
|
|
"source": [
|
|
|
|
|
"#### Set training parameters and start the training\n",
|
|
|
|
|
"This example will save outputs to a default Ray directory and use mostly default settings."
|
|
|
|
|
]
|
|
|
|
|
},
|
2023-11-22 20:22:34 +00:00
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
|
|
|
|
"execution_count": null,
|
|
|
|
|
"metadata": {},
|
|
|
|
|
"outputs": [],
|
|
|
|
|
"source": [
|
|
|
|
|
"tune.Tuner(\n",
|
|
|
|
|
" \"PPO\",\n",
|
|
|
|
|
" run_config=air.RunConfig(\n",
|
2024-05-16 10:55:17 +01:00
|
|
|
" stop={\"timesteps_total\": 5 * 128},\n",
|
2023-11-22 20:22:34 +00:00
|
|
|
" ),\n",
|
|
|
|
|
" param_space=config\n",
|
|
|
|
|
").fit()"
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
"metadata": {
|
|
|
|
|
"kernelspec": {
|
|
|
|
|
"display_name": "venv",
|
|
|
|
|
"language": "python",
|
|
|
|
|
"name": "python3"
|
|
|
|
|
},
|
|
|
|
|
"language_info": {
|
|
|
|
|
"codemirror_mode": {
|
|
|
|
|
"name": "ipython",
|
|
|
|
|
"version": 3
|
|
|
|
|
},
|
|
|
|
|
"file_extension": ".py",
|
|
|
|
|
"mimetype": "text/x-python",
|
|
|
|
|
"name": "python",
|
|
|
|
|
"nbconvert_exporter": "python",
|
|
|
|
|
"pygments_lexer": "ipython3",
|
|
|
|
|
"version": "3.10.12"
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
"nbformat": 4,
|
|
|
|
|
"nbformat_minor": 2
|
|
|
|
|
}
|