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.alloc_funcs.give_sim_work_first import give_sim_work_first
23from libensemble.gen_funcs.sampling import uniform_random_sample
24
25# Import libEnsemble items for this test
26from libensemble.libE import libE
27from libensemble.sim_funcs.mock_sim import mock_sim
28from libensemble.sim_funcs.six_hump_camel import six_hump_camel
29from libensemble.tests.regression_tests.common import read_generated_file
30from libensemble.tests.regression_tests.support import six_hump_camel_minima as minima
31from libensemble.tools import parse_args
32
33# Main block is necessary only when using local comms with spawn start method (default on macOS and Windows).
34if __name__ == "__main__":
35 nworkers, is_manager, libE_specs, _ = parse_args()
36 libE_specs["save_every_k_sims"] = 400
37 libE_specs["save_every_k_gens"] = 300
38 libE_specs["save_H_with_date"] = True
39 libE_specs["H_file_prefix"] = "TESTING"
40
41 sim_specs = {
42 "sim_f": six_hump_camel, # Function whose output is being minimized
43 "in": ["x"], # Keys to be given to sim_f
44 "out": [("f", float)], # Name of the outputs from sim_f
45 }
46 # end_sim_specs_rst_tag
47
48 gen_specs = {
49 "gen_f": uniform_random_sample, # Function generating sim_f input
50 "out": [("x", float, (2,))], # Tell libE gen_f output, type, size
51 "batch_size": 500,
52 "user": {
53 "lb": np.array([-3, -2]), # Used by this specific gen_f
54 "ub": np.array([3, 2]), # Used by this specific gen_f
55 },
56 }
57 # end_gen_specs_rst_tag
58
59 exit_criteria = {"gen_max": 501, "wallclock_max": 300}
60
61 alloc_specs = {
62 "alloc_f": give_sim_work_first,
63 }
64
65 for run in range(2):
66 if run == 1:
67 # Test running a mock sim using previous history file
68 sim_specs["sim_f"] = mock_sim
69 hfile = read_generated_file("TESTING_*_after_gen_1000.npy")
70 sim_specs["user"] = {"history_file": hfile}
71
72 # Perform the run
73 H, _, flag = libE(sim_specs, gen_specs, exit_criteria, alloc_specs=alloc_specs, libE_specs=libE_specs)
74
75 if is_manager:
76 assert flag == 0
77
78 tol = 0.1
79 for m in minima:
80 assert np.min(np.sum((H["x"] - m) ** 2, 1)) < tol
81
82 npy_files = [i for i in os.listdir(os.path.dirname(__file__)) if i.endswith(".npy")]
83 date = str(datetime.datetime.today()).split(" ")[0]
84 assert any([i.startswith("TESTING_") for i in npy_files])
85 assert any([date in i for i in npy_files])
86
87 print("\nlibEnsemble found the 6 minima within a tolerance " + str(tol))