circuits-rl
Safe HaskellNone
LanguageGHC2024

Circuit.RL.GridWorld

Description

A tiny gridworld for the circuits-rl frontier spike.

The demonstration pins the reward/policy design choice upfront:

  • Reward is a function of state, applied via a local scoreBy modality.
  • Transitions are deterministic for exact hand-checking.
  • Value iteration is shown both directly and as composition in Prob.
Synopsis

Gridworld

data State Source #

A one-dimensional chain of four states; Goal is the absorbing target.

Constructors

S0 
S1 
S2 
Goal 

Instances

Instances details
Eq State Source # 
Instance details

Defined in Circuit.RL.GridWorld

Methods

(==) :: State -> State -> Bool #

(/=) :: State -> State -> Bool #

Ord State Source # 
Instance details

Defined in Circuit.RL.GridWorld

Methods

compare :: State -> State -> Ordering #

(<) :: State -> State -> Bool #

(<=) :: State -> State -> Bool #

(>) :: State -> State -> Bool #

(>=) :: State -> State -> Bool #

max :: State -> State -> State #

min :: State -> State -> State #

Bounded State Source # 
Instance details

Defined in Circuit.RL.GridWorld

Enum State Source # 
Instance details

Defined in Circuit.RL.GridWorld

Show State Source # 
Instance details

Defined in Circuit.RL.GridWorld

Methods

showsPrec :: Int -> State -> ShowS #

show :: State -> String #

showList :: [State] -> ShowS #

data Action Source #

Move left or right; edges are clamped.

Constructors

L 
R 

Instances

Instances details
Eq Action Source # 
Instance details

Defined in Circuit.RL.GridWorld

Methods

(==) :: Action -> Action -> Bool #

(/=) :: Action -> Action -> Bool #

Ord Action Source # 
Instance details

Defined in Circuit.RL.GridWorld

Bounded Action Source # 
Instance details

Defined in Circuit.RL.GridWorld

Enum Action Source # 
Instance details

Defined in Circuit.RL.GridWorld

Show Action Source # 
Instance details

Defined in Circuit.RL.GridWorld

step :: Action -> State -> State Source #

Deterministic transition.

>>> step R S0
S1
>>> step L S0
S0
>>> step R S2
Goal

reward :: State -> Double Source #

State reward: living penalty, goal bonus.

>>> reward S0
-1.0
>>> reward Goal
10.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 S0
0.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.

transP :: Action -> Prob (->) Double State State Source #

Transition as a Prob morphism.

rewardP :: Prob (->) Double State State Source #

Reward as a state-dependent score modality.

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.

gridSystem :: System (Prob (->) Double) State (Mono Action State) Source #

The gridworld as a controlled stochastic Moore machine.

Input: action (L or R). Output: 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.

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

newtype Tropical Source #

Min-plus tropical semiring over Double.

Constructors

Tropical 

Fields

Instances

Instances details
Eq Tropical Source # 
Instance details

Defined in Circuit.RL.GridWorld

Ord Tropical Source # 
Instance details

Defined in Circuit.RL.GridWorld

Show Tropical Source # 
Instance details

Defined in Circuit.RL.GridWorld

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