Skip to content

Commit

Permalink
Enforce objectives and controls have at least one element in everest …
Browse files Browse the repository at this point in the history
…config
  • Loading branch information
DanSava committed Jan 29, 2025
1 parent b96b543 commit b0759ac
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/everest/config/everest_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,10 @@ class EverestConfig(BaseModel): # type: ignore
Controls should have unique names each control defines
a group of control variables
""",
min_length=1,
)
objective_functions: list[ObjectiveFunctionConfig] = Field(
description="List of objective function specifications",
description="List of objective function specifications", min_length=1
)
optimization: OptimizationConfig | None = Field(
default=OptimizationConfig(),
Expand Down Expand Up @@ -710,8 +711,17 @@ def with_defaults(cls, **kwargs):
without having to provide empty defaults.
"""
defaults = {
"controls": [],
"objective_functions": [],
"controls": [
{
"name": "default_group",
"type": "generic_control",
"initial_guess": 0.5,
"variables": [
{"name": "default_name", "min": 0, "max": 1},
],
}
],
"objective_functions": [{"name": "default"}],
"config_path": ".",
"model": {"realizations": [0]},
}
Expand Down
51 changes: 51 additions & 0 deletions tests/everest/test_config_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1025,3 +1025,54 @@ def test_load_file_with_errors(copy_math_func_test_data_to_tmp, capsys):

assert "line: 16, column: 5" in captured.err
assert "Extra inputs are not permitted (type=extra_forbidden)" in captured.err


@pytest.mark.parametrize(
["controls", "objectives", "error_msg"],
[
(
[],
[],
[
"controls\n List should have at least 1 item after validation, not 0",
"objective_functions\n List should have at least 1 item after validation, not 0",
],
),
(
[],
[{"name": "npv"}],
[
"controls\n List should have at least 1 item after validation, not 0",
],
),
(
[
{
"name": "test",
"type": "generic_control",
"initial_guess": 0.5,
"variables": [
{"name": "test", "min": 0, "max": 1},
],
}
],
[{"name": "npv"}],
[],
),
],
)
def test_warning_empty_controls_and_objectives(controls, objectives, error_msg):
if error_msg:
with pytest.raises(ValueError) as e:
EverestConfig.with_defaults(
objective_functions=objectives,
controls=controls,
)

for msg in error_msg:
assert msg in str(e.value)
else:
EverestConfig.with_defaults(
objective_functions=objectives,
controls=controls,
)

0 comments on commit b0759ac

Please sign in to comment.