# genhavoc/preydictable architecture

> Architecture diagram of the GitHub repository genhavoc/preydictable, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/genhavoc/preydictable
- Repository: https://github.com/genhavoc/preydictable

## Overview

This repository implements a predator–prey grid-world reinforcement-learning pipeline: define and simulate the game, build or estimate transition models, solve and evaluate policies, and train a neural policy with REINFORCE. It also supports dense and sparse kernels and model-free estimation. The README documents the major workflow; source-index references and shown excerpts support some internal calls. Several modules were not sampled, so their finer-grained connections are omitted.

Main entry points: [`predator_prey/env/state_space.py`](https://github.com/genhavoc/preydictable/blob/main/predator_prey/env/state_space.py) (State Encoding), [`predator_prey/env/transition_engine.py`](https://github.com/genhavoc/preydictable/blob/main/predator_prey/env/transition_engine.py) (Transition Engine), [`predator_prey/core/gradient_estimate.py`](https://github.com/genhavoc/preydictable/blob/main/predator_prey/core/gradient_estimate.py) (REINFORCE Gradient), [`predator_prey/env/simulator.py`](https://github.com/genhavoc/preydictable/blob/main/predator_prey/env/simulator.py) (Game Simulator)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_environment["Game Environment"]
  node_simulator["Game Simulator<br/>[simulator.py]"]
  node_state_space["State Encoding<br/>[state_space.py]"]
  node_reward["Reward Matrix<br/>[reward_function.py]"]
  node_transition_engine["Transition Engine"]
  node_induced_reward["Policy Reward<br/>[induced_reward.py]"]
end

subgraph group_models["Transition Models"]
  node_dense_kernel["Dense Kernel<br/>[kernel.py]"]
  node_sparse_kernel["Sparse Kernel<br/>[kernel_sparse.py]"]
  node_estimated_kernel["Estimated Kernel<br/>[estimate_kernel.py]"]
  node_induced_kernel["Policy Kernel<br/>[induced_kernel.py]"]
  node_induced_kernel_sparse["Sparse Policy Kernel"]
end

subgraph group_planning["Planning and Evaluation"]
  node_value_iteration["Value Iteration<br/>[value_iteration.py]"]
  node_policy_iteration["Policy Iteration"]
  node_induced_policy["Greedy Policy<br/>[induced_policy.py]"]
  node_state_value["State Evaluation"]
  node_state_value_sparse["Sparse Evaluation"]
  node_q_value["Q Evaluation<br/>[q_value_eval.py]"]
end

subgraph group_learning["Policy Learning"]
  node_policy_sample["Sample Policy<br/>[sample_policy.py]"]
  node_policy_network["Neural Policy<br/>[policy_network.py]"]
  node_gradient_estimate["REINFORCE Gradient"]
  node_simple_sga["SGA Optimizer<br/>[simple_sga.py]"]
  node_adam["Adam Runner<br/>[run_adam.py]"]
end

node_researcher(("RL User"))

node_researcher -->|"runs game"| node_simulator
node_simulator -->|"models dynamics"| node_transition_engine
node_transition_engine -->|"builds model"| node_dense_kernel
node_transition_engine -.->|"builds model"| node_sparse_kernel
node_simulator -->|"samples transitions"| node_estimated_kernel
node_state_space -->|"encodes states"| node_transition_engine
node_state_space -->|"encodes states"| node_dense_kernel
node_state_space -->|"encodes states"| node_sparse_kernel
node_state_space -->|"encodes samples"| node_estimated_kernel
node_researcher -->|"solves model"| node_value_iteration
node_dense_kernel -->|"provides kernel"| node_value_iteration
node_dense_kernel -->|"provides kernel"| node_policy_iteration
node_transition_engine -->|"applies matvecs"| node_policy_iteration
node_policy_iteration -->|"improves policy"| node_induced_policy
node_reward -->|"provides rewards"| node_state_value_sparse
node_induced_reward -->|"computes policy reward"| node_state_value
node_induced_reward -->|"computes policy reward"| node_state_value_sparse
node_induced_kernel -->|"evaluates transitions"| node_state_value
node_sparse_kernel -->|"provides sparse model"| node_state_value_sparse
node_policy_sample -->|"supplies policy"| node_state_value_sparse
node_state_space -->|"encodes states"| node_policy_sample
node_researcher -->|"estimates model"| node_estimated_kernel
node_estimated_kernel -->|"supports planning"| node_value_iteration
node_policy_network -->|"generates actions"| node_gradient_estimate
node_simulator -->|"runs episodes"| node_gradient_estimate
node_gradient_estimate -->|"supplies gradients"| node_simple_sga
node_gradient_estimate -->|"supplies gradients"| node_adam
node_researcher -->|"trains policy"| node_gradient_estimate
node_value_iteration -.->|"baseline policy"| node_policy_network

click node_simulator "https://github.com/genhavoc/preydictable/blob/main/predator_prey/env/simulator.py"
click node_state_space "https://github.com/genhavoc/preydictable/blob/main/predator_prey/env/state_space.py"
click node_reward "https://github.com/genhavoc/preydictable/blob/main/predator_prey/env/reward_function.py"
click node_transition_engine "https://github.com/genhavoc/preydictable/blob/main/predator_prey/env/transition_engine.py"
click node_induced_reward "https://github.com/genhavoc/preydictable/blob/main/predator_prey/env/induced_reward.py"
click node_dense_kernel "https://github.com/genhavoc/preydictable/blob/main/predator_prey/core/kernel.py"
click node_sparse_kernel "https://github.com/genhavoc/preydictable/blob/main/predator_prey/core/kernel_sparse.py"
click node_estimated_kernel "https://github.com/genhavoc/preydictable/blob/main/predator_prey/core/estimate_kernel.py"
click node_induced_kernel "https://github.com/genhavoc/preydictable/blob/main/predator_prey/core/induced_kernel.py"
click node_induced_kernel_sparse "https://github.com/genhavoc/preydictable/blob/main/predator_prey/core/induced_kernel_sparse.py"
click node_value_iteration "https://github.com/genhavoc/preydictable/blob/main/predator_prey/algorithms/value_iteration.py"
click node_policy_iteration "https://github.com/genhavoc/preydictable/blob/main/predator_prey/algorithms/policy_iteration.py"
click node_induced_policy "https://github.com/genhavoc/preydictable/blob/main/predator_prey/algorithms/induced_policy.py"
click node_state_value "https://github.com/genhavoc/preydictable/blob/main/predator_prey/core/state_value_eval.py"
click node_state_value_sparse "https://github.com/genhavoc/preydictable/blob/main/predator_prey/core/state_value_eval_sparse.py"
click node_q_value "https://github.com/genhavoc/preydictable/blob/main/predator_prey/core/q_value_eval.py"
click node_policy_sample "https://github.com/genhavoc/preydictable/blob/main/predator_prey/algorithms/sample_policy.py"
click node_policy_network "https://github.com/genhavoc/preydictable/blob/main/predator_prey/algorithms/policy_network.py"
click node_gradient_estimate "https://github.com/genhavoc/preydictable/blob/main/predator_prey/core/gradient_estimate.py"
click node_simple_sga "https://github.com/genhavoc/preydictable/blob/main/predator_prey/algorithms/simple_sga.py"
click node_adam "https://github.com/genhavoc/preydictable/blob/main/predator_prey/algorithms/run_adam.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_simulator,node_state_space,node_reward,node_transition_engine,node_induced_reward,node_researcher toneBlue
class node_dense_kernel,node_sparse_kernel,node_estimated_kernel,node_induced_kernel,node_induced_kernel_sparse toneAmber
class node_value_iteration,node_policy_iteration,node_induced_policy,node_state_value,node_state_value_sparse,node_q_value toneMint
class node_policy_sample,node_policy_network,node_gradient_estimate,node_simple_sga,node_adam toneRose
```

## Components

### Game Environment

- [Game Simulator](https://github.com/genhavoc/preydictable/blob/main/predator_prey/env/simulator.py): `predator_prey/env/simulator.py`
- [State Encoding](https://github.com/genhavoc/preydictable/blob/main/predator_prey/env/state_space.py): `predator_prey/env/state_space.py`
- [Reward Matrix](https://github.com/genhavoc/preydictable/blob/main/predator_prey/env/reward_function.py): `predator_prey/env/reward_function.py`
- [Transition Engine](https://github.com/genhavoc/preydictable/blob/main/predator_prey/env/transition_engine.py): `predator_prey/env/transition_engine.py`
- [Policy Reward](https://github.com/genhavoc/preydictable/blob/main/predator_prey/env/induced_reward.py): `predator_prey/env/induced_reward.py`

### Transition Models

- [Dense Kernel](https://github.com/genhavoc/preydictable/blob/main/predator_prey/core/kernel.py): `predator_prey/core/kernel.py`
- [Sparse Kernel](https://github.com/genhavoc/preydictable/blob/main/predator_prey/core/kernel_sparse.py): `predator_prey/core/kernel_sparse.py`
- [Estimated Kernel](https://github.com/genhavoc/preydictable/blob/main/predator_prey/core/estimate_kernel.py): `predator_prey/core/estimate_kernel.py`
- [Policy Kernel](https://github.com/genhavoc/preydictable/blob/main/predator_prey/core/induced_kernel.py): `predator_prey/core/induced_kernel.py`
- [Sparse Policy Kernel](https://github.com/genhavoc/preydictable/blob/main/predator_prey/core/induced_kernel_sparse.py): `predator_prey/core/induced_kernel_sparse.py`

### Planning and Evaluation

- [Value Iteration](https://github.com/genhavoc/preydictable/blob/main/predator_prey/algorithms/value_iteration.py): `predator_prey/algorithms/value_iteration.py`
- [Policy Iteration](https://github.com/genhavoc/preydictable/blob/main/predator_prey/algorithms/policy_iteration.py): `predator_prey/algorithms/policy_iteration.py`
- [Greedy Policy](https://github.com/genhavoc/preydictable/blob/main/predator_prey/algorithms/induced_policy.py): `predator_prey/algorithms/induced_policy.py`
- [State Evaluation](https://github.com/genhavoc/preydictable/blob/main/predator_prey/core/state_value_eval.py): `predator_prey/core/state_value_eval.py`
- [Sparse Evaluation](https://github.com/genhavoc/preydictable/blob/main/predator_prey/core/state_value_eval_sparse.py): `predator_prey/core/state_value_eval_sparse.py`
- [Q Evaluation](https://github.com/genhavoc/preydictable/blob/main/predator_prey/core/q_value_eval.py): `predator_prey/core/q_value_eval.py`

### Policy Learning

- [Sample Policy](https://github.com/genhavoc/preydictable/blob/main/predator_prey/algorithms/sample_policy.py): `predator_prey/algorithms/sample_policy.py`
- [Neural Policy](https://github.com/genhavoc/preydictable/blob/main/predator_prey/algorithms/policy_network.py): `predator_prey/algorithms/policy_network.py`
- [REINFORCE Gradient](https://github.com/genhavoc/preydictable/blob/main/predator_prey/core/gradient_estimate.py): `predator_prey/core/gradient_estimate.py`
- [SGA Optimizer](https://github.com/genhavoc/preydictable/blob/main/predator_prey/algorithms/simple_sga.py): `predator_prey/algorithms/simple_sga.py`
- [Adam Runner](https://github.com/genhavoc/preydictable/blob/main/predator_prey/algorithms/run_adam.py): `predator_prey/algorithms/run_adam.py`

### Other components

- **RL User**

## Connections

- RL User → Game Simulator: runs game (evidence: `README.md`)
- Game Simulator → Transition Engine: models dynamics (evidence: `README.md`)
- Transition Engine → Dense Kernel: builds model (evidence: `README.md`)
- Transition Engine → Sparse Kernel: builds model
- Game Simulator → Estimated Kernel: samples transitions (evidence: `predator_prey/core/estimate_kernel.py`)
- State Encoding → Transition Engine: encodes states (evidence: `predator_prey/env/transition_engine.py`)
- State Encoding → Dense Kernel: encodes states (evidence: `predator_prey/core/kernel.py`)
- State Encoding → Sparse Kernel: encodes states (evidence: `predator_prey/core/kernel_sparse.py`)
- State Encoding → Estimated Kernel: encodes samples (evidence: `predator_prey/core/estimate_kernel.py`)
- RL User → Value Iteration: solves model (evidence: `README.md`)
- Dense Kernel → Value Iteration: provides kernel (evidence: `README.md`)
- Dense Kernel → Policy Iteration: provides kernel (evidence: `README.md`)
- Transition Engine → Policy Iteration: applies matvecs (evidence: `predator_prey/algorithms/policy_iteration.py`)
- Policy Iteration → Greedy Policy: improves policy (evidence: `predator_prey/algorithms/policy_iteration.py`)
- Reward Matrix → Sparse Evaluation: provides rewards (evidence: `predator_prey/core/state_value_eval_sparse.py`)
- Policy Reward → State Evaluation: computes policy reward (evidence: `predator_prey/core/state_value_eval.py`)
- Policy Reward → Sparse Evaluation: computes policy reward (evidence: `predator_prey/core/state_value_eval_sparse.py`)
- Policy Kernel → State Evaluation: evaluates transitions (evidence: `predator_prey/core/state_value_eval.py`)
- Sparse Kernel → Sparse Evaluation: provides sparse model (evidence: `predator_prey/core/state_value_eval_sparse.py`)
- Sample Policy → Sparse Evaluation: supplies policy (evidence: `predator_prey/core/state_value_eval_sparse.py`)
- State Encoding → Sample Policy: encodes states (evidence: `predator_prey/algorithms/sample_policy.py`)
- RL User → Estimated Kernel: estimates model (evidence: `README.md`)
- Estimated Kernel → Value Iteration: supports planning (evidence: `README.md`)
- Neural Policy → REINFORCE Gradient: generates actions (evidence: `predator_prey/core/gradient_estimate.py`)
- Game Simulator → REINFORCE Gradient: runs episodes (evidence: `predator_prey/core/gradient_estimate.py`)
- REINFORCE Gradient → SGA Optimizer: supplies gradients (evidence: `predator_prey/algorithms/simple_sga.py`)
- REINFORCE Gradient → Adam Runner: supplies gradients (evidence: `predator_prey/algorithms/run_adam.py`)
- RL User → REINFORCE Gradient: trains policy (evidence: `README.md`)
- Value Iteration → Neural Policy: baseline policy
