{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# DSM (BETA Phase!)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import functions\n",
    "import FINE as fn"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Get basic energy system data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "dsm_test_esM_ = functions.dsm_test_esM()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Run without DSM for reference purposes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "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\\tmp3qranfcn.pyomo.lp\n",
      "Reading time = 0.00 seconds\n",
      "x97: 121 rows, 97 columns, 193 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",
      "Gurobi Optimizer version 9.0.0 build v9.0.0rc2 (win64)\n",
      "Optimize a model with 121 rows, 97 columns and 193 nonzeros\n",
      "Model fingerprint: 0x54f7adbc\n",
      "Coefficient statistics:\n",
      "  Matrix range     [1e+00, 1e+00]\n",
      "  Objective range  [9e+03, 4e+05]\n",
      "  Bounds range     [0e+00, 0e+00]\n",
      "  RHS range        [1e+00, 1e+03]\n",
      "Presolve removed 121 rows and 97 columns\n",
      "Presolve time: 0.00s\n",
      "Presolve: All rows and columns removed\n",
      "Iteration    Objective       Primal Inf.    Dual Inf.      Time\n",
      "       0    2.2995000e+07   0.000000e+00   0.000000e+00      0s\n",
      "\n",
      "Solved in 0 iterations and 0.00 seconds\n",
      "Optimal objective  2.299500000e+07\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 8500 characters]\n",
      "text/plain": [
       "<Figure size 288x144 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 8824 characters]\n",
      "text/plain": [
       "<Figure size 288x144 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 7884 characters]\n",
      "text/plain": [
       "<Figure size 288x144 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "\"\"\"\n",
    "Given a one-node system with two generators, check whether the load and generation is shifted correctly in both\n",
    "directions with and without demand side management.\n",
    "\"\"\"\n",
    "\n",
    "functions.run_esM_without_DSM()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Run with DSM (tFwd=tBwd=1)\n",
    "w/o & w/- time series aggregation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using license file C:\\Users\\t.gross\\gurobi.lic\r\n",
      "Academic license - for non-commercial use only\r\n",
      "Read LP format model from file C:\\Users\\T58C8~1.GRO\\AppData\\Local\\Temp\\tmptdzecj6q.pyomo.lp\r\n",
      "Reading time = 0.00 seconds\r\n",
      "x316: 484 rows, 316 columns, 991 nonzeros\r\n",
      "Changed value of parameter QCPDual to 1\r\n",
      "   Prev: 0  Min: 0  Max: 1  Default: 0\r\n",
      "Changed value of parameter Threads to 3\r\n",
      "   Prev: 0  Min: 0  Max: 1024  Default: 0\r\n",
      "Parameter logfile unchanged\r\n",
      "   Value:   Default: \r\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 9344 characters]\n",
      "text/plain": [
       "<Figure size 288x144 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 9504 characters]\n",
      "text/plain": [
       "<Figure size 288x144 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 10108 characters]\n",
      "text/plain": [
       "<Figure size 288x144 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 25684 characters]\n",
      "text/plain": [
       "<Figure size 432x216 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "functions.run_esM_with_DSM(timeSeriesAggregation=False, tBwd=1, tFwd=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using license file C:\\Users\\t.gross\\gurobi.lic\r\n",
      "Academic license - for non-commercial use only\r\n",
      "Read LP format model from file C:\\Users\\T58C8~1.GRO\\AppData\\Local\\Temp\\tmp87e40v26.pyomo.lp\r\n",
      "Reading time = 0.00 seconds\r\n",
      "x582: 680 rows, 444 columns, 1454 nonzeros\r\n",
      "Changed value of parameter QCPDual to 1\r\n",
      "   Prev: 0  Min: 0  Max: 1  Default: 0\r\n",
      "Changed value of parameter Threads to 3\r\n",
      "   Prev: 0  Min: 0  Max: 1024  Default: 0\r\n",
      "Parameter logfile unchanged\r\n",
      "   Value:   Default: \r\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 9344 characters]\n",
      "text/plain": [
       "<Figure size 288x144 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 9504 characters]\n",
      "text/plain": [
       "<Figure size 288x144 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 10108 characters]\n",
      "text/plain": [
       "<Figure size 288x144 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 25684 characters]\n",
      "text/plain": [
       "<Figure size 432x216 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "functions.run_esM_with_DSM(True, numberOfTypicalPeriods=23, tBwd=1, tFwd=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Run with DSM (tFwd=2, tBwd=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using license file C:\\Users\\t.gross\\gurobi.lic\r\n",
      "Academic license - for non-commercial use only\r\n",
      "Read LP format model from file C:\\Users\\T58C8~1.GRO\\AppData\\Local\\Temp\\tmppcuby6hr.pyomo.lp\r\n",
      "Reading time = 0.00 seconds\r\n",
      "x316: 484 rows, 316 columns, 991 nonzeros\r\n",
      "Changed value of parameter QCPDual to 1\r\n",
      "   Prev: 0  Min: 0  Max: 1  Default: 0\r\n",
      "Changed value of parameter Threads to 3\r\n",
      "   Prev: 0  Min: 0  Max: 1024  Default: 0\r\n",
      "Parameter logfile unchanged\r\n",
      "   Value:   Default: \r\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 9028 characters]\n",
      "text/plain": [
       "<Figure size 288x144 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 9200 characters]\n",
      "text/plain": [
       "<Figure size 288x144 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 9632 characters]\n",
      "text/plain": [
       "<Figure size 288x144 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 24448 characters]\n",
      "text/plain": [
       "<Figure size 432x216 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "functions.run_esM_with_DSM(timeSeriesAggregation=False, tBwd=0, tFwd=2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Running into trouble\n",
    "\n",
    "If tBwd + tFwd +1 is not divisor of total number of timesteps (24), infeasibilities can occur. These can be fixed with relaxing the dynamic state of charge constaints. Less notable for a larger amount of time steps!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tBwd+tFwd+1 is not a divisor of the total number of time steps of the energy system. This shortens the shiftable timeframe of demand_0 by 1 time steps\n",
      "tBwd+tFwd+1 is not a divisor of the total number of time steps of the energy system. This shortens the shiftable timeframe of demand_1 by 1 time steps\n",
      "tBwd+tFwd+1 is not a divisor of the total number of time steps of the energy system. This shortens the shiftable timeframe of demand_2 by 1 time steps\n",
      "tBwd+tFwd+1 is not a divisor of the total number of time steps of the energy system. This shortens the shiftable timeframe of demand_3 by 1 time steps\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\\tmpvfhxjwl0.pyomo.lp\n",
      "Reading time = 0.02 seconds\n",
      "x462: 678 rows, 462 columns, 1523 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",
      "WARNING: Loading a SolverResults object with a warning status into\n",
      "    model=unknown;\n",
      "        message from solver=Model was proven to be infeasible.\n",
      "warning infeasible\n",
      "\n",
      "\n",
      "Optimization failed. Try againg with relaxed state of charge formulation (socOffsetDown=socOffsetDown=200)\n",
      "\n",
      "\n",
      "tBwd+tFwd+1 is not a divisor of the total number of time steps of the energy system. This shortens the shiftable timeframe of demand_0 by 1 time steps\n",
      "tBwd+tFwd+1 is not a divisor of the total number of time steps of the energy system. This shortens the shiftable timeframe of demand_1 by 1 time steps\n",
      "tBwd+tFwd+1 is not a divisor of the total number of time steps of the energy system. This shortens the shiftable timeframe of demand_2 by 1 time steps\n",
      "tBwd+tFwd+1 is not a divisor of the total number of time steps of the energy system. This shortens the shiftable timeframe of demand_3 by 1 time steps\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\\tmpy_8nckww.pyomo.lp\n",
      "Reading time = 0.00 seconds\n",
      "x472: 678 rows, 472 columns, 1533 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"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 8368 characters]\n",
      "text/plain": [
       "<Figure size 288x144 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 9624 characters]\n",
      "text/plain": [
       "<Figure size 288x144 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 9364 characters]\n",
      "text/plain": [
       "<Figure size 288x144 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 27160 characters]\n",
      "text/plain": [
       "<Figure size 432x216 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "functions.run_esM_with_DSM(timeSeriesAggregation=False, tBwd=2, tFwd=2)"
   ]
  },
  {
   "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
}
