Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions libensemble/specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pathlib import Path

import pydantic
from gest_api.vocs import VOCS
from pydantic import BaseModel, Field

from libensemble.alloc_funcs.give_sim_work_first import give_sim_work_first
Expand Down Expand Up @@ -87,6 +88,12 @@ class GenSpecs(BaseModel):
A pre-initialized generator object.
"""

vocs: VOCS | None = None
"""
A `VOCS <https://github.com/campa-consortium/gest-api?tab=readme-ov-file#vocs>`_ object.
Required if specifying a generator object that adheres to the GEST API.
"""

inputs: list[str] | None = Field(default=[], alias="in")
"""
list of **field names** out of the complete history to pass
Expand Down
70 changes: 70 additions & 0 deletions libensemble/tests/regression_tests/test_asktell_optimas_sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
"""
Runs libEnsemble with APOSMM with the NLopt local optimizer.

Execute via one of the following commands (e.g. 3 workers):
mpiexec -np 4 python test_persistent_aposmm_nlopt.py
python test_persistent_aposmm_nlopt.py --nworkers 3 --comms local
python test_persistent_aposmm_nlopt.py --nworkers 3 --comms tcp

When running with the above commands, the number of concurrent evaluations of
the objective function will be 2, as one of the three workers will be the
persistent generator.
"""

# Do not change these lines - they are parsed by run-tests.sh
# TESTSUITE_COMMS: local mpi
# TESTSUITE_NPROCS: 4
# TESTSUITE_EXTRA: true

import sys
from time import time

from gest_api.vocs import VOCS
from optimas.generators import GridSamplingGenerator

# Import libEnsemble items for this test
from libensemble import Ensemble
from libensemble.alloc_funcs.start_only_persistent import only_persistent_gens as alloc_f
from libensemble.sim_funcs.six_hump_camel import six_hump_camel as sim_f
from libensemble.specs import AllocSpecs, ExitCriteria, GenSpecs, SimSpecs

if __name__ == "__main__":

workflow = Ensemble(parse_args=True)

if workflow.is_manager:
start_time = time()

if workflow.nworkers < 2:
sys.exit("Cannot run with a persistent worker if only one worker -- aborting...")

n = 2

workflow.sim_specs = SimSpecs(sim_f=sim_f, inputs=["x"], outputs=[("f", float)])
workflow.alloc_specs = AllocSpecs(alloc_f=alloc_f)
workflow.exit_criteria = ExitCriteria(sim_max=35)

vocs = VOCS(
variables={"x0": [-3, 3], "x1": [-2, 2]},
objectives={"f": "MINIMIZE"},
)

# Generator from Optimas
gen = GridSamplingGenerator(vocs, n_steps=[5, 7])

workflow.gen_specs = GenSpecs(
persis_in=["x", "f"],
outputs=[("x", float, 2)],
generator=gen,
batch_size=5,
initial_batch_size=10,
)

workflow.libE_specs.gen_on_manager = True
workflow.add_random_streams()

H, _, _ = workflow.run()

if workflow.is_manager:
print("[Manager]: Time taken =", time() - start_time, flush=True)
print("[Manager]:", H[["x", "f"]])
5 changes: 4 additions & 1 deletion libensemble/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ def list_dicts_to_np(list_dicts: list, dtype: list = None, mapping: dict = {}) -
entry["sim_id"] = entry.pop("_id")

# first entry is used to determine dtype
first = list_dicts[0]
try:
first = list_dicts[0]
except IndexError:
raise IndexError("Generator returned empty list upon internal suggest.")

# build a presumptive dtype
new_dtype_names = _get_new_dtype_fields(first, mapping)
Expand Down
2 changes: 2 additions & 0 deletions libensemble/utils/pydantic_bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
check_mpi_runner_type,
check_provided_ufuncs,
check_set_gen_specs_from_variables,
check_set_gen_specs_from_vocs,
check_valid_comms_type,
check_valid_in,
check_valid_out,
Expand Down Expand Up @@ -78,6 +79,7 @@
"check_valid_out": check_valid_out,
"check_valid_in": check_valid_in,
"check_set_gen_specs_from_variables": check_set_gen_specs_from_variables,
"check_set_gen_specs_from_vocs": check_set_gen_specs_from_vocs,
"genf_set_in_out_from_attrs": genf_set_in_out_from_attrs,
},
)
Expand Down
5 changes: 5 additions & 0 deletions libensemble/utils/specs_checkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ def _check_set_gen_specs_from_variables(values):
return values


def _check_set_gen_specs_from_vocs(values):
if not len(scg(values, "persis_in")) and scg(values, "generator"):
print("asdlkhfgasdoijhf")


def _check_H0(values):
if scg(values, "H0").size > 0:
H0 = scg(values, "H0")
Expand Down
6 changes: 6 additions & 0 deletions libensemble/utils/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
_check_logical_cores,
_check_set_calc_dirs_on_input_dir,
_check_set_gen_specs_from_variables,
_check_set_gen_specs_from_vocs,
_check_set_workflow_dir,
)

Expand Down Expand Up @@ -161,6 +162,11 @@ def check_set_gen_specs_from_variables(self):
return _check_set_gen_specs_from_variables(self)


@model_validator(mode="after")
def check_set_gen_specs_from_vocs(self):
return _check_set_gen_specs_from_vocs(self)


@model_validator(mode="after")
def check_provided_ufuncs(self):
assert hasattr(self.sim_specs, "sim_f"), "Simulation function not provided to SimSpecs."
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,4 @@ extend-exclude = ["*.bib", "*.xml", "docs/nitpicky"]
disable_error_code = ["import-not-found", "import-untyped"]

[dependency-groups]
dev = ["pyenchant", "enchant>=0.0.1,<0.0.2", "flake8-modern-annotations>=1.6.0,<2", "flake8-type-checking>=3.0.0,<4", "wat>=0.6.0,<0.7"]
dev = ["pyenchant", "enchant>=0.0.1,<0.0.2", "flake8-modern-annotations>=1.6.0,<2", "flake8-type-checking>=3.0.0,<4", "wat>=0.6.0,<0.7", "optimas @ git+https://github.com/optimas-org/optimas.git@vocs"]