From 25434ba3c249f80640d1985437bc22b4eecde964 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Artelt?= Date: Fri, 31 May 2024 09:08:02 +0200 Subject: [PATCH] Update uncertainty example --- docs/examples/uncertainties.ipynb | 14 ++++++++++---- examples/uncertainties.py | 17 ++++++++++++----- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/docs/examples/uncertainties.ipynb b/docs/examples/uncertainties.ipynb index 5559aa2..c333627 100644 --- a/docs/examples/uncertainties.ipynb +++ b/docs/examples/uncertainties.ipynb @@ -39,6 +39,7 @@ "metadata": {}, "outputs": [], "source": [ + "import numpy as np\n", "from epyt_flow.data.networks import load_ltown\n", "from epyt_flow.simulation import ScenarioSimulator, ModelUncertainty, \\\n", " RelativeUniformUncertainty\n", @@ -98,7 +99,8 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Add uncertainty (i.e. randomness) with respect to the demand pattern -- i.e. demand pattern values can deviate up to 25% from their original value." + "Add uncertainty (i.e. randomness) with respect to the demand pattern -- i.e. demand pattern values can deviate up to 25% from their original value.\n", + "Consequently, the simulation is no longer deterministic and the results vary from run to run." ] }, { @@ -116,7 +118,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Run simulation and retrieve sensor readings at node \"n105\"" + "Run simulation three times and retrieve sensor readings at node \"n105\"" ] }, { @@ -125,9 +127,13 @@ "metadata": {}, "outputs": [], "source": [ - "scada_data = sim.run_simulation()\n", + "measurements = []\n", + "for _ in range(3):\n", + " scada_data = sim.run_simulation()\n", + " measurements.append(scada_data.get_data_pressures(sensor_locations=[\"n105\"]).\n", + " flatten().tolist())\n", "\n", - "plot_timeseries_data(scada_data.get_data_pressures(sensor_locations=[\"n105\"]).T,\n", + "plot_timeseries_data(np.array(measurements),\n", " x_axis_label=\"Time (5min steps)\", y_axis_label=\"Pressure in $m$\")" ] }, diff --git a/examples/uncertainties.py b/examples/uncertainties.py index 784b552..9154969 100644 --- a/examples/uncertainties.py +++ b/examples/uncertainties.py @@ -1,6 +1,7 @@ """ Example of adding uncertainty with respect to WDN parameters (e.g. demand pattern). """ +import numpy as np from epyt_flow.data.networks import load_ltown from epyt_flow.simulation import ScenarioSimulator, ModelUncertainty, RelativeUniformUncertainty from epyt_flow.utils import to_seconds @@ -12,14 +13,20 @@ # Create scenario with ScenarioSimulator(scenario_config=network_config) as sim: - # Set simulation duration to five hours - sim.set_general_parameters(simulation_duration=to_seconds(hours=5)) + # Set simulation duration to three hours + sim.set_general_parameters(simulation_duration=to_seconds(hours=3)) # Add uncertainty (i.e. randomness) with respect to the demand pattern -- # i.e. demand pattern values can deviate up to 25% from their original value. + # Consequently, the simulation is no longer deterministic and the results vary + # from run to run. uc = RelativeUniformUncertainty(low=0.75, high=1.25) sim.set_model_uncertainty(ModelUncertainty(demand_pattern_uncertainty=uc)) - # Run simulation and retrieve sensor readings at node "n105" - scada_data = sim.run_simulation() - print(scada_data.get_data_pressures(sensor_locations=["n105"])) + # Run simulation three times and retrieve sensor readings at node "n105" + measurements = [] + for _ in range(3): + scada_data = sim.run_simulation() + measurements.append(scada_data.get_data_pressures(sensor_locations=["n105"]). + flatten().tolist()) + print(np.mean(measurements, axis=0), np.var(measurements, axis=0))