Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Adding bid config, shop model and related instances for a setup… #421

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
180 changes: 180 additions & 0 deletions scripts/scenario_0.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Create multiple SHOP yaml files with different price in the first 24 h based on a \"base file\" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import yaml\n",
"from datetime import datetime, timedelta\n",
"from copy import deepcopy\n",
"\n",
"with open('../tests/data/demo/v1/files/fornebu_model_with_time_series_values_base.yaml', 'r') as f:\n",
" base_case_dict = yaml.safe_load(f)\n",
"datetimes = [datetime(2023, 7, 7, 22, 0) + timedelta(hours=hh) for hh in range(24)]\n",
"def add_to_items(ts_dict, item_keys, add_value):\n",
" for key in item_keys:\n",
" if key in ts_dict:\n",
" ts_dict[key] += add_value\n",
"\n",
"add_to_price = range(-400, 600, 10)\n",
"model_file_names = []\n",
"for add in add_to_price:\n",
" new_case_dict = deepcopy(base_case_dict)\n",
" add_to_items(new_case_dict['model']['market'][\"Dayahead\"][\"buy_price\"], datetimes, add)\n",
" add_to_items(new_case_dict['model']['market'][\"Dayahead\"][\"sale_price\"], datetimes, add)\n",
" if add < 0:\n",
" suffix = f\"minus_{abs(add)}\"\n",
" elif add>0:\n",
" suffix = f\"plus_{add}\"\n",
" else:\n",
" continue\n",
" file_name = f'fornebu_model_with_time_series_values_price_{suffix}.yaml'\n",
" model_file_names.append(file_name)\n",
" with open(f'../tests/data/demo/v1/files/{file_name}', 'w') as new_file:\n",
" yaml.safe_dump(new_case_dict, new_file, sort_keys=False)\n",
" #print(f\"add: {add}\")\n",
" #print(f\"buy_price: {new_case_dict['model']['market']['Dayahead']['buy_price']}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Add files to resync_configuration (to make sure they are uploaded)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"resync_config_file_path = '../tests/data/demo/v1/resync_configuration.yaml'\n",
"with open(resync_config_file_path, 'r') as f:\n",
" resync_configuration_dict = yaml.safe_load(f)\n",
"files_config_metadata = resync_configuration_dict[\"file_configuration\"][\"file_metadata\"]\n",
"file_names_in_config = [file[\"name\"] for file in files_config_metadata if file[\"directory\"] == \"/files\"]\n",
"for file_name in model_file_names:\n",
" if file_name not in file_names_in_config:\n",
" files_config_metadata.append({\n",
" \"name\": file_name,\n",
" \"external_id\": file_name.split(\".\")[0],\n",
" \"directory\": \"/files\",\n",
" \"source\": \"resync\",\n",
" \"mime_type\": \"application/yaml\"})\n",
"with open(resync_config_file_path, 'w') as f:\n",
" yaml.safe_dump(resync_configuration_dict, f, sort_keys=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Define ShopModel and ShopScenario instances and update ShopScenarioSet"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"instances_def_path = \"../tests/data/demo/v1/fornebu_no_time_series\"\n",
"\n",
"def underscore_to_space(s):\n",
" return s.replace(\"_\", \" \")\n",
"\n",
"shop_model_file = f'{instances_def_path}/shop_model.yaml'\n",
"with open(shop_model_file, 'r') as f:\n",
" shop_model_dict_list = yaml.safe_load(f)\n",
"shop_model_names_in_file = [m[\"name\"] for m in shop_model_dict_list]\n",
"\n",
"shop_scenario_file = f'{instances_def_path}/shop_scenario.yaml'\n",
"with open(shop_scenario_file, 'r') as f:\n",
" shop_scenario_dict_list = yaml.safe_load(f)\n",
"shop_scenario_names_in_file = [m[\"name\"] for m in shop_scenario_dict_list]\n",
"\n",
"shop_scenario_set_file = f'{instances_def_path}/shop_scenario_set.yaml'\n",
"with open(shop_scenario_set_file, 'r') as f:\n",
" shop_scenario_set_dict_list = yaml.safe_load(f)\n",
"for scenario_set in shop_scenario_set_dict_list:\n",
" if scenario_set[\"name\"] == \"100 scenarios no time series\":\n",
" shop_scenario_set_dict = scenario_set\n",
" break\n",
"shop_scenario_set_scenarios = shop_scenario_set_dict[\"scenarios\"]\n",
"\n",
"for add_price in add_to_price:\n",
" if add_price < 0:\n",
" suffix = f\"minus_{abs(add_price)}\"\n",
" elif add_price > 0:\n",
" suffix = f\"plus_{add_price}\"\n",
" else:\n",
" continue\n",
" shop_model_name = f\"Fornebu with time series values price {underscore_to_space(suffix)}\"\n",
" if shop_model_name not in shop_model_names_in_file:\n",
" shop_model_dict_list.append({\n",
" 'name': shop_model_name,\n",
" 'model_version': '1',\n",
" 'shop_version': '15.4.1.1',\n",
" 'penalty_limit': 10000.0,\n",
" 'model': f\"[external_id]fornebu_model_with_time_series_values_price_{suffix}\",\n",
" 'cog_shop_files_config': [],\n",
" 'base_attribute_mappings': []\n",
" })\n",
" \n",
" shop_scenario_name=f\"Fornebu no time series price {underscore_to_space(suffix)}\"\n",
" if shop_scenario_name not in shop_scenario_names_in_file:\n",
" shop_scenario_dict_list.append({\n",
" \"name\": shop_scenario_name,\n",
" \"model\": f\"[name]{shop_model_name}\",\n",
" \"commands\": \"[name|type:ShopCommands]default\",\n",
" \"source\": \"resync\",\n",
" \"attribute_mappings_override\": [],\n",
" \"output_definition\": [\"[name]market price\", \"[name]plant production\", \"[name]plant consumption\"],\n",
" })\n",
"\n",
" if f\"[name]{shop_scenario_name}\" not in shop_scenario_set_scenarios:\n",
" shop_scenario_set_scenarios.append(f\"[name]{shop_scenario_name}\")\n",
"\n",
"with open(shop_model_file, 'w') as f:\n",
" yaml.safe_dump(shop_model_dict_list, f, sort_keys=False)\n",
"\n",
"with open(shop_scenario_file, 'w') as f:\n",
" yaml.safe_dump(shop_scenario_dict_list, f, sort_keys=False)\n",
"\n",
"with open(shop_scenario_set_file, 'w') as f:\n",
" yaml.safe_dump(shop_scenario_set_dict_list, f, sort_keys=False)"
]
}
],
"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.11.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading