-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #306 from novonordisk-research/develop
DOE branch up to date
- Loading branch information
Showing
24 changed files
with
1,372 additions
and
12 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
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
Empty file.
62 changes: 62 additions & 0 deletions
62
ProcessOptimizer/tests/test_suggestors/test_lhs_suggestor.py
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,62 @@ | ||
import numpy as np | ||
import pytest | ||
from ProcessOptimizer.space import space_factory | ||
from XpyriMentor.suggestors import LHSSuggestor, Suggestor, suggestor_factory, IncompatibleNumberAsked | ||
|
||
|
||
def test_initializaton(): | ||
suggestor = LHSSuggestor( | ||
space=space_factory([[1, 2], [1, 2]]), | ||
rng=np.random.default_rng(1), | ||
) | ||
assert isinstance(suggestor, Suggestor) | ||
assert suggestor.n_points == 5 | ||
assert len(suggestor.cache) == 5 | ||
|
||
|
||
def test_factory(): | ||
space = space_factory([[1, 2], [1, 2]]) | ||
suggestor = suggestor_factory( | ||
space=space, | ||
definition={"suggestor_name": "LHS", "n_points": 10}, | ||
) | ||
assert isinstance(suggestor, LHSSuggestor) | ||
assert suggestor.n_points == 10 | ||
|
||
|
||
def test_suggest(): | ||
space = space_factory([[0, 10], [0.0, 1.0], ["cat", "dog"]]) | ||
suggestor = LHSSuggestor( | ||
space, rng=np.random.default_rng(1), n_points=5 | ||
) | ||
suggestions = suggestor.suggest([], []) | ||
assert len(suggestions) == 1 | ||
assert len(suggestions[0]) == 3 | ||
assert suggestions[0] in space | ||
suggestions = suggestor.suggest([], [], n_asked=5) | ||
# Testing that the values for each dimension is regularly spaced over the range | ||
assert set(suggestion[0] for suggestion in suggestions) == {1, 3, 5, 7, 9} | ||
assert set(suggestion[1] for suggestion in suggestions) == {0.1, 0.3, 0.5, 0.7, 0.9} | ||
assert set(suggestion[2] for suggestion in suggestions) == {"cat", "dog"} | ||
|
||
|
||
def test_suggest_too_many(): | ||
space = space_factory([[0, 10], [0.0, 1.0], ["cat", "dog"]]) | ||
suggestor = LHSSuggestor( | ||
space, rng=np.random.default_rng(1), n_points=5 | ||
) | ||
for n_told in range(1, 5): | ||
told = suggestor.suggest([], [], n_asked=n_told) | ||
suggestor.suggest(told, [0]*n_told, n_asked=1) | ||
with pytest.raises(IncompatibleNumberAsked): | ||
suggestor.suggest([], [], n_asked=6) | ||
with pytest.raises(IncompatibleNumberAsked): | ||
suggestor.suggest([[1, 0.0, "cat"]], [1], n_asked=5) | ||
|
||
|
||
def test_n(): | ||
space = space_factory([[0, 10], [0.0, 1.0], ["cat", "dog"]]) | ||
for n in range(1, 10): | ||
suggestor = LHSSuggestor(space, rng=np.random.default_rng(1), n_points=n) | ||
assert suggestor.n_points == n | ||
assert len(suggestor.cache) == n |
37 changes: 37 additions & 0 deletions
37
ProcessOptimizer/tests/test_suggestors/test_po_suggestor.py
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,37 @@ | ||
import numpy as np | ||
from XpyriMentor.suggestors import POSuggestor, suggestor_factory | ||
from ProcessOptimizer.space import space_factory | ||
|
||
|
||
def test_initialization(): | ||
space = space_factory([[0, 1], [0, 1]]) | ||
suggestor = POSuggestor(space, rng=np.random.default_rng(1)) | ||
assert isinstance(suggestor, POSuggestor) | ||
assert suggestor.optimizer._n_initial_points == 0 | ||
nonstandard_suggestor = POSuggestor( | ||
space, rng=np.random.default_rng(1), n_initial_points=5 | ||
) | ||
assert nonstandard_suggestor.optimizer._n_initial_points == 5 | ||
|
||
|
||
def test_factory(): | ||
space = space_factory([[0, 1], [0, 1]]) | ||
suggestor = suggestor_factory( | ||
space=space, | ||
definition={"suggestor_name": "PO"}, | ||
) | ||
assert isinstance(suggestor, POSuggestor) | ||
assert suggestor.optimizer._n_initial_points == 0 | ||
|
||
|
||
def test_suggest(): | ||
space = space_factory([[0, 1], [0, 1]]) | ||
suggestor = POSuggestor(space, rng=np.random.default_rng(1)) | ||
suggestions = suggestor.suggest([[1, 1]], [1]) | ||
assert len(suggestions) == 1 | ||
assert len(suggestions[0]) == 2 | ||
assert suggestions[0] in space | ||
suggestions = suggestor.suggest([[1, 1]], [1], n_asked=5) | ||
assert len(suggestions) == 5 | ||
for suggestion in suggestions: | ||
assert suggestion in space |
125 changes: 125 additions & 0 deletions
125
ProcessOptimizer/tests/test_suggestors/test_random_strategizer.py
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,125 @@ | ||
import numpy as np | ||
import pytest | ||
import warnings | ||
from XpyriMentor.suggestors import ( | ||
RandomStragegizer, | ||
Suggestor, | ||
suggestor_factory, | ||
POSuggestor, | ||
LHSSuggestor, | ||
DefaultSuggestor | ||
) | ||
from XpyriMentor.suggestors.default_suggestor import NoDefaultSuggestorError | ||
from ProcessOptimizer.space import space_factory | ||
|
||
|
||
class MockSuggestor: | ||
def __init__(self, suggestions: list): | ||
self.suggestions = suggestions | ||
self.last_input = {} | ||
|
||
def suggest(self, Xi, Yi, n_asked=1): | ||
self.last_input = {"Xi": Xi, "Yi": Yi} | ||
return self.suggestions*n_asked | ||
|
||
|
||
def test_random_strategizer(): | ||
suggestor = RandomStragegizer( | ||
suggestors=[(0.8, MockSuggestor([[1]])), (0.2, MockSuggestor([[2]]))], | ||
rng=np.random.default_rng(1) | ||
) | ||
assert isinstance(suggestor, Suggestor) | ||
# np.random.default_rng(1).random() gives 0.5118216247148916, 0.9504636963259353, | ||
# and 0.14415961271963373 on the first three calls, so the first three calls | ||
# should return the suggestors with weights 0.8, 0.2, and 0.8, respectively. | ||
assert suggestor.suggest([], []) == [[1]] | ||
assert suggestor.suggest([], []) == [[2]] | ||
assert suggestor.suggest([], []) == [[1]] | ||
|
||
|
||
def test_factory(): | ||
space = space_factory([[0, 1], [0, 1]]) | ||
suggestor = suggestor_factory( | ||
space=space, | ||
definition={"suggestor_name": "Random", "suggestors": [ | ||
{"suggestor_usage_ratio": 0.8, "suggestor_name": "PO"}, | ||
{"suggestor_usage_ratio": 0.2, "suggestor_name": "LHS"},]}, | ||
) | ||
assert isinstance(suggestor, RandomStragegizer) | ||
assert len(suggestor.suggestors) == 2 | ||
assert suggestor.suggestors[0][0] == 0.8 | ||
assert suggestor.suggestors[1][0] == 0.2 | ||
assert isinstance(suggestor.suggestors[0][1], POSuggestor) | ||
assert isinstance(suggestor.suggestors[1][1], LHSSuggestor) | ||
|
||
|
||
def test_random_multiple_ask(): | ||
suggestor = RandomStragegizer( | ||
suggestors=[(0.8, MockSuggestor([[1]])), (0.2, MockSuggestor([[2]]))], | ||
rng=np.random.default_rng(1) | ||
) | ||
assert all(suggestor.suggest([], [], n_asked=2) == [[1], [2]]) | ||
assert all(suggestor.suggest([], [], n_asked=3) == [[1], [1], [2]]) | ||
|
||
|
||
def test_default_suggestor(): | ||
with pytest.raises(NoDefaultSuggestorError): | ||
RandomStragegizer( | ||
suggestors=[ | ||
(0.8, MockSuggestor([[1]])), | ||
(0.2, DefaultSuggestor(space=[], n_objectives=1, rng=None)) | ||
], | ||
rng=np.random.default_rng(1) | ||
) | ||
|
||
|
||
def test_wrong_sum(): | ||
with pytest.warns(UserWarning): | ||
# Warning if the sum of usage ratios is not 1 or 100 | ||
RandomStragegizer( | ||
suggestors=[(0.8, MockSuggestor([[1]])), (0.3, MockSuggestor([[2]]))], | ||
rng=np.random.default_rng(1) | ||
) | ||
with warnings.catch_warnings(): | ||
warnings.simplefilter("error") | ||
# No warnings if the sum of usage ratios is 1 | ||
RandomStragegizer( | ||
suggestors=[(0.8, MockSuggestor([[1]])), (0.2, MockSuggestor([[2]]))], | ||
rng=np.random.default_rng(1) | ||
) | ||
# No warnings if the sum of usage ratios is 100 | ||
RandomStragegizer( | ||
suggestors=[(80, MockSuggestor([[1]])), (20, MockSuggestor([[2]]))], | ||
rng=np.random.default_rng(1) | ||
) | ||
|
||
|
||
def test_random_with_suggestor_given(): | ||
space = space_factory([[0, 1], [0, 1]]) | ||
suggestor = suggestor_factory( | ||
space=space, | ||
definition={"suggestor_name": "Random", "suggestors": [ | ||
{"suggestor_usage_ratio": 0.8, "suggestor": MockSuggestor([[1]])}, | ||
{"suggestor_usage_ratio": 0.2, "suggestor": MockSuggestor([[2]])},]}, | ||
) | ||
assert isinstance(suggestor, RandomStragegizer) | ||
assert len(suggestor.suggestors) == 2 | ||
assert suggestor.suggestors[0][0] == 0.8 | ||
assert suggestor.suggestors[1][0] == 0.2 | ||
assert isinstance(suggestor.suggestors[0][1], MockSuggestor) | ||
assert isinstance(suggestor.suggestors[1][1], MockSuggestor) | ||
|
||
|
||
def test_random_with_suggestor_given_wrong_keys(): | ||
space = space_factory([[0, 1], [0, 1]]) | ||
with pytest.raises(ValueError): | ||
suggestor_factory( | ||
space=space, | ||
definition={"name": "Random", "suggestors": [ | ||
{"suggestor_usage_ratio": 0.8, "suggestor": MockSuggestor([[1]])}, | ||
{ | ||
"suggestor_usage_ratio": 0.2, | ||
"suggestor": MockSuggestor([[2]]), | ||
"additional_key": "Can't have this key", | ||
},]}, | ||
) |
Oops, something went wrong.