{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tutorial - Simple\n", "A simple optimization problem related to production optimization under resource constraints." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- - - \n", "IMPORT PACKAGE, DEFINE MODEL NAME AND ROOT PATH" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import cvxlab\n", "\n", "model_dir_name = ''\n", "main_dir_path = r''" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "- - - \n", "MODEL DIRECTORY GENERATION\n", "\n", "Generation of a model directory in a defined path. Notes:\n", "- Blank .yml or .xlsx files can be generated\n", "- API_reference.ipynb can be generated as guidance for main APIs\n", "- user_operators.py and user_constants.py templates can be generated" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cvxlab.create_model_dir(\n", " model_dir_name=model_dir_name,\n", " main_dir_path=main_dir_path,\n", " template_file_type='xlsx', \n", " include_tutorial=True,\n", " include_user_operators_template=True,\n", " include_user_constants_template=True\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- - - \n", "UTILITY FILE GENERATION\n", "\n", "Useful in case the model directory already exists and the utility files are missing. Notes:\n", "- API_reference.ipynb can be generated as guidance for main APIs\n", "- user_operators.py and user_constants.py templates can be generated" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cvxlab.copy_utility_files(\n", " path_destination=main_dir_path + '\\\\' + model_dir_name,\n", " include_custom_constants_template=True,\n", " include_custom_operators_template=True,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- - - \n", "MODEL GENERATION FROM SCRATCH\n", "\n", "Step-by-step model creation with sets, data and problems generation.\n", "\n", "- Generation of a Model instance defined by settings files filled by user\n", "- model_settings_from: select among yml and xlsx\n", "- use_existing_data=True : model coordinates are loaded and numerical problem initialized (model ready to be run). \n", "- use_existing_data=False : sets excel file generated only, to be filled by user" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model = cvxlab.Model(\n", " model_dir_name=model_dir_name,\n", " main_dir_path=main_dir_path,\n", " log_level='info',\n", " model_settings_from='xlsx',\n", " multiple_input_files=False,\n", " use_existing_data=True,\n", " detailed_validation=True,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Once sets.xlsx file has filled with sets data:\n", "\n", "- load_model_coordinates(): load user defined sets, fill coordinates\n", "- initialize_blank_data_structure():\n", " - generate blank sqlite database with sets and variables (empty numerical values)\n", " - generate blank xlsx files for exogenous data to be filled by the user" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model.load_model_coordinates()\n", "model.initialize_blank_data_structure()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Once exogenous xlsx files has filled:\n", "\n", "- load_exogenous_data_to_sqlite_database(): load input data into sqlite database\n", "- initialize_problems(): load and validate symbolic problem, generate numerical problem " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model.load_exogenous_data_to_sqlite_database(force_overwrite=True)\n", "model.initialize_problems()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- - - \n", "MODEL GENERATION FROM EXISTING DATA\n", "\n", "If use_existing_data=True, generation of Model instance working with existing database and data input files.\n", "\n", "The model is generated and ready to be solved." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model = cvxlab.Model(\n", " model_dir_name=model_dir_name,\n", " main_dir_path=main_dir_path,\n", " log_level='debug',\n", " model_settings_from='xlsx',\n", " use_existing_data=True,\n", " detailed_validation=True,\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model.run_model(\n", " verbose=False,\n", " solver='GUROBI',\n", " integrated_problems=True,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- - - \n", "SAVING/LOADING MODEL INSTANCE\n", "\n", "In case model instance generation is taking huge time, it is possible to save/load model instance to avoid regenerating it several times.\n", "\n", "Notice that - for unknown reasons - model instance can be loaded only if it has not already solve (i.e. do not save model instance if you run the model!)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cvxlab.handle_model_instance(\n", " action='save',\n", " instance=model,\n", " file_name='instance_name'\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model = cvxlab.handle_model_instance(\n", " action='load',\n", " file_name='file_name',\n", " source_dir_path=r\"D:\\git_repos\\pyesm\\tests\\models\\integrated\\1_coupled_model\\instances\"\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- - -\n", "DATA and SYMBOLIC PROBLEM UPDATE without regenerating Model instance\n", "\n", "- initialize_problems(): if symbolic problem has modified, upload it and generates a new numerical model\n", "\n", "- update_database_and_problem(): in case of modifications in input data files (but not in sets, nor in variables structures) and symbolic problem, update database and problem\n", "\n", "- reinitialize_sqlite_database(): reinitialize sqlite database endogenous variables\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# in case of need for generating only one or more data input files/tabs\n", "model.generate_input_data_files(table_key_list=[])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# in case of modifications in symbolic problem only,\n", "# update problems dataframe and symbolic problem\n", "model.initialize_problems()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# in case of modifications in input data files (but not in sets, nor in \n", "# variables structures) and symbolic problem, update database and problem\n", "model.update_database_and_problem()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# in case of modifications in input data files (but not in sets, nor in \n", "# variables structures) and symbolic problem, reinitialize sqlite database for\n", "# endogenous variables\n", "model.reinitialize_sqlite_database(force_overwrite=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- - -\n", "SOLVE NUMERICAL PROBLEM and ENDOGENOUS DATA EXPORT\n", "\n", "- run_model(): run model with various settings\n", "- load_results_to_database(): once model has successfully solved, load endogenous parameters data to sqlite database.\n", "- check_model_results(): check model results compared to an existing database (path and name of the database with the expected results in model settings attributes)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model.run_model(\n", " verbose=False,\n", " solver='GUROBI',\n", " integrated_problems=True,\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model.load_results_to_database(force_overwrite=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model.check_model_results()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- - -\n", "UTILITIES\n", "\n", "- sets(): list of model sets\n", "- variables(): dictionary with variable name (key) and shape of teh variable (value)\n", "- variable(name, ...): visual check of variables content in the database\n", "- set(name): visual check of sets content in the database" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model.variables\n", "model.sets" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model.set('products')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model.variable(name='L',)" ] } ], "metadata": { "kernelspec": { "display_name": "cvxlab", "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.13" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }