-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_search_space.py
142 lines (115 loc) · 5.17 KB
/
test_search_space.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# Black-box ABM Calibration Kit (Black-it)
# Copyright (C) 2021-2024 Banca d'Italia
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""This module contains tests for the black_it.SearchSpace class."""
from __future__ import annotations
import numpy as np
import pytest
from black_it.search_space import (
BadPrecisionLengthError,
BoundsNotOfSizeTwoError,
BoundsOfDifferentLengthError,
LowerBoundGreaterThanUpperBoundError,
PrecisionGreaterThanBoundsRangeError,
PrecisionZeroError,
SameLowerAndUpperBoundError,
SearchSpace,
)
@pytest.mark.parametrize(
("lower", "upper", "precision", "expected_num_params", "expected_cardinality"),
[
pytest.param([0], [1], [0.5], 1, 3, id="single_param"),
pytest.param([0], [100], [1], 1, 101, id="show_that_size_is_off_by_one"),
pytest.param([0, 0, 0], [10, 20, 30], [0.1, 1, 5], 3, 14847, id="three_params"),
],
)
def test_search_space_successful(
lower: list[float],
upper: list[float],
precision: list[float],
expected_num_params: int,
expected_cardinality: int,
) -> None:
"""Test successful construction of the SearchSpace object."""
search_space = SearchSpace(np.array([lower, upper]), np.array(precision), True)
assert search_space.dims == expected_num_params
assert search_space.space_size == expected_cardinality
def test_search_space_fails_bounds_not_of_size_two() -> None:
"""Failed construction of SearchSpace due to BoundsNotOfSizeTwoError."""
bounds = [[], [], [3]]
precision = [0.1, 1, 0.1]
with pytest.raises(BoundsNotOfSizeTwoError) as exc_info:
_ = SearchSpace(np.array(bounds, dtype=object), np.array(precision), True)
assert exc_info.value.count_bounds_subarrays == len(bounds)
def test_search_space_fails_bounds_of_different_length() -> None:
"""Failed construction of SearchSpace due to BoundsOfDifferentLengthError."""
lower = [0, 0]
upper = [10]
precision = [0.1, 1]
with pytest.raises(BoundsOfDifferentLengthError) as exc_info:
_ = SearchSpace(
np.array([lower, upper], dtype=object),
np.array(precision),
True,
)
assert exc_info.value.lower_bounds_length == len(lower)
assert exc_info.value.upper_bounds_length == len(upper)
def test_search_space_fails_bad_precision_length() -> None:
"""Failed construction of SearchSpace due to BadPrecisionLengthError."""
lower = [0, 0]
upper = [10, 0]
precision = [0.1]
with pytest.raises(BadPrecisionLengthError) as exc_info:
_ = SearchSpace(np.array([lower, upper]), np.array(precision), True)
assert exc_info.value.bounds_length == len(lower)
assert exc_info.value.precisions_length == len(precision)
def test_search_space_fails_same_lower_and_upper_bound() -> None:
"""Failed construction of SearchSpace due to SameLowerAndUpperBoundError."""
lower = [0, 5]
upper = [10, 5]
precision = [0.1, 0.1]
with pytest.raises(SameLowerAndUpperBoundError) as exc_info:
_ = SearchSpace(np.array([lower, upper]), np.array(precision), True)
assert exc_info.value.param_index == 1
assert exc_info.value.bound_value == lower[1]
def test_search_space_fails_lower_bound_greater_than_upper_bound() -> None:
"""Failed construction of SearchSpace due to LowerBoundGreaterThanUpperBoundError."""
lower = [0, 15]
upper = [10, 2]
precision = [0.1, 0.1]
with pytest.raises(LowerBoundGreaterThanUpperBoundError) as exc_info:
_ = SearchSpace(np.array([lower, upper]), np.array(precision), True)
assert exc_info.value.param_index == 1
assert exc_info.value.lower_bound == lower[1]
assert exc_info.value.upper_bound == upper[1]
def test_search_space_fails_precision_cant_be_zero() -> None:
"""Failed construction of SearchSpace due to PrecisionZeroError."""
lower = [0, 0]
upper = [10, 10]
precision = [1, 0]
with pytest.raises(PrecisionZeroError) as exc_info:
_ = SearchSpace(np.array([lower, upper]), np.array(precision), True)
assert exc_info.value.param_index == 1
def test_search_space_fails_precision_greater_than_bounds_range() -> None:
"""Failed construction of SearchSpace due to PrecisionGreaterThanBoundsRangeError."""
lower = [0]
upper = [10]
precision = [10.1]
with pytest.raises(PrecisionGreaterThanBoundsRangeError) as exc_info:
_ = SearchSpace(np.array([lower, upper]), np.array(precision), True)
assert exc_info.value.param_index == 0
assert exc_info.value.lower_bound == lower[0]
assert exc_info.value.upper_bound == upper[0]
assert exc_info.value.precision == precision[0]