sampling
This module contains multiple generation functions for sampling a domain. All
use (and return) a random stream in persis_info
, given by the allocation
function.
- sampling.uniform_random_sample(_, persis_info, gen_specs)
Generates
gen_specs["user"]["gen_batch_size"]
points uniformly over the domain defined bygen_specs["user"]["ub"]
andgen_specs["user"]["lb"]
.See also
test_uniform_sampling.py # noqa
- sampling.uniform_random_sample_with_variable_resources(_, persis_info, gen_specs)
Generates
gen_specs["user"]["gen_batch_size"]
points uniformly over the domain defined bygen_specs["user"]["ub"]
andgen_specs["user"]["lb"]
.Also randomly requests a different number of resource sets to be used in each evaluation.
This generator is used to test/demonstrate setting of resource sets.
- #.. seealso::
#`test_uniform_sampling_with_variable_resources.py <https://github.com/Libensemble/libensemble/blob/develop/libensemble/tests/regression_tests/test_uniform_sampling_with_variable_resources.py>`_ # noqa
- sampling.uniform_random_sample_with_var_priorities_and_resources(H, persis_info, gen_specs)
Generates points uniformly over the domain defined by
gen_specs["user"]["ub"]
andgen_specs["user"]["lb"]
. Also, randomly requests a different priority and number of resource sets to be used in the evaluation of the generated points, after the initial batch.This generator is used to test/demonstrate setting of priorities and resource sets.
- sampling.uniform_random_sample_obj_components(H, persis_info, gen_specs)
Generates points uniformly over the domain defined by
gen_specs["user"]["ub"]
andgen_specs["user"]["lb"]
but requests eachobj_component
be evaluated separately.See also
- sampling.latin_hypercube_sample(_, persis_info, gen_specs)
Generates
gen_specs["user"]["gen_batch_size"]
points in a Latin hypercube sample over the domain defined bygen_specs["user"]["ub"]
andgen_specs["user"]["lb"]
.See also
test_1d_sampling.py # noqa
- sampling.uniform_random_sample_cancel(_, persis_info, gen_specs)
Similar to uniform_random_sample but with immediate cancellation of selected points for testing.
sampling.py
1"""
2This module contains multiple generation functions for sampling a domain. All
3use (and return) a random stream in ``persis_info``, given by the allocation
4function.
5"""
6import numpy as np
7
8__all__ = [
9 "uniform_random_sample",
10 "uniform_random_sample_with_variable_resources",
11 "uniform_random_sample_with_var_priorities_and_resources",
12 "uniform_random_sample_obj_components",
13 "latin_hypercube_sample",
14 "uniform_random_sample_cancel",
15]
16
17
18def uniform_random_sample(_, persis_info, gen_specs):
19 """
20 Generates ``gen_specs["user"]["gen_batch_size"]`` points uniformly over the domain
21 defined by ``gen_specs["user"]["ub"]`` and ``gen_specs["user"]["lb"]``.
22
23 .. seealso::
24 `test_uniform_sampling.py <https://github.com/Libensemble/libensemble/blob/develop/libensemble/tests/regression_tests/test_uniform_sampling.py>`_ # noqa
25 """
26 ub = gen_specs["user"]["ub"]
27 lb = gen_specs["user"]["lb"]
28
29 n = len(lb)
30 b = gen_specs["user"]["gen_batch_size"]
31
32 H_o = np.zeros(b, dtype=gen_specs["out"])
33
34 H_o["x"] = persis_info["rand_stream"].uniform(lb, ub, (b, n))
35
36 return H_o, persis_info
37
38
39def uniform_random_sample_with_variable_resources(_, persis_info, gen_specs):
40 """
41 Generates ``gen_specs["user"]["gen_batch_size"]`` points uniformly over the domain
42 defined by ``gen_specs["user"]["ub"]`` and ``gen_specs["user"]["lb"]``.
43
44 Also randomly requests a different number of resource sets to be used in each evaluation.
45
46 This generator is used to test/demonstrate setting of resource sets.
47
48 #.. seealso::
49 #`test_uniform_sampling_with_variable_resources.py <https://github.com/Libensemble/libensemble/blob/develop/libensemble/tests/regression_tests/test_uniform_sampling_with_variable_resources.py>`_ # noqa
50 """
51
52 ub = gen_specs["user"]["ub"]
53 lb = gen_specs["user"]["lb"]
54 max_rsets = gen_specs["user"]["max_resource_sets"]
55
56 n = len(lb)
57 b = gen_specs["user"]["gen_batch_size"]
58
59 H_o = np.zeros(b, dtype=gen_specs["out"])
60
61 H_o["x"] = persis_info["rand_stream"].uniform(lb, ub, (b, n))
62 H_o["resource_sets"] = persis_info["rand_stream"].integers(1, max_rsets + 1, b)
63
64 print(f'GEN: H rsets requested: {H_o["resource_sets"]}')
65
66 return H_o, persis_info
67
68
69def uniform_random_sample_with_var_priorities_and_resources(H, persis_info, gen_specs):
70 """
71 Generates points uniformly over the domain defined by ``gen_specs["user"]["ub"]`` and
72 ``gen_specs["user"]["lb"]``. Also, randomly requests a different priority and number of
73 resource sets to be used in the evaluation of the generated points, after the initial batch.
74
75 This generator is used to test/demonstrate setting of priorities and resource sets.
76
77 """
78 ub = gen_specs["user"]["ub"]
79 lb = gen_specs["user"]["lb"]
80 max_rsets = gen_specs["user"]["max_resource_sets"]
81
82 n = len(lb)
83
84 if len(H) == 0:
85 b = gen_specs["user"]["initial_batch_size"]
86
87 H_o = np.zeros(b, dtype=gen_specs["out"])
88 for i in range(0, b):
89 # x= i*np.ones(n)
90 x = persis_info["rand_stream"].uniform(lb, ub, (1, n))
91 H_o["x"][i] = x
92 H_o["resource_sets"][i] = 1
93 H_o["priority"] = 1
94
95 else:
96 H_o = np.zeros(1, dtype=gen_specs["out"])
97 # H_o["x"] = len(H)*np.ones(n) # Can use a simple count for testing.
98 H_o["x"] = persis_info["rand_stream"].uniform(lb, ub)
99 H_o["resource_sets"] = persis_info["rand_stream"].integers(1, max_rsets + 1)
100 H_o["priority"] = 10 * H_o["resource_sets"]
101 # print("Created sim for {} resource sets".format(H_o["resource_sets"]), flush=True)
102
103 return H_o, persis_info
104
105
106def uniform_random_sample_obj_components(H, persis_info, gen_specs):
107 """
108 Generates points uniformly over the domain defined by ``gen_specs["user"]["ub"]``
109 and ``gen_specs["user"]["lb"]`` but requests each ``obj_component`` be evaluated
110 separately.
111
112 .. seealso::
113 `test_uniform_sampling_one_residual_at_a_time.py <https://github.com/Libensemble/libensemble/blob/develop/libensemble/tests/regression_tests/test_uniform_sampling_one_residual_at_a_time.py>`_ # noqa
114 """
115 ub = gen_specs["user"]["ub"]
116 lb = gen_specs["user"]["lb"]
117
118 n = len(lb)
119 m = gen_specs["user"]["components"]
120 b = gen_specs["user"]["gen_batch_size"]
121
122 H_o = np.zeros(b * m, dtype=gen_specs["out"])
123 for i in range(0, b):
124 x = persis_info["rand_stream"].uniform(lb, ub, (1, n))
125 H_o["x"][i * m : (i + 1) * m, :] = np.tile(x, (m, 1))
126 H_o["priority"][i * m : (i + 1) * m] = persis_info["rand_stream"].uniform(0, 1, m)
127 H_o["obj_component"][i * m : (i + 1) * m] = np.arange(0, m)
128
129 H_o["pt_id"][i * m : (i + 1) * m] = len(H) // m + i
130
131 return H_o, persis_info
132
133
134def uniform_random_sample_cancel(_, persis_info, gen_specs):
135 """
136 Similar to uniform_random_sample but with immediate cancellation of
137 selected points for testing.
138
139 """
140 ub = gen_specs["user"]["ub"]
141 lb = gen_specs["user"]["lb"]
142
143 n = len(lb)
144 b = gen_specs["user"]["gen_batch_size"]
145
146 H_o = np.zeros(b, dtype=gen_specs["out"])
147 for i in range(b):
148 if i % 10 == 0:
149 H_o[i]["cancel_requested"] = True
150
151 H_o["x"] = persis_info["rand_stream"].uniform(lb, ub, (b, n))
152
153 return H_o, persis_info
154
155
156def latin_hypercube_sample(_, persis_info, gen_specs):
157 """
158 Generates ``gen_specs["user"]["gen_batch_size"]`` points in a Latin
159 hypercube sample over the domain defined by ``gen_specs["user"]["ub"]`` and
160 ``gen_specs["user"]["lb"]``.
161
162 .. seealso::
163 `test_1d_sampling.py <https://github.com/Libensemble/libensemble/blob/develop/libensemble/tests/regression_tests/test_1d_sampling.py>`_ # noqa
164 """
165
166 ub = gen_specs["user"]["ub"]
167 lb = gen_specs["user"]["lb"]
168
169 n = len(lb)
170 b = gen_specs["user"]["gen_batch_size"]
171
172 H_o = np.zeros(b, dtype=gen_specs["out"])
173
174 A = lhs_sample(n, b, persis_info["rand_stream"])
175
176 H_o["x"] = A * (ub - lb) + lb
177
178 return H_o, persis_info
179
180
181def lhs_sample(n, k, stream):
182 # Generate the intervals and random values
183 intervals = np.linspace(0, 1, k + 1)
184 rand_source = stream.uniform(0, 1, (k, n))
185 rand_pts = np.zeros((k, n))
186 sample = np.zeros((k, n))
187
188 # Add a point uniformly in each interval
189 a = intervals[:k]
190 b = intervals[1:]
191 for j in range(n):
192 rand_pts[:, j] = rand_source[:, j] * (b - a) + a
193
194 # Randomly perturb
195 for j in range(n):
196 sample[:, j] = rand_pts[stream.permutation(k), j]
197
198 return sample
persistent_sampling
Persistent generator providing points using sampling
- persistent_sampling.persistent_uniform(_, persis_info, gen_specs, libE_info)
This generation function always enters into persistent mode and returns
gen_specs["initial_batch_size"]
uniformly sampled points the first time it is called. Afterwards, it returns the number of points given. This can be used in either a batch or asynchronous mode by adjusting the allocation function.
- persistent_sampling.persistent_request_shutdown(_, persis_info, gen_specs, libE_info)
This generation function is similar in structure to persistent_uniform, but uses a count to test exiting on a threshold value. This principle can be used with a supporting allocation function (e.g. start_only_persistent) to shutdown an ensemble when a condition is met.
- persistent_sampling.uniform_nonblocking(_, persis_info, gen_specs, libE_info)
This generation function is designed to test non-blocking receives.
See also
- persistent_sampling.batched_history_matching(_, persis_info, gen_specs, libE_info)
Given - sim_f with an input of x with len(x)=n - b, the batch size of points to generate - q<b, the number of best samples to use in the following iteration
Pseudocode: Let (mu, Sigma) denote a mean and covariance matrix initialized to the origin and the identity, respectively.
While true (batch synchronous for now):
Draw b samples x_1, … , x_b from MVN( mu, Sigma) Evaluate f(x_1), … , f(x_b) and determine the set of q x_i whose f(x_i) values are smallest (breaking ties lexicographically) Update (mu, Sigma) based on the sample mean and sample covariance of these q x values.
See also
- persistent_sampling.persistent_uniform_with_cancellations(_, persis_info, gen_specs, libE_info)
persistent_sampling.py
1"""Persistent generator providing points using sampling"""
2
3import numpy as np
4
5from libensemble.message_numbers import EVAL_GEN_TAG, FINISHED_PERSISTENT_GEN_TAG, PERSIS_STOP, STOP_TAG
6from libensemble.tools.persistent_support import PersistentSupport
7
8__all__ = [
9 "persistent_uniform",
10 "persistent_request_shutdown",
11 "uniform_nonblocking",
12 "batched_history_matching",
13 "persistent_uniform_with_cancellations",
14]
15
16
17def _get_user_params(user_specs):
18 """Extract user params"""
19 b = user_specs["initial_batch_size"]
20 ub = user_specs["ub"]
21 lb = user_specs["lb"]
22 n = len(lb) # dimension
23 return b, n, lb, ub
24
25
26def persistent_uniform(_, persis_info, gen_specs, libE_info):
27 """
28 This generation function always enters into persistent mode and returns
29 ``gen_specs["initial_batch_size"]`` uniformly sampled points the first time it
30 is called. Afterwards, it returns the number of points given. This can be
31 used in either a batch or asynchronous mode by adjusting the allocation
32 function.
33
34 .. seealso::
35 `test_persistent_uniform_sampling.py <https://github.com/Libensemble/libensemble/blob/develop/libensemble/tests/functionality_tests/test_persistent_uniform_sampling.py>`_
36 `test_persistent_sampling_async.py <https://github.com/Libensemble/libensemble/blob/develop/libensemble/tests/functionality_tests/test_persistent_sampling_async.py>`_
37 """ # noqa
38
39 b, n, lb, ub = _get_user_params(gen_specs["user"])
40 ps = PersistentSupport(libE_info, EVAL_GEN_TAG)
41
42 # Send batches until manager sends stop tag
43 tag = None
44 while tag not in [STOP_TAG, PERSIS_STOP]:
45 H_o = np.zeros(b, dtype=gen_specs["out"])
46 H_o["x"] = persis_info["rand_stream"].uniform(lb, ub, (b, n))
47 tag, Work, calc_in = ps.send_recv(H_o)
48 if hasattr(calc_in, "__len__"):
49 b = len(calc_in)
50
51 H_o = None
52 if gen_specs["user"].get("replace_final_fields", 0):
53 # This is only to test libE ability to accept History after a
54 # PERSIS_STOP. This history is returned in Work.
55 H_o = Work
56 H_o["x"] = -1.23
57
58 return H_o, persis_info, FINISHED_PERSISTENT_GEN_TAG
59
60
61def persistent_request_shutdown(_, persis_info, gen_specs, libE_info):
62 """
63 This generation function is similar in structure to persistent_uniform,
64 but uses a count to test exiting on a threshold value. This principle can
65 be used with a supporting allocation function (e.g. start_only_persistent)
66 to shutdown an ensemble when a condition is met.
67
68 .. seealso::
69 `test_persistent_uniform_gen_decides_stop.py <https://github.com/Libensemble/libensemble/blob/develop/libensemble/tests/functionality_tests/test_persistent_uniform_gen_decides_stop.py>`_
70 """ # noqa
71 b, n, lb, ub = _get_user_params(gen_specs["user"])
72 shutdown_limit = gen_specs["user"]["shutdown_limit"]
73 f_count = 0
74 ps = PersistentSupport(libE_info, EVAL_GEN_TAG)
75
76 # Send batches until manager sends stop tag
77 tag = None
78 while tag not in [STOP_TAG, PERSIS_STOP]:
79 H_o = np.zeros(b, dtype=gen_specs["out"])
80 H_o["x"] = persis_info["rand_stream"].uniform(lb, ub, (b, n))
81 tag, Work, calc_in = ps.send_recv(H_o)
82 if hasattr(calc_in, "__len__"):
83 b = len(calc_in)
84 f_count += b
85 if f_count >= shutdown_limit:
86 print("Reached threshold.", f_count, flush=True)
87 break # End the persistent gen
88
89 return H_o, persis_info, FINISHED_PERSISTENT_GEN_TAG
90
91
92def uniform_nonblocking(_, persis_info, gen_specs, libE_info):
93 """
94 This generation function is designed to test non-blocking receives.
95
96 .. seealso::
97 `test_persistent_uniform_sampling.py <https://github.com/Libensemble/libensemble/blob/develop/libensemble/tests/functionality_tests/test_persistent_uniform_sampling.py>`_
98 """ # noqa
99 b, n, lb, ub = _get_user_params(gen_specs["user"])
100 ps = PersistentSupport(libE_info, EVAL_GEN_TAG)
101
102 # Send batches until manager sends stop tag
103 tag = None
104 while tag not in [STOP_TAG, PERSIS_STOP]:
105 H_o = np.zeros(b, dtype=gen_specs["out"])
106 H_o["x"] = persis_info["rand_stream"].uniform(lb, ub, (b, n))
107 ps.send(H_o)
108
109 received = False
110 spin_count = 0
111 while not received:
112 tag, Work, calc_in = ps.recv(blocking=False)
113 if tag is not None:
114 received = True
115 else:
116 spin_count += 1
117
118 persis_info["spin_count"] = spin_count
119
120 if hasattr(calc_in, "__len__"):
121 b = len(calc_in)
122
123 return H_o, persis_info, FINISHED_PERSISTENT_GEN_TAG
124
125
126def batched_history_matching(_, persis_info, gen_specs, libE_info):
127 """
128 Given
129 - sim_f with an input of x with len(x)=n
130 - b, the batch size of points to generate
131 - q<b, the number of best samples to use in the following iteration
132
133 Pseudocode:
134 Let (mu, Sigma) denote a mean and covariance matrix initialized to the
135 origin and the identity, respectively.
136
137 While true (batch synchronous for now):
138
139 Draw b samples x_1, ... , x_b from MVN( mu, Sigma)
140 Evaluate f(x_1), ... , f(x_b) and determine the set of q x_i whose f(x_i) values are smallest (breaking ties lexicographically)
141 Update (mu, Sigma) based on the sample mean and sample covariance of these q x values.
142
143 .. seealso::
144 `test_persistent_uniform_sampling.py <https://github.com/Libensemble/libensemble/blob/develop/libensemble/tests/functionality_tests/test_persistent_uniform_sampling.py>`_
145 """ # noqa
146 lb = gen_specs["user"]["lb"]
147
148 n = len(lb)
149 b = gen_specs["user"]["initial_batch_size"]
150 q = gen_specs["user"]["num_best_vals"]
151 ps = PersistentSupport(libE_info, EVAL_GEN_TAG)
152
153 mu = np.zeros(n)
154 Sigma = np.eye(n)
155 tag = None
156
157 while tag not in [STOP_TAG, PERSIS_STOP]:
158 H_o = np.zeros(b, dtype=gen_specs["out"])
159 H_o["x"] = persis_info["rand_stream"].multivariate_normal(mu, Sigma, b)
160
161 # Send data and get next assignment
162 tag, Work, calc_in = ps.send_recv(H_o)
163 if calc_in is not None:
164 all_inds = np.argsort(calc_in["f"])
165 best_inds = all_inds[:q]
166 mu = np.mean(H_o["x"][best_inds], axis=0)
167 Sigma = np.cov(H_o["x"][best_inds].T)
168
169 return H_o, persis_info, FINISHED_PERSISTENT_GEN_TAG
170
171
172def persistent_uniform_with_cancellations(_, persis_info, gen_specs, libE_info):
173 ub = gen_specs["user"]["ub"]
174 lb = gen_specs["user"]["lb"]
175 n = len(lb)
176 b = gen_specs["user"]["initial_batch_size"]
177
178 # Start cancelling points from half initial batch onward
179 cancel_from = b // 2 # Should get at least this many points back
180
181 ps = PersistentSupport(libE_info, EVAL_GEN_TAG)
182
183 # Send batches until manager sends stop tag
184 tag = None
185 while tag not in [STOP_TAG, PERSIS_STOP]:
186 H_o = np.zeros(b, dtype=gen_specs["out"])
187 H_o["x"] = persis_info["rand_stream"].uniform(lb, ub, (b, n))
188 tag, Work, calc_in = ps.send_recv(H_o)
189
190 if hasattr(calc_in, "__len__"):
191 b = len(calc_in)
192
193 # Cancel as many points as got back
194 cancel_ids = list(range(cancel_from, cancel_from + b))
195 cancel_from += b
196 ps.request_cancel_sim_ids(cancel_ids)
197
198 return H_o, persis_info, FINISHED_PERSISTENT_GEN_TAG
persistent_sampling_var_resources
Persistent random sampling using various methods of dynamic resource assignment
Each function generates points uniformly over the domain defined by gen_specs["user"]["ub"]
and gen_specs["user"]["lb"]
.
- persistent_sampling_var_resources.uniform_sample(_, persis_info, gen_specs, libE_info)
Randomly requests a different number of resource sets to be used in the evaluation of the generated points.
- persistent_sampling_var_resources.uniform_sample_with_procs_gpus(_, persis_info, gen_specs, libE_info)
Randomly requests a different number of processors and gpus to be used in the evaluation of the generated points.
See also
- persistent_sampling_var_resources.uniform_sample_with_var_priorities(_, persis_info, gen_specs, libE_info)
Initial batch has matching priorities, after which a different number of resource sets and priorities are requested for each point.
- persistent_sampling_var_resources.uniform_sample_diff_simulations(_, persis_info, gen_specs, libE_info)
Randomly requests a different number of processors for each simulation. One simulation type also uses GPUs.