diff --git a/tutorial/transport/R_transport_scenario.ipynb b/tutorial/transport/R_transport_scenario.ipynb index 44885e83f..76ff034a2 100644 --- a/tutorial/transport/R_transport_scenario.ipynb +++ b/tutorial/transport/R_transport_scenario.ipynb @@ -35,63 +35,109 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "vscode": { + "languageId": "r" + } + }, "outputs": [], "source": [ "# Load reticulate, used to access the Python API from R\n", "library(reticulate)\n", "\n", "# Import ixmp and message_ix, just as in Python\n", - "ixmp <- import(\"ixmp\")" + "ixmp <- import(\"ixmp\")\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { - "collapsed": true + "collapsed": true, + "vscode": { + "languageId": "r" + } }, "outputs": [], "source": [ "# launch the ix modeling platform using the local default database\n", - "mp <- ixmp$Platform()" + "mp <- ixmp$Platform()\n" ] }, { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "vscode": { + "languageId": "r" + } + }, "outputs": [], "source": [ "scen_list <- mp$scenario_list()\n", "scen_list\n", "\n", - "# TODO: the conversion of the Java output of the `scenario_list()` function to a clean R dataframe is not yet implemented" + "# TODO: the conversion of the Java output of the `scenario_list()` function to a clean R dataframe is not yet implemented\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { - "collapsed": true + "collapsed": true, + "vscode": { + "languageId": "r" + } }, "outputs": [], "source": [ "# details for loading an existing datastructure from the IX modeling platform\n", "model <- \"transport problem\"\n", - "scenario <- \"standard\"" + "scenario <- \"standard\"\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If you have just run the first, ``R_transport`` tutorial, the existing scenario should appear, and we can load it.\n", + "Uncomment and run the following line." ] }, { "cell_type": "code", "execution_count": null, "metadata": { - "collapsed": true + "collapsed": true, + "vscode": { + "languageId": "r" + } }, "outputs": [], "source": [ "# load the default version scenario from the first tutorial\n", - "scen <- ixmp$Scenario(mp, model, scenario)" + "# scen <- ixmp$Scenario(mp, model, scenario)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If not (e.g. starting with this tutorial), we can use a function that creates the scenario from scratch in one step:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "vscode": { + "languageId": "r" + } + }, + "outputs": [], + "source": [ + "ixmp_testing <- import(\"ixmp.testing\")\n", + "scen <- ixmp_testing$make_dantzig(mp, solve = \".\")\n" ] }, { @@ -106,38 +152,50 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "vscode": { + "languageId": "r" + } + }, "outputs": [], "source": [ "# load the distance parameter\n", - "d = scen$par(\"d\")\n", - "d" + "d <- scen$par(\"d\")\n", + "d\n" ] }, { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "vscode": { + "languageId": "r" + } + }, "outputs": [], "source": [ "# show only the distances for connections from Seattle\n", - "d[d['i'] == \"seattle\",]" + "d[d[\"i\"] == \"seattle\", ]\n" ] }, { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "vscode": { + "languageId": "r" + } + }, "outputs": [], "source": [ "# for faster access or more complex filtering,\n", "# it may be easier to only load specific parameter elements using a dictionary\n", - "ele_filter = {}\n", - "ele_filter$i = c('seattle')\n", - "ele_filter$j = c('chicago', 'topeka')\n", + "ele_filter <- {}\n", + "ele_filter$i <- c(\"seattle\")\n", + "ele_filter$j <- c(\"chicago\", \"topeka\")\n", "\n", - "d_filtered = scen$par(\"d\", ele_filter)\n", - "d_filtered" + "d_filtered <- scen$par(\"d\", ele_filter)\n", + "d_filtered\n" ] }, { @@ -154,67 +212,83 @@ "cell_type": "code", "execution_count": null, "metadata": { - "collapsed": true + "collapsed": true, + "vscode": { + "languageId": "r" + } }, "outputs": [], "source": [ "# create a new scenario by cloning the datastructure (without keeping the solution)\n", - "scen_detroit <- scen$clone(model, 'detroit', annotation='extend the Transport problem by a new city', keep_solution=FALSE)" + "scen_detroit <- scen$clone(model, \"detroit\", annotation = \"extend the Transport problem by a new city\", keep_solution = FALSE)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { - "collapsed": true + "collapsed": true, + "vscode": { + "languageId": "r" + } }, "outputs": [], "source": [ "# check out the datastructure to make changes\n", - "scen_detroit$check_out()" + "scen_detroit$check_out()\n" ] }, { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "vscode": { + "languageId": "r" + } + }, "outputs": [], "source": [ - "# reduce demand \n", - "scen_detroit$add_par('b', 'chicago', 200, 'cases')\n", + "# reduce demand\n", + "scen_detroit$add_par(\"b\", \"chicago\", 200, \"cases\")\n", "\n", "# add a new city with demand and distances\n", - "scen_detroit$add_set('j', 'detroit')\n", - "scen_detroit$add_par('b', 'detroit', 150, 'cases')\n", + "scen_detroit$add_set(\"j\", \"detroit\")\n", + "scen_detroit$add_par(\"b\", \"detroit\", 150, \"cases\")\n", "\n", - "d_add = data.frame(i = c('seattle','san-diego'), j = c('detroit','detroit'), value = c(1.7,1.9) , unit = 'cases')\n", - "scen_detroit$add_par('d', d_add)" + "d_add <- data.frame(i = c(\"seattle\", \"san-diego\"), j = c(\"detroit\", \"detroit\"), value = c(1.7, 1.9), unit = \"cases\")\n", + "scen_detroit$add_par(\"d\", d_add)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { - "collapsed": true + "collapsed": true, + "vscode": { + "languageId": "r" + } }, "outputs": [], "source": [ - "d_add = data.frame(i = c('seattle','san-diego'), j = c('detroit','detroit'), value = c(1.7,1.9) , unit = 'cases')\n", - "scen_detroit$add_par('d', d_add)" + "d_add <- data.frame(i = c(\"seattle\", \"san-diego\"), j = c(\"detroit\", \"detroit\"), value = c(1.7, 1.9), unit = \"cases\")\n", + "scen_detroit$add_par(\"d\", d_add)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { - "collapsed": true + "collapsed": true, + "vscode": { + "languageId": "r" + } }, "outputs": [], "source": [ "# save changes to database\n", - "comment = \"add new city 'detroit' with demand, reduce demand in 'chicago'\"\n", - "scen_detroit$commit(comment) \n", - "scen_detroit$set_as_default()" + "comment <- \"add new city 'detroit' with demand, reduce demand in 'chicago'\"\n", + "scen_detroit$commit(comment)\n", + "scen_detroit$set_as_default()\n" ] }, { @@ -228,11 +302,14 @@ "cell_type": "code", "execution_count": null, "metadata": { - "collapsed": true + "collapsed": true, + "vscode": { + "languageId": "r" + } }, "outputs": [], "source": [ - "scen_detroit$solve(model='dantzig')" + "scen_detroit$solve(model = \"dantzig\")\n" ] }, { @@ -247,61 +324,85 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "vscode": { + "languageId": "r" + } + }, "outputs": [], "source": [ "# display the objective value of the solution in the baseline scenario\n", - "scen$var(\"z\")" + "scen$var(\"z\")\n" ] }, { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "vscode": { + "languageId": "r" + } + }, "outputs": [], "source": [ "# display the objective value of the solution in the \"detroit\" scenario\n", - "scen_detroit$var(\"z\")" + "scen_detroit$var(\"z\")\n" ] }, { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "vscode": { + "languageId": "r" + } + }, "outputs": [], "source": [ "# display the quantities transported from canning plants to demand locations in the baseline scenario\n", - "scen$var(\"x\")" + "scen$var(\"x\")\n" ] }, { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "vscode": { + "languageId": "r" + } + }, "outputs": [], "source": [ "# display the quantities transported from canning plants to demand locations in the \"detroit\" scenario\n", - "scen_detroit$var(\"x\")" + "scen_detroit$var(\"x\")\n" ] }, { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "vscode": { + "languageId": "r" + } + }, "outputs": [], "source": [ "# display the quantities and marginals (=shadow prices) of the demand balance constraints in the baseline scenario\n", - "scen$equ(\"demand\")" + "scen$equ(\"demand\")\n" ] }, { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "vscode": { + "languageId": "r" + } + }, "outputs": [], "source": [ "# display the quantities and marginals (=shadow prices) of the demand balance constraints in the \"detroit\" scenario\n", - "scen_detroit$equ(\"demand\")" + "scen_detroit$equ(\"demand\")\n" ] }, { @@ -315,19 +416,25 @@ "cell_type": "code", "execution_count": null, "metadata": { - "collapsed": true + "collapsed": true, + "vscode": { + "languageId": "r" + } }, "outputs": [], "source": [ "# close the connection of the platform instance to the local ixmp database files\n", - "mp$close_db()" + "mp$close_db()\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { - "collapsed": true + "collapsed": true, + "vscode": { + "languageId": "r" + } }, "outputs": [], "source": []