Skip to content

Commit

Permalink
Handle minimization/maximization conversion with the scaler
Browse files Browse the repository at this point in the history
  • Loading branch information
verveerpj committed Feb 7, 2025
1 parent c0bb0e8 commit 0aad2c2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
3 changes: 1 addition & 2 deletions src/ert/run_models/everest_run_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,8 +552,7 @@ def _get_objectives_and_constraints(
results: list[dict[str, NDArray[np.float64]]],
cached_results: dict[int, Any],
) -> tuple[NDArray[np.float64], NDArray[np.float64] | None]:
# We minimize the negative of the objectives:
objectives = -self._get_simulation_results(
objectives = self._get_simulation_results(
results, self._everest_config.objective_names, control_values, batch_data
)

Expand Down
7 changes: 2 additions & 5 deletions src/everest/everest_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import polars as pl
from ropt.enums import EventType
from ropt.plan import BasicOptimizer, Event
from ropt.results import FunctionResults, GradientResults, convert_to_maximize
from ropt.results import FunctionResults, GradientResults

from everest.config import EverestConfig
from everest.strings import EVEREST
Expand Down Expand Up @@ -661,14 +661,11 @@ def _store_gradient_results(self, results: GradientResults) -> _GradientResults:
def _on_batch_evaluation_finished(self, event: Event) -> None:
logger.debug("Storing batch results dataframes")

converted_results = tuple(
convert_to_maximize(result) for result in event.data.get("results", [])
)
results: list[FunctionResults | GradientResults] = []

best_value = -np.inf
best_results = None
for item in converted_results:
for item in event.data.get("results", []):
if isinstance(item, GradientResults):
results.append(item)
if (
Expand Down
10 changes: 8 additions & 2 deletions src/everest/optimizer/opt_model_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,17 @@ def __init__(
self._auto_scales = np.asarray(auto_scales, dtype=np.bool_)
self._weights = np.asarray(weights, dtype=np.float64)

# The transform methods below all return the negative of the objectives.
# This is because Everest maximizes the objectives, while ropt is a minimizer.

def forward(self, objectives: NDArray[np.float64]) -> NDArray[np.float64]:
return objectives / self._scales
return -objectives / self._scales

def backward(self, objectives: NDArray[np.float64]) -> NDArray[np.float64]:
return objectives * self._scales
return -objectives * self._scales

def transform_weighted_objective(self, weighted_objective):
return -weighted_objective

def calculate_auto_scales(self, objectives: NDArray[np.float64]) -> None:
auto_scales = np.abs(
Expand Down

0 comments on commit 0aad2c2

Please sign in to comment.