Skip to content

Commit

Permalink
update tests for design_space, run notebook for next week smt2 paper …
Browse files Browse the repository at this point in the history
…release and update setup.py
  • Loading branch information
Paul-Saves committed Nov 30, 2023
1 parent d47f6f3 commit c6d3b0b
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 20 deletions.
13 changes: 7 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""
Author: Dr. John T. Hwang <[email protected]>
Dr. Mohamed A. Bouhlel <[email protected]>
Author: John T. Hwang <[email protected]>
Mohamed A. Bouhlel <[email protected]>
Remi Lafage <[email protected]>
Lucas Alber <[email protected]>
Paul Saves <[email protected]>
This package is distributed under New BSD license.
"""
from setuptools import setup, Extension
Expand Down Expand Up @@ -90,8 +91,8 @@
version=__version__,
description="The Surrogate Modeling Toolbox (SMT)",
long_description=LONG_DESCRIPTION,
author="Mohamed Amine Bouhlel et al.",
author_email="[email protected]",
author="Remi Lafage et al.",
author_email="[email protected]",
license="BSD-3",
classifiers=[_f for _f in CLASSIFIERS.split("\n") if _f],
packages=[
Expand All @@ -116,7 +117,7 @@
"ConfigSpace~=0.6.1",
],
},
python_requires=">=3.7",
python_requires=">=3.8",
zip_safe=False,
ext_modules=ext,
url="https://github.com/SMTorg/smt", # use the URL to the github repo
Expand Down
22 changes: 20 additions & 2 deletions smt/utils/test/test_design_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def test_design_space(self):
)
self.assertEqual(len(ds.design_variables), 4)
if HAS_CONFIG_SPACE:
self.assertEqual(len(ds._cs.get_hyperparameters()), 4)
self.assertEqual(len(list(ds._cs.values())), 4)
self.assertTrue(np.all(~ds.is_conditionally_acting))
if HAS_CONFIG_SPACE:
x, is_acting = ds.sample_valid_x(3, random_state=42)
Expand Down Expand Up @@ -539,6 +539,24 @@ def _is_conditionally_acting(self) -> np.ndarray:
RuntimeError, lambda: ds.sample_valid_x(10, random_state=42)
)

def test_check_conditionally_acting_2(self):
for simulate_no_cs in [True, False]:
with simulate_no_config_space(simulate_no_cs):
ds = DesignSpace(
[
CategoricalVariable(["A", "B", "C"]), # x0
CategoricalVariable(["E", "F"]), # x1
IntegerVariable(0, 1), # x2
FloatVariable(0, 1), # x3
],
seed=42,
)
ds.declare_decreed_var(
decreed_var=0, meta_var=1, meta_value="E"
) # Activate x3 if x0 == A

ds.sample_valid_x(10, random_state=42)

@unittest.skipIf(
not HAS_CONFIG_SPACE, "Hierarchy ConfigSpace dependency not installed"
)
Expand All @@ -549,7 +567,7 @@ def test_restrictive_value_constraint(self):
IntegerVariable(0, 2),
]
)
assert ds._cs.get_hyperparameters()[0].default_value == 1
assert list(ds._cs.values())[0].default_value == 1

ds.add_value_constraint(var1=0, value1=1, var2=0, value2=1)
ds.sample_valid_x(100, random_state=42)
Expand Down
117 changes: 105 additions & 12 deletions tutorial/NotebookRunTestCases_Paper_SMT_v2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "fff43855",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -125,10 +125,19 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"id": "ccc49c3b",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Pred_RMSE on the training points 1.0052501496058814e-12\n",
"Var_RMSE on the training points 1.6419278095185747e-12\n"
]
}
],
"source": [
"def f_neu(x1, x2, x3):\n",
" return 2 * x1 + x2 - 0.5 * x3\n",
Expand Down Expand Up @@ -202,10 +211,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "9fc88694",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1.3098127 0.69714044 2.19224095] [1.3098127 0.69714044 2.19224095]\n",
"The outputs must be similar (inactive variables have no effect) 0.0\n",
"[-1.39420965] [10.45248007]\n",
"The outputs must be different (active variables have effect) 11.846689716902546\n"
]
}
],
"source": [
"#To check of some inactive variables have no effect on the output\n",
"xv1=np.array([\n",
Expand Down Expand Up @@ -234,10 +254,22 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"id": "c122b735",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Activation function ['ISRLU', 'SELU', 'ReLU ']\n",
"Batch size [3, 4, 5]\n",
"Number of hidden neurons -1st layer [2, 2, 2]\n",
"Number of hidden neurons -2nd layer [0, 1, 1]\n",
"Number of hidden neurons -3rd layer [0, 0, 5]\n"
]
}
],
"source": [
"#To have access to the \"real\" values of the input space variables\n",
"x2_decoded = design_space.decode_values(xv1, i_dv=2)\n",
Expand Down Expand Up @@ -295,10 +327,29 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"id": "1b3c3727",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[33mWARNING: Ignoring invalid distribution -umpy (/stck/psaves/miniconda3/lib/python3.8/site-packages)\u001b[0m\u001b[33m\n",
"\u001b[0mName: numba\n",
"Version: 0.58.1\n",
"Summary: compiling Python code using LLVM\n",
"Home-page: https://numba.pydata.org\n",
"Author: \n",
"Author-email: \n",
"License: BSD\n",
"Location: /stck/psaves/miniconda3/lib/python3.8/site-packages\n",
"Requires: importlib-metadata, llvmlite, numpy\n",
"Required-by: \n",
"Numba used or not in your environment= %USE_NUMBA_JIT%\n"
]
}
],
"source": [
"#to check if numba is available\n",
"!pip show numba\n",
Expand All @@ -308,15 +359,57 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"id": "b2d505b5",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Design space:\n",
"x0: Cat [0, 1, 2, 3]\n",
"x1: Ord [0, 1]\n",
"x2: Float (0, 100)\n",
"x3: Float (0, 100)\n",
"x4: Float (0, 100)\n",
"x5: Float (0, 100)\n",
"x6: Float (0, 100)\n",
"x7: Int (0, 2)\n",
"x8: Int (0, 2)\n",
"x9: Int (0, 2)\n",
"x10: Int (0, 2)\n",
"for 15 points time is = 0.0018038749694824219\n"
]
},
{
"ename": "IndexError",
"evalue": "index 11 is out of bounds for axis 0 with size 11",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mInactiveHyperparameterSetError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m~/smt/smt/utils/design_space.py\u001b[0m in \u001b[0;36m_get_correct_config\u001b[0;34m(self, vector)\u001b[0m\n\u001b[1;32m 968\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 969\u001b[0;31m \u001b[0mconfig\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mis_valid_configuration\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 970\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mconfig\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/miniconda3/lib/python3.8/site-packages/ConfigSpace/configuration.py\u001b[0m in \u001b[0;36mis_valid_configuration\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 128\u001b[0m \"\"\"\n\u001b[0;32m--> 129\u001b[0;31m c_util.check_configuration(\n\u001b[0m\u001b[1;32m 130\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mconfig_space\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/miniconda3/lib/python3.8/site-packages/ConfigSpace/c_util.pyx\u001b[0m in \u001b[0;36mConfigSpace.c_util.check_configuration\u001b[0;34m()\u001b[0m\n",
"\u001b[0;32m~/miniconda3/lib/python3.8/site-packages/ConfigSpace/c_util.pyx\u001b[0m in \u001b[0;36mConfigSpace.c_util.check_configuration\u001b[0;34m()\u001b[0m\n",
"\u001b[0;31mInactiveHyperparameterSetError\u001b[0m: Hyperparameter is inactive but has a value set as 0.5.\nx8, Type: UniformInteger, Range: [0, 2], Default: 1",
"\nDuring handling of the above exception, another exception occurred:\n",
"\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/tmp/ipykernel_3462256/3677463211.py\u001b[0m in \u001b[0;36m<cell line: 13>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0mend\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtime\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"for \"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mn_doe\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"points time is = \"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mend\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0mstart\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 13\u001b[0;31m \u001b[0mYt1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mproblem\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mXt1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 14\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0;31m#for 150 points\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/smt/smt/problems/problem.py\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, x, kx)\u001b[0m\n\u001b[1;32m 109\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 110\u001b[0m \u001b[0;31m# Correct the design vector and get information about which design variables are active\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 111\u001b[0;31m \u001b[0mx_corr\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0meval_is_acting\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdesign_space\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcorrect_get_acting\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 112\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0meval_x\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mx_corr\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 113\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/smt/smt/utils/design_space.py\u001b[0m in \u001b[0;36mcorrect_get_acting\u001b[0;34m(self, x)\u001b[0m\n\u001b[1;32m 273\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 274\u001b[0m \u001b[0;31m# Correct and get the is_acting matrix\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 275\u001b[0;31m \u001b[0mx_corrected\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mis_acting\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_correct_get_acting\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 276\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 277\u001b[0m \u001b[0;31m# Check conditionally-acting status\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/smt/smt/utils/design_space.py\u001b[0m in \u001b[0;36m_correct_get_acting\u001b[0;34m(self, x)\u001b[0m\n\u001b[1;32m 899\u001b[0m \u001b[0mconfigs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 900\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mxi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 901\u001b[0;31m \u001b[0mconfigs\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_get_correct_config\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mxi\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0minv_cs_var_idx\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 902\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 903\u001b[0m \u001b[0;31m# Convert Configuration objects to design vectors and get the is_active matrix\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/smt/smt/utils/design_space.py\u001b[0m in \u001b[0;36m_get_correct_config\u001b[0;34m(self, vector)\u001b[0m\n\u001b[1;32m 980\u001b[0m \u001b[0;31m# Modify the vector and create a new Configuration\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 981\u001b[0m \u001b[0mvector\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mconfig\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_array\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcopy\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 982\u001b[0;31m \u001b[0;32mwhile\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvector\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mparam_idx\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m\u001b[0;34m\"nan\"\u001b[0m \u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 983\u001b[0m \u001b[0mparam_idx\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mparam_idx\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 984\u001b[0m \u001b[0mvector\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mparam_idx\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnan\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mIndexError\u001b[0m: index 11 is out of bounds for axis 0 with size 11"
]
}
],
"source": [
"#compare the CPU time to build a DOE with 15 or 150 points\n",
"problem = HierarchicalGoldstein()\n",
"design_space = problem.design_space\n",
"\n",
"print(design_space)\n",
"#for 15 points\n",
"n_doe = 15\n",
"design_space.seed = 42\n",
Expand Down

0 comments on commit c6d3b0b

Please sign in to comment.