Running an Ensemble¶
- class libensemble.ensemble.Ensemble¶
The primary object for a libEnsemble workflow. Parses and validates settings and maintains output.
Example
1from gest_api.vocs import VOCS 2 3from libensemble import Ensemble 4from libensemble.gen_classes.sampling import UniformSample 5from libensemble.sim_funcs.simple_sim import norm_eval 6from libensemble.specs import ExitCriteria, GenSpecs, SimSpecs 7 8sampling = Ensemble(parse_args=True) 9 10sampling.sim_specs = SimSpecs( 11 sim_f=norm_eval, 12 inputs=["x"], 13 outputs=[("f", float)], 14) 15 16vocs = VOCS( 17 variables={"x": [-3, 3]}, 18 objectives={"f": "EXPLORE"}, 19) 20 21generator = UniformSample(vocs=vocs) 22 23sampling.gen_specs = GenSpecs( 24 generator=generator, 25 batch_size=50, 26) 27 28sampling.exit_criteria = ExitCriteria(sim_max=100) 29 30if __name__ == "__main__": 31 sampling.run() 32 sampling.save_output(__file__)
Configure by:
Option 1: Providing parameters on instantiation
1from libensemble import Ensemble 2from libensemble.specs import SimSpecs 3from my_simulator import sim_find_energy 4 5sim_specs = SimSpecs( 6 sim_f=sim_find_energy, 7 inputs=["x"], 8 outputs=[("y", float)], 9) 10 11experiment = Ensemble(sim_specs=sim_specs)
Option 2: Assigning parameters to an instance
1from libensemble import Ensemble 2from libensemble.specs import SimSpecs 3from my_simulator import sim_find_energy 4 5sim_specs = SimSpecs( 6 sim_f=sim_find_energy, 7 inputs=["x"], 8 outputs=[("y", float)], 9) 10 11experiment = Ensemble() 12experiment.sim_specs = sim_specs
- Parameters:
sim_specs (class:SimSpecs<libensemble.specs.SimSpecs>) – Specifications for the simulator function.
gen_specs (class:GenSpecs<libensemble.specs.GenSpecs>, Optional) – Specifications for the generator.
exit_criteria (class:ExitCriteria<libensemble.specs.ExitCriteria>) – Tell libEnsemble when to stop a run.
libE_specs (class:LibeSpecs<libensemble.specs.LibeSpecs>, Optional) – Specifications for libEnsemble.
alloc_specs (class:AllocSpecs<libensemble.specs.AllocSpecs>, Optional) – Specifications for the allocation function.
persis_info (
dict, Optional) – Persistent information to be passed between user function instances (example)executor (
Executor, Optional) – libEnsemble Executor instance for use within simulator functions or generators.H0 (NumPy structured array, Optional) – A libEnsemble history to be prepended to this run’s history (example).
parse_args (bool, Optional) – Read
nworkers,comms, and other arguments from the command-line. For MPI, calculatenworkersand set theis_managerBoolean attribute on MPI rank 0. See theparse_argsdocs for more information.
- ready()¶
Verify that all necessary data has been provided before calling
run().Performs a pre-flight check on the ensemble configuration, covering:
A simulation callable (
sim_forsimulator) is set onsim_specs.At least one exit condition is configured on
exit_criteria.Workers are available (
nworkers > 0for local/threads/tcp comms, or MPI comms is set, which infers workers from the MPI communicator).If both
gen_specsandsim_specsuse the classic field-name interface, the generator output field names are a superset of the simulator input field names.
- Returns:
A 2-tuple of
(is_ready, issues).is_readyisTruewhen all checks pass.issuesis a list of human-readable strings describing each problem found; it is empty whenis_readyisTrue.- Return type:
tuple[bool, list[str]]
Example
ok, issues = sampling.ready() if not ok: for issue in issues: print(f" - {issue}")
- run()¶
Initializes libEnsemble.
MPI/comms Notes
Manager–worker intercommunications are parsed from the
commskey of libE_specs. An MPI runtime is assumed by default if-n Nwasn’t specified on the command-line orcomms="local"inlibE_specs.If a MPI communicator was provided in
libE_specs, then each.run()call will initiate on a duplicate of that communicator. Otherwise, a duplicate ofCOMM_WORLDwill be used.- Returns:
H (NumPy structured array) – History array storing rows for each point. (example)
persis_info (
dict) – Final state of persistent information (example)exit_flag (
int) – Flag containing final task status0 = No errors 1 = Exception occurred 2 = Manager timed out and ended simulation 3 = Current process is not in libEnsemble MPI communicator
- Return type:
tuple[ndarray[tuple[Any, …], dtype[_ScalarT]], dict, int]
- save_output(basename, append_attrs=True)¶
Writes out History array and persis_info to files. If using a
workflow_dir_pathinlibE_specs, will place with specified filename in that directory.- Parameters:
Format (
<basename>_results_History_length=<length>_evals=<Completed evals>_ranks=<nworkers>)basename (str)
append_attrs=False (set)
Format
basename
append_attrs (bool)