Skip to content

Commit

Permalink
Perform validation earlier by returning EnOptConfig instead of dict (#…
Browse files Browse the repository at this point in the history
…9249)

Perform earlier validation on the EnOptConfig
  • Loading branch information
StephanDeHoop authored Nov 19, 2024
1 parent 49dde01 commit f38fd9d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 24 deletions.
5 changes: 3 additions & 2 deletions src/everest/optimizer/everest2ropt.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
Union,
)

from ropt.config.enopt import EnOptConfig
from ropt.enums import ConstraintType, PerturbationType, VariableType
from typing_extensions import Final, TypeAlias

Expand Down Expand Up @@ -504,7 +505,7 @@ def _parse_environment(ever_config: EverestConfig, ropt_config):
ropt_config["gradient"]["seed"] = ever_config.environment.random_seed


def everest2ropt(ever_config: EverestConfig) -> Dict[str, Any]:
def everest2ropt(ever_config: EverestConfig) -> EnOptConfig:
"""Generate a ropt configuration from an Everest one
NOTE: This method is a work in progress. So far only the some of
Expand All @@ -531,4 +532,4 @@ def everest2ropt(ever_config: EverestConfig) -> Dict[str, Any]:
_parse_model(ever_config, ropt_config)
_parse_environment(ever_config, ropt_config)

return ropt_config
return EnOptConfig.model_validate(ropt_config)
39 changes: 17 additions & 22 deletions tests/everest/test_ropt_initialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import numpy
import pytest
from pydantic import ValidationError
from ropt.config.enopt import EnOptConfig
from ropt.enums import ConstraintType

from everest.config import EverestConfig
Expand All @@ -17,7 +16,7 @@

def test_tutorial_everest2ropt():
ever_config = EverestConfig.load_file(os.path.join(_CONFIG_DIR, _CONFIG_FILE))
ropt_config = EnOptConfig.model_validate(everest2ropt(ever_config))
ropt_config = everest2ropt(ever_config)

realizations = ropt_config.realizations

Expand All @@ -32,7 +31,7 @@ def test_everest2ropt_controls():
controls = config.controls
assert len(controls) == 1

ropt_config = EnOptConfig.model_validate(everest2ropt(config))
ropt_config = everest2ropt(config)

assert len(ropt_config.variables.lower_bounds) == 16
assert len(ropt_config.variables.upper_bounds) == 16
Expand All @@ -43,7 +42,7 @@ def test_everest2ropt_controls_auto_scale():
controls = config.controls
controls[0].auto_scale = True
controls[0].scaled_range = [0.3, 0.7]
ropt_config = EnOptConfig.model_validate(everest2ropt(config))
ropt_config = everest2ropt(config)
assert numpy.allclose(ropt_config.variables.lower_bounds, 0.3)
assert numpy.allclose(ropt_config.variables.upper_bounds, 0.7)

Expand All @@ -53,7 +52,7 @@ def test_everest2ropt_variables_auto_scale():
controls = config.controls
controls[0].variables[1].auto_scale = True
controls[0].variables[1].scaled_range = [0.3, 0.7]
ropt_config = EnOptConfig.model_validate(everest2ropt(config))
ropt_config = everest2ropt(config)
assert ropt_config.variables.lower_bounds[0] == 0.0
assert ropt_config.variables.upper_bounds[0] == 0.1
assert ropt_config.variables.lower_bounds[1] == 0.3
Expand All @@ -70,7 +69,7 @@ def test_everest2ropt_controls_input_constraint():
# Check that there are two input constraints entries in the config
assert len(input_constraints_ever_config) == 2

ropt_config = EnOptConfig.model_validate(everest2ropt(config))
ropt_config = everest2ropt(config)

# The input has two constraints: one two-sided inequality constraint,
# and an equality constraint. The first is converted into LE and GE
Expand All @@ -96,7 +95,7 @@ def test_everest2ropt_controls_input_constraint_auto_scale():
# Check that there are two input constraints entries in the config
assert len(input_constraints_ever_config) == 2

ropt_config = EnOptConfig.model_validate(everest2ropt(config))
ropt_config = everest2ropt(config)
min_values = ropt_config.variables.lower_bounds.copy()
max_values = ropt_config.variables.upper_bounds.copy()
coefficients = ropt_config.linear_constraints.coefficients
Expand All @@ -117,7 +116,7 @@ def test_everest2ropt_controls_input_constraint_auto_scale():
scaled_coefficients = coefficients * (max_values - min_values) / 0.4
scaled_coefficients[:2, 1] = coefficients[:2, 1] * 2.0 / 0.4

ropt_config = EnOptConfig.model_validate(everest2ropt(config))
ropt_config = everest2ropt(config)
assert numpy.allclose(
ropt_config.linear_constraints.coefficients,
scaled_coefficients,
Expand All @@ -131,7 +130,7 @@ def test_everest2ropt_controls_input_constraint_auto_scale():
def test_everest2ropt_controls_optimizer_setting():
config = os.path.join(_CONFIG_DIR, "config_full_gradient_info.yml")
config = EverestConfig.load_file(config)
ropt_config = EnOptConfig.model_validate(everest2ropt(config))
ropt_config = everest2ropt(config)
assert len(ropt_config.realizations.names) == 15
assert ropt_config.optimizer.method == "dakota/conmin_mfd"
assert ropt_config.gradient.number_of_perturbations == 20
Expand All @@ -141,7 +140,7 @@ def test_everest2ropt_controls_optimizer_setting():
def test_everest2ropt_constraints():
config = os.path.join(_CONFIG_DIR, "config_output_constraints.yml")
config = EverestConfig.load_file(config)
ropt_config = EnOptConfig.model_validate(everest2ropt(config))
ropt_config = everest2ropt(config)
assert len(ropt_config.nonlinear_constraints.names) == 16


Expand All @@ -150,24 +149,24 @@ def test_everest2ropt_backend_options():
config = EverestConfig.load_file(config)

config.optimization.options = ["test = 1"]
ropt_config = EnOptConfig.model_validate(everest2ropt(config))
ropt_config = everest2ropt(config)
assert ropt_config.optimizer.options == ["test = 1"]

config.optimization.backend = "scipy"
config.optimization.backend_options = {"test": 1}
with pytest.raises(RuntimeError):
_ = EnOptConfig.model_validate(everest2ropt(config))
_ = everest2ropt(config)

config.optimization.options = None
ropt_config = EnOptConfig.model_validate(everest2ropt(config))
ropt_config = everest2ropt(config)
assert ropt_config.optimizer.options["test"] == 1


def test_everest2ropt_samplers():
config = os.path.join(_CONFIG_DIR, "config_samplers.yml")
config = EverestConfig.load_file(config)

ropt_config = EnOptConfig.model_validate(everest2ropt(config))
ropt_config = everest2ropt(config)

assert len(ropt_config.samplers) == 5
assert ropt_config.gradient.samplers.tolist() == [0, 0, 1, 2, 3, 4]
Expand Down Expand Up @@ -205,9 +204,7 @@ def test_everest2ropt_cvar():
"number_of_realizations": 1,
}

ropt_config = EnOptConfig.model_validate(
everest2ropt(EverestConfig.model_validate(config_dict))
)
ropt_config = everest2ropt(EverestConfig.model_validate(config_dict))

assert ropt_config.objective_functions.realization_filters == [0]
assert len(ropt_config.realization_filters) == 1
Expand All @@ -220,9 +217,7 @@ def test_everest2ropt_cvar():
"percentile": 0.3,
}

ropt_config = EnOptConfig.model_validate(
everest2ropt(EverestConfig.model_validate(config_dict))
)
ropt_config = everest2ropt(EverestConfig.model_validate(config_dict))
assert ropt_config.objective_functions.realization_filters == [0]
assert len(ropt_config.realization_filters) == 1
assert ropt_config.realization_filters[0].method == "cvar-objective"
Expand All @@ -234,7 +229,7 @@ def test_everest2ropt_arbitrary_backend_options():
config = EverestConfig.load_file(os.path.join(_CONFIG_DIR, _CONFIG_FILE))
config.optimization.backend_options = {"a": [1]}

ropt_config = EnOptConfig.model_validate(everest2ropt(config))
ropt_config = everest2ropt(config)
assert "a" in ropt_config.optimizer.options
assert ropt_config.optimizer.options["a"] == [1]

Expand All @@ -245,5 +240,5 @@ def test_everest2ropt_no_algorithm_name(copy_test_data_to_tmp):
)

config.optimization.algorithm = None
ropt_config = EnOptConfig.model_validate(everest2ropt(config))
ropt_config = everest2ropt(config)
assert ropt_config.optimizer.method == "dakota/default"

0 comments on commit f38fd9d

Please sign in to comment.