{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Using Excel for parameter input and model runs\n",
    "\n",
    "Besides using a python script or a jupyter notebook it is also possible to read and run a model using excel files.\n",
    "\n",
    "The energySystemModelRunFromExcel() function reads a model from excel, optimizes it and stores it to an excel file.\n",
    "\n",
    "The readEnergySystemModelFromExcel() function reads a model from excel.\n",
    "\n",
    "The model run can also be started on double-klicking on the run.bat Windows batch script in the folder where this notebook is located (still requires that a Python version, the FINE package and an optmization solver are installed).\n",
    "\n",
    "Moreover, it is possible to run the model inside the Excel file with a VBA Macro (see scenarioInputWithMacro.xlsx)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Import FINE package\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import FINE as fn \n",
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Call energySystemModelRunFromExcel function"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Read model from excel file, optimize and store to excel file\n",
    "Checkout the output excel file in the folder where this notebook is located"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The distances of a component are set to a normalized value of 1.\n",
      "\n",
      "Clustering time series data with 3 typical periods and 24 time steps per period...\n",
      "\t\t(3.9367 sec)\n",
      "\n",
      "Time series aggregation specifications:\n",
      "Number of typical periods:3, number of time steps per periods:24\n",
      "\n",
      "Declaring sets, variables and constraints for SourceSinkModel\n",
      "\tdeclaring sets... \n",
      "\tdeclaring variables... \n",
      "\tdeclaring constraints... \n",
      "\t\t(0.7967 sec)\n",
      "\n",
      "Declaring sets, variables and constraints for ConversionModel\n",
      "\tdeclaring sets... \n",
      "\tdeclaring variables... \n",
      "\tdeclaring constraints... \n",
      "\t\t(0.0469 sec)\n",
      "\n",
      "Declaring sets, variables and constraints for TransmissionModel\n",
      "\tdeclaring sets... \n",
      "\tdeclaring variables... \n",
      "\tdeclaring constraints... \n",
      "\t\t(0.1406 sec)\n",
      "\n",
      "Declaring sets, variables and constraints for StorageModel\n",
      "\tdeclaring sets... \n",
      "\tdeclaring variables... \n",
      "\tdeclaring constraints... \n",
      "\t\t(1.3747 sec)\n",
      "\n",
      "Declaring shared potential constraint...\n",
      "\t\t(0.0000 sec)\n",
      "\n",
      "Declaring commodity balances...\n",
      "\t\t(0.3281 sec)\n",
      "\n",
      "Declaring objective function...\n",
      "\t\t(0.1250 sec)\n",
      "\n",
      "Using license file C:\\Users\\t.gross\\gurobi.lic\n",
      "Academic license - for non-commercial use only\n",
      "Read LP format model from file C:\\Users\\T58C8~1.GRO\\AppData\\Local\\Temp\\tmphq_37dwq.pyomo.lp\n",
      "Reading time = 0.17 seconds\n",
      "x27264: 50111 rows, 27264 columns, 147376 nonzeros\n",
      "Changed value of parameter QCPDual to 1\n",
      "   Prev: 0  Min: 0  Max: 1  Default: 0\n",
      "Changed value of parameter Threads to 3\n",
      "   Prev: 0  Min: 0  Max: 1024  Default: 0\n",
      "Parameter logfile unchanged\n",
      "   Value:   Default: \n",
      "Changed value of parameter OptimalityTol to 0.001\n",
      "   Prev: 1e-06  Min: 1e-09  Max: 0.01  Default: 1e-06\n",
      "Changed value of parameter cuts to 0\n",
      "   Prev: -1  Min: -1  Max: 3  Default: -1\n",
      "Changed value of parameter method to 2\n",
      "   Prev: -1  Min: -1  Max: 5  Default: -1\n",
      "Gurobi Optimizer version 9.0.0 build v9.0.0rc2 (win64)\n",
      "Optimize a model with 50111 rows, 27264 columns and 147376 nonzeros\n",
      "Model fingerprint: 0xf96a5502\n",
      "Variable types: 27226 continuous, 38 integer (28 binary)\n",
      "Coefficient statistics:\n",
      "  Matrix range     [1e-06, 5e+02]\n",
      "  Objective range  [2e-06, 3e-01]\n",
      "  Bounds range     [1e+00, 6e+04]\n",
      "  RHS range        [4e-02, 5e+01]\n",
      "Presolve removed 13620 rows and 10101 columns\n",
      "Presolve time: 0.20s\n",
      "Presolved: 36491 rows, 17163 columns, 119873 nonzeros\n",
      "Variable types: 17139 continuous, 24 integer (14 binary)\n",
      "Root barrier log...\n",
      "\n",
      "Ordering time: 0.27s\n",
      "\n",
      "Barrier statistics:\n",
      " Dense cols : 168\n",
      " Free vars  : 552\n",
      " AA' NZ     : 4.919e+05\n",
      " Factor NZ  : 1.876e+06 (roughly 40 MBytes of memory)\n",
      " Factor Ops : 3.721e+08 (less than 1 second per iteration)\n",
      " Threads    : 3\n",
      "\n",
      "                  Objective                Residual\n",
      "Iter       Primal          Dual         Primal    Dual     Compl     Time\n",
      "   0   7.24214535e+04 -1.09882080e+06  1.61e+05 1.39e-02  4.57e+02     1s\n",
      "   1   5.78179208e+04 -7.90481120e+05  1.07e+05 1.75e-01  2.67e+02     1s\n",
      "   2   2.30878061e+04 -6.35715479e+05  3.92e+04 1.11e-01  1.17e+02     1s\n",
      "   3   6.54179305e+03 -2.03375624e+05  6.41e+03 5.56e-03  1.72e+01     1s\n",
      "   4   3.69117021e+03 -9.78583063e+04  2.29e+03 1.43e-03  6.12e+00     1s\n",
      "   5   2.20618442e+03 -3.25205470e+04  7.27e+02 1.52e-04  1.71e+00     1s\n",
      "   6   6.37193071e+02 -2.25569832e+04  5.37e+01 7.28e-05  4.76e-01     1s\n",
      "   7   3.50281789e+02 -4.01521329e+03  5.93e+00 2.68e-05  7.79e-02     1s\n",
      "   8   2.42508688e+02 -1.64292813e+03  4.45e-02 4.40e-05  3.23e-02     1s\n",
      "   9   1.49420342e+02 -7.63942812e+02  1.20e-02 5.51e-05  1.54e-02     1s\n",
      "  10   1.01753817e+02 -3.35026763e+02  5.48e-03 4.76e-05  7.34e-03     1s\n",
      "  11   8.24169458e+01 -5.70088733e+01  3.03e-03 3.47e-05  2.34e-03     1s\n",
      "  12   6.51471329e+01  1.59074391e+01  1.37e-03 1.65e-05  8.24e-04     1s\n",
      "  13   5.82641332e+01  2.87021376e+01  8.70e-04 1.01e-05  4.95e-04     1s\n",
      "  14   5.35277097e+01  3.53618141e+01  5.59e-04 6.47e-06  3.04e-04     1s\n",
      "  15   5.07708950e+01  3.92044030e+01  3.88e-04 4.03e-06  1.93e-04     1s\n",
      "  16   4.83812506e+01  4.06462915e+01  2.36e-04 3.15e-06  1.29e-04     1s\n",
      "  17   4.72856261e+01  4.12406411e+01  1.57e-04 2.76e-06  1.01e-04     1s\n",
      "  18   4.68514994e+01  4.17854370e+01  1.29e-04 2.38e-06  8.47e-05     1s\n",
      "  19   4.66530497e+01  4.18793449e+01  1.16e-04 2.31e-06  7.98e-05     1s\n",
      "  20   4.63589520e+01  4.23966333e+01  9.63e-05 1.97e-06  6.63e-05     1s\n",
      "  21   4.58071387e+01  4.28004812e+01  6.19e-05 1.66e-06  5.03e-05     1s\n",
      "  22   4.56482616e+01  4.29286279e+01  5.32e-05 1.56e-06  4.55e-05     1s\n",
      "  23   4.55505755e+01  4.30926995e+01  4.71e-05 1.43e-06  4.11e-05     1s\n",
      "  24   4.54252100e+01  4.34194696e+01  3.89e-05 1.19e-06  3.35e-05     1s\n",
      "  25   4.52508895e+01  4.38126129e+01  2.81e-05 8.58e-07  2.40e-05     1s\n",
      "  26   4.52010925e+01  4.38692324e+01  2.51e-05 8.09e-07  2.23e-05     1s\n",
      "  27   4.50573806e+01  4.40419547e+01  1.56e-05 6.69e-07  1.70e-05     1s\n",
      "  28   4.49576320e+01  4.42325426e+01  9.56e-06 5.06e-07  1.21e-05     2s\n",
      "  29   4.49130495e+01  4.43873070e+01  6.68e-06 3.75e-07  8.79e-06     2s\n",
      "  30   4.48687082e+01  4.44494712e+01  4.17e-06 3.22e-07  7.01e-06     2s\n",
      "  31   4.48374241e+01  4.45536983e+01  2.41e-06 2.26e-07  4.74e-06     2s\n",
      "  32   4.48129241e+01  4.46822305e+01  9.87e-07 1.03e-07  2.19e-06     2s\n",
      "  33   4.48061989e+01  4.47200232e+01  7.18e-07 6.56e-08  1.44e-06     2s\n",
      "  34   4.48007297e+01  4.47489986e+01  5.22e-07 3.73e-08  8.65e-07     2s\n",
      "  35   4.47964532e+01  4.47650486e+01  3.65e-07 2.16e-08  5.25e-07     2s\n",
      "  36   4.47925074e+01  4.47717972e+01  2.30e-07 1.49e-08  3.46e-07     2s\n",
      "  37   4.47901776e+01  4.47762846e+01  1.52e-07 1.02e-08  2.32e-07     2s\n",
      "  38   4.47887492e+01  4.47801867e+01  1.00e-07 6.14e-09  1.43e-07     2s\n",
      "  39   4.47878619e+01  4.47817434e+01  7.05e-08 4.51e-09  1.02e-07     2s\n",
      "  40   4.47869990e+01  4.47847442e+01  4.08e-08 1.38e-09  3.77e-08     2s\n",
      "  41   4.47867638e+01  4.47847607e+01  3.35e-08 1.27e-09  3.35e-08     2s\n",
      "  42   4.47864733e+01  4.47852243e+01  2.41e-08 6.31e-10  2.09e-08     2s\n",
      "  43   4.47859661e+01  4.47854664e+01  8.94e-09 4.93e-10  8.35e-09     2s\n",
      "  44   4.47858732e+01  4.47854799e+01  6.58e-09 4.26e-10  6.58e-09     2s\n",
      "  45   4.47858348e+01  4.47855372e+01  5.49e-09 2.78e-10  4.97e-09     2s\n",
      "  46   4.47857771e+01  4.47856031e+01  3.89e-09 8.44e-11  2.91e-09     2s\n",
      "  47   4.47856625e+01  4.47856200e+01  1.04e-09 4.07e-11  7.10e-10     2s\n",
      "  48   4.47856447e+01  4.47856326e+01  3.26e-10 4.55e-12  2.02e-10     2s\n",
      "  49   4.47856407e+01  4.47856341e+01  1.96e-10 2.02e-12  1.11e-10     2s\n",
      "  50   4.47856357e+01  4.47856350e+01  2.10e-10 1.31e-10  1.16e-11     2s\n",
      "  51   4.47856352e+01  4.47856351e+01  4.39e-09 7.74e-11  1.94e-12     2s\n",
      "\n",
      "Barrier solved model in 51 iterations and 2.12 seconds\n",
      "Optimal objective 4.47856352e+01\n",
      "\n",
      "\n",
      "Root relaxation: objective 4.478564e+01, 5822 iterations, 2.06 seconds\n",
      "\n",
      "    Nodes    |    Current Node    |     Objective Bounds      |     Work\n",
      " Expl Unexpl |  Obj  Depth IntInf | Incumbent    BestBd   Gap | It/Node Time\n",
      "\n",
      "     0     0   44.78564    0   14          -   44.78564      -     -    2s\n",
      "H    0     0                      45.5389686   44.78564  1.65%     -    3s\n",
      "     0     0     cutoff    0        45.53897   45.53897  0.00%     -    3s\n",
      "\n",
      "Explored 1 nodes (5838 simplex iterations) in 3.61 seconds\n",
      "Thread count was 3 (of 8 available processors)\n",
      "\n",
      "Solution count 1: 45.539 \n",
      "\n",
      "Optimal solution found (tolerance 1.00e-04)\n",
      "Warning: max constraint violation (2.7835e-06) exceeds tolerance\n",
      "Best objective 4.553896855278e+01, best bound 4.553896855278e+01, gap 0.0000%\n",
      "\n",
      "Status: ok\n",
      "Return code: 0\n",
      "Message: Model was solved to optimality (subject to tolerances), and an optimal solution is available.\n",
      "Termination condition: optimal\n",
      "Termination message: Model was solved to optimality (subject to tolerances), and an optimal solution is available.\n",
      "Wall time: 3.6086292266845703\n",
      "Error rc: 0\n",
      "Time: 4.2334794998168945\n",
      "\n",
      "\n",
      "Name: x27264\n",
      "Lower bound: 45.538968552776126\n",
      "Upper bound: 45.538968552776126\n",
      "Number of objectives: 1\n",
      "Number of constraints: 50111\n",
      "Number of variables: 27264\n",
      "Number of binary variables: 28\n",
      "Number of integer variables: 38\n",
      "Number of continuous variables: 27226\n",
      "Number of nonzeros: 147376\n",
      "Sense: minimize\n",
      "\n",
      "Solve time: 6.076839447021484 sec.\n",
      "\n",
      "Processing optimization output...\n",
      "for SourceSinkModel ...    (0.7811sec)\n",
      "for ConversionModel ...    (0.4999sec)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "for TransmissionModel ...  (1.5622sec)\n",
      "for StorageModel ...       (4.1241sec)\n",
      "\t\t(6.9673 sec)\n",
      "\n",
      "\n",
      "Writing output to Excel... \n",
      "\tProcessing SourceSinkModel ...\n",
      "\tProcessing ConversionModel ...\n",
      "\tProcessing TransmissionModel ...\n",
      "\tProcessing StorageModel ...\n",
      "\tSaving file...\n",
      "Done. (40.2449 sec)\n"
     ]
    }
   ],
   "source": [
    "esM = fn.energySystemModelRunFromExcel()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Read only"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The distances of a component are set to a normalized value of 1.\n"
     ]
    }
   ],
   "source": [
    "esM, esMData = fn.readEnergySystemModelFromExcel()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
