Skip to content

Solving

Functions for building and solving optimization models.

solve_network

python
solve_network((db_path: str, scenario_id: Optional[int] = None, solver_name: str = 'highs', solver_options: Optional[Dict[str, Any]] = None, constraints_dsl: Optional[str] = None, discount_rate: Optional[float] = None, progress_callback: Optional[Callable[[int, str], NoneType]] = None, return_detailed_results: bool = True, custom_solver_config: Optional[Dict[str, Any]] = None, include_unmet_loads: bool = True, verbose: bool = False) -> Dict[str, Any])

Complete solve workflow: build PyPSA network from database, solve, store results (single network per database).

This is the main high-level function that most users should use. It handles the complete workflow of loading data from database, building a PyPSA network, solving it, and storing results back to the database.

Parameters:

  • db_path: Path to the database file
  • scenario_id: Optional scenario ID (NULL for base network)
  • solver_name: Solver to use (default: "highs"). Use "custom" for custom_solver_config.
  • solver_options: Optional solver-specific options
  • constraints_dsl: Optional DSL constraints to apply
  • discount_rate: Optional discount rate for multi-period optimization
  • progress_callback: Optional callback for progress updates (progress: int, message: str)
  • return_detailed_results: If True, return comprehensive results; if False, return simple status
  • custom_solver_config: Optional custom solver configuration when solver_name="custom"
  • Format: {"solver": "actual_solver_name", "solver_options": {...}}
  • Example: {"solver": "gurobi", "solver_options": {"Method": 2, "Crossover": 0}}
  • include_unmet_loads: Whether to include unmet load components in the network (default: True)
  • verbose: Enable detailed logging output (default: False)

Returns:

Dictionary with solve results - comprehensive if return_detailed_results=True, simple status otherwise

Raises:

  • DatabaseError: If database operations fail
  • ValidationError: If network data is invalid
  • ImportError: If PyPSA is not available

build_pypsa_network

python
build_pypsa_network((db_path: str, scenario_id: Optional[int] = None, progress_callback: Optional[Callable[[int, str], NoneType]] = None, verbose: bool = False) -> 'pypsa.Network')

Build PyPSA network object from database (single network per database).

This function loads all network data from the database and constructs a PyPSA Network object ready for solving or analysis. Useful when you want to inspect or modify the network before solving.

Parameters:

  • db_path: Path to the database file
  • scenario_id: Optional scenario ID (NULL for base network)
  • progress_callback: Optional callback for progress updates
  • verbose: Enable detailed logging output (default: False)

Returns:

PyPSA Network object ready for solving

Raises:

  • DatabaseError: If database operations fail
  • ValidationError: If network data is invalid
  • ImportError: If PyPSA is not available

solve_pypsa_network

python
solve_pypsa_network((network: 'pypsa.Network', db_path: str, scenario_id: Optional[int] = None, solver_name: str = 'highs', solver_options: Optional[Dict[str, Any]] = None, discount_rate: Optional[float] = None, store_results: bool = True, progress_callback: Optional[Callable[[int, str], NoneType]] = None, custom_solver_config: Optional[Dict[str, Any]] = None, verbose: bool = False) -> Dict[str, Any])

Solve PyPSA network and optionally store results back to database (single network per database).

This function takes an existing PyPSA network (e.g., from build_pypsa_network), solves it, and optionally stores the results back to the database.

Parameters:

  • network: PyPSA Network object to solve
  • db_path: Path to the database file (needed for result storage)
  • scenario_id: Optional scenario ID (NULL for base network)
  • solver_name: Solver to use (default: "highs"). Use "custom" for custom_solver_config.
  • solver_options: Optional solver-specific options
  • discount_rate: Optional discount rate for multi-period optimization (default: 0.0)
  • store_results: Whether to store results back to database (default: True)
  • progress_callback: Optional callback for progress updates
  • custom_solver_config: Optional custom solver configuration when solver_name="custom"
  • Format: {"solver": "actual_solver_name", "solver_options": {...}}
  • verbose: Enable detailed logging output (default: False)

Returns:

Dictionary with solve results and statistics

Raises:

  • DatabaseError: If database operations fail (when store_results=True)
  • ImportError: If PyPSA is not available

© Copyright 2025 Bayesian Energy