Skip to content

Commit 94817e0

Browse files
VilockLifacebook-github-bot
authored andcommitted
Update Augmented Rosenbrock function
Summary: Fixed a typo and extended it to more dimensions in `X`. Reviewed By: bkarrer Differential Revision: D16090823 fbshipit-source-id: 86f7b941d3eaeff8d531877e15f099ee468916f8
1 parent c048ace commit 94817e0

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

botorch/test_functions/aug_rosenbrock.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,39 @@
55
from torch import Tensor
66

77

8-
# This function has infinitely many global maximizers.
8+
GLOBAL_MAXIMIZER = [1.0]
99
GLOBAL_MAXIMUM = 0.0
1010

1111

1212
def neg_aug_rosenbrock(X: Tensor):
13-
r"""Augmented Rosenbrock test function.
13+
r"""Augmented-Rosenbrock test function.
1414
15-
4-dimensional function, the last two dimensions are for fidelity parameters,
16-
X is usually in [-5,10]^2 * [0,1]^2:
15+
d-dimensional function (usually evaluated on `[-5, 10]^(d-2) * [0, 1]^2`),
16+
the last two dimensions are the fidelity parameters:
1717
18-
f(x) = 100 (x_2 - x_1^2 + 0.1 * (1 - x_3))^2 + (x_1 - 1 + 0.1 * (1 - x_4)^2)^2
18+
`f(x) = sum_{i=1}^{d-1} (100 (x_{i+1} - x_i^2 + 0.1 * (1-x_{d-1}))^2
19+
+ (x_i - 1 + 0.1 * (1 - x_d)^2)^2)'
1920
20-
f has infinitely many minimizers, with `f_min = 0.0`
21+
f has one minimizer for its global minimum at
22+
23+
`z_1 = (1, 1, ..., 1)`
24+
25+
with `f(z_i) = 0.0`
2126
2227
Args:
23-
X: A Tensor of size `4` or `k x 4` (`k` batch evaluations).
28+
X: A Tensor of size `d` or `k x d` (`k` batch evaluations).
29+
2430
Returns:
25-
`-f(X)`, the negative value of the augmented Rosenbrock function.
31+
`-f(X)`, the negative value of the Augmented-Rosenbrock function.
2632
"""
2733
batch = X.ndimension() > 1
2834
X = X if batch else X.unsqueeze(0)
35+
X_curr = X[..., :-3]
36+
X_next = X[..., 1:-2]
2937
result = (
30-
-100 * (X[:, 1] - X[:, 0] ** 2 + 0.1 * (1 - X[:, 2])) ** 2
31-
+ (X[:, 0] - 1 + 0.1 * (1 - X[:, 3]) ** 2) ** 2
32-
)
38+
-(
39+
100 * (X_next - X_curr ** 2 + 0.1 * (1 - X[..., -2].unsqueeze(-1))) ** 2
40+
+ (X_curr - 1 + 0.1 * (1 - X[..., -1].unsqueeze(-1)) ** 2) ** 2
41+
)
42+
).sum(dim=-1)
3343
return result if batch else result.squeeze(0)

test/test_functions/test_aug_rosenbroc.py renamed to test/test_functions/test_aug_rosenbrock.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,21 @@
55
import unittest
66

77
import torch
8-
from botorch.test_functions.aug_rosenbrock import GLOBAL_MAXIMUM, neg_aug_rosenbrock
8+
from botorch.test_functions.aug_rosenbrock import (
9+
GLOBAL_MAXIMIZER,
10+
GLOBAL_MAXIMUM,
11+
neg_aug_rosenbrock,
12+
)
13+
14+
15+
DIMENSION = 5
916

1017

1118
class TestNegAugRosenbrock(unittest.TestCase):
1219
def test_single_eval_neg_aug_rosenbrock(self, cuda=False):
1320
device = torch.device("cuda") if cuda else torch.device("cpu")
1421
for dtype in (torch.float, torch.double):
15-
X = torch.zeros(4, device=device, dtype=dtype)
22+
X = torch.zeros(5, device=device, dtype=dtype)
1623
res = neg_aug_rosenbrock(X)
1724
self.assertEqual(res.dtype, dtype)
1825
self.assertEqual(res.device.type, device.type)
@@ -25,7 +32,7 @@ def test_single_eval_neg_aug_rosenbrock_cuda(self):
2532
def test_batch_eval_neg_aug_rosenbrock(self, cuda=False):
2633
device = torch.device("cuda") if cuda else torch.device("cpu")
2734
for dtype in (torch.float, torch.double):
28-
X = torch.zeros(2, 4, device=device, dtype=dtype)
35+
X = torch.zeros(2, 5, device=device, dtype=dtype)
2936
res = neg_aug_rosenbrock(X)
3037
self.assertEqual(res.dtype, dtype)
3138
self.assertEqual(res.device.type, device.type)
@@ -39,14 +46,13 @@ def test_neg_aug_rosenbrock_global_maximum(self, cuda=False):
3946
device = torch.device("cuda") if cuda else torch.device("cpu")
4047
for dtype in (torch.float, torch.double):
4148
X = torch.tensor(
42-
[[1, 1, 1, 1], [1, 0.95, 0.5, 1], [1, 0.91, 0.1, 1], [1, 0.99, 0.9, 1]],
49+
GLOBAL_MAXIMIZER * DIMENSION,
4350
device=device,
4451
dtype=dtype,
4552
requires_grad=True,
4653
)
4754
res = neg_aug_rosenbrock(X)
48-
for r in res:
49-
self.assertAlmostEqual(r.item(), GLOBAL_MAXIMUM, places=4)
55+
self.assertAlmostEqual(res.item(), GLOBAL_MAXIMUM, places=4)
5056
grad = torch.autograd.grad(res.sum(), X)[0]
5157
self.assertLess(grad.abs().max().item(), 1e-4)
5258

0 commit comments

Comments
 (0)