Mock simulation evaluations (re-run using history file)
- mock_sim.mock_sim(H, persis_info, sim_specs, libE_info)
Places
sim_specs["out"]
from a numpy file into the outputs. Allows a user to reproduce an existing run while, for example, capturing additional information from a gen. Requires a user to have setsim_specs["user"]["history_file"]
to point to a history file from a previous run.
mock_sim.py
1import numpy as np
2
3
4def mock_sim(H, persis_info, sim_specs, libE_info):
5 """
6 Places ``sim_specs["out"]`` from a numpy file into the outputs. Allows a
7 user to reproduce an existing run while, for example, capturing additional
8 information from a gen. Requires a user to have set
9 ``sim_specs["user"]["history_file"]`` to point to a history file from a
10 previous run.
11 """
12
13 hfile = sim_specs["user"]["history_file"]
14 nparray = np.load(hfile)
15 row = libE_info["H_rows"][:][0]
16 libE_output = np.zeros(1, dtype=sim_specs["out"])
17
18 for field in libE_output.dtype.names:
19 libE_output[field] = nparray[row][field]
20
21 return libE_output, persis_info
Example usage
This test runs two repetitions. The first ensemble dumps a history file, the second replays the first run using the mock sim with the history file.
1"""
2Runs libEnsemble with a non-persistent generator performing uniform random
3sampling.
4
5Execute via one of the following commands (e.g. 3 workers):
6 mpiexec -np 4 python test_uniform_sampling.py
7 python test_uniform_sampling.py --nworkers 3
8 python test_uniform_sampling.py --nworkers 3 --comms tcp
9
10The number of concurrent evaluations of the objective function will be 4-1=3.
11"""
12
13# Do not change these lines - they are parsed by run-tests.sh
14# TESTSUITE_COMMS: mpi local tcp
15# TESTSUITE_NPROCS: 2 4
16
17import datetime
18import os
19
20import numpy as np
21
22from libensemble.gen_funcs.sampling import uniform_random_sample
23
24# Import libEnsemble items for this test
25from libensemble.libE import libE
26from libensemble.sim_funcs.mock_sim import mock_sim
27from libensemble.sim_funcs.six_hump_camel import six_hump_camel
28from libensemble.tests.regression_tests.common import read_generated_file
29from libensemble.tests.regression_tests.support import six_hump_camel_minima as minima
30from libensemble.tools import add_unique_random_streams, parse_args
31
32# Main block is necessary only when using local comms with spawn start method (default on macOS and Windows).
33if __name__ == "__main__":
34 nworkers, is_manager, libE_specs, _ = parse_args()
35 libE_specs["save_every_k_sims"] = 400
36 libE_specs["save_every_k_gens"] = 300
37 libE_specs["save_H_with_date"] = True
38 libE_specs["H_file_prefix"] = "TESTING"
39
40 sim_specs = {
41 "sim_f": six_hump_camel, # Function whose output is being minimized
42 "in": ["x"], # Keys to be given to sim_f
43 "out": [("f", float)], # Name of the outputs from sim_f
44 }
45 # end_sim_specs_rst_tag
46
47 gen_specs = {
48 "gen_f": uniform_random_sample, # Function generating sim_f input
49 "out": [("x", float, (2,))], # Tell libE gen_f output, type, size
50 "user": {
51 "gen_batch_size": 500, # Used by this specific gen_f
52 "lb": np.array([-3, -2]), # Used by this specific gen_f
53 "ub": np.array([3, 2]), # Used by this specific gen_f
54 },
55 }
56 # end_gen_specs_rst_tag
57
58 persis_info = add_unique_random_streams({}, nworkers + 1)
59
60 exit_criteria = {"gen_max": 501, "wallclock_max": 300}
61
62 for run in range(2):
63 if run == 1:
64 # Test running a mock sim using previous history file
65 sim_specs["sim_f"] = mock_sim
66 hfile = read_generated_file("TESTING_*_after_gen_1000.npy")
67 sim_specs["user"] = {"history_file": hfile}
68
69 # Perform the run
70 H, persis_info, flag = libE(sim_specs, gen_specs, exit_criteria, persis_info, libE_specs=libE_specs)
71
72 if is_manager:
73 assert flag == 0
74
75 tol = 0.1
76 for m in minima:
77 assert np.min(np.sum((H["x"] - m) ** 2, 1)) < tol
78
79 npy_files = [i for i in os.listdir(os.path.dirname(__file__)) if i.endswith(".npy")]
80 date = str(datetime.datetime.today()).split(" ")[0]
81 assert any([i.startswith("TESTING_") for i in npy_files])
82 assert any([date in i for i in npy_files])
83
84 print("\nlibEnsemble found the 6 minima within a tolerance " + str(tol))