From 0a8cb03073dc1800a0afe19457aeb2eeb2633004 Mon Sep 17 00:00:00 2001 From: Tim Head Date: Tue, 29 Mar 2016 11:29:22 +0200 Subject: [PATCH 1/2] Refactor benchmark functions --- skopt/benchmarks.py | 35 ++++++++++++++++++++++++++++++++++ skopt/tests/test_benchmarks.py | 19 ++++++++++++++++++ skopt/tests/test_gp_opt.py | 20 +------------------ 3 files changed, 55 insertions(+), 19 deletions(-) create mode 100644 skopt/benchmarks.py create mode 100644 skopt/tests/test_benchmarks.py diff --git a/skopt/benchmarks.py b/skopt/benchmarks.py new file mode 100644 index 000000000..d0dcc5ccb --- /dev/null +++ b/skopt/benchmarks.py @@ -0,0 +1,35 @@ +"""A collection of benchmark problems""" + +import numpy as np + + +def branin(x, a=1, b=5.1 / (4 * np.pi**2), c=5. / np.pi, + r=6, s=10, t=1. / (8 * np.pi)): + """Branin-Hoo function is defined on the square x1 ∈ [-5, 10], x2 ∈ [0, 15]. + + It has three minima with f(x*) = 0.397887 at x* = (-pi, 12.275), + (+pi, 2.275), and (9.42478, 2.475). + + More details: + """ + return (a * (x[1] - b * x[0]**2 + c * x[0] - r)**2 + + s * (1 - t) * np.cos(x[0]) + s) + +def hartmann_6(x, + alpha=np.asarray([1.0, 1.2, 3.0, 3.2]), + P=10**-4 * np.asarray([[1312, 1696, 5569, 124, 8283, 5886], + [2329, 4135, 8307, 3736, 1004, 9991], + [2348, 1451, 3522, 2883, 3047, 6650], + [4047, 8828, 8732, 5743, 1091, 381]]), + A=np.asarray([[10, 3, 17, 3.50, 1.7, 8], + [0.05, 10, 17, 0.1, 8, 14], + [3, 3.5, 1.7, 10, 17, 8], + [17, 8, 0.05, 10, 0.1, 14]])): + """The six dimensional Hartmann function is defined on the unit hypercube. + + It has six local minima and one global minimum f(x*) = -3.32237 at + x* = (0.20169, 0.15001, 0.476874, 0.275332, 0.311652, 0.6573). + + More details: + """ + return -np.sum(alpha * np.exp(-np.sum(A * (x - P)**2, axis=1))) diff --git a/skopt/tests/test_benchmarks.py b/skopt/tests/test_benchmarks.py new file mode 100644 index 000000000..7764c063d --- /dev/null +++ b/skopt/tests/test_benchmarks.py @@ -0,0 +1,19 @@ +import numpy as np +from sklearn.utils.testing import assert_array_almost_equal +from sklearn.utils.testing import assert_almost_equal + +from skopt.benchmarks import branin, hartmann_6 + + +def test_branin(): + xstars = np.asarray([(-np.pi, 12.275), (+np.pi, 2.275), (9.42478, 2.475)]) + f_at_xstars = np.asarray([branin(xstar) for xstar in xstars]) + branin_min = np.array([0.397887] * xstars.shape[0]) + assert_array_almost_equal(f_at_xstars, branin_min) + + +def test_hartmann6(): + assert_almost_equal(hartmann_6((0.20169, 0.15001, 0.476874, + 0.275332, 0.311652, 0.6573)), + -3.32237, + decimal=5) diff --git a/skopt/tests/test_gp_opt.py b/skopt/tests/test_gp_opt.py index d83de7174..506b2e630 100644 --- a/skopt/tests/test_gp_opt.py +++ b/skopt/tests/test_gp_opt.py @@ -3,25 +3,7 @@ from sklearn.utils.testing import assert_less from skopt.gp_opt import gp_minimize - - -def branin(x, a=1, b=5.1 / (4 * pi ** 2), c=5. / pi, - r=6, s=10, t=1. / (8 * pi)): - return (a * (x[1] - b * x[0] ** 2 + c * x[0] - r) ** 2 + - s * (1 - t) * cos(x[0]) + s) - - -def hartmann_6(x, - alpha=np.asarray([1.0, 1.2, 3.0, 3.2]), - P=10**-4 * np.asarray([[1312, 1696, 5569, 124, 8283, 5886], - [2329, 4135, 8307, 3736, 1004, 9991], - [2348, 1451, 3522, 2883, 3047, 6650], - [4047, 8828, 8732, 5743, 1091, 381]]), - A=np.asarray([[10, 3, 17, 3.50, 1.7, 8], - [0.05, 10, 17, 0.1, 8, 14], - [3, 3.5, 1.7, 10, 17, 8], - [17, 8, 0.05, 10, 0.1, 14]])): - return -np.sum(alpha * np.exp(-np.sum(A * (x - P)**2, axis=1))) +from skopt.benchmarks import branin, hartmann_6 def check_branin(search): From 1867ea1b6127f7b7ce3cc481cdab9ad923f0e568 Mon Sep 17 00:00:00 2001 From: Tim Head Date: Tue, 29 Mar 2016 11:39:58 +0200 Subject: [PATCH 2/2] One import per line --- skopt/tests/test_benchmarks.py | 3 ++- skopt/tests/test_gp_opt.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/skopt/tests/test_benchmarks.py b/skopt/tests/test_benchmarks.py index 7764c063d..c853285e8 100644 --- a/skopt/tests/test_benchmarks.py +++ b/skopt/tests/test_benchmarks.py @@ -2,7 +2,8 @@ from sklearn.utils.testing import assert_array_almost_equal from sklearn.utils.testing import assert_almost_equal -from skopt.benchmarks import branin, hartmann_6 +from skopt.benchmarks import branin +from skopt.benchmarks import hartmann_6 def test_branin(): diff --git a/skopt/tests/test_gp_opt.py b/skopt/tests/test_gp_opt.py index 506b2e630..51647a2bf 100644 --- a/skopt/tests/test_gp_opt.py +++ b/skopt/tests/test_gp_opt.py @@ -3,7 +3,8 @@ from sklearn.utils.testing import assert_less from skopt.gp_opt import gp_minimize -from skopt.benchmarks import branin, hartmann_6 +from skopt.benchmarks import branin +from skoprt.benchmarks import hartmann_6 def check_branin(search):