-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d1f9d0a
commit 90cd747
Showing
10 changed files
with
235 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import unittest | ||
|
||
import torch | ||
from botorch.acquisition import ExpectedImprovement, qExpectedImprovement | ||
|
||
from globopt import GaussHermiteSampler, Ms, Rbf, make_idw_acq_factory | ||
from globopt.problems import SimpleProblem | ||
|
||
|
||
class TestNonMyopicAcquisitionFunction(unittest.TestCase): | ||
def test_make_idw_acq_factory(self): | ||
c1, c2, span_Y_min = 1.0, 0.5, 1e-3 | ||
factory = make_idw_acq_factory(c1, c2, span_Y_min) | ||
self.assertTrue(callable(factory)) | ||
self.assertDictEqual(factory(), {"c1": c1, "c2": c2, "span_Y_min": span_Y_min}) | ||
|
||
def test_init__overrides_default_samplers__with_base_MC_acq_func(self): | ||
problem = SimpleProblem() | ||
X = torch.as_tensor([-2.61, -1.92, -0.63, 0.38, 2], device="cpu").unsqueeze(-1) | ||
Y = problem(X) | ||
mdl = Rbf(X, Y, 0.5) | ||
fantasies_samplers = [GaussHermiteSampler(torch.Size([1]))] | ||
valfunc_sampler = GaussHermiteSampler(torch.Size([16])) | ||
|
||
acqfun = Ms( | ||
mdl, | ||
fantasies_samplers, | ||
qExpectedImprovement, | ||
valfunc_sampler=valfunc_sampler, | ||
) | ||
|
||
self.assertTrue(all(s is valfunc_sampler for s in acqfun.inner_samplers)) | ||
|
||
def test_init__does_not_override_default_samplers__with_base_analytical_acq_func( | ||
self, | ||
): | ||
problem = SimpleProblem() | ||
X = torch.as_tensor([-2.61, -1.92, -0.63, 0.38, 2], device="cpu").unsqueeze(-1) | ||
Y = problem(X) | ||
mdl = Rbf(X, Y, 0.5) | ||
fantasies_samplers = [GaussHermiteSampler(torch.Size([1]))] | ||
valfunc_sampler = GaussHermiteSampler(torch.Size([16])) | ||
|
||
acqfun = Ms( | ||
mdl, | ||
fantasies_samplers, | ||
ExpectedImprovement, | ||
valfunc_sampler=valfunc_sampler, | ||
) | ||
|
||
self.assertTrue(all(s is None for s in acqfun.inner_samplers)) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import unittest | ||
from math import pi, sqrt | ||
|
||
import torch | ||
from botorch.posteriors import GPyTorchPosterior | ||
from gpytorch.distributions import MultivariateNormal | ||
|
||
from globopt import GaussHermiteSampler | ||
|
||
|
||
class TestSampling(unittest.TestCase): | ||
def test_GH__supports_only_single_dim(self) -> None: | ||
with self.assertRaisesRegex( | ||
AssertionError, "Only a single dimension is supported." | ||
): | ||
GaussHermiteSampler(torch.Size([1000, 100])) | ||
|
||
def test_GH__returns_correct_base_samples(self) -> None: | ||
# with such posterior, the base samples are returned as they are, i.e., 1*s + 0 | ||
distribution = MultivariateNormal(torch.zeros((1,)), torch.eye(1)) | ||
posterior = GPyTorchPosterior(distribution) | ||
SQRT2 = sqrt(2.0) | ||
SQRTPI = sqrt(pi) | ||
EXPECTED = { | ||
2: ([-0.707107, 0.707107], [0.886227, 0.886227]), | ||
3: ([-1.22474, 0, 1.22474], [0.295409, 1.18164, 0.295409]), | ||
4: ( | ||
[-1.65068, -0.524648, 0.524648, 1.65068], | ||
[0.0813128, 0.804914, 0.804914, 0.0813128], | ||
), | ||
5: ( | ||
[-2.02018, -0.958572, 0, 0.958572, 2.02018], | ||
[0.0199532, 0.393619, 0.945309, 0.393619, 0.0199532], | ||
), | ||
} | ||
|
||
for n, (samples, weights) in EXPECTED.items(): | ||
sampler = GaussHermiteSampler(torch.Size([n])) | ||
actual_samples = sampler(posterior) | ||
torch.testing.assert_close( | ||
actual_samples.flatten(), torch.tensor(samples) * SQRT2, msg=f"{n}" | ||
) | ||
torch.testing.assert_close( | ||
sampler.base_weights.flatten(), | ||
torch.tensor(weights) / SQRTPI, | ||
msg=f"{n}", | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |