7
7
import os
8
8
import queue
9
9
import shutil
10
- from collections import defaultdict
11
10
from collections .abc import Callable
12
11
from dataclasses import dataclass
13
12
from enum import IntEnum
@@ -125,14 +124,6 @@ def __init__(
125
124
self ._fm_errors : dict [int , dict [str , Any ]] = {}
126
125
self ._result : OptimalResult | None = None
127
126
self ._exit_code : EverestExitCode | None = None
128
- self ._simulator_cache = (
129
- SimulatorCache ()
130
- if (
131
- everest_config .simulator is not None
132
- and everest_config .simulator .enable_cache
133
- )
134
- else None
135
- )
136
127
self ._experiment : EverestExperiment | None = None
137
128
self ._eval_server_cfg : EvaluatorServerConfig | None = None
138
129
self ._batch_id : int = 0
@@ -387,7 +378,6 @@ def _forward_model_evaluator(
387
378
388
379
realizations = self ._everest_config .model .realizations
389
380
num_perturbations = self ._everest_config .optimization .perturbation_num
390
- realization_mapping : dict [int , EverestRealizationInfo ] = {}
391
381
392
382
realization_mapping : dict [int , EverestRealizationInfo ] = {}
393
383
if len (evaluator_context .realizations ) == len (realizations ):
@@ -444,15 +434,6 @@ def _forward_model_evaluator(
444
434
control_values , batch_data , results , cached_results
445
435
)
446
436
447
- # Add the results from the evaluations to the cache:
448
- self ._add_results_to_cache (
449
- control_values ,
450
- evaluator_context ,
451
- batch_data ,
452
- evaluator_result .objectives ,
453
- evaluator_result .constraints ,
454
- )
455
-
456
437
# Increase the batch ID for the next evaluation:
457
438
self ._batch_id += 1
458
439
@@ -477,16 +458,6 @@ def controls_1d_to_dict(values_: list[float]):
477
458
)
478
459
}
479
460
480
- cached_results : dict [int , Any ] = {}
481
- if self ._simulator_cache is not None :
482
- for control_idx , real_idx in enumerate (evaluator_context .realizations ):
483
- cached_data = self ._simulator_cache .get (
484
- self ._everest_config .model .realizations [real_idx ],
485
- control_values [control_idx , :],
486
- )
487
- if cached_data is not None :
488
- cached_results [control_idx ] = cached_data
489
-
490
461
cached_results2 : dict [int , Any ] = {}
491
462
for control_values_ in control_values .tolist ():
492
463
parameter_group_values = controls_1d_to_dict (control_values_ )
@@ -522,20 +493,6 @@ def controls_1d_to_dict(values_: list[float]):
522
493
)
523
494
cached_results2 [ert_realization ] = cached_data
524
495
525
- # Redundant double-checking for sanity, to be removed
526
- # +--------------------------------------------------------+
527
- for k , expected in cached_results .items ():
528
- actual_objs , actual_constrs = cached_results2 [k ]
529
- exp_objs , exp_constrs = expected
530
- if not np .allclose (actual_objs , exp_objs , atol = 1e-6 ):
531
- print ("Something wrong with caching!" )
532
-
533
- if actual_constrs != exp_constrs and not np .allclose (
534
- actual_constrs , exp_constrs , atol = 1e-6
535
- ):
536
- print ("Something wrong with caching!" )
537
- # +--------------------------------------------------------+
538
-
539
496
return cached_results2
540
497
541
498
def _init_batch_data (
@@ -720,15 +677,14 @@ def _make_evaluator_result(
720
677
batch_data ,
721
678
)
722
679
723
- if self ._simulator_cache is not None :
724
- for control_idx , (
725
- cached_objectives ,
726
- cached_constraints ,
727
- ) in cached_results .items ():
728
- objectives [control_idx , ...] = cached_objectives
729
- if constraints is not None :
730
- assert cached_constraints is not None
731
- constraints [control_idx , ...] = cached_constraints
680
+ for control_idx , (
681
+ cached_objectives ,
682
+ cached_constraints ,
683
+ ) in cached_results .items ():
684
+ objectives [control_idx , ...] = cached_objectives
685
+ if constraints is not None :
686
+ assert cached_constraints is not None
687
+ constraints [control_idx , ...] = cached_constraints
732
688
733
689
sim_ids = np .full (control_values .shape [0 ], - 1 , dtype = np .intc )
734
690
sim_ids [list (batch_data .keys ())] = np .arange (len (batch_data ), dtype = np .intc )
@@ -755,25 +711,6 @@ def _get_simulation_results(
755
711
)
756
712
return values
757
713
758
- def _add_results_to_cache (
759
- self ,
760
- control_values : NDArray [np .float64 ],
761
- evaluator_context : EvaluatorContext ,
762
- batch_data : dict [int , Any ],
763
- objectives : NDArray [np .float64 ],
764
- constraints : NDArray [np .float64 ] | None ,
765
- ) -> None :
766
- if self ._simulator_cache is not None :
767
- for control_idx in batch_data :
768
- self ._simulator_cache .add (
769
- self ._everest_config .model .realizations [
770
- evaluator_context .realizations [control_idx ]
771
- ],
772
- control_values [control_idx , ...],
773
- objectives [control_idx , ...],
774
- None if constraints is None else constraints [control_idx , ...],
775
- )
776
-
777
714
def check_if_runpath_exists (self ) -> bool :
778
715
return (
779
716
self ._everest_config .simulation_dir is not None
@@ -855,55 +792,3 @@ def _handle_errors(
855
792
elif fm_id not in self ._fm_errors [error_hash ]["ids" ]:
856
793
self ._fm_errors [error_hash ]["ids" ].append (fm_id )
857
794
error_id = self ._fm_errors [error_hash ]["error_id" ]
858
- fm_logger .error (err_msg .format ("Already reported as" , error_id ))
859
-
860
-
861
- class SimulatorCache :
862
- EPS = float (np .finfo (np .float32 ).eps )
863
-
864
- def __init__ (self ) -> None :
865
- self ._data : defaultdict [
866
- int ,
867
- list [
868
- tuple [
869
- NDArray [np .float64 ], NDArray [np .float64 ], NDArray [np .float64 ] | None
870
- ]
871
- ],
872
- ] = defaultdict (list )
873
-
874
- def add (
875
- self ,
876
- realization : int ,
877
- control_values : NDArray [np .float64 ],
878
- objectives : NDArray [np .float64 ],
879
- constraints : NDArray [np .float64 ] | None ,
880
- ) -> None :
881
- """Add objective and constraints for a given realization and control values.
882
-
883
- The realization is the index of the realization in the ensemble, as specified
884
- in by the realizations entry in the everest model configuration. Both the control
885
- values and the realization are used as keys to retrieve the objectives and
886
- constraints later.
887
- """
888
- self ._data [realization ].append (
889
- (
890
- control_values .copy (),
891
- objectives .copy (),
892
- None if constraints is None else constraints .copy (),
893
- ),
894
- )
895
-
896
- def get (
897
- self , realization : int , controls : NDArray [np .float64 ]
898
- ) -> tuple [NDArray [np .float64 ], NDArray [np .float64 ] | None ] | None :
899
- """Get objective and constraints for a given realization and control values.
900
-
901
- The realization is the index of the realization in the ensemble, as specified
902
- in by the realizations entry in the everest model configuration. Both the control
903
- values and the realization are used as keys to retrieve the objectives and
904
- constraints from the cached values.
905
- """
906
- for control_values , objectives , constraints in self ._data .get (realization , []):
907
- if np .allclose (controls , control_values , rtol = 0.0 , atol = self .EPS ):
908
- return objectives , constraints
909
- return None
0 commit comments