-
Notifications
You must be signed in to change notification settings - Fork 0
/
suggest.jl
84 lines (65 loc) · 2.21 KB
/
suggest.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
function _suggest(var::Symbol, ::Nothing, trial::Trial)
return trial.values[var]
end
function _suggest(var::Symbol, ::Nothing, scenario::Scenario)
return scenario.best_trial.values[var]
end
#=
function get_search_space(scenario::Scenario, key)
scenario.parameters.domain[key]
end
function _suggest(var::Symbol, ::Nothing, scenario::Scenario)
if var in keys(scenario.parameters.domain)
searchspace = get_search_space(scenario, var)
return SearchSpaces.value(SearchSpaces.Sampler(scenario.sampler, searchspace))
end
throw(ErrorException("$var not defined in scenario."))
end
function _register!(var::Symbol, searchspace, scenario::Scenario)
scenario.parameters.domain[var] = searchspace
end
function _suggest(var::Symbol,searchspace::AbstractSearchSpace,scenario::Scenario)
# TODO improve performance finding var in keys
if var in keys(scenario.parameters.domain)
# TODO compare with saved search space
searchspace = get_search_space(scenario, var)
return SearchSpaces.value(SearchSpaces.Sampler(scenario.sampler, searchspace))
end
_register!(var, searchspace, scenario)
SearchSpaces.value(SearchSpaces.Sampler(scenario.sampler, searchspace))
end
=#
function _pre_proc(ex::Expr)
#=
if length(ex.args) == 2 # scenario(searchspace)
return ex.args[1], ex.args[2]
end
=#
error("Expression needs to be of form `x in trial`")
end
function _pre_proc(scenario::Symbol)
return scenario, nothing
end
function _exec_suggest(var_name::Symbol, _scenario::Symbol, _searchspace)
quote
local searchspace = $(esc(_searchspace))
local scenario = $(esc(_scenario))
local v = $(QuoteNode(var_name))
$(esc(var_name)) = _suggest(v, searchspace, scenario)
end
end
function _suggest_single(ex::Expr)
var_name = ex.args[2]
_scenario, _searchspace = _pre_proc(ex.args[3])
_exec_suggest(var_name, _scenario, _searchspace)
end
"""
@suggest x in trial
Return a value for x stored in the sampled trial.
"""
macro suggest(ex::Expr)
if length(ex.args) == 3 && ex.args[1] ∈ [:in, :∈]
return _suggest_single(ex)
end
error("Expression needs to be of form `x in trial`")
end