diff --git a/botorch/test_functions/__init__.py b/botorch/test_functions/__init__.py index 612d8d946d..136a3e0854 100644 --- a/botorch/test_functions/__init__.py +++ b/botorch/test_functions/__init__.py @@ -54,13 +54,17 @@ Levy, Michalewicz, Powell, + PressureVessel, Rastrigin, Rosenbrock, Shekel, SixHumpCamel, + SpeedReducer, StyblinskiTang, SyntheticTestFunction, + TensionCompressionString, ThreeHumpCamel, + WeldedBeamSO, ) @@ -99,17 +103,21 @@ "OSY", "Penicillin", "Powell", + "PressureVessel", "Rastrigin", "Rosenbrock", "Shekel", "SixHumpCamel", + "SpeedReducer", "SRN", "StyblinskiTang", "SyntheticTestFunction", + "TensionCompressionString", "ThreeHumpCamel", "ToyRobust", "VehicleSafety", "WeldedBeam", + "WeldedBeamSO", "ZDT1", "ZDT2", "ZDT3", diff --git a/botorch/test_functions/multi_objective.py b/botorch/test_functions/multi_objective.py index a46ea5373a..bf9a4efd17 100644 --- a/botorch/test_functions/multi_objective.py +++ b/botorch/test_functions/multi_objective.py @@ -133,11 +133,10 @@ def _rescaled_branin(self, X: Tensor) -> Tensor: @staticmethod def _currin(X: Tensor) -> Tensor: - x_0 = X[..., 0] - x_1 = X[..., 1] - factor1 = 1 - torch.exp(-1 / (2 * x_1)) - numer = 2300 * x_0.pow(3) + 1900 * x_0.pow(2) + 2092 * x_0 + 60 - denom = 100 * x_0.pow(3) + 500 * x_0.pow(2) + 4 * x_0 + 20 + x1, x2 = X.unbind(-1) + factor1 = 1 - torch.exp(-1 / (2 * x2)) + numer = 2300 * x1.pow(3) + 1900 * x1.pow(2) + 2092 * x1 + 60 + denom = 100 * x1.pow(3) + 500 * x1.pow(2) + 4 * x1 + 20 return factor1 * numer / denom def evaluate_true(self, X: Tensor) -> Tensor: @@ -875,40 +874,40 @@ class VehicleSafety(MultiObjectiveTestProblem): num_objectives = 3 def evaluate_true(self, X: Tensor) -> Tensor: - X1, X2, X3, X4, X5 = torch.split(X, 1, -1) + x1, x2, x3, x4, x5 = torch.split(X, 1, -1) f1 = ( 1640.2823 - + 2.3573285 * X1 - + 2.3220035 * X2 - + 4.5688768 * X3 - + 7.7213633 * X4 - + 4.4559504 * X5 + + 2.3573285 * x1 + + 2.3220035 * x2 + + 4.5688768 * x3 + + 7.7213633 * x4 + + 4.4559504 * x5 ) f2 = ( 6.5856 - + 1.15 * X1 - - 1.0427 * X2 - + 0.9738 * X3 - + 0.8364 * X4 - - 0.3695 * X1 * X4 - + 0.0861 * X1 * X5 - + 0.3628 * X2 * X4 - - 0.1106 * X1.pow(2) - - 0.3437 * X3.pow(2) - + 0.1764 * X4.pow(2) + + 1.15 * x1 + - 1.0427 * x2 + + 0.9738 * x3 + + 0.8364 * x4 + - 0.3695 * x1 * x4 + + 0.0861 * x1 * x5 + + 0.3628 * x2 * x4 + - 0.1106 * x1.pow(2) + - 0.3437 * x3.pow(2) + + 0.1764 * x4.pow(2) ) f3 = ( -0.0551 - + 0.0181 * X1 - + 0.1024 * X2 - + 0.0421 * X3 - - 0.0073 * X1 * X2 - + 0.024 * X2 * X3 - - 0.0118 * X2 * X4 - - 0.0204 * X3 * X4 - - 0.008 * X3 * X5 - - 0.0241 * X2.pow(2) - + 0.0109 * X4.pow(2) + + 0.0181 * x1 + + 0.1024 * x2 + + 0.0421 * x3 + - 0.0073 * x1 * x2 + + 0.024 * x2 * x3 + - 0.0118 * x2 * x4 + - 0.0204 * x3 * x4 + - 0.008 * x3 * x5 + - 0.0241 * x2.pow(2) + + 0.0109 * x4.pow(2) ) f_X = torch.cat([f1, f2, f3], dim=-1) return f_X @@ -1109,56 +1108,56 @@ class CarSideImpact(MultiObjectiveTestProblem): _max_hv = 484.72654347642793 def evaluate_true(self, X: Tensor) -> Tensor: - X1, X2, X3, X4, X5, X6, X7 = torch.split(X, 1, -1) + x1, x2, x3, x4, x5, x6, x7 = torch.split(X, 1, -1) f1 = ( 1.98 - + 4.9 * X1 - + 6.67 * X2 - + 6.98 * X3 - + 4.01 * X4 - + 1.78 * X5 - + 10**-5 * X6 - + 2.73 * X7 + + 4.9 * x1 + + 6.67 * x2 + + 6.98 * x3 + + 4.01 * x4 + + 1.78 * x5 + + 10**-5 * x6 + + 2.73 * x7 ) - f2 = 4.72 - 0.5 * X4 - 0.19 * X2 * X3 - V_MBP = 10.58 - 0.674 * X1 * X2 - 0.67275 * X2 - V_FD = 16.45 - 0.489 * X3 * X7 - 0.843 * X5 * X6 + f2 = 4.72 - 0.5 * x4 - 0.19 * x2 * x3 + V_MBP = 10.58 - 0.674 * x1 * x2 - 0.67275 * x2 + V_FD = 16.45 - 0.489 * x3 * x7 - 0.843 * x5 * x6 f3 = 0.5 * (V_MBP + V_FD) - g1 = 1 - 1.16 + 0.3717 * X2 * X4 + 0.0092928 * X3 + g1 = 1 - 1.16 + 0.3717 * x2 * x4 + 0.0092928 * x3 g2 = ( 0.32 - 0.261 - + 0.0159 * X1 * X2 - + 0.06486 * X1 - + 0.019 * X2 * X7 - - 0.0144 * X3 * X5 - - 0.0154464 * X6 + + 0.0159 * x1 * x2 + + 0.06486 * x1 + + 0.019 * x2 * x7 + - 0.0144 * x3 * x5 + - 0.0154464 * x6 ) g3 = ( 0.32 - 0.214 - - 0.00817 * X5 - + 0.045195 * X1 - + 0.0135168 * X1 - - 0.03099 * X2 * X6 - + 0.018 * X2 * X7 - - 0.007176 * X3 - - 0.023232 * X3 - + 0.00364 * X5 * X6 - + 0.018 * X2.pow(2) + - 0.00817 * x5 + + 0.045195 * x1 + + 0.0135168 * x1 + - 0.03099 * x2 * x6 + + 0.018 * x2 * x7 + - 0.007176 * x3 + - 0.023232 * x3 + + 0.00364 * x5 * x6 + + 0.018 * x2.pow(2) ) - g4 = 0.32 - 0.74 + 0.61 * X2 + 0.031296 * X3 + 0.031872 * X7 - 0.227 * X2.pow(2) - g5 = 32 - 28.98 - 3.818 * X3 + 4.2 * X1 * X2 - 1.27296 * X6 + 2.68065 * X7 + g4 = 0.32 - 0.74 + 0.61 * x2 + 0.031296 * x3 + 0.031872 * x7 - 0.227 * x2.pow(2) + g5 = 32 - 28.98 - 3.818 * x3 + 4.2 * x1 * x2 - 1.27296 * x6 + 2.68065 * x7 g6 = ( 32 - 33.86 - - 2.95 * X3 - + 5.057 * X1 * X2 - + 3.795 * X2 - + 3.4431 * X7 + - 2.95 * x3 + + 5.057 * x1 * x2 + + 3.795 * x2 + + 3.4431 * x7 - 1.45728 ) - g7 = 32 - 46.36 + 9.9 * X2 + 4.4505 * X1 + g7 = 32 - 46.36 + 9.9 * x2 + 4.4505 * x1 g8 = 4 - f2 g9 = 9.9 - V_MBP g10 = 15.7 - V_FD @@ -1191,8 +1190,9 @@ def evaluate_true(self, X: Tensor) -> Tensor: ) def evaluate_slack_true(self, X: Tensor) -> Tensor: - c1 = 25.0 - (X[..., 0] - 5.0) ** 2 - X[..., 1] ** 2 - c2 = (X[..., 0] - 8.0) ** 2 + (X[..., 1] + 3.0) ** 2 - 7.7 + x1, x2 = X.unbind(-1) + c1 = 25.0 - (x1 - 5.0) ** 2 - x2**2 + c2 = (x1 - 8.0) ** 2 + (x2 + 3.0) ** 2 - 7.7 return torch.stack([c1, c2], dim=-1) @@ -1210,13 +1210,14 @@ class CONSTR(MultiObjectiveTestProblem, ConstrainedBaseTestProblem): _ref_point = [10.0, 10.0] def evaluate_true(self, X: Tensor) -> Tensor: - obj1 = X[..., 0] - obj2 = (1.0 + X[..., 1]) / X[..., 0] - return torch.stack([obj1, obj2], dim=-1) + x1, x2 = X.unbind(-1) + obj2 = (1.0 + x2) / x1 + return torch.stack([x1, obj2], dim=-1) def evaluate_slack_true(self, X: Tensor) -> Tensor: - c1 = 9.0 * X[..., 0] + X[..., 1] - 6.0 - c2 = 9.0 * X[..., 0] - X[..., 1] - 1.0 + x1, x2 = X.unbind(-1) + c1 = 9.0 * x1 + x2 - 6.0 + c2 = 9.0 * x1 - x2 - 1.0 return torch.stack([c1, c2], dim=-1) @@ -1300,24 +1301,23 @@ def evaluate_true(self, X: Tensor) -> Tensor: *X.shape[:-1], self.num_objectives, dtype=X.dtype, device=X.device ) - X1, X2, X3, X4 = torch.split(X, 1, -1) - sq_diff = X2.pow(2) - X1.pow(2) - f[..., :1] = 4.9 * 1e-5 * sq_diff * (X4 - 1.0) - f[..., 1:] = (9.82 * 1e6) * sq_diff / (X3 * X4 * (X2.pow(3) - X1.pow(3))) - + x1, x2, x3, x4 = torch.split(X, 1, -1) + sq_diff = x2.pow(2) - x1.pow(2) + f[..., :1] = 4.9 * 1e-5 * sq_diff * (x4 - 1.0) + f[..., 1:] = (9.82 * 1e6) * sq_diff / (x3 * x4 * (x2.pow(3) - x1.pow(3))) return f def evaluate_slack_true(self, X: Tensor) -> Tensor: g = torch.zeros( *X.shape[:-1], self.num_constraints, dtype=X.dtype, device=X.device ) - X1, X2, X3, X4 = torch.split(X, 1, -1) - sq_diff = X2.pow(2) - X1.pow(2) - cub_diff = X2.pow(3) - X1.pow(3) - g[..., :1] = X2 - X1 - 20.0 - g[..., 1:2] = 0.4 - X3 / (3.14 * sq_diff) - g[..., 2:3] = 1.0 - (2.22 * 1e-3 * X3 * cub_diff) / sq_diff.pow(2) - g[..., 3:] = (2.66 * 1e-2 * X3 * X4 * cub_diff) / sq_diff - 900.0 + x1, x2, x3, x4 = torch.split(X, 1, -1) + sq_diff = x2.pow(2) - x1.pow(2) + cub_diff = x2.pow(3) - x1.pow(3) + g[..., :1] = x2 - x1 - 20.0 + g[..., 1:2] = 0.4 - x3 / (3.14 * sq_diff) + g[..., 2:3] = 1.0 - (2.22 * 1e-3 * x3 * cub_diff) / sq_diff.pow(2) + g[..., 3:] = (2.66 * 1e-2 * x3 * x4 * cub_diff) / sq_diff - 900.0 return g @@ -1398,23 +1398,25 @@ class OSY(MultiObjectiveTestProblem, ConstrainedBaseTestProblem): _ref_point = [-75.0, 75.0] def evaluate_true(self, X: Tensor) -> Tensor: + x1, x2, x3, x4, x5, x6 = X.unbind(-1) f1 = -( - 25 * (X[..., 0] - 2) ** 2 - + (X[..., 1] - 2) ** 2 - + (X[..., 2] - 1) ** 2 - + (X[..., 3] - 4) ** 2 - + (X[..., 4] - 1) ** 2 + 25 * (x1 - 2) ** 2 + + (x2 - 2) ** 2 + + (x3 - 1) ** 2 + + (x4 - 4) ** 2 + + (x5 - 1) ** 2 ) f2 = (X**2).sum(-1) return torch.stack([f1, f2], dim=-1) def evaluate_slack_true(self, X: Tensor) -> Tensor: - g1 = X[..., 0] + X[..., 1] - 2.0 - g2 = 6.0 - X[..., 0] - X[..., 1] - g3 = 2.0 - X[..., 1] + X[..., 0] - g4 = 2.0 - X[..., 0] + 3.0 * X[..., 1] - g5 = 4.0 - (X[..., 2] - 3.0) ** 2 - X[..., 3] - g6 = (X[..., 4] - 3.0) ** 2 + X[..., 5] - 4.0 + x1, x2, x3, x4, x5, x6 = X.unbind(-1) + g1 = x1 + x2 - 2.0 + g2 = 6.0 - x1 - x2 + g3 = 2.0 - x2 + x1 + g4 = 2.0 - x1 + 3.0 * x2 + g5 = 4.0 - (x3 - 3.0) ** 2 - x4 + g6 = (x5 - 3.0) ** 2 + x6 - 4.0 return torch.stack([g1, g2, g3, g4, g5, g6], dim=-1) @@ -1444,7 +1446,10 @@ def evaluate_slack_true(self, X: Tensor) -> Tensor: class WeldedBeam(MultiObjectiveTestProblem, ConstrainedBaseTestProblem): r""" - The Welded Beam test problem. + The Welded Beam multi-objective test problem. Similar to `WeldedBeamSO` in + `botorch.test_function.synthetic`, but with an additional output, somewhat + modified constraints, and a different domain. + Implementation from https://github.com/msu-coinlab/pymoo/blob/master/pymoo/problems/multi/welded_beam.py Note that this implementation assumes minimization, so please choose negate=True. @@ -1462,35 +1467,44 @@ class WeldedBeam(MultiObjectiveTestProblem, ConstrainedBaseTestProblem): _ref_point = [40, 0.015] def evaluate_true(self, X: Tensor) -> Tensor: - f1 = 1.10471 * X[..., 0] ** 2 * X[..., 1] + 0.04811 * X[..., 2] * X[..., 3] * ( - 14.0 + X[..., 1] - ) - f2 = 2.1952 / (X[..., 3] * X[..., 2] ** 3) + # We could do the following, but the constraints are using somewhat + # different numbers (see below). + # f1 = WeldedBeam.evaluate_true(self, X) + x1, x2, x3, x4 = X.unbind(-1) + f1 = 1.10471 * (x1**2) * x2 + 0.04811 * x3 * x4 * (14.0 + x2) + f2 = 2.1952 / (x4 * x3**3) return torch.stack([f1, f2], dim=-1) def evaluate_slack_true(self, X: Tensor) -> Tensor: - P = 6000 - L = 14 - t_max = 13600 - s_max = 30000 - - R = torch.sqrt(0.25 * (X[..., 1] ** 2 + (X[..., 0] + X[..., 2]) ** 2)) - M = P * (L + X[..., 1] / 2) - J = ( - 2 - * math.sqrt(0.5) - * X[..., 0] - * X[..., 1] - * (X[..., 1] ** 2 / 12 + 0.25 * (X[..., 0] + X[..., 2]) ** 2) - ) - t1 = P / (math.sqrt(2) * X[..., 0] * X[..., 1]) + x1, x2, x3, x4 = X.unbind(-1) + P = 6000.0 + L = 14.0 + t_max = 13600.0 + s_max = 30000.0 + + # Ideally, we could just do the following, but the numbers in the + # single-outcome WeldedBeam are different (see below) + # g1_, g2_, g3_, _, _, g6_ = WeldedBeam.evaluate_slack_true(self, X) + # g1 = g1_ / t_max + # g2 = g2_ / s_max + # g3 = 1 / (5 - 0.125) * g3_ + # g4 = 1 / P * g6_ + + R = torch.sqrt(0.25 * (x2**2 + (x1 + x3) ** 2)) + M = P * (L + x2 / 2) + # This `J` is different than the one in [CoelloCoello2002constraint]_ + # by a factor of 2 (sqrt(2) instead of sqrt(0.5)) + J = 2 * math.sqrt(0.5) * x1 * x2 * (x2**2 / 12 + 0.25 * (x1 + x3) ** 2) + t1 = P / (math.sqrt(2) * x1 * x2) t2 = M * R / J - t = torch.sqrt(t1**2 + t2**2 + t1 * t2 * X[..., 1] / R) - s = 6 * P * L / (X[..., 3] * X[..., 2] ** 2) - P_c = 64746.022 * (1 - 0.0282346 * X[..., 2]) * X[..., 2] * X[..., 3] ** 3 - - g1 = (1 / t_max) * (t - t_max) - g2 = (1 / s_max) * (s - s_max) - g3 = (1 / (5 - 0.125)) * (X[..., 0] - X[..., 3]) - g4 = (1 / P) * (P - P_c) + t = torch.sqrt(t1**2 + t1 * t2 * x2 / R + t2**2) + s = 6 * P * L / (x4 * x3**2) + # These numbers are also different from [CoelloCoello2002constraint]_ + P_c = 64746.022 * (1 - 0.0282346 * x3) * x3 * x4**3 + + g1 = (t - t_max) / t_max + g2 = (s - s_max) / s_max + g3 = 1 / (5 - 0.125) * (x1 - x4) + g4 = (P - P_c) / P + return -torch.stack([g1, g2, g3, g4], dim=-1) diff --git a/botorch/test_functions/multi_objective_multi_fidelity.py b/botorch/test_functions/multi_objective_multi_fidelity.py index 506d41aa7d..27c7aaee72 100644 --- a/botorch/test_functions/multi_objective_multi_fidelity.py +++ b/botorch/test_functions/multi_objective_multi_fidelity.py @@ -56,10 +56,7 @@ class MOMFBraninCurrin(MultiObjectiveTestProblem): _max_hv = 0.5235514158034145 def _branin(self, X: Tensor) -> Tensor: - x1 = X[..., 0] - x2 = X[..., 1] - s = X[..., 2] - + x1, x2, s = X.unbind(-1) x11 = 15 * x1 - 5 x22 = 15 * x2 b = 5.1 / (4 * math.pi**2) - 0.01 * (1 - s) @@ -71,9 +68,8 @@ def _branin(self, X: Tensor) -> Tensor: return B / 22 def _currin(self, X: Tensor) -> Tensor: - x1 = X[..., 0] - x2 = X[..., 1] - s = X[..., 2] + x1, x2, s = X.unbind(-1) + A = 2300 * x1**3 + 1900 * x1**2 + 2092 * x1 + 60 B = 100 * x1**3 + 500 * x1**2 + 4 * x1 + 20 y = (1 - 0.1 * (1 - s) * torch.exp(-1 / (2 * x2))) * A / B @@ -116,11 +112,7 @@ class MOMFPark(MultiObjectiveTestProblem): _max_hv = 0.08551927363087991 def _transform(self, X: Tensor) -> Tensor: - x1 = X[..., 0] - x2 = X[..., 1] - x3 = X[..., 2] - x4 = X[..., 3] - s = X[..., 4] + x1, x2, x3, x4, s = X.unbind(-1) _x1 = 1 - 2 * (x1 - 0.6) ** 2 _x2 = x2 _x3 = 1 - 3 * (x3 - 0.5) ** 2 @@ -128,11 +120,7 @@ def _transform(self, X: Tensor) -> Tensor: return torch.stack([_x1, _x2, _x3, _x4, s], dim=-1) def _park1(self, X: Tensor) -> Tensor: - x1 = X[..., 0] - x2 = X[..., 1] - x3 = X[..., 2] - x4 = X[..., 3] - s = X[..., 4] + x1, x2, x3, x4, s = X.unbind(-1) T1 = ( (x1 + 1e-3 * (1 - s)) / 2 @@ -144,11 +132,7 @@ def _park1(self, X: Tensor) -> Tensor: return A * (T1 + T2 - B) / 22 - 0.8 def _park2(self, X: Tensor) -> Tensor: - x1 = X[..., 0] - x2 = X[..., 1] - x3 = X[..., 2] - x4 = X[..., 3] - s = X[..., 4] + x1, x2, x3, x4, s = X.unbind(-1) A = 0.9 + 0.1 * s B = 0.1 * (1 - s) return ( diff --git a/botorch/test_functions/synthetic.py b/botorch/test_functions/synthetic.py index f44cc1b5df..1e710fd06d 100644 --- a/botorch/test_functions/synthetic.py +++ b/botorch/test_functions/synthetic.py @@ -6,7 +6,32 @@ r""" Synthetic functions for optimization benchmarks. -Reference: https://www.sfu.ca/~ssurjano/optimization.html + +Most test functions (if not indicated otherwise) are taken from +[Bingham2013virtual]_. + + +References: + +.. [Bingham2013virtual] + D. Bingham, S. Surjanovic. Virtual Library of Simulation Experiments. + https://www.sfu.ca/~ssurjano/optimization.html + +.. [CoelloCoello2002constraint] + C. A. Coello Coello and E. Mezura Montes. Constraint-handling in genetic + algorithms through the use of dominance-based tournament selection. + Advanced Engineering Informatics, 16(3):193–203, 2002. + +.. [Hedar2006derivfree] + A.-R. Hedar and M. Fukushima. Derivative-free filter simulated annealing + method for constrained continuous global optimization. Journal of Global + Optimization, 35(4):521–549, 2006. + +.. [Lemonge2010constrained] + A. C. C. Lemonge, H. J. C. Barbosa, C. C. H. Borges, and F. B. dos Santos + Silva. Constrained optimization problems in mechanical engineering design + using a real-coded steady-state genetic algorithm. Mecánica Computacional, + XXIX:9287–9303, 2010. """ from __future__ import annotations @@ -15,7 +40,8 @@ from typing import List, Optional, Tuple import torch -from botorch.test_functions.base import BaseTestProblem +from botorch.test_functions.base import BaseTestProblem, ConstrainedBaseTestProblem +from botorch.test_functions.utils import round_nearest from torch import Tensor @@ -761,3 +787,182 @@ class ThreeHumpCamel(SyntheticTestFunction): def evaluate_true(self, X: Tensor) -> Tensor: x1, x2 = X[..., 0], X[..., 1] return 2.0 * x1**2 - 1.05 * x1**4 + x1**6 / 6.0 + x1 * x2 + x2**2 + + +# ------------ Constrained synthetic test functions ----------- # + + +class PressureVessel(SyntheticTestFunction, ConstrainedBaseTestProblem): + r"""Pressure vessel design problem with constraints. + + The four-dimensional pressure vessel design problem with four black-box + constraints from [CoelloCoello2002constraint]_. + """ + + dim = 4 + num_constraints = 4 + _bounds = [(0.0, 10.0), (0.0, 10.0), (10.0, 50.0), (150.0, 200.0)] + + def evaluate_true(self, X: Tensor) -> Tensor: + x1, x2, x3, x4 = X.unbind(-1) + x1 = round_nearest(x1, increment=0.0625, bounds=self._bounds[0]) + x2 = round_nearest(x2, increment=0.0625, bounds=self._bounds[1]) + return ( + 0.6224 * x1 * x3 * x4 + + 1.7781 * x2 * (x3**2) + + 3.1661 * (x1**2) * x4 + + 19.84 * (x1**2) * x3 + ) + + def evaluate_slack_true(self, X: Tensor) -> Tensor: + x1, x2, x3, x4 = X.unbind(-1) + return -torch.stack( + [ + -x1 + 0.0193 * x3, + -x2 + 0.00954 * x3, + -math.pi * (x3**2) * x4 - (4 / 3) * math.pi * (x3**3) + 1296000.0, + x4 - 240.0, + ], + dim=-1, + ) + + +class WeldedBeamSO(SyntheticTestFunction, ConstrainedBaseTestProblem): + r"""Welded beam design problem with constraints (single-outcome). + + The four-dimensional welded beam design proble problem with six + black-box constraints from [CoelloCoello2002constraint]_. + + For a (somewhat modified) multi-objective version, see + `botorch.test_functions.multi_objective.WeldedBeam`. + """ + + dim = 4 + num_constraints = 6 + _bounds = [(0.125, 10.0), (0.1, 10.0), (0.1, 10.0), (0.1, 10.0)] + + def evaluate_true(self, X: Tensor) -> Tensor: + x1, x2, x3, x4 = X.unbind(-1) + return 1.10471 * (x1**2) * x2 + 0.04811 * x3 * x4 * (14.0 + x2) + + def evaluate_slack_true(self, X: Tensor) -> Tensor: + x1, x2, x3, x4 = X.unbind(-1) + P = 6000.0 + L = 14.0 + E = 30e6 + G = 12e6 + t_max = 13600.0 + s_max = 30000.0 + d_max = 0.25 + + M = P * (L + x2 / 2) + R = torch.sqrt(0.25 * (x2**2 + (x1 + x3) ** 2)) + J = 2 * math.sqrt(2) * x1 * x2 * (x2**2 / 12 + 0.25 * (x1 + x3) ** 2) + P_c = ( + 4.013 + * E + * x3 + * (x4**3) + * 6 + / (L**2) + * (1 - 0.25 * x3 * math.sqrt(E / G) / L) + ) + t1 = P / (math.sqrt(2) * x1 * x2) + t2 = M * R / J + t = torch.sqrt(t1**2 + t1 * t2 * x2 / R + t2**2) + s = 6 * P * L / (x4 * x3**2) + d = 4 * P * L**3 / (E * x3**3 * x4) + + g1 = t - t_max + g2 = s - s_max + g3 = x1 - x4 + g4 = 0.10471 * x1**2 + 0.04811 * x3 * x4 * (14.0 + x2) - 5.0 + g5 = d - d_max + g6 = P - P_c + + return -torch.stack([g1, g2, g3, g4, g5, g6], dim=-1) + + +class TensionCompressionString(SyntheticTestFunction, ConstrainedBaseTestProblem): + r"""Tension compression string optimization problem with constraints. + + The three-dimensional tension compression string optimization problem with + four black-box constraints from [Hedar2006derivfree]_. + """ + + dim = 3 + num_constraints = 4 + _bounds = [(0.01, 1.0), (0.01, 1.0), (0.01, 20.0)] + + def evaluate_true(self, X: Tensor) -> Tensor: + x1, x2, x3 = X.unbind(-1) + return (x1**2) * x2 * (x3 + 2) + + def evaluate_slack_true(self, X: Tensor) -> Tensor: + x1, x2, x3 = X.unbind(-1) + constraints = torch.stack( + [ + 1 - (x2**3) * x3 / (71785 * (x1**4)), + (4 * (x2**2) - x1 * x2) / (12566 * (x1**3) * (x2 - x1)) + + 1 / (5108 * (x1**2)) + - 1, + 1 - 140.45 * x1 / (x3 * (x2**2)), + (x1 + x2) / 1.5 - 1, + ], + dim=-1, + ) + return -constraints.clamp_max(100) + + +class SpeedReducer(SyntheticTestFunction, ConstrainedBaseTestProblem): + r"""Speed Reducer design problem with constraints. + + The seven-dimensional speed reducer design problem with eleven black-box + constraints from [Lemonge2010constrained]_. + """ + + dim = 7 + num_constraints = 11 + _bounds = [ + (2.6, 3.6), + (0.7, 0.8), + (17.0, 28.0), + (7.3, 8.3), + (7.8, 8.3), + (2.9, 3.9), + (5.0, 5.5), + ] + + def evaluate_true(self, X: Tensor) -> Tensor: + x1, x2, x3, x4, x5, x6, x7 = X.unbind(-1) + return ( + 0.7854 * x1 * (x2**2) * (3.3333 * (x3**2) + 14.9334 * x3 - 43.0934) + + -1.508 * x1 * (x6**2 + x7**2) + + 7.4777 * (x6**3 + x7**3) + + 0.7854 * (x4 * (x6**2) + x5 * (x7**2)) + ) + + def evaluate_slack_true(self, X: Tensor) -> Tensor: + x1, x2, x3, x4, x5, x6, x7 = X.unbind(-1) + return -torch.stack( + [ + 27.0 * (1 / x1) * (1 / (x2**2)) * (1 / x3) - 1, + 397.5 * (1 / x1) * (1 / (x2**2)) * (1 / (x3**2)) - 1, + 1.93 * (1 / x2) * (1 / x3) * (x4**3) * (1 / (x6**4)) - 1, + 1.93 * (1 / x2) * (1 / x3) * (x5**3) * (1 / (x7**4)) - 1, + 1 + / (0.1 * (x6**3)) + * torch.sqrt((745 * x4 / (x2 * x3)) ** 2 + 16.9 * 1e6) + - 1100, + 1 + / (0.1 * (x7**3)) + * torch.sqrt((745 * x5 / (x2 * x3)) ** 2 + 157.5 * 1e6) + - 850, + x2 * x3 - 40, + 5 - x1 / x2, + x1 / x2 - 12, + (1.5 * x6 + 1.9) / x4 - 1, + (1.1 * x7 + 1.9) / x5 - 1, + ], + dim=-1, + ) diff --git a/botorch/test_functions/utils.py b/botorch/test_functions/utils.py new file mode 100644 index 0000000000..969524822d --- /dev/null +++ b/botorch/test_functions/utils.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python3 +# Copyright (c) Meta Platforms, Inc. and affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + + +from __future__ import annotations + +from typing import Optional, Tuple + +import torch + +from torch import Tensor + + +def round_nearest( + X: Tensor, increment: float, bounds: Optional[Tuple[float, float]] +) -> Tensor: + r"""Rounds the input tensor to the nearest multiple of `increment`. + + Args: + X: The input to be rounded. + increment: The increment to round to. + bounds: An optional tuple of two floats representing the lower and upper + bounds on `X`. If provided, this will round to the nearest multiple + of `increment` that lies within the bounds. + + Returns: + The rounded input. + """ + X_round = torch.round(X / increment) * increment + if bounds is not None: + X_round = torch.where(X_round < bounds[0], X_round + increment, X_round) + X_round = torch.where(X_round > bounds[1], X_round - increment, X_round) + return X_round diff --git a/sphinx/source/test_functions.rst b/sphinx/source/test_functions.rst index cf9e324b37..bfde346766 100644 --- a/sphinx/source/test_functions.rst +++ b/sphinx/source/test_functions.rst @@ -35,3 +35,8 @@ Sensitivity Analysis Test Functions ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. automodule:: botorch.test_functions.sensitivity_analysis :members: + +Utilities For Test Functions +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +.. automodule:: botorch.test_functions.utils + :members: diff --git a/test/test_functions/test_synthetic.py b/test/test_functions/test_synthetic.py index 9c37979970..4e8198f6cf 100644 --- a/test/test_functions/test_synthetic.py +++ b/test/test_functions/test_synthetic.py @@ -21,17 +21,22 @@ Levy, Michalewicz, Powell, + PressureVessel, Rastrigin, Rosenbrock, Shekel, SixHumpCamel, + SpeedReducer, StyblinskiTang, SyntheticTestFunction, + TensionCompressionString, ThreeHumpCamel, + WeldedBeamSO, ) from botorch.utils.testing import ( BaseTestProblemTestCaseMixIn, BotorchTestCase, + ConstrainedTestProblemTestCaseMixin, SyntheticTestFunctionTestCaseMixin, ) from torch import Tensor @@ -308,3 +313,42 @@ class TestThreeHumpCamel( ThreeHumpCamel(negate=True), ThreeHumpCamel(noise_std=0.1), ] + + +# ------------------ Constrained synthetic test problems ------------------ # + + +class TestPressureVessel( + BotorchTestCase, + BaseTestProblemTestCaseMixIn, + ConstrainedTestProblemTestCaseMixin, +): + + functions = [PressureVessel()] + + +class TestSpeedReducer( + BotorchTestCase, + BaseTestProblemTestCaseMixIn, + ConstrainedTestProblemTestCaseMixin, +): + + functions = [SpeedReducer()] + + +class TestTensionCompressionString( + BotorchTestCase, + BaseTestProblemTestCaseMixIn, + ConstrainedTestProblemTestCaseMixin, +): + + functions = [TensionCompressionString()] + + +class TestWeldedBeamSO( + BotorchTestCase, + BaseTestProblemTestCaseMixIn, + ConstrainedTestProblemTestCaseMixin, +): + + functions = [WeldedBeamSO()]