Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: check objective in test routines #1468

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,23 @@ install-pinned-macos: _conda_check
# Run default tests
test:
set -e
snakemake solve_elec_networks --configfile config/test/config.electricity.yaml --rerun-triggers=mtime
snakemake --configfile config/test/config.overnight.yaml --rerun-triggers=mtime
snakemake --configfile config/test/config.myopic.yaml --rerun-triggers=mtime
snakemake make_summary_perfect --configfile config/test/config.perfect.yaml --rerun-triggers=mtime
snakemake --configfile config/test/config.scenarios.yaml --rerun-triggers=mtime -n
snakemake solve_elec_networks --configfile config/test/config.electricity.yaml
snakemake --configfile config/test/config.overnight.yaml
snakemake --configfile config/test/config.myopic.yaml
snakemake make_summary_perfect --configfile config/test/config.perfect.yaml
snakemake --configfile config/test/config.scenarios.yaml -n
echo "All tests completed successfully."

unit-test:
pytest test

# Cleans all output files from tests
clean-tests:
snakemake solve_elec_networks --configfile config/test/config.electricity.yaml --rerun-triggers=mtime --delete-all-output
snakemake --configfile config/test/config.overnight.yaml --rerun-triggers=mtime --delete-all-output
snakemake --configfile config/test/config.myopic.yaml --rerun-triggers=mtime --delete-all-output
snakemake make_summary_perfect --configfile config/test/config.perfect.yaml --rerun-triggers=mtime --delete-all-output
snakemake --configfile config/test/config.scenarios.yaml --rerun-triggers=mtime -n --delete-all-output
snakemake solve_elec_networks --configfile config/test/config.electricity.yaml --delete-all-output
snakemake --configfile config/test/config.overnight.yaml --delete-all-output
snakemake --configfile config/test/config.myopic.yaml --delete-all-output
snakemake make_summary_perfect --configfile config/test/config.perfect.yaml --delete-all-output
snakemake --configfile config/test/config.scenarios.yaml -n --delete-all-output

# Removes all created files except for large cutout files (similar to fresh clone)
reset:
Expand Down
6 changes: 6 additions & 0 deletions config/config.default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,12 @@ solving:
cbc-default: {} # Used in CI
glpk-default: {} # Used in CI

check_objective:
enable: false
expected_value: None
atol: 1000
rtol: 0.001

mem_mb: 30000 #memory in MB; 20 GB enough for 50+B+I+H2; 100 GB for 181+B+I+H2
runtime: 6h #runtime in humanfriendly style https://humanfriendly.readthedocs.io/en/latest/

Expand Down
3 changes: 3 additions & 0 deletions config/test/config.electricity.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ solving:
name: highs
options: highs-default

check_objective:
enable: true
expected_value: 39541845.44

plotting:
map:
Expand Down
4 changes: 4 additions & 0 deletions config/test/config.overnight.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ solving:
options: highs-default
mem: 4000

check_objective:
enable: true
expected_value: 7.1040937763e+08

plotting:
map:
boundaries:
Expand Down
4 changes: 4 additions & 0 deletions config/test/config.perfect.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ solving:
options: highs-default
mem: 4000

check_objective:
enable: true
expected_value: 14385130476

plotting:
map:
boundaries:
Expand Down
1 change: 0 additions & 1 deletion matplotlibrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
#
# SPDX-License-Identifier: CC0-1.0
font.family: sans-serif
font.sans-serif: Ubuntu, DejaVu Sans
image.cmap: viridis
figure.autolayout : True
28 changes: 24 additions & 4 deletions scripts/solve_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
pypsa.pf.logger.setLevel(logging.WARNING)


class ObjectiveValueError(Exception):
pass


def add_land_use_constraint_perfect(n):
"""
Add global constraints for tech capacity limit.
Expand Down Expand Up @@ -986,6 +990,19 @@ def extra_functionality(n, snapshots):
custom_extra_functionality(n, snapshots, snakemake)


def check_objective_value(n, solving):
check_objective = solving["check_objective"]
if check_objective["enable"]:
atol = check_objective["atol"]
rtol = check_objective["rtol"]
expected_value = check_objective["expected_value"]
if not np.isclose(n.objective, expected_value, atol=atol, rtol=rtol):
raise ObjectiveValueError(
f"Objective value {n.objective} differs from expected value "
f"{expected_value} by more than {atol}."
)


def solve_network(n, config, params, solving, **kwargs):
set_of_options = solving["solver"]["options"]
cf_solving = solving["options"]
Expand Down Expand Up @@ -1034,10 +1051,13 @@ def solve_network(n, config, params, solving, **kwargs):
**kwargs
)

if status != "ok" and not rolling_horizon:
logger.warning(
f"Solving status '{status}' with termination condition '{condition}'"
)
if not rolling_horizon:
if status != "ok":
logger.warning(
f"Solving status '{status}' with termination condition '{condition}'"
)
check_objective_value(n, solving)

if "infeasible" in condition:
labels = n.model.compute_infeasibilities()
logger.info(f"Labels:\n{labels}")
Expand Down
Loading