forked from optuna/optuna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_multi_objective.py
72 lines (53 loc) · 2.86 KB
/
test_multi_objective.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
from typing import Tuple
from optuna import create_study
from optuna.study._multi_objective import _get_pareto_front_trials_2d
from optuna.study._multi_objective import _get_pareto_front_trials_nd
from optuna.trial import FrozenTrial
def _trial_to_values(t: FrozenTrial) -> Tuple[float, ...]:
assert t.values is not None
return tuple(t.values)
def test_get_pareto_front_trials_2d() -> None:
study = create_study(directions=["minimize", "maximize"])
assert {_trial_to_values(t) for t in _get_pareto_front_trials_2d(study)} == set()
study.optimize(lambda t: [2, 2], n_trials=1)
assert {_trial_to_values(t) for t in _get_pareto_front_trials_2d(study)} == {(2, 2)}
study.optimize(lambda t: [1, 1], n_trials=1)
assert {_trial_to_values(t) for t in _get_pareto_front_trials_2d(study)} == {(1, 1), (2, 2)}
study.optimize(lambda t: [3, 1], n_trials=1)
assert {_trial_to_values(t) for t in _get_pareto_front_trials_2d(study)} == {(1, 1), (2, 2)}
study.optimize(lambda t: [3, 2], n_trials=1)
assert {_trial_to_values(t) for t in _get_pareto_front_trials_2d(study)} == {(1, 1), (2, 2)}
study.optimize(lambda t: [1, 3], n_trials=1)
assert {_trial_to_values(t) for t in _get_pareto_front_trials_2d(study)} == {(1, 3)}
assert len(_get_pareto_front_trials_2d(study)) == 1
study.optimize(lambda t: [1, 3], n_trials=1) # The trial result is the same as the above one.
assert {_trial_to_values(t) for t in _get_pareto_front_trials_2d(study)} == {(1, 3)}
assert len(_get_pareto_front_trials_2d(study)) == 2
def test_get_pareto_front_trials_nd() -> None:
study = create_study(directions=["minimize", "maximize", "minimize"])
assert {_trial_to_values(t) for t in _get_pareto_front_trials_nd(study)} == set()
study.optimize(lambda t: [2, 2, 2], n_trials=1)
assert {_trial_to_values(t) for t in _get_pareto_front_trials_nd(study)} == {(2, 2, 2)}
study.optimize(lambda t: [1, 1, 1], n_trials=1)
assert {_trial_to_values(t) for t in _get_pareto_front_trials_nd(study)} == {
(1, 1, 1),
(2, 2, 2),
}
study.optimize(lambda t: [3, 1, 3], n_trials=1)
assert {_trial_to_values(t) for t in _get_pareto_front_trials_nd(study)} == {
(1, 1, 1),
(2, 2, 2),
}
study.optimize(lambda t: [3, 2, 3], n_trials=1)
assert {_trial_to_values(t) for t in _get_pareto_front_trials_nd(study)} == {
(1, 1, 1),
(2, 2, 2),
}
study.optimize(lambda t: [1, 3, 1], n_trials=1)
assert {_trial_to_values(t) for t in _get_pareto_front_trials_nd(study)} == {(1, 3, 1)}
assert len(_get_pareto_front_trials_nd(study)) == 1
study.optimize(
lambda t: [1, 3, 1], n_trials=1
) # The trial result is the same as the above one.
assert {_trial_to_values(t) for t in _get_pareto_front_trials_nd(study)} == {(1, 3, 1)}
assert len(_get_pareto_front_trials_nd(study)) == 2