Last active
April 30, 2025 02:43
-
-
Save zsunberg/bfc03cfebbd2d3cf5c4603f305b072ef to your computer and use it in GitHub Desktop.
Proof of concept for allowing a package writer to insert code between two users using a compatibility wrapper
This file contains hidden or 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
using POMDPTools: SparseCat | |
using Distributions: pdf | |
module DecisionMaking | |
import Distributions | |
using Distributions: pdf | |
export | |
transition, | |
T, | |
CompatibleModel | |
struct CompatibleModel{M} | |
m::M | |
cache::Dict # Would want to make this concrete - I have some ideas | |
end | |
CompatibleModel(m) = CompatibleModel(m, Dict()) | |
function transition end | |
function T end | |
function transition(c::CompatibleModel, s, a) | |
if applicable(transition, c.m, s, a) | |
return transition(c.m, s, a) | |
elseif hasmethod(T, Tuple{typeof(c.m), typeof(s), typeof(a), typeof(s)}) | |
return MassFunctionDistribution(T, (c.m, s, a)) | |
else | |
error("You need to implement transition or T") # error message can be improved | |
end | |
end | |
function T(c::CompatibleModel, s, a, sp) | |
if applicable(T, c.m, s, a, sp) | |
return T(c.m, s, a, sp) | |
elseif applicable(transition, c.m, s, a) | |
return pdf(transition(c.m, s, a), sp) | |
else | |
error("You need to implement T or transition") | |
end | |
end | |
struct MassFunctionDistribution{F, A} | |
p::F | |
args::A | |
end | |
Distributions.pdf(d::MassFunctionDistribution, x) = d.p(d.args..., x) | |
end | |
# This is what the problem implementer would write | |
import .DecisionMaking | |
struct TransitionBasedMDP end | |
DecisionMaking.transition(m::TransitionBasedMDP, s, a) = SparseCat([1, 2], [0.1, 0.9]) | |
struct TBasedMDP end | |
DecisionMaking.T(m::TBasedMDP, s, a, sp) = sp/3 # this is silly - returns 1/3 for sp=1 and 2/3 for sp=2 | |
# This is what you would write to interact with a model | |
using .DecisionMaking | |
m_transition = CompatibleModel(TransitionBasedMDP()) | |
@show T(m_transition, 1, 1, 2) | |
m_T = CompatibleModel(TBasedMDP()) | |
@show pdf(transition(m_T, 1, 1), 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment