forked from optuna/optuna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_multi_objective.py
137 lines (114 loc) · 4.95 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
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
from __future__ import annotations
from optuna import create_study
from optuna import trial
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.trials, study.directions)
} == set()
study.optimize(lambda t: [2, 2], n_trials=1)
assert {
_trial_to_values(t) for t in _get_pareto_front_trials_2d(study.trials, study.directions)
} == {(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.trials, study.directions)
} == {(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.trials, study.directions)
} == {(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.trials, study.directions)
} == {(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.trials, study.directions)
} == {(1, 3)}
assert len(_get_pareto_front_trials_2d(study.trials, study.directions)) == 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.trials, study.directions)
} == {(1, 3)}
assert len(_get_pareto_front_trials_2d(study.trials, study.directions)) == 2
def test_get_pareto_front_trials_2d_with_constraint() -> None:
study = create_study(directions=["minimize", "maximize"])
trials = [
trial.create_trial(values=[1, 1], system_attrs={"constraints": [0]}),
trial.create_trial(values=[2, 2], system_attrs={"constraints": [1]}),
trial.create_trial(values=[3, 2], system_attrs={"constraints": [0]}),
]
study.add_trials(trials)
assert {
_trial_to_values(t)
for t in _get_pareto_front_trials_2d(study.trials, study.directions, False)
} == {(1, 1), (2, 2)}
assert {
_trial_to_values(t)
for t in _get_pareto_front_trials_2d(study.trials, study.directions, True)
} == {(1, 1), (3, 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.trials, study.directions)
} == 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.trials, study.directions)
} == {(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.trials, study.directions)
} == {
(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.trials, study.directions)
} == {
(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.trials, study.directions)
} == {
(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.trials, study.directions)
} == {(1, 3, 1)}
assert len(_get_pareto_front_trials_nd(study.trials, study.directions)) == 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.trials, study.directions)
} == {(1, 3, 1)}
assert len(_get_pareto_front_trials_nd(study.trials, study.directions)) == 2
def test_get_pareto_front_trials_nd_with_constraint() -> None:
study = create_study(directions=["minimize", "maximize", "minimize"])
trials = [
trial.create_trial(values=[1, 1, 1], system_attrs={"constraints": [0]}),
trial.create_trial(values=[2, 2, 2], system_attrs={"constraints": [1]}),
trial.create_trial(values=[3, 2, 3], system_attrs={"constraints": [0]}),
]
study.add_trials(trials)
assert {
_trial_to_values(t)
for t in _get_pareto_front_trials_nd(study.trials, study.directions, False)
} == {(1, 1, 1), (2, 2, 2)}
assert {
_trial_to_values(t)
for t in _get_pareto_front_trials_nd(study.trials, study.directions, True)
} == {(1, 1, 1), (3, 2, 3)}