forked from JuliaML/Reinforce.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstates.jl
50 lines (37 loc) · 1.25 KB
/
states.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
abstract AbstractState
# ----------------------------------------------------------------
"A StateVector holds both the functions which will populate the state, and the most recent state."
type StateVector{S} <: AbstractState
queries::Vector{Function}
state::Vector{S}
names::Vector{String}
end
function StateVector(queries::AbstractVector{Function}; names=fill("",length(queries)))
StateVector(queries, zeros(length(queries)), names)
end
function StateVector(queries::Function...; names=fill("",length(queries)))
StateVector(Function[f for f in queries], names=names)
end
Base.length(sv::StateVector) = length(sv.queries)
"retreive the most recently calculated state"
state(sv::StateVector) = sv.state
"update the state, then return it"
function state!(sv::StateVector)
for (i,f) in enumerate(sv.queries)
sv.state[i] = f()
end
sv.state
end
# ----------------------------------------------------------------
type History{T}
sv::StateVector{T}
states::Matrix{T}
end
History{T}(sv::StateVector{T}) = History(sv, Matrix{T}(length(sv),0))
function state!(hist::History)
s = state!(hist.sv)
hist.states = hcat(hist.states, s)
s
end
StatsBase.nobs(hist::History) = size(hist.states, 2)
# ----------------------------------------------------------------