-
Notifications
You must be signed in to change notification settings - Fork 98
/
random.jl
47 lines (40 loc) · 1.59 KB
/
random.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
### RandomPolicy ###
# maintained by @zsunberg
"""
RandomPolicy{RNG<:AbstractRNG, P<:Union{POMDP,MDP}, U<:Updater}
a generic policy that uses the actions function to create a list of actions and then randomly samples an action from it.
Constructor:
`RandomPolicy(problem::Union{POMDP,MDP};
rng=Random.default_rng(),
updater=NothingUpdater())`
# Fields
- `rng::RNG` a random number generator
- `probelm::P` the POMDP or MDP problem
- `updater::U` a belief updater (default to `NothingUpdater` in the above constructor)
"""
mutable struct RandomPolicy{RNG<:AbstractRNG, P<:Union{POMDP,MDP}, U<:Updater} <: Policy
rng::RNG
problem::P
updater::U # set this to use a custom updater, by default it will be a void updater
end
# The constructor below should be used to create the policy so that the action space is initialized correctly
RandomPolicy(problem::Union{POMDP,MDP};
rng=Random.default_rng(),
updater=NothingUpdater()) = RandomPolicy(rng, problem, updater)
## policy execution ##
function action(policy::RandomPolicy, s)
return rand(policy.rng, actions(policy.problem, s))
end
function action(policy::RandomPolicy, b::Nothing)
return rand(policy.rng, actions(policy.problem))
end
## convenience functions ##
POMDPs.updater(policy::RandomPolicy) = policy.updater
"""
solver that produces a random policy
"""
mutable struct RandomSolver <: Solver
rng::AbstractRNG
end
RandomSolver(;rng=Random.default_rng()) = RandomSolver(rng)
solve(solver::RandomSolver, problem::Union{POMDP,MDP}) = RandomPolicy(solver.rng, problem, NothingUpdater())