programdemos/world-model/world-model.xtl
A tiny world model: a network that has watched a ball bounce in a box predicts the next frame from the last three. The world is a 2 x 2 ball in a 12 x 12 box, moving 1 or 2 cells a step on each axis and bouncing off the walls: 1,936 states in all. The network (432 pixels in, 64 ReLU units, 144 pixels out) was trained offline on four fifths of them (train/); the other fifth it never saw. Here it predicts the next frame of every state at once, as one batch, then dreams: fed its own predictions, it plays the ball forward on its own.
The world
ᵘs̲tep : (Num a, Truthy a) => a -> a
One step of the states s (rows of x y vx vy): move, and bounce off a wall at 0 or 10 (the ball is 2 wide in a box of 12).
ᵘs̲tep ← { s → P ← 2 t̲ake o̲\ s V ← -2 t̲ake o̲\ s n ← P + V lo ← n < 0 hi ← n > 10 o̲\ (n + ((n̲eg 2 × n) × lo) + (20 − 2 × n) × hi) c̲at V × 1 − 2 × lo + hi }
ᵘi̲n : Num a => a -> Float
1.0 where a ball's corner is at the place or one before it (D is -1 or 0).
ᵘi̲n ← { D → f̲loat (D ≤ 0) × D ≥ -1 }
ᵘf̲rames : Int -> Float
The frames of the balls at xy (rows of x y), one row of 144 pixels each.
ᵘf̲rames ← { xy → c ← o̲ffsets 144 (ᵘi̲n (2 s̲elect o̲\ xy) '− t̲able c d̲iv 12) × ᵘi̲n (1 s̲elect o̲\ xy) '− t̲able c m̲od 12 }
ᵘw̲indow : Int -> (Float, Float)
The window of the states s: their frame and the next two, side by side (432 pixels a row), then the frame after them.
ᵘw̲indow ← { s → s1 ← ᵘs̲tep s s2 ← ᵘs̲tep s1 s3 ← ᵘs̲tep s2 ((ᵘf̲rames 2 t̲ake₂ s) c̲at₂ (ᵘf̲rames 2 t̲ake₂ s1) c̲at₂ ᵘf̲rames 2 t̲ake₂ s2, ᵘf̲rames 2 t̲ake₂ s3) }
The network, read from data/ (just world-model-data)
wb1 : Float
Each layer's weights with its bias as one more row, as NN's d_ense takes them.
wb1 ← 433 64 r̲eshape (n̲umbers ⎕N̲GET "data/w1.txt") c̲at n̲umbers ⎕N̲GET "data/b1.txt"
wb2 : Float
wb2 ← 65 144 r̲eshape (n̲umbers ⎕N̲GET "data/w2.txt") c̲at n̲umbers ⎕N̲GET "data/b2.txt"
ᵘp̲redict : Float -> Float
Each window's next frame, a probability per pixel.
ᵘp̲redict ← { X → ᵃᶜᵗs̲igmoid (ᵃᶜᵗr̲elu X ⁿⁿd̲ense wb1) ⁿⁿd̲ense wb2 }
ᵘe̲xact : Float -> Float -> Int
How many rows of P are the frames T exactly: every pixel on the right side of one half.
ᵘe̲xact ← { P T → f̲loor '+ r̲/ 'm̲in r̲/₂ f̲loat (P > 0.5) = T > 0.5 }
ᵘp̲lace : Num a => a -> Int
The likeliest place for the ball in a predicted frame p: the 2 x 2 block with the most probability, as x y.
ᵘp̲lace ← { p → M ← 12 12 r̲eshape p S ← (11 t̲ake 11 t̲ake₂ M) + (11 t̲ake 11 t̲ake₂ 1 d̲rop M) + (11 t̲ake 11 t̲ake₂ 1 d̲rop₂ M) + 11 t̲ake 11 t̲ake₂ 1 d̲rop 1 d̲rop₂ M k ← (r̲avel S) i̲ndexOf 'm̲ax r̲/ r̲avel S 1 2 r̲eshape ((k − 1) m̲od 11) c̲at (k − 1) d̲iv 11 }
Every state at once
all : Int
all ← o̲\ 4 1936 r̲eshape (i d̲iv 176) c̲at ((i d̲iv 16) m̲od 11) c̲at ((1 + (i d̲iv 4) m̲od 4) s̲elect -2 -1 1 2) c̲at (1 + i m̲od 4) s̲elect -2 -1 1 2
x1 : Float
(x1, _) ← ᵘw̲indow 1 4 r̲eshape 3 7 2 -1