Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
yngve-sk committed Jan 20, 2025
1 parent cde99c8 commit d171890
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/ert/run_models/everest_run_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,33 @@ def _forward_model_evaluator(
def _get_cached_results(
self, control_values: NDArray[np.float64], evaluator_context: EvaluatorContext
) -> dict[int, Any]:
control_groups = {c.name: c for c in self._everest_config.controls}
control_variables = {g: len(c.variables) for g, c in control_groups.items()}
control_group_spans = []
span_ = 0
for num_vars in control_variables.values():
control_group_spans.append((span_, span_ + num_vars))
span_ += num_vars

def controls_1d_to_dict(realization: int):

Check failure on line 462 in src/ert/run_models/everest_run_model.py

View workflow job for this annotation

GitHub Actions / type-checking (3.12)

Function is missing a return type annotation
values_ = control_values[realization]
return {
group: values_[span_[0] : span_[1]]
for group, span_ in zip(
control_groups, control_group_spans, strict=False
)
}

for ert_realization in control_values:
parameter_group_values = controls_1d_to_dict(ert_realization)
matching_realization = (
self._experiment.find_realization_by_parameter_values(

Check failure on line 474 in src/ert/run_models/everest_run_model.py

View workflow job for this annotation

GitHub Actions / type-checking (3.12)

Item "None" of "LocalExperiment | None" has no attribute "find_realization_by_parameter_values"
parameter_group_values
)
)
if matching_realization is not None:
print("Found a cached one!")

cached_results: dict[int, Any] = {}
if self._simulator_cache is not None:
for control_idx, real_idx in enumerate(evaluator_context.realizations):
Expand Down
31 changes: 31 additions & 0 deletions src/ert/storage/local_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,3 +390,34 @@ def _update_response_keys(

if self.response_type_to_response_keys is not None:
del self.response_type_to_response_keys

def find_realization_by_parameter_values(
self, parameter_values: dict[str, np.array]

Check failure on line 395 in src/ert/storage/local_experiment.py

View workflow job for this annotation

GitHub Actions / type-checking (3.12)

Function "numpy.core.multiarray.array" is not valid as a type
) -> int | None:
if not list(self.ensembles):
return None

for ensemble in self.ensembles:
ens_parameters = {
group: ensemble.load_parameters(group)
.to_dataarray()
.data.reshape((ensemble.ensemble_size, -1))
for group in parameter_values
}

matching_real = next(
(
i
for i in range(ensemble.ensemble_size)
if all(
np.allclose(ens_parameters[group][i], group_data)
for group, group_data in parameter_values.items()
)
),
None,
)

if matching_real is not None:
return matching_real

return None
14 changes: 14 additions & 0 deletions test-data/everest/math_func/config_advanced.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ controls:
index: 1
- name: x
index: 2
- name: point2
max: 1.0
min: -1.0
initial_guess: 0.25
perturbation_magnitude: 0.005
type: generic_control
variables:
- name: x
index: 0
- name: x
index: 1
- name: x
index: 2


objective_functions:
- name: distance
Expand Down

0 comments on commit d171890

Please sign in to comment.