Zero-dependency C library for deploying compiled tile policies on microcontrollers. Train with gradients in Python, deploy as O(1) hash lookups in C99. Works on ESP8266 (80KB RAM, 4MB flash), Arduino, STM32, and any platform with a C99 compiler.
Train with gradients, deploy with lookups.
Reinforcement learning produces policies that map game states to actions. In training, this mapping is a neural network — expensive to evaluate, memory-hungry, and requires floating-point hardware. But once training converges, the policy can be compiled into a lookup table: hash the state → look up the best action. No floating-point math, no matrix multiplication, no neural network runtime.
This matters enormously for edge deployment:
| Platform | RAM | Flash | Neural Net Feasible? | Lookup Table Feasible? |
|---|---|---|---|---|
| ESP8266 | 80 KB | 4 MB | ❌ (no FPU, ~50KB free) | ✅ (< 5 KB) |
| Arduino Uno | 2 KB | 32 KB | ❌ | ✅ (< 1 KB for small policy) |
| STM32F103 | 20 KB | 128 KB | ❌ (no FPU on Cortex-M0) | ✅ (< 10 KB) |
| Raspberry Pi Zero | 512 MB | 16 GB | ✅ (slowly) | ✅ |
The compiled-policy approach has three advantages over on-device inference:
- Deterministic latency — O(1) hash lookup completes in <300 ns regardless of policy complexity
- Zero dependencies — only
string.h,stdint.h,stdlib.h(C99 standard library) - Provably correct — the lookup table IS the policy; no inference bugs, no floating-point drift
Game State (string)
│
▼
BLAKE2b-64 ──► 16 hex chars
│
▼
Hash Table (FNV-1a buckets)
│
▼
Policy Entry {hash, action, score}
│
▼
Softmax Selection (optional, temperature-controlled)
│
▼
Best Action
Game states are converted to canonical strings (e.g., "X O X O " for tic-tac-toe) and hashed using a compact BLAKE2b-64 implementation (~200 lines of C):
hash = BLAKE2b-64(state_string)
output: 16 hex chars (8 bytes → 16 hex nibbles)
BLAKE2b is chosen because:
- Cryptographic strength — collision resistance prevents state misidentification
- Compact implementation — ~200 LOC vs. SHA-256's ~400 LOC
- Fast on microcontrollers — ~200 ns per hash on x86, ~2 μs on ESP8266
- 64-bit output — sufficient for policy tables (< 100K entries), 16 hex chars for human readability
Complexity: BLAKE2b hashing is O(N) where N = input length. For typical game states (< 64 chars), this is effectively O(1).
The 16-char hex hash is mapped to a table bucket using FNV-1a (Fowler-Noll-Vo):
bucket = FNV-1a(hash_string) mod TABLE_SIZE
The hash table uses separate chaining (linked list per bucket) with TABLE_SIZE = 256:
bucket[i] → entry → entry → entry → NULL
Load factor: α = N / TABLE_SIZE. For N = 56 (TTT policy): α = 0.22. For N = 228 (C4 policy): α = 0.89.
Lookup complexity:
| Operation | Average | Worst Case |
|---|---|---|
| Hash computation | O(L) where L = state length | O(L) |
| Bucket selection | O(1) | O(1) |
| Chain traversal | O(1 + α) | O(N) (all in one bucket) |
| String comparison | O(16) per entry | O(16) |
| Total | O(L + 16(1+α)) | O(L + 16N) |
For the bundled TTT policy (56 entries, α = 0.22): expected ~1.2 chain traversals per lookup.
For stochastic policies, policy_select() applies softmax sampling over valid moves:
P(action_i) = exp(score_i / T) / Σ_j exp(score_j / T)
Where T is the temperature:
- T → 0: Greedy (always pick highest score)
- T = 1.0: Original policy distribution
- T → ∞: Uniform random (maximum exploration)
The softmax is computed using the numerically stable variant:
logits[i] = score[i] / T
max_logit = max(logits)
P[i] = exp(logits[i] - max_logit) / Σ exp(logits[j] - max_logit)
This prevents overflow when scores are large.
Complexity: O(N_valid × log(N_valid)) for sampling (due to sorting for cumulative distribution).
Each entry in the compiled policy is a PolicyEntry:
typedef struct {
const char* hash; /* 16-char hex string (BLAKE2b-64) */
int action; /* best action index */
float score; /* confidence / Q-value */
} PolicyEntry; /* 24 bytes on 64-bit, 12 bytes on 32-bit */Memory budget:
| Policy | Entries | Size/Entry | Total RAM |
|---|---|---|---|
| TTT | 56 | 24 B | 1,344 B (~1.3 KB) |
| Connect-4 | 228 | 24 B | 5,472 B (~5.5 KB) |
| Hash table | 256 buckets | 4 B/bucket (pointers) | 1,024 B |
| Total (TTT) | < 5 KB |
# Build and test
make test # Run test suite
make bench # Performance benchmarks
make examples # TTT and C4 demos#include "compiled_policy.h"
/* Hash a game state to 16 hex chars */
char hash[17];
policy_hash("X O X O ", hash);
/* Look up best action (greedy) */
int action = policy_lookup(&ttt_policy, hash);
/* action = 8 (or -1 if state not in policy) */
/* Softmax selection from valid moves */
int valid[] = {0, 1, 3, 4, 6};
int selected = policy_select(&c4_policy, hash, valid, 5);# ESP8266 (80KB RAM)
make esp8266 CC=xtensa-lx106-elf-gcc
# ARM Cortex-M (STM32)
make arm CC=arm-none-eabi-gcc
# Arduino (copy into sketch)
# Copy compiled_policy.h + compiled_policy.c + ttt_policy_data.h| Function | Signature | Returns | Description |
|---|---|---|---|
policy_lookup |
(const CompiledPolicy*, const char* hash) |
int action or -1 |
O(1) greedy lookup |
policy_select |
(const CompiledPolicy*, const char* hash, const int* valid, int n) |
int action |
Softmax selection from valid moves |
policy_hash |
(const char* state, char* out) |
void |
BLAKE2b-64 → 16 hex chars |
policy_version |
(void) |
const char* |
Library version string |
typedef struct {
const char* hash; /* 16 hex chars */
int action;
float score;
} PolicyEntry;
typedef struct {
const PolicyEntry* entries;
int count;
float temperature; /* 0 = greedy */
} CompiledPolicy;| Operation | Target | Typical (x86) | Typical (ESP8266) |
|---|---|---|---|
| Hash | < 500 ns | ~200 ns | ~2 μs |
| Lookup | < 100 ns | ~30 ns | ~200 ns |
| Full decision | < 1 μs | ~300 ns | ~3 μs |
Compiled-policy-c instantiates the crystallization endpoint of the γ + η = C framework. During training (γ phase), gradient-based learning produces fluid intelligence as neural network weights. During compilation (η phase), this intelligence is distilled into a lookup table — solid, immutable, deployable. The conservation invariant (C) ensures that the compiled policy's behavior matches the trained network's behavior within bounded error.
The "train with gradients, deploy with lookups" philosophy is the computational analog of gamma (fluid learning) + eta (solid execution) = C (conserved competence). The total intelligence is conserved across the training-to-deployment transition.
Policy data is generated by the zeroclaw-arena tile compiler: trained on 1,000 games of tic-tac-toe, 1,644 tiles learned, 1,174 compiled, 81.2% win rate.
- Aumasson, J.-P. et al. (2013). "BLAKE2: Simpler, Smaller, Fast as MD5." SAC 2013. (BLAKE2b specification)
- Fowler, G. et al. (1991). "FNV Hash." (FNV-1a algorithm)
- Sutton, R.S. & Barto, A.G. (2018). Reinforcement Learning: An Introduction. 2nd ed. MIT Press. Chapter 13: "Policy Gradient Methods." (Training policies)
- Bridle, J.S. (1990). "Training Stochastic Model Recognition Algorithms as Networks Can Lead to Maximum Mutual Information Estimation of Parameters." NIPS 1989. (Softmax temperature in RL)
MIT