-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
72a9db7
commit b870c6d
Showing
13 changed files
with
254 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
""" | ||
Wrappers for MultivariateStats.jl. See https://juliastats.org/MultivariateStats.jl/stable/. | ||
""" | ||
|
||
using ManifoldLearning | ||
|
||
|
||
mutable struct ManifoldCompressor{T} <: Compressor | ||
const maxoutdim::Integer | ||
M | ||
end | ||
|
||
ManifoldCompressor(maxoutdim::Integer, T) = ManifoldCompressor{T}(maxoutdim, missing) | ||
|
||
function (c::ManifoldCompressor)(beliefs) | ||
return ndims(beliefs) == 2 ? ManifoldLearning.predict(c.M, beliefs')' : vec(ManifoldLearning.predict(c.M, beliefs)) | ||
end | ||
|
||
function fit!(compressor::ManifoldCompressor{T}, beliefs; kwargs...) where T | ||
compressor.M = ManifoldLearning.fit(T, beliefs'; maxoutdim=compressor.maxoutdim, kwargs...) | ||
end | ||
|
||
### ManifoldLearning.jl Wrappers ### | ||
IsomapCompressor(maxoutdim::Integer) = ManifoldCompressor(maxoutdim, Isomap) | ||
|
||
### Discontinued ### | ||
|
||
# function (c::ManifoldCompressor{T})(beliefs) where T | ||
# M = ManifoldLearning.fit(T, beliefs'; maxoutdim=c.maxoutdim, c.kwargs...) | ||
# R = ManifoldLearning.predict(M) | ||
# # R = ndims(beliefs) == 2 ? ManifoldLearning.predict(M, beliefs')' : vec(ManifoldLearning.predict(M, beliefs)) | ||
# return R | ||
# end | ||
|
||
# function fit!(compressor::ManifoldCompressor{T}, beliefs; kwargs...) where T | ||
# compressor.kwargs = kwargs | ||
# end | ||
|
||
# LLECompressor(maxoutdim::Integer) = ManifoldCompressor(maxoutdim, LLE) | ||
# HLLECompressor(maxoutdim::Integer) = ManifoldCompressor(maxoutdim, HLLE) | ||
# LEMCompressor(maxoutdim::Integer) = ManifoldCompressor(maxoutdim, LEM) | ||
# LTSACompressor(maxoutdim::Integer) = ManifoldCompressor(maxoutdim, LTSA) | ||
# DiffMapCompressor(maxoutdim::Integer) = ManifoldCompressor(maxoutdim, DiffMap) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
""" | ||
Wrappers for MultivariateStats.jl. See https://juliastats.org/MultivariateStats.jl/stable/. | ||
""" | ||
|
||
using MultivariateStats | ||
using MultivariateStats: predict | ||
|
||
|
||
mutable struct MVSCompressor{T} <: Compressor | ||
const maxoutdim::Integer | ||
M | ||
end | ||
|
||
MVSCompressor(maxoutdim::Integer, T) = MVSCompressor{T}(maxoutdim, missing) | ||
|
||
function (c::MVSCompressor)(beliefs) | ||
return ndims(beliefs) == 2 ? predict(c.M, beliefs')' : vec(predict(c.M, beliefs)) | ||
end | ||
|
||
function fit!(compressor::MVSCompressor{T}, beliefs; kwargs...) where T | ||
compressor.M = fit(T, beliefs'; maxoutdim=compressor.maxoutdim, kwargs...) | ||
end | ||
|
||
### MultivariateStats.jl Wrappers ### | ||
# PCA Compressors | ||
PCACompressor(maxoutdim::Integer) = MVSCompressor(maxoutdim, PCA) | ||
KernelPCACompressor(maxoutdim::Integer) = MVSCompressor(maxoutdim, KernelPCA) | ||
PPCACompressor(maxoutdim::Integer) = MVSCompressor(maxoutdim, PPCA) | ||
|
||
# Factor Analysis Compressor | ||
FactorAnalysisCompressor(maxoutdim::Integer) = MVSCompressor(maxoutdim, FactorAnalysis) | ||
|
||
# Multidimensional Scaling | ||
# MDSCompressor(maxoutdim::Integer) = MVSCompressor(maxoutdim, MDS) | ||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using Distances | ||
|
||
|
||
### Utilities ### | ||
|
||
function _make_numeric(b, pomdp::POMDP) | ||
b = convert_s(AbstractArray{Float64}, b, pomdp) | ||
return SVector{length(b)}(b) | ||
end | ||
|
||
""" | ||
Adapted from PointBasedValueIteration.jl: https://github.com/JuliaPOMDP/PointBasedValueIteration.jl/blob/master/src/pbvi.jl | ||
""" | ||
function _successors(pomdp::POMDP, b, updater::Updater) | ||
succs = [] | ||
for a in actions(pomdp, b) | ||
for o in observations(pomdp) | ||
s = update(updater, b, a, o) | ||
push!(succs, s) | ||
end | ||
end | ||
return unique!(succs) | ||
end | ||
|
||
### Body ### | ||
|
||
""" | ||
Effecient adaptation of algorithm 21.13 from AFDM that uses KDTree. | ||
Only works for finite S, A, O. | ||
""" | ||
function exploratory_belief_expansion!(pomdp::POMDP, B::Set, B_numeric, updater::Updater; metric::NearestNeighbors.MinkowskiMetric=Euclidean()) | ||
tree = KDTree(B_numeric, metric) | ||
B_new = typeof(B)() | ||
for b in B | ||
if isterminal(pomdp, b) | ||
println("woops") | ||
end | ||
succs = _successors(pomdp, b, updater) | ||
succs_numeric = map(s->_make_numeric(s, pomdp), succs) | ||
if !isempty(succs) | ||
_, dists = nn(tree, succs_numeric) | ||
i = argmax(dists) | ||
b_new = succs[i] | ||
b_numeric_new = succs_numeric[i] | ||
if !in(b_new, B) | ||
push!(B_new, b_new) | ||
push!(B_numeric, b_numeric_new) | ||
end | ||
end | ||
end | ||
union!(B, B_new) | ||
end | ||
|
||
""" | ||
Wrapper for exploratory_belief_expansion!. | ||
Creates an initial belief set and calls exploratory_belief_expansion! on POMDP n times. | ||
""" | ||
function exploratory_belief_expansion(pomdp::POMDP, updater::Updater; n::Integer=10, metric::NearestNeighbors.MinkowskiMetric=Euclidean()) | ||
b0 = initialize_belief(updater, initialstate(pomdp)) | ||
b0_numeric = _make_numeric(b0, pomdp) | ||
B = Set([b0]) | ||
B_numeric = [b0_numeric] | ||
for _ in 1:n | ||
exploratory_belief_expansion!(pomdp, B, B_numeric, updater; metric) | ||
end | ||
return B | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# TODO: add RNG support for seeding | ||
|
||
function sample(pomdp::POMDP, explorer::Policy, updater::Updater, n::Integer; rng::AbstractRNG=Random.GLOBAL_RNG) | ||
mdp = GenerativeBeliefMDP(pomdp, updater) | ||
iter = stepthrough(mdp, explorer, "s"; max_steps=n) | ||
B = collect(Iterators.take(Iterators.cycle(iter), n)) | ||
return unique!(B) | ||
end | ||
|
||
function sample(pomdp::POMDP, explorer::ExplorationPolicy, updater::Updater, n::Integer; rng::AbstractRNG=Random.GLOBAL_RNG) | ||
samples = [] | ||
mdp = GenerativeBeliefMDP(pomdp, updater) | ||
on_policy = RandomPolicy(mdp) | ||
while true | ||
b = initialstate(mdp).val | ||
for k in 1:n | ||
if length(samples) == n | ||
return unique!(samples) | ||
end | ||
|
||
if isterminal(mdp, b) | ||
break | ||
end | ||
a = action(explorer, on_policy, k, b) | ||
b = @gen(:sp)(mdp, b, a, rng) | ||
push!(samples, b) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.