From 8c679c1f3753359fdf656db36faa5721e182a865 Mon Sep 17 00:00:00 2001 From: Peter Verveer Date: Mon, 4 Nov 2024 06:16:44 +0000 Subject: [PATCH] Update ropt dependency to 0.9 --- pyproject.toml | 4 +- src/ert/run_models/everest_run_model.py | 81 +++++++++++-------------- src/everest/optimizer/everest2ropt.py | 2 + tests/everest/test_simulator_cache.py | 14 +---- 4 files changed, 44 insertions(+), 57 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 675a4c38240..f65d5b9c14c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -153,8 +153,8 @@ everest = [ "decorator", "resdata", "colorama", - "ropt[pandas]>=0.8,<0.9", - "ropt-dakota>=0.7,<0.8", + "ropt[pandas]>=0.9,<0.10", + "ropt-dakota>=0.9,<0.10", "seba-sqlite", ] diff --git a/src/ert/run_models/everest_run_model.py b/src/ert/run_models/everest_run_model.py index c294b11f89f..3a3a58323f2 100644 --- a/src/ert/run_models/everest_run_model.py +++ b/src/ert/run_models/everest_run_model.py @@ -29,8 +29,7 @@ import seba_sqlite.sqlite_storage from ropt.enums import EventType, OptimizerExitCode -from ropt.optimization import Event -from ropt.plan import OptimizationPlanRunner +from ropt.plan import BasicOptimizer, Event from seba_sqlite import SqliteStorage from ert.config import ErtConfig @@ -294,7 +293,6 @@ def __call__(self) -> str | None: ... class EverestRunModel(BaseRunModel): def __init__( self, - random_seed: Optional[int], config: ErtConfig, everest_config: EverestConfig, simulation_callback: SimulationCallback, @@ -335,7 +333,6 @@ def __init__( config.queue_config, status_queue, active_realizations=[True] * config.model_config.num_realizations, - random_seed=random_seed, minimum_required_realizations=config.model_config.num_realizations, # OK? ) @@ -372,7 +369,6 @@ def create( ever_config: EverestConfig, simulation_callback: Optional[SimulationCallback] = None, optimization_callback: Optional[OptimizerCallback] = None, - random_seed: Optional[int] = None, ) -> EverestRunModel: def default_simulation_callback( simulation_status: SimulationStatus | None, event: str @@ -384,7 +380,6 @@ def default_optimization_callback() -> str | None: ert_config = everest_to_ert_config(cls._add_defaults(ever_config)) return cls( - random_seed=random_seed, config=ert_config, everest_config=ever_config, simulation_callback=simulation_callback or default_simulation_callback, @@ -485,7 +480,7 @@ def _simulation_callback(self, ctx: BatchContext | None) -> None: self._monitor_thread.start() def _ropt_callback( - self, _: Event, optimizer: OptimizationPlanRunner, simulator: Simulator + self, _: Event, optimizer: BasicOptimizer, simulator: Simulator ) -> None: logging.getLogger(EVEREST).debug("Optimization callback called") @@ -507,50 +502,48 @@ def _ropt_callback( logging.getLogger(EVEREST).info("User abort requested.") optimizer.abort_optimization() - def _configure_optimizer(self, simulator: Simulator) -> OptimizationPlanRunner: + def _configure_optimizer(self, simulator: Simulator) -> BasicOptimizer: assert ( self.everest_config.environment is not None and self.everest_config.environment is not None ) - optimizer = OptimizationPlanRunner( - enopt_config=self.ropt_config, - evaluator=simulator, - seed=self.everest_config.environment.random_seed, - ) - # Initialize output tables. `min_header_len` is set to ensure that all - # tables have the same number of header lines, simplifying code that - # reads them as fixed width tables. `maximize` is set because ropt - # reports minimization results, while everest wants maximization - # results, necessitating a conversion step. ropt_output_folder = Path(self.everest_config.optimization_output_dir) - optimizer.add_table( - columns=RESULT_COLUMNS, - path=ropt_output_folder / "results.txt", - min_header_len=MIN_HEADER_LEN, - maximize=True, - ) - optimizer.add_table( - columns=GRADIENT_COLUMNS, - path=ropt_output_folder / "gradients.txt", - table_type="gradients", - min_header_len=MIN_HEADER_LEN, - maximize=True, - ) - optimizer.add_table( - columns=SIMULATION_COLUMNS, - path=ropt_output_folder / "simulations.txt", - min_header_len=MIN_HEADER_LEN, - maximize=True, - ) - optimizer.add_table( - columns=PERTURBATIONS_COLUMNS, - path=ropt_output_folder / "perturbations.txt", - table_type="gradients", - min_header_len=MIN_HEADER_LEN, - maximize=True, + + # Initialize the optimizer with output tables. `min_header_len` is set + # to ensure that all tables have the same number of header lines, + # simplifying code that reads them as fixed width tables. `maximize` is + # set because ropt reports minimization results, while everest wants + # maximization results, necessitating a conversion step. + return ( + BasicOptimizer(enopt_config=self.ropt_config, evaluator=simulator) + .add_table( + columns=RESULT_COLUMNS, + path=ropt_output_folder / "results.txt", + min_header_len=MIN_HEADER_LEN, + maximize=True, + ) + .add_table( + columns=GRADIENT_COLUMNS, + path=ropt_output_folder / "gradients.txt", + table_type="gradients", + min_header_len=MIN_HEADER_LEN, + maximize=True, + ) + .add_table( + columns=SIMULATION_COLUMNS, + path=ropt_output_folder / "simulations.txt", + min_header_len=MIN_HEADER_LEN, + maximize=True, + ) + .add_table( + columns=PERTURBATIONS_COLUMNS, + path=ropt_output_folder / "perturbations.txt", + table_type="gradients", + min_header_len=MIN_HEADER_LEN, + maximize=True, + ) ) - return optimizer @classmethod def name(cls) -> str: diff --git a/src/everest/optimizer/everest2ropt.py b/src/everest/optimizer/everest2ropt.py index 9b957d4439c..6e75986d33a 100644 --- a/src/everest/optimizer/everest2ropt.py +++ b/src/everest/optimizer/everest2ropt.py @@ -500,6 +500,8 @@ def _parse_environment(ever_config: EverestConfig, ropt_config): ropt_config["optimizer"]["output_dir"] = os.path.abspath( ever_config.optimization_output_dir ) + if ever_config.environment.random_seed is not None: + ropt_config["gradient"]["seed"] = ever_config.environment.random_seed def everest2ropt(ever_config: EverestConfig) -> Dict[str, Any]: diff --git a/tests/everest/test_simulator_cache.py b/tests/everest/test_simulator_cache.py index 6e99a3e6560..1eb152a73a6 100644 --- a/tests/everest/test_simulator_cache.py +++ b/tests/everest/test_simulator_cache.py @@ -1,5 +1,5 @@ import numpy as np -from ropt.plan import OptimizationPlanRunner +from ropt.plan import BasicOptimizer from ert.storage import open_storage from everest.config import EverestConfig, SimulatorConfig @@ -33,11 +33,7 @@ def new_call(*args): # Run once, populating the cache of the simulator: variables1 = ( - OptimizationPlanRunner( - enopt_config=ropt_config, - evaluator=simulator, - seed=config.environment.random_seed, - ) + BasicOptimizer(enopt_config=ropt_config, evaluator=simulator) .run() .variables ) @@ -48,11 +44,7 @@ def new_call(*args): # Run again with the same simulator: n_evals = 0 variables2 = ( - OptimizationPlanRunner( - enopt_config=ropt_config, - evaluator=simulator, - seed=config.environment.random_seed, - ) + BasicOptimizer(enopt_config=ropt_config, evaluator=simulator) .run() .variables )