Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python API: Simplify mixed-integer type declaration #115

Merged
merged 5 commits into from
Nov 10, 2023
Merged
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
124 changes: 65 additions & 59 deletions doc/Egor_Tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 1,
"id": "1504e619-5775-42d3-8f48-7339272303ec",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -48,7 +48,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 2,
"id": "0edaf00f",
"metadata": {},
"outputs": [],
Expand All @@ -67,7 +67,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 3,
"id": "af2d82be",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -97,7 +97,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 4,
"id": "7f6da807",
"metadata": {},
"outputs": [],
Expand All @@ -113,7 +113,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 5,
"id": "4c436437",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -163,7 +163,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 6,
"id": "c8942031",
"metadata": {},
"outputs": [],
Expand All @@ -186,40 +186,42 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 7,
"id": "c12b8e9d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Optimization f=[-5.50823898e+00 -1.02637525e-03 7.31165162e-04] at [2.32965679 3.1785822 ]\n",
"Optimization f=[-5.50837809e+00 3.84933482e-04 3.56699512e-04] at [2.329518 3.17886009]\n",
"Optimization history: \n",
"Inputs = [[1.00714219 2.83680177]\n",
" [2.01726227 0.97229664]\n",
" [0.64443743 1.49235589]\n",
" [2.98520626 2.47781049]\n",
" [0.51144494 3.69603895]\n",
" [1.39143602 0.0521766 ]\n",
" [2.19062703 2.07848306]\n",
" [1.79749186 0.5511925 ]\n",
" [0.04433832 3.41661282]\n",
" [2.42351384 1.66930727]\n",
" [2.31595596 3.18731429]\n",
" [2.32965679 3.1785822 ]]\n",
"Outputs = [[-3.84394396e+00 -1.16299419e+00 2.83599142e+00]\n",
" [-2.98955891e+00 -1.03012858e+00 -3.02531983e+00]\n",
" [-2.13679332e+00 -2.03391158e+00 -1.31360327e+00]\n",
" [-5.46301675e+00 -1.68216678e+01 2.47436043e+00]\n",
" [-4.20748389e+00 5.36841086e-01 -2.21660239e+00]\n",
" [-1.44361262e+00 -3.38188883e+00 -1.53365927e+00]\n",
" [-4.26911010e+00 -2.70284316e-01 -1.63608961e+00]\n",
" [-2.34868436e+00 -1.71380930e+00 -3.12745825e+00]\n",
" [-3.46095114e+00 1.40157532e+00 -2.84971328e+01]\n",
" [-4.09282111e+00 -2.43765241e+00 -1.02446667e+00]\n",
" [-5.50327026e+00 1.16427163e-01 -5.39230013e-02]\n",
" [-5.50823898e+00 -1.02637525e-03 7.31165162e-04]]\n"
"Inputs = [[2.14941171 1.2022835 ]\n",
" [2.89591634 3.04421182]\n",
" [1.13129495 3.27983113]\n",
" [2.50882074 2.47459348]\n",
" [1.34917886 1.15978118]\n",
" [1.99449618 0.14570364]\n",
" [1.68323092 3.96900812]\n",
" [0.26740239 1.98981025]\n",
" [0.823056 0.52179224]\n",
" [0.34794318 2.23644392]\n",
" [2.3323044 3.09159185]\n",
" [2.32962545 3.17922313]\n",
" [2.329518 3.17886009]]\n",
"Outputs = [[-3.35169521e+00 -1.00398765e+00 -2.62111904e+00]\n",
" [-5.94012816e+00 -1.24186360e+01 2.88844914e+00]\n",
" [-4.41112609e+00 -6.51809729e-01 3.03904161e+00]\n",
" [-4.98341421e+00 -2.78451535e+00 2.77667993e-01]\n",
" [-2.50896004e+00 -2.38224715e+00 -1.69313518e-01]\n",
" [-2.14019982e+00 -1.85453736e+00 -3.85405403e+00]\n",
" [-5.65223904e+00 1.40041321e+00 7.31474755e-01]\n",
" [-2.25721264e+00 -4.39484898e-01 -1.40405159e+01]\n",
" [-1.34484824e+00 -3.35493157e+00 -7.17152437e-02]\n",
" [-2.58438710e+00 -4.24396527e-01 -9.72535570e+00]\n",
" [-5.42389625e+00 -1.09766687e-01 -7.37742400e-02]\n",
" [-5.50884858e+00 -1.29446921e-04 1.22477349e-03]\n",
" [-5.50837809e+00 3.84933482e-04 3.56699512e-04]]\n"
]
}
],
Expand Down Expand Up @@ -251,12 +253,12 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 8,
"id": "6948efc1",
"metadata": {},
"outputs": [],
"source": [
"xspecs_mixint_xsinx = [egx.XSpec(egx.XType(egx.XType.INT), [0, 25])]\n",
"xspecs_mixint_xsinx = [egx.XSpec(egx.XType.INT, [0, 25])]\n",
"n_cstr_mixint_xsinx = 0\n",
"\n",
"def mixint_xsinx(x: np.ndarray) -> np.ndarray:\n",
Expand All @@ -279,7 +281,7 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 9,
"id": "928d1f38",
"metadata": {},
"outputs": [
Expand All @@ -289,19 +291,23 @@
"text": [
"Optimization f=[-15.12161154] at [19.]\n",
"Optimization history: \n",
"Inputs = [[10.]\n",
" [ 7.]\n",
" [22.]\n",
" [25.]\n",
" [21.]\n",
" [20.]\n",
"Inputs = [[ 7.]\n",
" [24.]\n",
" [11.]\n",
" [ 6.]\n",
" [ 5.]\n",
" [ 4.]\n",
" [17.]\n",
" [18.]\n",
" [19.]]\n",
"Outputs = [[ 5.70983099]\n",
" [ 3.14127616]\n",
" [ -7.10960014]\n",
" [ 11.42919546]\n",
" [-11.44370682]\n",
" [-14.15453288]\n",
"Outputs = [[ 3.14127616]\n",
" [ 4.91604976]\n",
" [ 5.1356682 ]\n",
" [ 1.78601478]\n",
" [ 0.68929352]\n",
" [ 0.07924194]\n",
" [-12.35295142]\n",
" [-14.43198471]\n",
" [-15.12161154]]\n"
]
}
Expand Down Expand Up @@ -349,7 +355,7 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 10,
"id": "f1615d5c",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -385,7 +391,7 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 11,
"id": "d14fff89",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -436,10 +442,10 @@
],
"source": [
"xtypes = [\n",
" egx.XSpec(egx.XType(egx.XType.FLOAT), [-5.0, 5.0]),\n",
" egx.XSpec(egx.XType(egx.XType.ENUM), tags=[\"blue\", \"red\", \"green\"]),\n",
" egx.XSpec(egx.XType(egx.XType.ENUM), xlimits=[2]),\n",
" egx.XSpec(egx.XType(egx.XType.ORD), [0, 2, 3]),\n",
" egx.XSpec(egx.XType.FLOAT, [-5.0, 5.0]),\n",
" egx.XSpec(egx.XType.ENUM, tags=[\"blue\", \"red\", \"green\"]),\n",
" egx.XSpec(egx.XType.ENUM, xlimits=[2]),\n",
" egx.XSpec(egx.XType.ORD, [0, 2, 3]),\n",
"]\n",
"egor = egx.Egor(mixobj, xtypes, seed=42)\n",
"res = egor.minimize(n_iter=10)\n",
Expand Down Expand Up @@ -467,7 +473,7 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 12,
"id": "b91f14f2",
"metadata": {},
"outputs": [
Expand All @@ -478,7 +484,7 @@
"Help on Egor in module builtins object:\n",
"\n",
"class Egor(object)\n",
" | Egor(fun, n_cstr=0, n_start=20, n_doe=0, regression_spec=7, correlation_spec=15, infill_strategy=1, q_points=1, par_infill_strategy=1, infill_optimizer=1, n_clusters=1)\n",
" | Egor(fun, xspecs, n_cstr=0, cstr_tol=None, n_start=20, n_doe=0, doe=None, regr_spec=Ellipsis, corr_spec=Ellipsis, infill_strategy=Ellipsis, q_points=1, par_infill_strategy=Ellipsis, infill_optimizer=Ellipsis, kpls_dim=None, n_clusters=1, target=Ellipsis, outdir=None, hot_start=False, seed=None)\n",
" | \n",
" | Optimizer constructor\n",
" | \n",
Expand All @@ -494,9 +500,9 @@
" | n_cstr (int):\n",
" | the number of constraint functions.\n",
" | \n",
" | cstr_tol (list(n_cstr)):\n",
" | List of tolerances for constraints to be satisfied (cstr < tol), its size shoulb equalt to n_cstr.\n",
" | None by default meaning zero tolerances.\n",
" | cstr_tol (list(n_cstr,)):\n",
" | List of tolerances for constraints to be satisfied (cstr < tol), list size should be equal to n_cstr.\n",
" | None by default means zero tolerances.\n",
" | \n",
" | xspecs (list(XSpec)) where XSpec(xtype=FLOAT|INT|ORD|ENUM, xlimits=[<f(xtype)>] or tags=[strings]):\n",
" | Specifications of the nx components of the input x (eg. len(xspecs) == nx)\n",
Expand Down Expand Up @@ -575,7 +581,7 @@
" | \n",
" | Methods defined here:\n",
" | \n",
" | minimize(self, /, n_iter=Ellipsis)\n",
" | minimize(self, /, n_iter=20)\n",
" | This function finds the minimum of a given function `fun`\n",
" | \n",
" | # Parameters\n",
Expand Down
66 changes: 33 additions & 33 deletions doc/Gpx_MaunaLoaCO2.ipynb

Large diffs are not rendered by default.

75 changes: 42 additions & 33 deletions doc/Gpx_Tutorial.ipynb

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions python/egobox/tests/test_mixintegor.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def mixobj(X):

class TestMixintEgx(unittest.TestCase):
def test_int(self):
xtypes = [egx.XSpec(egx.XType(egx.XType.INT), [0.0, 25.0])]
xtypes = [egx.XSpec(egx.XType.INT, [0.0, 25.0])]

egor = egx.Egor(xsinx, xtypes, seed=42, n_doe=3)
res = egor.minimize(n_iter=10)
Expand All @@ -45,10 +45,10 @@ def test_int(self):

def test_ord_enum(self):
xtypes = [
egx.XSpec(egx.XType(egx.XType.FLOAT), [-5.0, 5.0]),
egx.XSpec(egx.XType(egx.XType.ENUM), tags=["blue", "red", "green"]),
egx.XSpec(egx.XType(egx.XType.ENUM), xlimits=[2]),
egx.XSpec(egx.XType(egx.XType.ORD), [0, 2, 3]),
egx.XSpec(egx.XType.FLOAT, [-5.0, 5.0]),
egx.XSpec(egx.XType.ENUM, tags=["blue", "red", "green"]),
egx.XSpec(egx.XType.ENUM, xlimits=[2]),
egx.XSpec(egx.XType.ORD, [0, 2, 3]),
]
egor = egx.Egor(mixobj, xtypes, seed=42)
res = egor.minimize(n_iter=10)
Expand Down
20 changes: 10 additions & 10 deletions python/egobox/tests/test_sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
class TestSampling(unittest.TestCase):
def test_lhs(self):
xtypes = [
egx.XSpec(egx.XType(egx.XType.FLOAT), [-5.0, 5.0]),
egx.XSpec(egx.XType(egx.XType.ENUM), tags=["blue", "red", "green"]),
egx.XSpec(egx.XType(egx.XType.ENUM), xlimits=[2]),
egx.XSpec(egx.XType(egx.XType.ORD), [0, 2, 3]),
egx.XSpec(egx.XType.FLOAT, [-5.0, 5.0]),
egx.XSpec(egx.XType.ENUM, tags=["blue", "red", "green"]),
egx.XSpec(egx.XType.ENUM, xlimits=[2]),
egx.XSpec(egx.XType.ORD, [0, 2, 3]),
]

actual = egx.lhs(xtypes, 10, seed=42)
Expand All @@ -31,8 +31,8 @@ def test_lhs(self):

def test_ffact(self):
xtypes = [
egx.XSpec(egx.XType(egx.XType.FLOAT), [-5.0, 5.0]),
egx.XSpec(egx.XType(egx.XType.INT), [-10, 10]),
egx.XSpec(egx.XType.FLOAT, [-5.0, 5.0]),
egx.XSpec(egx.XType.INT, [-10, 10]),
]

actual = egx.sampling(egx.Sampling.FULL_FACTORIAL, xtypes, 10, seed=42)
Expand All @@ -55,10 +55,10 @@ def test_ffact(self):

def test_random(self):
xtypes = [
egx.XSpec(egx.XType(egx.XType.FLOAT), [-5.0, 5.0]),
egx.XSpec(egx.XType(egx.XType.ENUM), tags=["blue", "red", "green"]),
egx.XSpec(egx.XType(egx.XType.ENUM), xlimits=[2]),
egx.XSpec(egx.XType(egx.XType.ORD), [0, 2, 3]),
egx.XSpec(egx.XType.FLOAT, [-5.0, 5.0]),
egx.XSpec(egx.XType.ENUM, tags=["blue", "red", "green"]),
egx.XSpec(egx.XType.ENUM, xlimits=[2]),
egx.XSpec(egx.XType.ORD, [0, 2, 3]),
]

actual = egx.sampling(egx.Sampling.RANDOM, xtypes, 10, seed=42)
Expand Down
8 changes: 4 additions & 4 deletions python/egobox/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
class TestUtils(unittest.TestCase):
def test_to_specs(self):
actual = egx.to_specs([[0.0, 25.0]])
expected = [egx.XSpec(egx.XType(egx.XType.FLOAT), [0.0, 25.0])]
expected = [egx.XSpec(egx.XType.FLOAT, [0.0, 25.0])]
self.assertEqual(len(expected), len(actual))
self.assertEqual(expected[0].xtype.id(), actual[0].xtype.id())
self.assertEqual(expected[0].xtype, actual[0].xtype)
self.assertEqual(expected[0].xlimits, actual[0].xlimits)

def test_to_specs_empty(self):
Expand All @@ -22,8 +22,8 @@ def test_lhs(self):

def test_mixint_lhs(self):
xspecs = [
egx.XSpec(egx.XType(egx.XType.FLOAT), [0.0, 25.0]),
egx.XSpec(egx.XType(egx.XType.INT), [0, 25]),
egx.XSpec(egx.XType.FLOAT, [0.0, 25.0]),
egx.XSpec(egx.XType.INT, [0, 25]),
]
doe = egx.lhs(xspecs, 10)
self.assertEqual(doe.shape, (10, 2))
Loading