Skip to content

Commit

Permalink
Make simulator fm invocation more explicit
Browse files Browse the repository at this point in the history
  • Loading branch information
yngve-sk committed Nov 5, 2024
1 parent cac529f commit d5f9a80
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/ert/run_models/everest_run_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,14 +501,15 @@ def _create_optimizer(self, simulator: Simulator) -> BasicOptimizer:
)

ropt_output_folder = Path(self.everest_config.optimization_output_dir)
ropt_evaluator_fn = simulator.create_forward_model_evaluator_function()

# 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.
optimizer = (
BasicOptimizer(enopt_config=self.ropt_config, evaluator=simulator)
BasicOptimizer(enopt_config=self.ropt_config, evaluator=ropt_evaluator_fn)
.add_table(
columns=RESULT_COLUMNS,
path=ropt_output_folder / "results.txt",
Expand Down
15 changes: 13 additions & 2 deletions src/everest/simulator/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import numpy as np
from numpy import float64
from numpy._typing import NDArray
from ropt.evaluator import EvaluatorContext, EvaluatorResult
from ropt.evaluator import Evaluator, EvaluatorContext, EvaluatorResult

from ert import BatchSimulator, WorkflowRunner
from ert.config import ErtConfig, HookRuntime
Expand Down Expand Up @@ -90,7 +90,18 @@ def _get_aliases(self, ever_config: EverestConfig) -> Dict[str, str]:
aliases[f"{constraint.name}:upper"] = constraint.name
return aliases

def __call__(
def create_forward_model_evaluator_function(
self,
) -> Evaluator:
def run_forward_model(
control_values: NDArray[np.float64], metadata: EvaluatorContext
) -> EvaluatorResult:
nonlocal self
return self._run_forward_model(control_values, metadata)

return run_forward_model

def _run_forward_model(
self, control_values: NDArray[np.float64], metadata: EvaluatorContext
) -> EvaluatorResult:
active = (
Expand Down
14 changes: 10 additions & 4 deletions tests/everest/test_simulator_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@

def test_simulator_cache(monkeypatch, copy_math_func_test_data_to_tmp):
n_evals = 0
original_call = Simulator.__call__
original_call = Simulator._run_forward_model

def new_call(*args):
nonlocal n_evals
result = original_call(*args)
n_evals += (result.evaluation_ids >= 0).sum()
return result

monkeypatch.setattr(Simulator, "__call__", new_call)
monkeypatch.setattr(Simulator, "_run_forward_model", new_call)

config = EverestConfig.load_file(CONFIG_FILE)
config.simulator = SimulatorConfig(enable_cache=True)
Expand All @@ -33,7 +33,10 @@ def new_call(*args):

# Run once, populating the cache of the simulator:
variables1 = (
BasicOptimizer(enopt_config=ropt_config, evaluator=simulator)
BasicOptimizer(
enopt_config=ropt_config,
evaluator=simulator.create_forward_model_evaluator_function(),
)
.run()
.variables
)
Expand All @@ -44,7 +47,10 @@ def new_call(*args):
# Run again with the same simulator:
n_evals = 0
variables2 = (
BasicOptimizer(enopt_config=ropt_config, evaluator=simulator)
BasicOptimizer(
enopt_config=ropt_config,
evaluator=simulator.create_forward_model_evaluator_function(),
)
.run()
.variables
)
Expand Down

0 comments on commit d5f9a80

Please sign in to comment.