Skip to content

Commit

Permalink
Merge pull request #930 from GbotemiB/monte-carlo
Browse files Browse the repository at this point in the history
Monte carlo Improvement
  • Loading branch information
davide-f authored Jan 25, 2024
2 parents 2dd6f2a + 5a1b2f1 commit d912d0d
Show file tree
Hide file tree
Showing 6 changed files with 328 additions and 88 deletions.
4 changes: 2 additions & 2 deletions Snakefile
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ if config["custom_rules"] is not []:

rule clean:
run:
shell("snakemake -j 1 solve_all_networks --delete-all-output")
try:
shell("snakemake -j 1 solve_all_networks_monte --delete-all-output")
shell("snakemake -j 1 solve_all_networks --delete-all-output")
except:
shell("snakemake -j 1 solve_all_networks_monte --delete-all-output")
pass
shell("snakemake -j 1 run_all_scenarios --delete-all-output")

Expand Down
43 changes: 32 additions & 11 deletions config.default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -340,18 +340,39 @@ costs:


monte_carlo:
# Description: Specify Monte Carlo sampling options for uncertainty analysis.
# Define the option list for Monte Carlo sampling.
# Make sure add_to_snakefile is set to true to enable Monte-Carlo
options:
add_to_snakefile: false
samples: 7 # number of optimizations
sampling_strategy: "chaospy" # "pydoe2", "chaospy", "scipy", packages that are supported
pypsa_standard:
# User can add here flexibly more features for the Monte-Carlo sampling.
# Given as "key: value" format
# Key: add below the pypsa object for the monte_carlo sampling, "network" is only allowed for filtering!
# Value: currently supported format [l_bound, u_bound] or empty [], represent multiplication factors for the object
loads_t.p_set: [0.9, 1.1]
# generators_t.p_max_pu.loc[:, n.generators.carrier == "wind"]: [0.9, 1.1]
# generators_t.p_max_pu.loc[:, n.generators.carrier == "solar"]: [0.9, 1.1]
add_to_snakefile: false # When set to true, enables Monte Carlo sampling
samples: 9 # number of optimizations. Note that number of samples when using scipy has to be the square of a prime number
sampling_strategy: "chaospy" # "pydoe2", "chaospy", "scipy", packages that are supported
seed: 42 # set seedling for reproducibilty
# Uncertanties on any PyPSA object are specified by declaring the specific PyPSA object under the key 'uncertainties'.
# For each PyPSA object, the 'type' and 'args' keys represent the type of distribution and its argument, respectively.
# Supported distributions types are uniform, normal, lognormal, triangle, beta and gamma.
# The arguments of the distribution are passed using the key 'args' as follows, tailored by distribution type
# normal: [mean, std], lognormal: [mean, std], uniform: [lower_bound, upper_bound],
# triangle: [mid_point (between 0 - 1)], beta: [alpha, beta], gamma: [shape, scale]
# More info on the distributions are documented in the Chaospy reference guide...
# https://chaospy.readthedocs.io/en/master/reference/distribution/index.html
# An abstract example is as follows:
# {pypsa network object, e.g. "loads_t.p_set"}:
# type: {any supported distribution among the previous: "uniform", "normal", ...}
# args: {arguments passed as a list depending on the distribution, see the above and more at https://pypsa.readthedocs.io/}
uncertainties:
loads_t.p_set:
type: uniform
args: [0, 1]
generators_t.p_max_pu.loc[:, n.generators.carrier == "onwind"]:
type: lognormal
args: [1.5]
generators_t.p_max_pu.loc[:, n.generators.carrier == "solar"]:
type: beta
args: [0.5, 2]
# TODO: Support inputs to simulate outages biggest lines "lines.p_nom_opt.max()": [-3000MW 0MW]
# TODO: Support inputs to simulate outages of biggest power plant "generators.p_nom.max()": [-1000MW 0MW]



solving:
Expand Down
43 changes: 31 additions & 12 deletions config.tutorial.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -336,19 +336,38 @@ costs:


monte_carlo:
# Description: Specify Monte Carlo sampling options for uncertainty analysis.
# Define the option list for Monte Carlo sampling.
# Make sure add_to_snakefile is set to true to enable Monte-Carlo
options:
add_to_snakefile: false
samples: 7 # number of optimizations
sampling_strategy: "chaospy" # "pydoe2", "chaospy", "scipy", packages that are supported
pypsa_standard:
# User can add here flexibly more features for the Monte-Carlo sampling.
# Given as "key: value" format
# Key: add below the pypsa object for the monte_carlo sampling, "network" is only allowed for filtering!
# Value: currently supported format [l_bound, u_bound] or empty [], represent multiplication factors for the object
loads_t.p_set: [0.9, 1.1]
# generators_t.p_max_pu.loc[:, n.generators.carrier == "wind"]: [0.9, 1.1]
# generators_t.p_max_pu.loc[:, n.generators.carrier == "solar"]: [0.9, 1.1]

