-
Notifications
You must be signed in to change notification settings - Fork 0
/
trial.jl
372 lines (304 loc) · 9.04 KB
/
trial.jl
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
Base.@kwdef mutable struct Trial{I}
values::Dict = Dict()
value_id::Int = 1
fval::Union{Float64, Vector{Float64}} = Inf
instance::I = 0
instance_id::Int = 0
seed::Int = 1
record::Vector = []
pruned::Bool = false
success::Bool = false
time_eval::Float64 = 0.0
_pruner::AbstractPruner = NeverPrune()
end
"""
get_instance(trial)
Get instance problem which trials has to be evaluated.
"""
get_instance(trial::Trial) = trial.instance
get_seed(trial::Trial) = trial.seed
"""
report_success!(trial)
Report that the trial successfully solved the instance.
"""
report_success!(trial::Trial) = (trial.success = true)
"""
report_value!(trial, value)
Report (to the pruner) evaluated value at trail.
"""
report_value!(trial::Trial, val) = push!(trial.record, val)
"""
should_prune(trial)
Check whether trial should be pruned.
"""
function should_prune(trial::Trial)
step = length(trial.record)
if step == 0
return false
end
val = last(trial.record)
instance_id = trial.instance_id
pruned = should_prune(trial._pruner, step, instance_id, val)
trial.pruned = pruned
pruned
end
"""
GroupedTrial
Trials grouped per instance.
"""
struct GroupedTrial
trials::Vector{Trial}
values::Dict
id::Int
performance::Float64
count_success::Int
pruned::Bool
time_eval::Float64
end
function GroupedTrial(trials::Vector{T}) where T <: Trial
if isempty(trials)
return GroupedTrial([Trial()])
end
performance = trial_performance(trials)
counter = count_success(trials)
pruned = all(t.pruned for t in trials)
values = first(trials).values
value_id = first(trials).value_id
t = sum(trial.time_eval for trial in trials)
GroupedTrial(trials,
values,
value_id,
performance,
counter,
pruned,
t)
end
function getobjectives(trial::GroupedTrial)
f1 = -count_success(trial)
f2 = trial.performance
f3 = trial.time_eval
f1, f2, f3
end
function lexicographic_coparison(Fa::Tuple, Fb::Tuple)
for (a, b) in zip(Fa, Fb)
if a < b
return true
elseif a > b
return false
end
end
true
end
function compare(a::Tuple, b::Tuple)
k = length(a)
if k != length(b)
return 3
end
i = 1
while i <= k && a[i] == b[i]
i += 1
end
if i > k
return 0 # equals
end
if a[i] < b[i]
for j = i+1:k
if b[j] < a[j]
return 3 #a and b are incomparable
end
end
return 1 # a dominates b
end
for j = i+1:k
if a[j] < b[j]
return 3 # a and b are incomparable
end
end
return 2 # b dominates a
end
function isbetter(ga::GroupedTrial, gb::GroupedTrial)
lexicographic_coparison(getobjectives(ga), getobjectives(gb))
end
function findtradeoffs(Fs)
mask = [1]
n = length(Fs)
for i in 2:n
j = 1
while j <= length(mask)
relation = compare(Fs[i], Fs[mask[j]])
if relation == 2 # j dominates i
break
elseif relation == 1 # i dominates j
deleteat!(mask, j)
continue
end
j += 1
end
if j > length(mask)
push!(mask, i)
end
end
return mask
end
function Base.show(io::IO, trial::GroupedTrial)
if isempty(trial.trials)
println(io, "GroupedTrial - Empty")
return
end
h = (["Trial", "Value"], [trial.id, ""])
_trial = first(trial.trials)
ks = sort(keys(_trial.values) |> collect)
v = [_trial.values[k] for k in ks]
parameters = Any[ks v]
data = parameters
if length(trial.trials) == 1
data = vcat(
data,
["Pruned" _trial.pruned],
["Success" _trial.success],
["Objective" _trial.fval],
)
else
data = vcat(data, Any["Instance" ""])
vals = [t.fval for t in trial.trials]
labs = [t.instance for t in trial.trials]
# success = [t.success for t in trial.trials]
# pruned = [t.pruned for t in trial.trials]
data = vcat(data, Any[labs vals])
end
PrettyTables.pretty_table(io, data, header=h)
end
function trials_to_table(io, trials::Array{<:GroupedTrial})
if isempty(trials)
return PrettyTables.pretty_table(io, zeros(0,0))
end
ks = collect(keys(first(trials).trials[1].values)) |> collect |> sort
parameters = Any[t.trials[1].values[k] for t in trials, k in ks]
ids = [t.id for t in trials]
nsuccess = [t.count_success for t in trials]
pruned = [t.pruned for t in trials]
obj = length(first(trials).trials) > 1 ? "Mean" : "Objective"
ttime = [t.time_eval for t in trials]
means = [t.performance for t in trials]
data = hcat(ids, parameters, nsuccess, means, ttime, pruned)
h = vcat("ID", ks, "Success", obj, "Time", "Pruned")
mask = sortperm(trials, lt = isbetter, alg=InsertionSort)
data = data[mask, :]
PrettyTables.pretty_table(io, data, header=h)
end
function Base.show(io::IO, m::MIME"text/plain", trials::Array{<:GroupedTrial})
if length(trials) == 1
return Base.show(io, m, first(trials))
end
println(io, "PARAMETERS:")
trials_to_table(io, trials)
end
"""
best_parameters(scenario)
Return best parameters saved in scenario.
"""
function best_parameters(scenario)
return scenario.best_trial
end
"""
top_parameters(scenario; ignore_pruned)
Return an array of trade-off trials (regarding success, mean objective value, time, etc).
"""
function top_parameters(scenario; ignore_pruned=true)
if ignore_pruned
hs = [trial for trial in history(scenario) if !trial.pruned]
else
hs = history(scenario)
end
if isempty(hs)
return GroupedTrial[]
end
mask = getobjectives.(hs) |> findtradeoffs
hs[mask]
end
function group_trials_by_instance(trials::Vector{<:Trial}, instances)
if length(instances) <= 1
return [GroupedTrial([trial]) for trial in trials]
end
trials = copy(trials)
grouped_trials = GroupedTrial[]
value_id = unique([t.value_id for t in trials])
for i in value_id
indices = findall(t -> t.value_id==i, trials)
if isempty(indices)
continue
end
push!(grouped_trials, GroupedTrial(trials[indices]))
deleteat!(trials, indices)
end
grouped_trials
end
function get_fvals(trials::AbstractVector{<:Trial})
[t.fval for t in trials]
end
function get_fvals(trial::GroupedTrial)
get_fvals(trial.trials)
end
"""
StatusHyperTuning
Current status of the optimize process for scenario.
"""
Base.@kwdef mutable struct StatusHyperTuning
history::Vector{GroupedTrial} = GroupedTrial[]
f_evals::Int = 0
n_trials::Int = 0
start_time::Float64 = time()
stop::Bool = false
stop_reason::AbstractStop = NotOptimized()
end
history(status::StatusHyperTuning) = status.history
function get_convergence(status::StatusHyperTuning, only_performance=true)
hs = history(status)
best = hs[1]
[
begin
best = best.performance >= h.performance ? h : best
only_performance ? (step, best.performance) : (step, best)
end
for (step, h) in enumerate(hs) if best.performance > h.performance
]
end
function trial_performance(trial::AbstractVector{<:Trial})
if isempty(trial)
return Inf
end
if length(trial) == 1
return first(get_fvals(trial))
end
# TODO improve this
return sts.mean(get_fvals(trial))
end
function trial_performance(trial::GroupedTrial)
trial.performance
end
UnPack.unpack(trial::Trial, ::Val{k}) where {k} = trial.values[k]
count_success(trials::Vector{<:Trial}) = count(t.success for t in trials)
count_success(trial::GroupedTrial) = count_success(trial.trials)
allsucceeded(grouped::GroupedTrial) = length(grouped.trials) > 0 && count_success(grouped) == length(grouped.trials)
function Base.show(io::IO, trial::Trial)
if trial.pruned
step = length(trial.record)
printstyled(io, "[-] Trial ", trial.value_id, " pruned in step ", step," at instance ", trial.instance_id, "\n", color=:light_black)
else
c = trial.success ? :green : :default
m = trial.success ? "[*]" : "[+]"
printstyled(io, m, " Trial ", trial.value_id, " evaluated ", trial.fval, " at instance ", trial.instance_id, "\n", color = c)
end
end
function print_trial(trial::Trial)
p = collect(trial.values)
sort!(p, by=first)
if trial.pruned
step = length(trial.record)
printstyled("[-] Trial ", trial.value_id,": ", p, " pruned in step ", step," at instance ", trial.instance_id, "\n", color=:light_black)
else
c = trial.success ? :green : :default
m = trial.success ? "[*]" : "[+]"
printstyled(m, " Trial ", trial.value_id, ": ", p, " evaluated ", trial.fval, " at instance ", trial.instance_id, "\n", color = c)
end
end