Skip to content

Commit

Permalink
Merge pull request xopt-org#196 from ChristopherMayes/fix_asynchronou…
Browse files Browse the repository at this point in the history
…s_inputs

Fix constants not propagating in AsynchronousXopt
  • Loading branch information
ChristopherMayes authored Feb 8, 2024
2 parents 6cb0704 + af32d90 commit 27fc0ce
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 7 deletions.
2 changes: 1 addition & 1 deletion tests/test_vocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def test_convert_dataframe_to_inputs(self):
vocs.convert_dataframe_to_inputs(test_data)

res = vocs.convert_dataframe_to_inputs(test_data[vocs.variable_names])
assert "cnt1" in res
assert "constant1" in res

def test_validate_input_data(self):
test_vocs = deepcopy(TEST_VOCS_BASE)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_xopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def test_evaluate(self):
xopt = Xopt(generator=generator, evaluator=evaluator, vocs=test_vocs)

test_vocs.variables = {"x2": [0, 1]}
test_vocs.constants = {"x1": 2.0}
test_vocs.constants["x1"] = 2.0

out = xopt.evaluate({"x2": 0.2})
assert isinstance(out, dict)
Expand Down Expand Up @@ -259,7 +259,7 @@ def test_str_method(self):
val = str(xopt)
assert "Data size: 2" in val
assert (
"vocs:\n constants:\n cnt1: 1.0\n constraints:\n c1:\n - "
"vocs:\n constants:\n constant1: 1.0\n constraints:\n c1:\n - "
"GREATER_THAN\n - 0.5\n objectives:\n" in val
)

Expand Down
4 changes: 4 additions & 0 deletions xopt/asynchronous.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ def prepare_input_data(self, input_data: pd.DataFrame):
"""
input_data = pd.DataFrame(input_data, copy=True) # copy for reindexing

# add constants to input data
for name, value in self.vocs.constants.items():
input_data[name] = value

# Reindex input dataframe
input_data.index = np.arange(
self._ix_last + 1, self._ix_last + 1 + len(input_data)
Expand Down
9 changes: 7 additions & 2 deletions xopt/generators/bayesian/visualize.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,15 @@ def visualize_generator_model(
x_axis, predictions["acq"][0], "C0--", label="Base Acq. Function"
)
ax[len(output_names)].plot(
x_axis, predictions["acq"][1], "C0-", label="Constrained Acq. Function"
x_axis,
predictions["acq"][1],
"C0-",
label="Constrained Acq. Function",
)
ax[len(output_names)].legend()
ax[len(output_names)].set_ylabel(r"$\alpha\,$[{}]".format(vocs.output_names[0]))
ax[len(output_names)].set_ylabel(
r"$\alpha\,$[{}]".format(vocs.output_names[0])
)
# feasibility
if show_feasibility:
ax[-1].plot(x_axis, predictions["feasibility"], "C0-")
Expand Down
9 changes: 7 additions & 2 deletions xopt/resources/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ def xtest_callable(input_dict: dict, a=0) -> dict:
x1 = input_dict["x1"]
x2 = input_dict["x2"]

assert "constant1" in input_dict

y1 = x2
c1 = x1
return {"y1": y1, "c1": c1}
Expand All @@ -19,22 +21,23 @@ def xtest_callable(input_dict: dict, a=0) -> dict:
"variables": {"x1": [0, 1.0], "x2": [0, 10.0]},
"objectives": {"y1": "MINIMIZE"},
"constraints": {"c1": ["GREATER_THAN", 0.5]},
"constants": {"cnt1": 1.0},
"constants": {"constant1": 1.0},
}
)

cnames = (
list(TEST_VOCS_BASE.variables.keys())
+ list(TEST_VOCS_BASE.objectives.keys())
+ list(TEST_VOCS_BASE.constraints.keys())
+ list(TEST_VOCS_BASE.constants.keys())
)

# TODO: figure out why having the range from 0->1 or 0->10 breaks objective test data
# test
test_init_data = {
"x1": np.linspace(0.01, 1.0, 10),
"x2": np.linspace(0.01, 1.0, 10) * 10.0,
"cnt1": 1.0,
"constant1": 1.0,
}
test_init_data.update(xtest_callable(test_init_data))

Expand All @@ -57,4 +60,6 @@ def xtest_callable(input_dict: dict, a=0) -> dict:
constraints:
c1: [GREATER_THAN, 0]
c2: ['LESS_THAN', 0.5]
constants:
constant1: 1
"""

0 comments on commit 27fc0ce

Please sign in to comment.