Closed
Conversation
5 tasks
Stochastic Network: refactor and add tests
Co-authored-by: Fabian Hofmann <[email protected]>
2 tasks
Contributor
Author
|
update: there is an ongoing restructuring process in PyPSA with the introduction of the component class (#1075) and a component xarray view (#1076) and the coming update of the optimization module (all optimization parameters will be handled as xarray objects instead of dataframes. PR to be created soon). |
This was referenced Nov 25, 2024
3 tasks
This was referenced Feb 25, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implement stochastic programming formulation in PyPSA
This is a draft.
This PR will:
-- add an option to cast a two-stage stochastic programming formulation with pypsa out-of-the-box.
-- add related documentation
-- add related notebook examples
This PR will close #575
Eventually, it should be possible to replicate this stochastic problem out-of-the-box with a few lines of code: https://pypsa.readthedocs.io/en/latest/examples/stochastic-problem.html
Update
there is an ongoing restructuring process in PyPSA with the introduction of the component class (#1075) and a component xarray view (#1076) and the coming update of the optimization module (all optimization parameters will be handled as xarray objects instead of dataframes. PR to be created soon).
This pipeline, once complete, will simplify the handling of stochastic programming in PyPSA by eliminating the need for case-specific equation handling, the StochasticNetwork class, and update of all relevant descriptors, as this PR suggests.
We complete the three PRs mentioned above and revise this PR accordingly.
work log
__repr__anditerate_componentsare compatibleget_switchable_as_denseis compatible for varying and static cases in SPget_bounds_puis compatible with SPoptimize.create_model()correctly casts for a toy stochastic problemn.optimize()call works for a toy stochastic probleminvestment_periodsn.optimize()call works for any StochasticNetworkstochastic_problem(True or False) into n.optimize() with default FalseWhat it should do
The linear cost-minimization problem behind PyPSA's LOPF is deterministic and can be represented as:
where x: a vector of variables; c and b: vectors of parameters; A: a matrix of parameters. The inequalities specify a convex polytope that constrains the objective function. The optimal deterministic solution is to find a vector x that minimises the objective function given a set of constraints.
This PR adds a possibility to cast a two-stage risk-neutral stochastic linear optimization problem that uses a set of scenarios provided by a modeller to describe the uncertain parameters. The optimal first-stage decisions must be made before the information about uncertain parameters is revealed, while the second-stage decisions are made after it is revealed:
where x: first-stage variables; y: second-stage variables; s: vector of uncertain data (the discrete set of scenarios that include a finite number of scenarios). The first-stage parameters (c, A, b) are assumed to be known with certainty. The second-stage decisions are restricted by the first-stage decisions x; the parameters (q, T, W, d) are actual realizations of uncertain data (the solution must be feasible for every scenario s ∈ S). The first-stage problem seeks to minimize the sum of the costs of the first-stage decisions and the expected costs of the second-stage decisions. The second-stage problem seeks to minimize the second-stage costs.
How it works
Basic PyPSA network:
Network and Components
User specifies a dict with scenario names and probabilities:
scenarios = {"low": 0.33, "medium": 0.33, "high": 0.34}This call converts the network, i.e., all static and time-dependent components are broadcasted over the provided list of scenarios.
n = StochasticNetwork(n, scenarios)Time-dependent components get new index "scenario" -- thus we use here a similar structure to the case when the network has "investment_periods".
Static components are now a dictionary containing scenarios (keys) and component dataframes (values) . This will make possible to pass any static components' attribute as uncertainty vector.
A new function for retrieving scenario-dependent static component data:


get_switchable_by_scenario(n, "Generator", "p_nom")In stochastic network, get_switchable_as_dense is redefined to work out-of-the-box. It can be called from network instance:
sn.get_switchable_as_dense("Generator", "p_max_pu")Same applies for other descriptors, e.g., a call
sn.get_bounds_pu("StorageUnit", sn.snapshots, None, "p_store")would return bounds per scenario.Variables
Operational variables get new dimension "scenario". Thus for e.g.
n.model.variables["Generator-p"]:NB: We thought about keeping things aligned with "investment_periods", where periods are NOT explicitly added to variable dims, but rather broadcasted via snapshots, which contain information about
periodandtimestep. After implementing it, we realized this approach is not a good idea for scenarios since we have combination of static and time-dependent attr. This could work -- but requires an update in every constraint. Explicit scenario dim keeps chaos under control.Constraints
n.optimize()
PR Checklist
doc.environment.yaml,environment_docs.yamlandsetup.py(if applicable).doc/release_notes.rstof the upcoming release is included.