Skip to content

Commit

Permalink
Add Everest storage
Browse files Browse the repository at this point in the history
(will be split into nicer commits)
  • Loading branch information
yngve-sk committed Nov 21, 2024
1 parent 5cbe374 commit 7e2fd78
Show file tree
Hide file tree
Showing 2 changed files with 996 additions and 364 deletions.
33 changes: 13 additions & 20 deletions src/ert/run_models/everest_run_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import shutil
import threading
import time
from dataclasses import dataclass
from pathlib import Path
from types import TracebackType
from typing import (
Expand All @@ -28,7 +27,6 @@
TypedDict,
)

import seba_sqlite.sqlite_storage
from ropt.enums import EventType, OptimizerExitCode
from ropt.plan import BasicOptimizer, Event
from seba_sqlite import SqliteStorage
Expand All @@ -37,6 +35,7 @@
from ert.ensemble_evaluator import EvaluatorServerConfig
from ert.storage import open_storage
from everest.config import EverestConfig
from everest.everest_storage import EverestStorage, OptimalResult
from everest.optimizer.everest2ropt import everest2ropt
from everest.simulator import Simulator
from everest.simulator.everest_to_ert import everest_to_ert_config
Expand Down Expand Up @@ -291,24 +290,6 @@ class OptimizerCallback(Protocol):
def __call__(self) -> str | None: ...


@dataclass
class OptimalResult:
batch: int
controls: List[Any]
total_objective: float

@staticmethod
def from_seba_optimal_result(
o: Optional[seba_sqlite.sqlite_storage.OptimalResult] = None,
) -> "OptimalResult" | None:
if o is None:
return None

return OptimalResult(
batch=o.batch, controls=o.controls, total_objective=o.total_objective
)


class EverestRunModel(BaseRunModel):
def __init__(
self,
Expand Down Expand Up @@ -420,6 +401,16 @@ def run_experiment(
# Initialize the ropt optimizer:
optimizer = self._create_optimizer(simulator)

self.ever_storage = EverestStorage(
output_dir=Path(self.everest_config.optimization_output_dir),
)
self.ever_storage.observe_optimizer(
optimizer,
Path(self.everest_config.optimization_output_dir)
/ "dakota"
/ "OPT_DEFAULT.out",
)

# The SqliteStorage object is used to store optimization results from
# Seba in an sqlite database. It reacts directly to events emitted by
# Seba and is not called by Everest directly. The stored results are
Expand All @@ -437,6 +428,8 @@ def run_experiment(
self._result = OptimalResult.from_seba_optimal_result(
seba_storage.get_optimal_result() # type: ignore
)
optimal_result_from_everstorage = self.ever_storage.get_optimal_result()
assert self._result == optimal_result_from_everstorage

if self._monitor_thread is not None:
self._monitor_thread.stop()
Expand Down
Loading

0 comments on commit 7e2fd78

Please sign in to comment.