Nodal Clearing#
Nodal-clearing allocation algorithms and utilities.
DCOPF#
API path: apem.unit_based_model.allocation.algorithms.nodal_clearing.dcopf
- class DCOPF[source]#
Bases:
PowerFlowModelImplementation of the Direct Current Optimal Power Flow Model.
The class is also used for computing redispatch.
- add_redispatch_constraints_objective(
- redispatch_type,
- model,
- scenario,
- y_stl,
- u_st,
- abs_slack,
- seller_cost_dict,
- seller_no_load_cost_dict,
- zonal_allocation,
- redispatch_constraint_units=False,
- redispatch_threshold=0.001,
- slack_penalty=1000000000000000,
Include redispatch constraints and objective in the DCOPF model.
- Parameters:
redispatch_type (RedispatchAlgorithms) – member of RedispatchAlgorithms. - MinAbsCostRD: minimize absolute cost deviations relative to zonal_allocation - MinAbsVolRD: minimize absolute volume deviations relative to zonal_allocation - MinCostRD: minimize (signed) redispatch cost relative to zonal_allocation
model (Any) – The working optimization model
scenario (Scenario) – Holds df_sellers, periods, and blocks_sellers
y_stl (Dict) – Decision variables for seller s, period t, block ls
u_st (Dict) – Commitment (on/off) variables for seller s in period t
abs_slack (Any) – Absolute values of the slack variables used in the node balance constraints
seller_cost_dict (Dict) – Marginal cost per block (by (s, t)) for each ls
seller_no_load_cost_dict (Dict) – No-load/startup-like (fixed) cost per (s, t)
zonal_allocation (SellersAllocation) – Reference allocation
redispatch_constraint_units (bool) – If True and a seller’s max_prod < redispatch_threshold, set u_st == zonal_allocation.u_st
redispatch_threshold (float) – Production threshold for filtering which units can be redispatched
slack_penalty (float) – Penalty coefficient for absolute nodal slack in objective
- Returns:
updated model
- Return type:
gurobipy.Model
- compare_zonal_vs_final_allocation(zonal_allocation, final_allocation, file)[source]#
Create a CSV file comparing the zonal allocation and the final allocation obtained after redispatch.
- Parameters:
zonal_allocation (SellersAllocation)
final_allocation (SellersAllocation)
file (str)
- compute_redispatch_costs(
- zonal_allocation,
- final_allocation,
- seller_cost_dict,
- seller_no_load_cost_dict,
- periods,
- blocks_sellers,
- sellers,
- file,
Compute the signed redispatch cost relative to the zonal allocation.
- Parameters:
zonal_allocation (SellersAllocation)
final_allocation (SellersAllocation)
file (str)
- compute_redispatch_volumes(
- zonal_allocation,
- final_allocation,
- periods,
- blocks_sellers,
- sellers,
- file,
Compute the total redispatch volume as the L1 norm of dispatch changes between the zonal solution and the post-redispatch solution. For each seller s, period t, and seller block l, the redispatch contribution equals the absolute dispatch difference between the final and zonal allocations.
- Parameters:
zonal_allocation (SellersAllocation)
final_allocation (SellersAllocation)
file (str)
- solve(
- scenario,
- configuration,
- results_file=None,
- stats_file=None,
- u_fixed=None,
- redispatch_type=None,
- zonal_allocation=None,
- redispatch_constraint_units=False,
- redispatch_threshold=0.001,
- shadow_prices=False,
- alpha=0,
Formulate and solve a DCOPF problem in Gurobi similar to the one from the paper “Pricing Optimal Outcomes in Coupled and Non-Convex Markets: Theory and Applications to Electricity Markets” (Appendix B, https://arxiv.org/abs/2209.07386).
- Parameters:
scenario (Scenario) – scenario for which DCOPF is computed
configuration (SolverConfiguration) – values of some parameters to be set in the optimizer
results_file (str | None) – name of the file in which results are written
stats_file (str | None) – name of the file that contains the statistics
u_fixed (dict | None) – values of the commitment decision variables to be fixed in the problem
redispatch_type (RedispatchAlgorithms | None) – redispatch algorithm enum used when solving a redispatch problem
zonal_allocation (SellersAllocation | None) – zonal allocation for which a redispatch solution should be computed
redispatch_constraint_units (bool) – True if all units can be used for redispatch, False otherwise
redispatch_threshold (float) – production threshold for filtering what units can be redispatched
shadow_prices (bool) – whether shadow prices for the computed allocation should be calculated
alpha (float) – used for markup pricing
- Returns:
Allocation object if the problem can be solved optimally or an Error object otherwise
- Return type:
Nodal FBMC#
API path: apem.unit_based_model.allocation.algorithms.nodal_clearing.nodal_fbmc
- class NodalDispatchModel[source]#
Bases:
objectImplements the Nodal Dispatch model (BC1) from the paper: “Modeling flow-based market coupling: Base case, redispatch, and unit commitment matter” (https://ieeexplore.ieee.org/document/9221922).
Includes verification and logging logic.
- solve(network, ptdf, verbose=True, configuration=None)[source]#
Formulates and solves the nodal dispatch problem.
- Parameters:
network (pypsa.Network) – A pypsa.Network object containing generators, loads, lines, etc.
ptdf (DataFrame) – A pandas DataFrame with PTDF values (lines as index, buses as columns).
verbose (bool) – If True, prints detailed verification logs.
configuration (SolverConfiguration)
- Returns:
A dictionary with result DataFrames if the problem is solved optimally, otherwise None.
Nodal FBMC Included#
API path: apem.unit_based_model.allocation.algorithms.nodal_clearing.nodal_fbmc_included
- class NodalFBMC[source]#
Bases:
PowerFlowModelImplementation of the Nodal Flow-Based Market Coupling Model.
This class adapts the NodalDispatchModel to work with the APEM framework, taking Scenario objects and returning Allocation objects compatible with the existing codebase.
- solve(
- scenario,
- configuration,
- results_file=None,
- stats_file=None,
- u_fixed=None,
- redispatch=False,
- min_cost=False,
- min_vol=False,
- zonal_allocation=None,
Formulate and solve a Nodal FBMC problem using PyPSA and the NodalDispatchModel.
- Parameters:
scenario (Scenario) – scenario for which Nodal FBMC is computed
configuration (SolverConfiguration) – values of some parameters to be set in the optimizer
results_file (str | None) – name of the file in which results are written
stats_file (str | None) – name of the file that contains the statistics
u_fixed (dict | None) – values of the commitment decision variables to be fixed in the problem
redispatch (bool | None) – True if a redispatch solution should be computed
min_cost (bool | None) – True if a minimum-cost redispatch solution should be computed
min_vol (bool | None) – True if a minimum-volume redispatch solution should be computed
zonal_allocation (SellersAllocation | None) – zonal allocation for which a redispatch solution should be computed
- Returns:
Allocation object if the problem can be solved optimally or an Error object otherwise
- Return type:
- create_allocation_from_nodal_results(nodal_results, network, scenario, power_flow_model)[source]#
Converts results from the NodalDispatchModel into an APEM Allocation object.
This function is specifically designed to handle a mismatch between integer ‘seller’ IDs in the APEM scenario and string ‘generator’ names used in the PyPSA model results. It creates a mapping to correctly translate between them.
- Parameters:
nodal_results – Dictionary from NodalDispatchModel.solve(), containing result DataFrames and the Gurobi model.
network – The PyPSA network object used for the simulation.
scenario – The input APEM Scenario object.
power_flow_model – The instance of the power flow model (e.g., NodalFBMC) that was run.
- Returns:
An Allocation object containing the detailed results.
- Raises:
ValueError – If the model in nodal_results was not solved to optimality.