add_to_snakefile: false # When set to true, enables Monte Carlo sampling
samples: 9 # number of optimizations. Note that number of samples when using scipy has to be the square of a prime number
sampling_strategy: "chaospy" # "pydoe2", "chaospy", "scipy", packages that are supported
seed: 42 # set seedling for reproducibilty
# Uncertanties on any PyPSA object are specified by declaring the specific PyPSA object under the key 'uncertainties'.
# For each PyPSA object, the 'type' and 'args' keys represent the type of distribution and its argument, respectively.
# Supported distributions types are uniform, normal, lognormal, triangle, beta and gamma.
# The arguments of the distribution are passed using the key 'args' as follows, tailored by distribution type
# normal: [mean, std], lognormal: [mean, std], uniform: [lower_bound, upper_bound],
# triangle: [mid_point (between 0 - 1)], beta: [alpha, beta], gamma: [shape, scale]
# More info on the distributions are documented in the Chaospy reference guide...
# https://chaospy.readthedocs.io/en/master/reference/distribution/index.html
# An abstract example is as follows:
# {pypsa network object, e.g. "loads_t.p_set"}:
# type: {any supported distribution among the previous: "uniform", "normal", ...}
# args: {arguments passed as a list depending on the distribution, see the above and more at https://pypsa.readthedocs.io/}
uncertainties:
loads_t.p_set:
type: uniform
args: [0, 1]
generators_t.p_max_pu.loc[:, n.generators.carrier == "onwind"]:
type: lognormal
args: [1.5]
generators_t.p_max_pu.loc[:, n.generators.carrier == "solar"]:
type: beta
args: [0.5, 2]
# TODO: Support inputs to simulate outages biggest lines "lines.p_nom_opt.max()": [-3000MW 0MW]
# TODO: Support inputs to simulate outages of biggest power plant "generators.p_nom.max()": [-1000MW 0MW]

solving:
options:
Expand Down
6 changes: 6 additions & 0 deletions doc/api_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,9 @@ make_statistics

.. automodule:: make_statistics
:members:

monte_carlo
-------------------------------

.. automodule:: monte_carlo
:members:
15 changes: 9 additions & 6 deletions doc/configtables/monte-carlo.csv
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
,Unit,Values,Description
options,,,
-- add_to_snakemake,,true or false,Add rule to Snakefile or not
-- samples,,"int** read description!", "Defines the number of total sample networks that will be later optimized. if orthogonal latin hypercube sampling is applied (default option in scripts), then a prime number, that is not a quadrat of a prime, needs to be chosen. E.g. 7 not 9 (3^2)"
-- sampling_strategy,,"Any subset of {""pydoe2"", ""chaospy"", ""scipy""}",Current supported packages to create an experimental design
pypsa_standard,,,
-- <any pypsa.object syntax>,MW/MWh,"[l_bound, u_bound] or empty []","`Key` is a dynamic PyPSA object that allows to access any pypsa object such as `loads_t.p_set` or the max. wind generation per hour `generators_t.p_max_pu.loc[:, n.generators.carrier == ""wind""]`. `Values` or bounds are multiplication for each object."
**options**,,,
add_to_snakemake,,"true or false","Set to true to enable Monte-Carlo"
samples,,"int","Defines the number of total sample networks that will be optimized. If the chosen sampling strategy is scipy, then a square of a prime number needs to be chosen. E.g. 49 which is (7^2)"
sampling_strategy,,"Any subset of {pydoe2, chaospy, scipy}","Current supported packages to create an experimental design"
seed,,"int","Allows experimentation to be reproduced easily"
**uncertainties**,,,
<any pypsa.object syntax>,MW/MWh,,"`Key` is a dynamic PyPSA object that allows to access any pypsa object such as `loads_t.p_set` or the max. wind generation per hour `generators_t.p_max_pu.loc[:, n.generators.carrier == ""wind""]`. `Values` or bounds are multiplication for each object."
type,,"str","Defines the distribution for the chosen pypsa.object parameter. Distribution can be either uniform, normal, lognormal, triangle, beta or gamma"
args,,"list","Defines parameters for the chosen distribution. [mean, std] for normal and lognormal, [lower_bound, upper_bound] for uniform, [mid_point (between 0 - 1)] for triangle, [alpha, beta] for beta, [shape, scale] for gamma"
Loading

0 comments on commit d912d0d

Please sign in to comment.