#2476 Add proof of concept yaml combining notebook
This commit is contained in:
142
src/primaite/notebooks/Scenario-Placeholders.ipynb
Normal file
142
src/primaite/notebooks/Scenario-Placeholders.ipynb
Normal file
@@ -0,0 +1,142 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import yaml\n",
|
||||
"from pprint import pprint\n",
|
||||
"from pathlib import Path\n",
|
||||
"from typing import Sequence\n",
|
||||
"from primaite.session.environment import PrimaiteGymEnv\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"froot = Path('/home/cade/repos/PrimAITE/src/primaite/config/_package_data/scenario_with_placeholders/')\n",
|
||||
"sch = froot / 'schedule.yaml'\n",
|
||||
"fp = froot / 'scenario.yaml'\n",
|
||||
"fpr1 = froot / 'reds_1.yaml'\n",
|
||||
"fpr2 = froot / 'reds_2.yaml'\n",
|
||||
"fpg1 = froot / 'greens_1.yaml'\n",
|
||||
"fpg2 = froot / 'greens_2.yaml'"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"with open(sch,'r') as f:\n",
|
||||
" schedule = yaml.safe_load(f)\n",
|
||||
"\n",
|
||||
"base_scenario_path = froot / schedule['base_scenario']\n",
|
||||
"episodes = [v for n,v in schedule['schedule'].items()]\n",
|
||||
"all_episode_paths = {x for ep in episodes for x in ep.values()}\n",
|
||||
"episode_data = {fp:open(froot / fp, 'r').read() for fp in all_episode_paths}\n",
|
||||
"base_scenario = open(base_scenario_path).read()\n",
|
||||
"\n",
|
||||
"def get_ep_config(ep_num):\n",
|
||||
" episode = episodes[ep_num]\n",
|
||||
" # print(episode.values())\n",
|
||||
" parsed_cfg = yaml.safe_load('\\n'.join([episode_data[v] for v in episode.values()] + [base_scenario]))\n",
|
||||
" flat_agents_list = []\n",
|
||||
" for a in parsed_cfg['agents']:\n",
|
||||
" if isinstance(a,Sequence):\n",
|
||||
" flat_agents_list.extend(a)\n",
|
||||
" else:\n",
|
||||
" flat_agents_list.append(a)\n",
|
||||
" parsed_cfg['agents'] = flat_agents_list\n",
|
||||
" return parsed_cfg\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"pprint(len(get_ep_config(0)['agents']))\n",
|
||||
"# pprint(get_ep_config(0)['agents'])\n",
|
||||
"\n",
|
||||
"# conf_data = open('test_data_1.yaml','r').read()\n",
|
||||
"# variables = open('variables.yaml','r').read()\n",
|
||||
"\n",
|
||||
"# yaml.safe_load(f\"{variables}\\n{conf_data}\")\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"gym = PrimaiteGymEnv(game_config=get_ep_config(0))\n",
|
||||
"print(list(gym.game.agents.keys()))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"gym = PrimaiteGymEnv(game_config=get_ep_config(1))\n",
|
||||
"print(list(gym.game.agents.keys()))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"gym = PrimaiteGymEnv(game_config=get_ep_config(2))\n",
|
||||
"print(list(gym.game.agents.keys()))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"gym = PrimaiteGymEnv(game_config=get_ep_config(3))\n",
|
||||
"print(list(gym.game.agents.keys()))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
||||
7
src/primaite/notebooks/variables.yaml
Normal file
7
src/primaite/notebooks/variables.yaml
Normal file
@@ -0,0 +1,7 @@
|
||||
placeholder_1: &placeholder_1
|
||||
- a
|
||||
- b
|
||||
|
||||
placeholder_2: &placeholder_2
|
||||
- c
|
||||
- d
|
||||
Reference in New Issue
Block a user