| Safe Haskell | None |
|---|---|
| Language | GHC2024 |
Circuit.RL.GridWorld
Description
A tiny gridworld for the circuits-rl frontier spike.
The demonstration pins the reward/policy design choice upfront:
Synopsis
- data State
- data Action
- step :: Action -> State -> State
- reward :: State -> Double
- bellmanPolicy :: Double -> Action -> (State -> Double) -> State -> Double
- bellmanOpt :: Double -> (State -> Double) -> State -> Double
- valueIter :: Int -> Double -> State -> Double
- optimalPolicy :: Double -> (State -> Double) -> State -> Action
- scoreBy :: (a -> r -> r) -> Prob (->) r a a
- transP :: Action -> Prob (->) Double State State
- rewardP :: Prob (->) Double State State
- bellmanP :: Double -> Action -> Prob (->) Double State State
- backupP :: Double -> Action -> (State -> Double) -> State -> Double
- discountedReturn :: Double -> Action -> Int -> State -> Double
- closedFormReturn :: Double -> Action -> Int -> State -> Double
- expectSystem :: (Eq s, Semiring r) => [s] -> System (Prob (->) r) s (Mono i o) -> [i] -> (s -> r) -> s -> r
- gridSystem :: System (Prob (->) Double) State (Mono Action State)
- mdpSystem :: System (Prob (->) Double) State (Mono Action (State, Double))
- mdpCheck :: Action -> State -> State -> Double -> Bool
- pomdpSystem :: System (Prob (->) Double) State ('Prod ('Const State) (Mono Action Observation))
- pomdpCheck :: Action -> State -> State -> Observation -> Bool
- data Observation
- observe :: State -> Observation
- bellmanSystem :: Double -> (State -> Double) -> State -> Double
- valueIterSystem :: Int -> Double -> State -> Double
- newtype Tropical = Tropical {}
- shortestPath :: Int -> State -> Tropical
Gridworld
A one-dimensional chain of four states; Goal is the absorbing target.
Move left or right; edges are clamped.
step :: Action -> State -> State Source #
Deterministic transition.
>>>step R S0S1
>>>step L S0S0
>>>step R S2Goal
reward :: State -> Double Source #
State reward: living penalty, goal bonus.
>>>reward S0-1.0
>>>reward Goal10.0
Direct value iteration
bellmanPolicy :: Double -> Action -> (State -> Double) -> State -> Double Source #
One-step Bellman backup for a fixed deterministic policy.
bellmanOpt :: Double -> (State -> Double) -> State -> Double Source #
One-step Bellman optimality backup.
valueIter :: Int -> Double -> State -> Double Source #
Finite-horizon value iteration from the zero value function.
>>>valueIter 0 0.9 S00.0
>>>valueIter 1 0.9 S0-1.0
>>>valueIter 2 0.9 S0-1.9
optimalPolicy :: Double -> (State -> Double) -> State -> Action Source #
Greedy policy with respect to a value function.
Prob-composition view
scoreBy :: (a -> r -> r) -> Prob (->) r a a Source #
State-dependent score modality. Not exported by Prob because it
leaks the input into the scalar map; useful for RL rewards.
bellmanP :: Double -> Action -> Prob (->) Double State State Source #
Bellman backup for a fixed action, expressed as three Prob morphisms:
reward, then discount, then transition. The contravariant composition in
Prob reads right-to-left on continuations, so the written order is the
operational order.
backupP :: Double -> Action -> (State -> Double) -> State -> Double Source #
Apply a Prob Bellman backup to a value function at a state.
Discounted-return oracle
discountedReturn :: Double -> Action -> Int -> State -> Double Source #
N-step discounted return via Prob composition.
Composes 'bellmanP gamma a' n times via the Category instance, then
applies the result to a zero continuation. By the laws of Prob Category
composition, this is the n-step Bellman backup: reward on each step,
discounted and summed.
>>>discountedReturn 0.5 R 4 S0-0.5
closedFormReturn :: Double -> Action -> Int -> State -> Double Source #
Closed-form discounted return on a deterministic chain.
Σ_{t=0}^{n-1} γ^t · reward(step^t(a, s)).
All terms are exact in Double when γ is a dyadic rational (e.g. 0.5)
and rewards are integers.
>>>closedFormReturn 0.5 R 4 S0-0.5
System (Prob) view
expectSystem :: (Eq s, Semiring r) => [s] -> System (Prob (->) r) s (Mono i o) -> [i] -> (s -> r) -> s -> r Source #
Step a finite-state stochastic Moore machine by expectation, exactly as in the circuits keystone, but specialised to 'Mono i o' with full state observation.
mdpSystem :: System (Prob (->) Double) State (Mono Action (State, Double)) Source #
MDP interface: action in, next-state and reward out.
This matches the instance-table claim that the MDP row uses
Mono a (s', r). The reward is pinned on the current state to match
bellmanSystem / bellmanOpt.
mdpCheck :: Action -> State -> State -> Double -> Bool Source #
Check one deterministic MDP step by continuation.
pomdpSystem :: System (Prob (->) Double) State ('Prod ('Const State) (Mono Action Observation)) Source #
POMDP interface: hidden state carried as a Const position, external loop
is action in / observation out.
This matches the instance-table claim that the POMDP row uses a state-hiding
Prod (Const s) (Mono a o). The Const s position exposes the hidden
state as output but supplies no direction, so the external agent cannot feed
it back as input.
pomdpCheck :: Action -> State -> State -> Observation -> Bool Source #
Check one deterministic POMDP step by continuation.
data Observation Source #
POMDP observation: coarse location, not the true state.
Instances
observe :: State -> Observation Source #
Coarse observation function.
bellmanSystem :: Double -> (State -> Double) -> State -> Double Source #
One-step Bellman optimality backup via 'System (Prob)'.
Reward is pinned on the current state (matching bellmanOpt); the System
runner computes the expected discounted future value of the next state.
valueIterSystem :: Int -> Double -> State -> Double Source #
Finite-horizon value iteration using the System runner.
Tropical / shortest-path row
Min-plus tropical semiring over Double.
Constructors
| Tropical | |
Fields | |
shortestPath :: Int -> State -> Tropical Source #
Finite-horizon shortest-path cost to goal.
>>>getTropical (shortestPath 0 S0)Infinity
>>>getTropical (shortestPath 1 S0)Infinity
>>>getTropical (shortestPath 2 S0)Infinity
>>>getTropical (shortestPath 3 S0)3.0
>>>getTropical (shortestPath 3 Goal)0.0