|
1 | | -export @E_cmd, Experiment |
| 1 | +export Experiment |
2 | 2 |
|
3 | | - |
4 | | -import Parsers |
5 | | - |
6 | | -macro E_cmd(s) |
7 | | - Experiment(s) |
8 | | -end |
9 | | - |
10 | | -function try_parse(s, TS=(Bool, Int, Float32, Float64)) |
11 | | - if s == "nothing" |
12 | | - nothing |
13 | | - else |
14 | | - for T in TS |
15 | | - res = Parsers.tryparse(T, s) |
16 | | - if !isnothing(res) |
17 | | - return res |
18 | | - end |
19 | | - end |
20 | | - s |
21 | | - end |
22 | | -end |
23 | | - |
24 | | -function try_parse_kw(s) |
25 | | - kw = [] |
26 | | - # !!! obviously, it's not correct when a value is string and contains "," |
27 | | - for part in split(s, ",") |
28 | | - kv = split(part, "=") |
29 | | - @assert length(kv) == 2 |
30 | | - k, v = kv |
31 | | - push!(kw, Symbol(strip(k)) => try_parse(strip(v))) |
32 | | - end |
33 | | - NamedTuple(kw) |
34 | | -end |
35 | | - |
36 | | -struct Experiment{S} |
37 | | - policy::Any |
38 | | - env::Any |
39 | | - stop_condition::Any |
40 | | - hook::Any |
41 | | -end |
42 | | - |
43 | | -Experiment(args...) = Experiment{Symbol()}(args...) |
44 | | - |
45 | | -function Experiment(s::String) |
46 | | - m = match(r"(?<source>\w+)_(?<method>\w+)_(?<env>\w+)(\((?<game>.*)\))?", s) |
47 | | - isnothing(m) && throw( |
48 | | - ArgumentError( |
49 | | - "invalid format, got $s, expected format is JuliaRL_DQN_Atari(game=\"pong\")`", |
50 | | - ), |
51 | | - ) |
52 | | - source = m[:source] |
53 | | - method = m[:method] |
54 | | - env = m[:env] |
55 | | - kw_args = isnothing(m[:game]) ? (;) : try_parse_kw(m[:game]) |
56 | | - ex = Experiment(Val(Symbol(source)), Val(Symbol(method)), Val(Symbol(env)); kw_args...) |
57 | | - Experiment{Symbol(s)}(ex.policy, ex.env, ex.stop_condition, ex.hook) |
| 3 | +struct Experiment |
| 4 | + policy::AbstractPolicy |
| 5 | + env::AbstractEnv |
| 6 | + stop_condition::AbstractStopCondition |
| 7 | + hook::AbstractHook |
58 | 8 | end |
59 | 9 |
|
60 | | -Base.show(io::IO, m::MIME"text/plain", t::Experiment{S}) where {S} = show(io, m, convert(AnnotatedStructTree, t; description=string(S))) |
| 10 | +Base.show(io::IO, m::MIME"text/plain", t::Experiment) = show(io, m, convert(AnnotatedStructTree, t)) |
61 | 11 |
|
62 | 12 | function Base.run(ex::Experiment) |
63 | 13 | run(ex.policy, ex.env, ex.stop_condition, ex.hook) |
|
0 commit comments