Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add convert_s function #18

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 28 additions & 23 deletions src/LaserTag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,23 @@ export
LTState,
CMeas,
DMeas,
LaserTagVis,

MoveTowards,
LaserTagVis, MoveTowards,
MoveTowardsSampled,
OptimalMLSolver,
OptimalML,
BestExpectedSolver,
BestExpected,

DESPOTEmu,

gen_lasertag,
BestExpected, DESPOTEmu, gen_lasertag,
cpp_emu_lasertag,
tikz_pic,
n_clear_cells


const Coord = SVector{2, Int}
const CMeas = MVector{8, Float64}
const DMeas = MVector{8, Int}
const Coord = SVector{2,Int}
const CMeas = MVector{8,Float64}
const DMeas = MVector{8,Int}

const C_SAME_LOC = fill!(MVector{8, Float64}(undef), -1.0)
const D_SAME_LOC = fill!(MVector{8, Int64}(undef), -1)
const C_SAME_LOC = fill!(MVector{8,Float64}(undef), -1.0)
const D_SAME_LOC = fill!(MVector{8,Int64}(undef), -1)

@auto_hash_equals struct LTState # XXX auto_hash_equals isn't correct for terminal
robot::Coord
Expand Down Expand Up @@ -72,16 +66,16 @@ obs_type(om::ObsModel) = obs_type(typeof(om))

include("distance_cache.jl")

@with_kw struct LaserTagPOMDP{M<:ObsModel, O<:Union{CMeas, DMeas}} <: POMDP{LTState, Int, O}
tag_reward::Float64 = 10.0
step_cost::Float64 = 1.0
discount::Float64 = 0.95
floor::Floor = Floor(7, 11)
obstacles::Set{Coord} = Set{Coord}()
robot_init::Union{Coord, Nothing} = nothing
diag_actions::Bool = false
dcache::LTDistanceCache = LTDistanceCache(floor, obstacles)
obs_model::M = DESPOTEmu(floor, 2.5)
@with_kw struct LaserTagPOMDP{M<:ObsModel,O<:Union{CMeas,DMeas}} <: POMDP{LTState,Int,O}
tag_reward::Float64 = 10.0
step_cost::Float64 = 1.0
discount::Float64 = 0.95
floor::Floor = Floor(7, 11)
obstacles::Set{Coord} = Set{Coord}()
robot_init::Union{Coord,Nothing} = nothing
diag_actions::Bool = false
dcache::LTDistanceCache = LTDistanceCache(floor, obstacles)
obs_model::M = DESPOTEmu(floor, 2.5)
end

ltfloor(m::LaserTagPOMDP) = m.floor
Expand Down Expand Up @@ -143,6 +137,17 @@ function POMDPs.reward(p::LaserTagPOMDP, s::LTState, a::Int, sp::LTState)
end
end


# add a function to transform a LTState to a vector
function POMDPs.convert_s(T::Type{<:AbstractArray}, s::LTState, p::LaserTagPOMDP)
return convert(T, vcat(s.robot, s.opponent, s.terminal))
end

# transform a vector to a LTState
function POMDPs.convert_s(T::Type{LTState}, v::AbstractArray{Float64}, p::LaserTagPOMDP)
return LTState(Coord(v[1], v[2]), Coord(v[3], v[4]), v[5])
end

POMDPs.isterminal(p::LaserTagPOMDP, s::LTState) = s.terminal
POMDPs.discount(p::LaserTagPOMDP) = p.discount

Expand Down
8 changes: 8 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ tikz_pic(LaserTagVis(p))
# discrete
p = gen_lasertag()

# check convert_s function
b0 = initialstate(p)
s_test = rand(b0)
v_s_test = convert_s(Vector{Float64}, s_test, p)
s_back = convert_s(LTState, v_s_test, p)
@test s_back == s_test


# check observation model consistency
rng = MersenneTwister(12)
N = 1_000_000
Expand Down