{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# This Notebook will develop how to explain an Agent and assess its performance." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is recommended to have a look at the [0_basic_functionalities](0_basic_functionalities.ipynb) notebook before getting into this one." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Objective**\n", "\n", "This notebook will cover the basic of how to \"code\" an Agent that takes action on the powergrid. Examples will be given of \"expert agent\" that can take actions based on some fixed rules. More generic type of *Agent*, relying for example on machine learning / deep learning will be covered in the notebook [3_TrainingAnAgent](3_TrainingAnAgent.ipynb).\n", "\n", "This notebook will also cover the description of the *Observation* class, usefull to take some actions." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import os\n", "import sys\n", "import grid2op" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
run previous cell, wait for 2 seconds
\n", "" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "res = None\n", "try:\n", " from jyquickhelper import add_notebook_menu\n", " res = add_notebook_menu()\n", "except ModuleNotFoundError:\n", " print(\"Impossible to automatically add a menu / table of content to this notebook.\\nYou can download \\\"jyquickhelper\\\" package with: \\n\\\"pip install jyquickhelper\\\"\")\n", "res" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## I) Description of the observations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this paragraph we will cover the observation class. For more information about it, we recommend to have a look at the official documentation, or [here](https://grid2op.readthedocs.io/en/latest/observation.html) or in the [Observations.py](grid2op/Observation/Observation.py) files for more information. Only basic concepts are detailed in this notebook." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### I.A) Getting an observation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An observation can be accessed when calling `env.step()`. The next cell is dedicated to create an environment, and to get an observation instance. We use the default `case14_fromfile` from Grid2Op framework." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/donnotben/Documents/Grid2Op_dev/getting_started/grid2op/MakeEnv.py:667: UserWarning:\n", "\n", "Your are using only 2 chronics for this environment. More can be download by running, from a command line:\n", "python -m grid2op.download --name \"case14_realistic\" --path_save PATH\\WHERE\\YOU\\WANT\\TO\\DOWNLOAD\\DATA\n", "\n" ] } ], "source": [ "env = grid2op.make() #\"case14_fromfile\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To perform a step, as stated on the short description above, we need an action. More information about them is given in the [2_ActionRepresentation](2_ActionRepresentation.ipynb) notebook. Here we just assume we do nothing." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "do_nothing_act = env.helper_action_player({})\n", "obs, reward, done, info = env.step(do_nothing_act)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### I.B) Information present in an Observation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this notebook we will detail only the \"CompleteObservation\". `Grid2Op` allows to modeled different kind of observations, for example some with incomplete data, or with noisy data etc. CompletelyObservation gives the full state of the powergrid, without any noise. It's the default observation used." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### a) some of its attributes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An observation has calendar data (eg the time stamp of the observation):" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2019, 1, 1, 0, 10, 1)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "obs.year, obs.month, obs.day, obs.hour_of_day, obs.minute_of_hour, obs.day_of_week" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It has some powegrid generic information:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Number of generators of the powergrid: 5\n", "Number of loads of the powergrid: 11\n", "Number of powerline of the powergrid: 20\n", "Number of elements connected to each substations in the powergrid: [3 6 4 6 5 6 3 2 5 3 3 3 4 3]\n", "Total number of elements: 56\n" ] } ], "source": [ "print(\"Number of generators of the powergrid: {}\".format(obs.n_gen))\n", "print(\"Number of loads of the powergrid: {}\".format(obs.n_load))\n", "print(\"Number of powerline of the powergrid: {}\".format(obs.n_line))\n", "print(\"Number of elements connected to each substations in the powergrid: {}\".format(obs.sub_info))\n", "print(\"Total number of elements: {}\".format(obs.dim_topo))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It has some information about the generators (each generator can be viewed as a point in a 3 dimensional space)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Generators active production: [81.6 81.1 12.9 0. 77.7200955]\n", "Generators reactive production: [ 21.79066889 70.21426433 48.05804585 24.508776 -16.54165458]\n", "Generators voltage setpoint : [142.1 142.1 22. 13.2 142.1]\n" ] } ], "source": [ "print(\"Generators active production: {}\".format(obs.prod_p))\n", "print(\"Generators reactive production: {}\".format(obs.prod_q))\n", "print(\"Generators voltage setpoint : {}\".format(obs.prod_v))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It has some information the loads (each load is a point in a 3 dimensional space too)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Loads active consumption: [25.4 84.8 45. 6.8 12.7 28.8 9.5 3.4 5.6 11.9 15.4]\n", "Loads reactive consumption: [ 21.79066889 70.21426433 48.05804585 24.508776 -16.54165458]\n", "Loads voltage (voltage magnitude of the bus to which it is connected) : [142.1 142.1 138.70157283 139.39478126 22.\n", " 21.09213719 21.08565879 21.45353383 21.56920312 21.43075597\n", " 20.69996034]\n" ] } ], "source": [ "print(\"Loads active consumption: {}\".format(obs.load_p))\n", "print(\"Loads reactive consumption: {}\".format(obs.prod_q))\n", "print(\"Loads voltage (voltage magnitude of the bus to which it is connected) : {}\".format(obs.load_v))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this setting a powerline can be viewed as a point in an 8 dimensional space:\n", " * active flow\n", " * reactive flow\n", " * voltage magnitude\n", " * current flow\n", " \n", "from both of its end.\n", "\n", "It it is then:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Origin active flow: [ 3.99223950e+01 3.77977005e+01 2.17807668e+01 4.01616963e+01\n", " 3.38599023e+01 1.78608153e+01 -2.80522122e+01 9.77398067e+00\n", " 7.72872347e+00 1.80516148e+01 3.35936909e+00 7.78590005e+00\n", " -6.14406783e+00 2.03646124e+00 7.87604573e+00 2.54371835e+01\n", " 1.45080856e+01 3.53543190e+01 -2.08721929e-14 -2.54371835e+01]\n", "Origin reactive flow: [-15.33405699 -1.20759759 -7.00249457 0.66352597 -0.38311737\n", " 7.32923584 -2.94368951 10.46283538 5.57631797 14.9276252\n", " -0.85521377 4.0310593 -7.4643436 1.48429454 7.41027529\n", " -15.62563657 -2.70329755 -5.64124747 -23.63431484 -5.57332913]\n", "Origin current flow: [173.75765493 153.64987497 92.95599257 163.19866665 137.58110509\n", " 78.44050551 117.40948087 375.74684345 250.10808762 614.72674809\n", " 94.88822467 239.99175304 264.71528254 67.45319927 291.33413073\n", " 124.26482512 61.42982996 148.28416711 918.84080639 712.80312847]\n", "Origin voltage (voltage magnitude to the bus to which the origin end is connected): [142.1 142.1 142.1 142.1 142.1\n", " 142.1 138.70157283 22. 22. 22.\n", " 21.09213719 21.09213719 21.08565879 21.56920312 21.43075597\n", " 138.70157283 138.70157283 139.39478126 14.85053552 21.09213719]\n", "Extremity active flow: [-3.96023654e+01 -3.70686934e+01 -2.15608153e+01 -3.92743782e+01\n", " -3.32429776e+01 -1.76186787e+01 2.81573520e+01 -9.61306287e+00\n", " -7.63646124e+00 -1.77516465e+01 -3.35593217e+00 -7.69804766e+00\n", " 6.21306287e+00 -2.02439918e+00 -7.70195234e+00 -2.54371835e+01\n", " -1.45080856e+01 -3.53543190e+01 2.08721929e-14 2.54371835e+01]\n", "Extremity reactive flow: [ 10.71275486 -0.90132842 3.2850285 -1.4910293 -1.33275675\n", " -8.03634708 3.27533264 -10.12585338 -5.38429454 -14.33689404\n", " 0.8643436 -3.84418549 7.62585338 -1.47338125 -7.05581451\n", " 17.39024818 3.82920054 8.39126729 24.508776 6.24406666]\n", "Extremity current flow: [ 166.6869517 153.5778135 88.61223426 163.59877739 137.7975573\n", " 80.60723679 117.40948087 375.74684345 250.10808762 614.72674809\n", " 94.88822467 239.99175304 264.71528254 67.45319927 291.33413073\n", " 1197.94841837 410.7259861 953.58582187 1071.98094079 1018.29018352]\n", "Extremity voltage (voltage magnitude to the bus to which the origin end is connected): [142.1 139.39478126 142.1 138.70157283 139.39478126\n", " 138.70157283 139.39478126 21.45353383 21.56920312 21.43075597\n", " 21.08565879 20.69996034 21.45353383 21.43075597 20.69996034\n", " 14.85053552 21.09213719 22. 13.2 14.85053552]\n" ] } ], "source": [ "print(\"Origin active flow: {}\".format(obs.p_or))\n", "print(\"Origin reactive flow: {}\".format(obs.q_or))\n", "print(\"Origin current flow: {}\".format(obs.a_or))\n", "print(\"Origin voltage (voltage magnitude to the bus to which the origin end is connected): {}\".format(obs.v_or))\n", "print(\"Extremity active flow: {}\".format(obs.p_ex))\n", "print(\"Extremity reactive flow: {}\".format(obs.q_ex))\n", "print(\"Extremity current flow: {}\".format(obs.a_ex))\n", "print(\"Extremity voltage (voltage magnitude to the bus to which the origin end is connected): {}\".format(obs.v_ex))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The last informations about the powerlines is the $\\rho$ ratio, *ie.* the ratio between the current flow on each powerlines and the its thermal limits. It can be accessed with:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0.45143563, 0.39919409, 0.24462103, 0.42947018, 0.87631277,\n", " 0.20642238, 0.30897232, 0.34864962, 0.54149989, 0.79855347,\n", " 0.3521812 , 0.62351687, 0.34830958, 0.17750842, 0.38333438,\n", " 0.32284949, 0.26599897, 0.86817705, 0.27006916, 0.20950979])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "obs.rho" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It also store information of the topology and the state of the powerline." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n", " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n", " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "obs.timestep_overflow # the number of timestep each of the powerline is in overflow (1 powerline per component)\n", "obs.line_status # the status of each powerline: True connected, False disconnected\n", "obs.topo_vect # the topology vector the each element (generator, load, each end of a powerline) to which the object\n", "# is connected: 1 = bus 1, 2 = bus 2." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " More information about this `topology vector` is given in the documentation [here](https://grid2op.readthedocs.io/en/latest/observation.html)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### b) some of its methods" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It can be converted to / from flat numpy vector. This function is usefull for interacting with machine learning or to store it, but probably less human readable. It consists in stacking all the above-mentionned information in a single `numpy.float64` vector." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 2.01900000e+03, 1.00000000e+00, 1.00000000e+00, 0.00000000e+00,\n", " 1.00000000e+01, 1.00000000e+00, 8.16000000e+01, 8.11000000e+01,\n", " 1.29000000e+01, 0.00000000e+00, 7.77200955e+01, 2.17906689e+01,\n", " 7.02142643e+01, 4.80580458e+01, 2.45087760e+01, -1.65416546e+01,\n", " 1.42100000e+02, 1.42100000e+02, 2.20000000e+01, 1.32000000e+01,\n", " 1.42100000e+02, 2.54000000e+01, 8.48000000e+01, 4.50000000e+01,\n", " 6.80000000e+00, 1.27000000e+01, 2.88000000e+01, 9.50000000e+00,\n", " 3.40000000e+00, 5.60000000e+00, 1.19000000e+01, 1.54000000e+01,\n", " 1.78000000e+01, 5.96000000e+01, 3.08000000e+01, 4.60000000e+00,\n", " 8.70000000e+00, 1.97000000e+01, 6.60000000e+00, 2.50000000e+00,\n", " 3.90000000e+00, 8.40000000e+00, 1.09000000e+01, 1.42100000e+02,\n", " 1.42100000e+02, 1.38701573e+02, 1.39394781e+02, 2.20000000e+01,\n", " 2.10921372e+01, 2.10856588e+01, 2.14535338e+01, 2.15692031e+01,\n", " 2.14307560e+01, 2.06999603e+01, 3.99223950e+01, 3.77977005e+01,\n", " 2.17807668e+01, 4.01616963e+01, 3.38599023e+01, 1.78608153e+01,\n", " -2.80522122e+01, 9.77398067e+00, 7.72872347e+00, 1.80516148e+01,\n", " 3.35936909e+00, 7.78590005e+00, -6.14406783e+00, 2.03646124e+00,\n", " 7.87604573e+00, 2.54371835e+01, 1.45080856e+01, 3.53543190e+01,\n", " -2.08721929e-14, -2.54371835e+01, -1.53340570e+01, -1.20759759e+00,\n", " -7.00249457e+00, 6.63525968e-01, -3.83117367e-01, 7.32923584e+00,\n", " -2.94368951e+00, 1.04628354e+01, 5.57631797e+00, 1.49276252e+01,\n", " -8.55213773e-01, 4.03105930e+00, -7.46434360e+00, 1.48429454e+00,\n", " 7.41027529e+00, -1.56256366e+01, -2.70329755e+00, -5.64124747e+00,\n", " -2.36343148e+01, -5.57332913e+00, 1.42100000e+02, 1.42100000e+02,\n", " 1.42100000e+02, 1.42100000e+02, 1.42100000e+02, 1.42100000e+02,\n", " 1.38701573e+02, 2.20000000e+01, 2.20000000e+01, 2.20000000e+01,\n", " 2.10921372e+01, 2.10921372e+01, 2.10856588e+01, 2.15692031e+01,\n", " 2.14307560e+01, 1.38701573e+02, 1.38701573e+02, 1.39394781e+02,\n", " 1.48505355e+01, 2.10921372e+01, 1.73757655e+02, 1.53649875e+02,\n", " 9.29559926e+01, 1.63198667e+02, 1.37581105e+02, 7.84405055e+01,\n", " 1.17409481e+02, 3.75746843e+02, 2.50108088e+02, 6.14726748e+02,\n", " 9.48882247e+01, 2.39991753e+02, 2.64715283e+02, 6.74531993e+01,\n", " 2.91334131e+02, 1.24264825e+02, 6.14298300e+01, 1.48284167e+02,\n", " 9.18840806e+02, 7.12803128e+02, -3.96023654e+01, -3.70686934e+01,\n", " -2.15608153e+01, -3.92743782e+01, -3.32429776e+01, -1.76186787e+01,\n", " 2.81573520e+01, -9.61306287e+00, -7.63646124e+00, -1.77516465e+01,\n", " -3.35593217e+00, -7.69804766e+00, 6.21306287e+00, -2.02439918e+00,\n", " -7.70195234e+00, -2.54371835e+01, -1.45080856e+01, -3.53543190e+01,\n", " 2.08721929e-14, 2.54371835e+01, 1.07127549e+01, -9.01328416e-01,\n", " 3.28502850e+00, -1.49102930e+00, -1.33275675e+00, -8.03634708e+00,\n", " 3.27533264e+00, -1.01258534e+01, -5.38429454e+00, -1.43368940e+01,\n", " 8.64343601e-01, -3.84418549e+00, 7.62585338e+00, -1.47338125e+00,\n", " -7.05581451e+00, 1.73902482e+01, 3.82920054e+00, 8.39126729e+00,\n", " 2.45087760e+01, 6.24406666e+00, 1.42100000e+02, 1.39394781e+02,\n", " 1.42100000e+02, 1.38701573e+02, 1.39394781e+02, 1.38701573e+02,\n", " 1.39394781e+02, 2.14535338e+01, 2.15692031e+01, 2.14307560e+01,\n", " 2.10856588e+01, 2.06999603e+01, 2.14535338e+01, 2.14307560e+01,\n", " 2.06999603e+01, 1.48505355e+01, 2.10921372e+01, 2.20000000e+01,\n", " 1.32000000e+01, 1.48505355e+01, 1.66686952e+02, 1.53577813e+02,\n", " 8.86122343e+01, 1.63598777e+02, 1.37797557e+02, 8.06072368e+01,\n", " 1.17409481e+02, 3.75746843e+02, 2.50108088e+02, 6.14726748e+02,\n", " 9.48882247e+01, 2.39991753e+02, 2.64715283e+02, 6.74531993e+01,\n", " 2.91334131e+02, 1.19794842e+03, 4.10725986e+02, 9.53585822e+02,\n", " 1.07198094e+03, 1.01829018e+03, 4.51435630e-01, 3.99194086e-01,\n", " 2.44621033e-01, 4.29470175e-01, 8.76312771e-01, 2.06422383e-01,\n", " 3.08972318e-01, 3.48649620e-01, 5.41499895e-01, 7.98553469e-01,\n", " 3.52181199e-01, 6.23516865e-01, 3.48309582e-01, 1.77508419e-01,\n", " 3.83334383e-01, 3.22849486e-01, 2.65998967e-01, 8.68177053e-01,\n", " 2.70069157e-01, 2.09509785e-01, 1.00000000e+00, 1.00000000e+00,\n", " 1.00000000e+00, 1.00000000e+00, 1.00000000e+00, 1.00000000e+00,\n", " 1.00000000e+00, 1.00000000e+00, 1.00000000e+00, 1.00000000e+00,\n", " 1.00000000e+00, 1.00000000e+00, 1.00000000e+00, 1.00000000e+00,\n", " 1.00000000e+00, 1.00000000e+00, 1.00000000e+00, 1.00000000e+00,\n", " 1.00000000e+00, 1.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n", " 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n", " 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n", " 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n", " 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n", " 0.00000000e+00, 0.00000000e+00, 1.00000000e+00, 1.00000000e+00,\n", " 1.00000000e+00, 1.00000000e+00, 1.00000000e+00, 1.00000000e+00,\n", " 1.00000000e+00, 1.00000000e+00, 1.00000000e+00, 1.00000000e+00,\n", " 1.00000000e+00, 1.00000000e+00, 1.00000000e+00, 1.00000000e+00,\n", " 1.00000000e+00, 1.00000000e+00, 1.00000000e+00, 1.00000000e+00,\n", " 1.00000000e+00, 1.00000000e+00, 1.00000000e+00, 1.00000000e+00,\n", " 1.00000000e+00, 1.00000000e+00, 1.00000000e+00, 1.00000000e+00,\n", " 1.00000000e+00, 1.00000000e+00, 1.00000000e+00, 1.00000000e+00,\n", " 1.00000000e+00, 1.00000000e+00, 1.00000000e+00, 1.00000000e+00,\n", " 1.00000000e+00, 1.00000000e+00, 1.00000000e+00, 1.00000000e+00,\n", " 1.00000000e+00, 1.00000000e+00, 1.00000000e+00, 1.00000000e+00,\n", " 1.00000000e+00, 1.00000000e+00, 1.00000000e+00, 1.00000000e+00,\n", " 1.00000000e+00, 1.00000000e+00, 1.00000000e+00, 1.00000000e+00,\n", " 1.00000000e+00, 1.00000000e+00, 1.00000000e+00, 1.00000000e+00,\n", " 1.00000000e+00, 1.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n", " 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n", " 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n", " 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n", " 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n", " 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n", " 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n", " 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n", " 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n", " 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n", " 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n", " 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n", " 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n", " 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n", " -1.00000000e+00, -1.00000000e+00, -1.00000000e+00, -1.00000000e+00,\n", " -1.00000000e+00, -1.00000000e+00, -1.00000000e+00, -1.00000000e+00,\n", " -1.00000000e+00, -1.00000000e+00, -1.00000000e+00, -1.00000000e+00,\n", " -1.00000000e+00, -1.00000000e+00, -1.00000000e+00, -1.00000000e+00,\n", " -1.00000000e+00, -1.00000000e+00, -1.00000000e+00, -1.00000000e+00,\n", " 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n", " 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n", " 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n", " 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n", " 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n", " 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n", " 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n", " 0.00000000e+00, 0.00000000e+00])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "vector_representation_of_observation = obs.to_vect()\n", "vector_representation_of_observation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An observation can be copied, of course:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "obs2 = obs.copy()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or reset:" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[nan nan nan nan nan]\n" ] } ], "source": [ "obs2.reset()\n", "print(obs2.prod_p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or loaded from a vector:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([81.6 , 81.1 , 12.9 , 0. , 77.7200955])" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "obs2.from_vect(vector_representation_of_observation)\n", "obs2.prod_p" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is also possible to assess whether two observations are equals or not:" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "obs == obs2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For this type of observation, it is also possible to retrieve the topology as a matrix. The topology matrix can be obtained in two different format." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Format 1*: the `connectivity matrix` which has as many row / columns as the number of elements in the powergrid (remember an element is either an end of a powerline, or a generator or a load) and that says if 2 elements are connected to one another or not:\n", "\n", "$$\n", "\\left\\{\n", "\\begin{aligned}\n", "\\text{conn mat}[i,j] = 0 & ~\\text{element i and j are NOT connected to the same bus}\\\\\n", "\\text{conn mat}[i,j] = 1 & ~\\text{element i and j are connected to the same bus, or i and j are both end of the same powerline}\\\\\n", "\\end{aligned}\n", "\\right.\n", "$$" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0., 1., 1., ..., 0., 0., 0.],\n", " [1., 0., 1., ..., 0., 0., 0.],\n", " [1., 1., 0., ..., 0., 0., 0.],\n", " ...,\n", " [0., 0., 0., ..., 0., 1., 1.],\n", " [0., 0., 0., ..., 1., 0., 1.],\n", " [0., 0., 0., ..., 1., 1., 0.]])" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "obs.connectivity_matrix()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This representation has the advantages to always have the same dimension, regardless of the topology of the powergrid." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Format 2*: the `bus connectivity matrix` has as many row / columns as the number of active buses of the powergrid. It should be understood as followed:\n", "\n", "$$\n", "\\left\\{\n", "\\begin{aligned}\n", "\\text{bus conn mat}[i,j] = 0 & ~\\text{no powerline connect bus i to bus j}\\\\\n", "\\text{bus conn mat}[i,j] = 1 & ~\\text{at least a powerline connectes bus i to bus j (or i == j)}\\\\\n", "\\end{aligned}\n", "\\right.\n", "$$" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1., 1., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [1., 1., 1., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 1., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 1., 1., 1., 1., 0., 1., 0., 1., 0., 0., 0., 0., 0.],\n", " [1., 1., 0., 1., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 1., 1., 0., 0., 0., 0., 1., 1., 1., 0.],\n", " [0., 0., 0., 1., 0., 0., 1., 1., 1., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 1., 1., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 1., 0., 0., 1., 0., 1., 1., 0., 0., 0., 1.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 1., 1., 1., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 1., 0., 0., 0., 1., 1., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 1., 1., 0.],\n", " [0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 1., 1., 1.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 1., 1.]])" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "obs.bus_connectivity_matrix()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### c) Simulate" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As opposed to most RL problems, in this framework we add the possibility to \"simulate\" the impact of a possible action on the power grid.\n", "\n", "This \"simulate\" method used some forecasts available (forecasts are made available by the way we loaded the data here, with the class `GridStateFromFileWithForecasts`. For this class, only 1 time step ahead forecasts are provided, but this might be adapted in the future).\n", "\n", "Note that this `simulate` function can use a different simulator than the one used by the Environment. Fore more information, we encourage you to read the official documentation or if it has been built locally (recommended) to consult [this page](https://grid2op.readthedocs.io/en/latest/observation.html#grid2op.Observation.Observation.simulate).\n", "\n", "This function will:\n", "\n", "1. apply the forecasted injection on the powergrid\n", "2. run a powerflow with the decidated `simulate` powerflow simulator\n", "3. return:\n", " 1. the anticipated observation (after the action has been taken)\n", " 2. the anticipated reward (of this simulated action)\n", " 3. whether or not there has been an error\n", " 4. some more informations\n", " \n", "From a user point of view, this is the main difference with the previous [pypownet](https://github.com/MarvinLer/pypownet) framework. This \"simulation\" used to be performed directly by the environment, thus giving a direct access to the Agent to the Environment, which could break the RL framework (it was not the case in the first edition of the Learning to Run A Power Network as the Environment was fully observable)." ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "do_nothing_act = env.helper_action_player({})\n", "obs_sim, reward_sim, is_done_sim, info_sim = obs.simulate(do_nothing_act)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([81.5 , 79.7 , 12.9 , 0. , 79.57809957])" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "obs_sim.prod_p" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([81.6 , 81.1 , 12.9 , 0. , 77.7200955])" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "obs.prod_p" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## II) Taking actions based on these" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this section we will make our first *Agent* that will act based on these observations.\n", "\n", "All *Agent* must derived from the grid2op.Agent class. The main function to code for the Agents is the \"act\" function (more information on the official documentation or [here](https://grid2op.readthedocs.io/en/latest/agent.html) ). \n", "\n", "Basically, the Agent receive a reward and an observation, and suggest a new action. Some different *Agent* are pre-define in the grid2op package. We won't expose them here (for more information see the documantation or the [Agent.py](grid2op/Agent/Agent.py) file), but rather we will make a custom Agent.\n", "\n", "This *Agent* will select among:\n", "\n", "- do nothing \n", "- disconnecting the powerline having the higher relative flows\n", "- reconnecting a powerline disconnected\n", "- disconnecting the powerline having the lower relative flows\n", "\n", "by using `simulate` on the corresponding actions, and choosing the one that has the highest predicted reward.\n", "\n", "Note that this kind of Agent is not particularly smart and is given only as an example.\n", "\n", "More information about the creation / manipulation of *Action* will be given in the notebook [2_Action_GridManipulation](2_Action_GridManipulation.ipynb)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "from grid2op.Agent import BaseAgent\n", "import numpy as np\n", "import pdb\n", "\n", "\n", "class MyAgent(BaseAgent):\n", " def __init__(self, action_space):\n", " # python required method to code\n", " BaseAgent.__init__(self, action_space)\n", " self.do_nothing = self.action_space({})\n", " self.print_next = False\n", " \n", " def act(self, observation, reward, done=False):\n", " i_max = np.argmax(observation.rho)\n", " new_status_max = np.zeros(observation.rho.shape)\n", " new_status_max[i_max] = -1\n", " act_max = self.action_space({\"set_line_status\": new_status_max})\n", " \n", " i_min = np.argmin(observation.rho)\n", " new_status_min = np.zeros(observation.rho.shape)\n", " if observation.rho[i_min] > 0:\n", " # all powerlines are connected, i try to disconnect this one\n", " new_status_min[i_min] = -1\n", " act_min = self.action_space({\"set_line_status\": new_status_min})\n", " else:\n", " # at least one powerline is disconnected, i try to reconnect it\n", " new_status_min[i_min] = 1\n", "# act_min = self.action_space({\"set_status\": new_status_min})\n", " act_min = self.action_space({\"set_line_status\": new_status_min,\n", " \"set_bus\": {\"lines_or_id\": [(i_min, 1)], \"lines_ex_id\": [(i_min, 1)]}})\n", " \n", " _, reward_sim_dn, *_ = observation.simulate(self.do_nothing)\n", " _, reward_sim_max, *_ = observation.simulate(act_max)\n", " _, reward_sim_min, *_ = observation.simulate(act_min)\n", " \n", " if reward_sim_dn >= reward_sim_max and reward_sim_dn >= reward_sim_min:\n", " self.print_next = False\n", " res = self.do_nothing\n", " elif reward_sim_max >= reward_sim_min:\n", " self.print_next = True\n", " res = act_max\n", " print(res)\n", " else:\n", " self.print_next = True\n", " res = act_min\n", " print(res)\n", " return res" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We compare this Agent with the Donothing agent (already coded) on the 3 episode made available with this package. To make the comparison more interesting, it's better to use the L2RPN rewards." ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The results for DoNothing agent are:\n", "\tFor chronics with id 1\n", "\t\t - cumulative reward: 199.998003\n", "\t\t - number of time steps completed: 10 / 10\n" ] } ], "source": [ "from grid2op.main import main\n", "from grid2op.Agent import DoNothingAgent\n", "from grid2op.Reward import L2RPNReward\n", "from grid2op.Chronics import GridStateFromFileWithForecasts\n", "\n", "max_iter = 10 # to make computation much faster we will only consider 50 time steps instead of 287\n", "\n", "res = main(nb_episode=1,\n", " agent_class=DoNothingAgent,\n", " path_casefile=grid2op.CASE_14_FILE,\n", " path_chronics=grid2op.CHRONICS_MLUTIEPISODE,\n", " names_chronics_to_backend=grid2op.NAMES_CHRONICS_TO_BACKEND,\n", " gridStateclass_kwargs={\"gridvalueClass\": GridStateFromFileWithForecasts, \"max_iter\": max_iter},\n", " reward_class=L2RPNReward\n", " )\n", "print(\"The results for DoNothing agent are:\")\n", "for _, chron_name, cum_reward, nb_time_step, max_ts in res:\n", " msg_tmp = \"\\tFor chronics with id {}\\n\".format(chron_name)\n", " msg_tmp += \"\\t\\t - cumulative reward: {:.6f}\\n\".format(cum_reward)\n", " msg_tmp += \"\\t\\t - number of time steps completed: {:.0f} / {:.0f}\".format(nb_time_step, max_ts)\n", " print(msg_tmp)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "res = main(nb_episode=1,\n", " agent_class=MyAgent,\n", " path_casefile=grid2op.CASE_14_FILE,\n", " path_chronics=grid2op.CHRONICS_MLUTIEPISODE,\n", " names_chronics_to_backend=grid2op.NAMES_CHRONICS_TO_BACKEND,\n", " gridStateclass_kwargs={\"gridvalueClass\": GridStateFromFileWithForecasts, \"max_iter\": max_iter},\n", " reward_class=L2RPNReward\n", " )\n", "print(\"The results for the custom agent are:\")\n", "for _, chron_name, cum_reward, nb_time_step, max_ts in res:\n", " msg_tmp = \"\\tFor chronics with id {}\\n\".format(chron_name)\n", " msg_tmp += \"\\t\\t - cumulative reward: {:.6f}\\n\".format(cum_reward)\n", " msg_tmp += \"\\t\\t - number of time steps completed: {:.0f} / {:.0f}\".format(nb_time_step, max_ts)\n", " print(msg_tmp)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As we can see, there is no change in the performance for both agent (there would be if we didn't limit the episode length to 10 time steps)\n", "\n", "This agent is NOT recommended.\n", "\n", "**NB** These scores are given if setting `max_iter=-1` in the previous cells. Here, when `max_iter=10` we don't see any difference in the score (max_iter=10 has been set to make the tests of these notebooks run faster)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from grid2op.Agent import PowerLineSwitch\n", "res = main(nb_episode=1,\n", " agent_class=PowerLineSwitch,\n", " path_casefile=grid2op.CASE_14_FILE,\n", " path_chronics=grid2op.CHRONICS_MLUTIEPISODE,\n", " names_chronics_to_backend=grid2op.NAMES_CHRONICS_TO_BACKEND,\n", " gridStateclass_kwargs={\"gridvalueClass\": GridStateFromFileWithForecasts, \"max_iter\": max_iter},\n", " reward_class=L2RPNReward\n", " )\n", "print(\"The results for the PowerLineSwitch agent are:\")\n", "for _, chron_name, cum_reward, nb_time_step, max_ts in res:\n", " msg_tmp = \"\\tFor chronics with ID {}\\n\".format(chron_name)\n", " msg_tmp += \"\\t\\t - cumulative reward: {:.6f}\\n\".format(cum_reward)\n", " msg_tmp += \"\\t\\t - number of time steps completed: {:.0f} / {:.0f}\".format(nb_time_step, max_ts)\n", " print(msg_tmp)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We want however to emphasize that do nothing is NOT the best solution, even in this simple case. For example, an agent choosing at each time step to disconnect / reconnect as to greedily maximize the anticipated reward will have a cumulative reward of 199.998***134*** in this situation.\n", "\n", "**NB** For these simulations, the score is completely irrealistic. Indeed, no special care has been taken to set the thermal limits to plausible values. This explain the very little different observed between the three agents above.\n", "\n", "**NB** These scores are given if setting `max_iter=-1` in the previous cells. Here, when `max_iter=10` we don't see any difference in the score (max_iter=10 has been set to make the tests of these notebooks run faster)" ] } ], "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.6" } }, "nbformat": 4, "nbformat_minor": 2 }