-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimize.jl
92 lines (69 loc) · 2.03 KB
/
optimize.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
include("evaluator/evaluate_objective.jl")
function before_evaluation!(scenario::Scenario)
scenario
end
function after_evaluation!(scenario::Scenario)
update_pruner!(scenario.pruner, scenario.status.history, length(scenario.instances))
scenario
end
"""
optimize!(f, scenario)
Perform an iteration of the optimization process.
When this function is called, `scenario.batch_size` trials are sampled and evaluated,
then update the sampler and pruner.
See also [`optimize`](@ref)
"""
function optimize!(f::Function, scenario::Scenario)
# prepare some required stuff
before_evaluation!(scenario)
# evaluate trails
evaluate_objective(f, scenario)
# report f values to samplers for further decisions
after_evaluation!(scenario)
end
"""
optimize(f, scenario)
Optimize f on provided scenario while the budget is not exceeded
(limited by max_trials, max_evals, etc).
See also [`optimize!`](@ref)
"""
function optimize(f::Function, scenario::Scenario)
while !default_stop_criteria(scenario)
optimize!(f, scenario)
end
scenario
end
"""
budget_exceeded(scenario)
Check whether if budget is not exceeded.
"""
function budget_exceeded(scenario::Scenario)
status = scenario.status
if scenario.status.stop
# nothing to do
return true
end
if status.n_trials >= scenario.budget.max_trials
status.stop = true
status.stop_reason = BudgetExceeded("Due to max_trials")
elseif status.f_evals >= scenario.budget.max_evals
status.stop = true
status.stop_reason = BudgetExceeded("Due to max_evals")
end
status.stop
end
"""
all_instances_succeeded(scenario)
Check if all instances are successfully solved.
See also [`report_success`](@ref)
"""
function all_instances_succeeded(scenario::Scenario)
status = scenario.status
if scenario.status.stop
# nothing to do
return true
end
scenario.status.stop = allsucceeded(scenario)
status.stop_reason = AllInstancesSucceeded()
scenario.status.stop
end