Skip to content

Instantly share code, notes, and snippets.

@zsunberg
Last active April 30, 2025 02:33
Show Gist options
  • Save zsunberg/8f94f389b62608f6bed3faa7baf42d95 to your computer and use it in GitHub Desktop.
Save zsunberg/8f94f389b62608f6bed3faa7baf42d95 to your computer and use it in GitHub Desktop.
Proof of concept for allowing a package writer to insert code between two users using two modules
using POMDPTools: SparseCat
using Distributions: pdf
module DecisionMaking
import Distributions
module ModelerInterface
function transition end
function T end
end
struct MassFunctionDistribution{F, A}
p::F
args::A
end
Distributions.pdf(d::MassFunctionDistribution, x) = d.p(d.args..., x)
module UserInterface
import ..MassFunctionDistribution
import ..ModelerInterface
using Distributions: pdf
export
transition,
T
function transition(m, s, a)
if applicable(ModelerInterface.transition, m, s, a)
return ModelerInterface.transition(m, s, a)
elseif hasmethod(ModelerInterface.T, Tuple{typeof(m), typeof(s), typeof(a), typeof(s)})
return MassFunctionDistribution(ModelerInterface.T, (m, s, a))
else
error("You need to implement transition or T") # error message can be improved
end
end
function T(m, s, a, sp)
if applicable(ModelerInterface.T, m, s, a, sp)
return ModelerInterface.T(m, s, a, sp)
elseif applicable(ModelerInterface.transition, m, s, a)
return pdf(ModelerInterface.transition(m, s, a), sp)
else
error("You need to implement T or transition")
end
end
end
end
# This is what the problem implementer would write
import .DecisionMaking: ModelerInterface
struct TransitionBasedMDP end
ModelerInterface.transition(m::TransitionBasedMDP, s, a) = SparseCat([1, 2], [0.1, 0.9])
struct TBasedMDP end
ModelerInterface.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.UserInterface
@show T(TransitionBasedMDP(), 1, 1, 2)
@show pdf(transition(TBasedMDP(), 1, 1), 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment