|
| 1 | +""" |
| 2 | +Tests libEnsemble with Optimas Multitask Ax Generator |
| 3 | +
|
| 4 | +Runs an initial ensemble, followed by another using the first as an H0. |
| 5 | +
|
| 6 | +*****currently fixing nworkers to batch_size***** |
| 7 | +
|
| 8 | +Execute via one of the following commands (e.g. 4 workers): |
| 9 | + mpiexec -np 5 python test_optimas_ax_multitask.py |
| 10 | + python test_optimas_ax_multitask.py -n 4 |
| 11 | +
|
| 12 | +When running with the above commands, the number of concurrent evaluations of |
| 13 | +the objective function will be 4 as the generator is on the manager. |
| 14 | +
|
| 15 | +Issues: In some cases, the generator fails to produce points. This is |
| 16 | +intermittent and can be seen by the message "alloc_f did not return any work". |
| 17 | +This needs to be resolved in the generator by generating extra points |
| 18 | +as needed (exluding from until then). |
| 19 | +""" |
| 20 | + |
| 21 | +# Do not change these lines - they are parsed by run-tests.sh |
| 22 | +# TESTSUITE_COMMS: local |
| 23 | +# TESTSUITE_NPROCS: 4 |
| 24 | +# TESTSUITE_EXTRA: true |
| 25 | +# TESTSUITE_EXCLUDE: true |
| 26 | + |
| 27 | +import numpy as np |
| 28 | +from gest_api.vocs import VOCS |
| 29 | + |
| 30 | +from optimas.core import Task |
| 31 | +from optimas.generators import AxMultitaskGenerator |
| 32 | + |
| 33 | +from libensemble import Ensemble |
| 34 | +from libensemble.alloc_funcs.start_only_persistent import only_persistent_gens as alloc_f |
| 35 | +from libensemble.specs import AllocSpecs, ExitCriteria, GenSpecs, LibeSpecs, SimSpecs |
| 36 | + |
| 37 | + |
| 38 | +def eval_func_multitask(input_params): |
| 39 | + """Evaluation function for task1 or task2 in multitask test""" |
| 40 | + print(f'input_params: {input_params}') |
| 41 | + x0 = input_params["x0"] |
| 42 | + x1 = input_params["x1"] |
| 43 | + trial_type = input_params["trial_type"] |
| 44 | + |
| 45 | + if trial_type == "task_1": |
| 46 | + result = -(x0 + 10 * np.cos(x0)) * (x1 + 5 * np.cos(x1)) |
| 47 | + else: |
| 48 | + result = -0.5 * (x0 + 10 * np.cos(x0)) * (x1 + 5 * np.cos(x1)) |
| 49 | + |
| 50 | + output_params = {"f": result} |
| 51 | + return output_params |
| 52 | + |
| 53 | + |
| 54 | +# Main block is necessary only when using local comms with spawn start method (default on macOS and Windows). |
| 55 | +if __name__ == "__main__": |
| 56 | + |
| 57 | + n = 2 |
| 58 | + batch_size = 2 |
| 59 | + |
| 60 | + libE_specs = LibeSpecs(gen_on_manager=True, nworkers=batch_size) |
| 61 | + |
| 62 | + vocs = VOCS( |
| 63 | + variables={ |
| 64 | + "x0": [-50.0, 5.0], |
| 65 | + "x1": [-5.0, 15.0], |
| 66 | + "trial_type": {"task_1", "task_2"}, |
| 67 | + }, |
| 68 | + objectives={"f": "MAXIMIZE"}, |
| 69 | + ) |
| 70 | + |
| 71 | + sim_specs = SimSpecs( |
| 72 | + simulator=eval_func_multitask, |
| 73 | + vocs=vocs, |
| 74 | + ) |
| 75 | + |
| 76 | + alloc_specs = AllocSpecs(alloc_f=alloc_f) |
| 77 | + exit_criteria = ExitCriteria(sim_max=15) |
| 78 | + |
| 79 | + H0 = None # or np.load("multitask_first_pass.npy") |
| 80 | + for run_num in range(2): |
| 81 | + print(f"\nRun number: {run_num}") |
| 82 | + task1 = Task("task_1", n_init=2, n_opt=1) |
| 83 | + task2 = Task("task_2", n_init=5, n_opt=3) |
| 84 | + gen = AxMultitaskGenerator(vocs=vocs, hifi_task=task1, lofi_task=task2) |
| 85 | + |
| 86 | + gen_specs = GenSpecs( |
| 87 | + generator=gen, |
| 88 | + batch_size=batch_size, |
| 89 | + vocs=vocs, |
| 90 | + ) |
| 91 | + |
| 92 | + workflow = Ensemble( |
| 93 | + libE_specs=libE_specs, |
| 94 | + sim_specs=sim_specs, |
| 95 | + alloc_specs=alloc_specs, |
| 96 | + gen_specs=gen_specs, |
| 97 | + exit_criteria=exit_criteria, |
| 98 | + H0=H0, |
| 99 | + ) |
| 100 | + |
| 101 | + H, _, _ = workflow.run() |
| 102 | + |
| 103 | + if run_num == 0: |
| 104 | + H0 = H |
| 105 | + workflow.save_output("multitask_first_pass", append_attrs=False) # Allows restart only run |
| 106 | + |
| 107 | + if workflow.is_manager: |
| 108 | + if run_num == 1: |
| 109 | + workflow.save_output("multitask_with_H0") |
| 110 | + print(f"Second run completed: {len(H)} simulations") |
0 commit comments