Skip to content

Simulator engine, Python/Gymnasium binding, policies, workloads and replay - #1

Merged
dev0Guy merged 7 commits into
mainfrom
setup
Nov 25, 2026
Merged

dev0Guy merged 7 commits into
mainfrom
setup

Conversation

@dev0Guy

@dev0Guy dev0Guy commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Summary

Builds the job-to-machine scheduling simulator out from the initial skeleton.

  • Engine: layered architecture (grid → model → ports → engine → adapters → app), SIMD fit test, sliding-window machine time (advance is O(1); 300 machines × 1000 ticks: ~415 ms → ~6 ms per episode, identical results).
  • Policies: fifo, fifo_strict, sjf, best_fit, tetris, tetris_srpt (tunable weight), random; policy specs accept numeric options.
  • Workloads: generators random, deeprm; arrivals poisson, batch, mmpp (bursty), pareto (heavy-tailed), selfsimilar (Pareto ON/OFF, H = (3−α)/2).
  • Rewards: new Reward port with neg_in_system (default), neg_waiting, slowdown (DeepRM), zero.
  • Python: C ABI (zig build lib) + sched_env Gymnasium env with zero-copy views and action masks, Stable-Baselines3 MaskablePPO example, matplotlib visualizer.
  • Trace & replay: trace observer writes JSON Lines; replay.py rebuilds and animates episodes offline.
  • Docs: guide, Python, architecture, data layout.

Testing

  • zig build test: unit tests, integration tests, hand-worked scenarios, and a property test against a naive reference simulator (20 random episodes, checked every step). Deliberately planted bugs were each caught.
  • PYTHONPATH=python pytest python/tests: check_env, masked episodes, policy stepping, sliding-window views, visualizer, and trace replay matching a live run frame for frame.

🤖 Generated with Claude Code

dev0Guy and others added 4 commits November 24, 2026 21:42
…hines

- Engine split into grid → model → ports → engine → adapters → app layers,
  with policies (fifo, fifo_strict, sjf, best_fit, random), arrival sources
  (poisson, batch), usage generators (random, deeprm) and observers
  (viz, metrics, log), a spec-based CLI and an adapter registry.
- Machine time is a sliding window: advancing a tick moves a head index and
  rows are compacted once every n_ticks ticks, instead of shifting every
  grid each tick (300 machines × 1000 ticks: ~415 ms → ~6 ms per episode,
  identical results).
- C ABI (src/app/capi.zig, `zig build lib`) and the sched_env Python
  package: Gymnasium env with zero-copy NumPy views and action masks,
  stepping of built-in policies, a matplotlib visualizer, and a
  Stable-Baselines3 MaskablePPO training example.
- Benchmarks (SIMD, engine, CPU-vs-GPU fit mask), integration and Python
  tests, and docs (guide, python, architecture, data layout).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- tests/scenario_test.zig: 18 hand-worked scenarios with exact expected
  placements, waits, makespan and reward (blocking, multi-machine, exact
  fit, time-varying profiles, per-resource limits, backfill vs strict
  FIFO, SJF, best_fit, illegal actions, action masks, admission and
  rejection reasons, truncation, skip_empty, reward kinds, reset).
- Property test: 20 random episodes with random, sometimes illegal,
  actions checked at every step against a naive reference simulator
  (free capacity per tick, legality, reward, makespan); small horizon so
  the sliding window compacts often.
- Wire the new test into `zig build test`; document test coverage in
  docs/guide.md and CLAUDE.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Policies: `tetris` (multi-resource alignment over the job's whole
  duration, Grandl et al. 2014) and `tetris_srpt` (alignment minus
  weight·ε·duration, the paper's SRPT combination; default weight 1.5).
  Policy specs now accept options: any numeric field of a policy struct,
  e.g. `--policy tetris_srpt:weight=2` (also from Python).
- Arrivals: `mmpp` (two-state Markov-modulated Poisson, short-range
  bursts), `pareto` (heavy-tailed gaps) and `selfsimilar` (Pareto ON/OFF
  sources, long-range dependent, H = (3 − α)/2). Unit test checks mean
  rate, determinism and index of dispersion across window sizes.
- Reward is now a port (`ports.Reward`, `engine.setReward`) receiving the
  waiting and running jobs; adapters `neg_in_system` (default),
  `neg_waiting`, `zero` and `slowdown` (DeepRM −Σ 1/T_j, optional bounded
  slowdown via min_dur). Running jobs are tracked as `RunningJob`.
- CLI `--reward <spec>`, C API reward spec string, Python `reward=` takes
  the same specs.
- Scenario tests for slowdown, Tetris vs best_fit, tetris_srpt weights;
  docs: policies, arrival burstiness and reward tables, trade-off results.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- `trace` observer (src/adapters/observers/trace.zig): writes every event
  plus each job's demand profile and the machine capacities as JSON Lines
  (`--observe trace:path=run.jsonl`), self-contained per episode.
- C API `sim_trace_open/close`; Python `env.start_trace()/stop_trace()`.
- Python `sched_env.replay.load_trace` rebuilds one frame per tick from a
  trace without the simulator; `python/examples/replay.py` lists episodes
  and animates them with the existing visualizer.
- Tests: Zig test parses every line and checks event counts; Python test
  checks replayed frames and stats match a live run exactly.
- Docs: recording/replay section, observer tables.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sourcery-ai

sourcery-ai Bot commented Nov 24, 2026

Copy link
Copy Markdown

Sorry @dev0Guy, your pull request is larger than the review limit of 150,000 diff characters

dev0Guy and others added 3 commits November 24, 2026 23:01
Runs on pushes to main/setup and on pull requests, on ubuntu-latest
(x86-64, AVX2) and macos-latest (arm64, NEON) so both SIMD widths are
covered: zig build test, the same tests with -Dlanes=32, a smoke run of
every policy × arrival process with the trace observer, libsched, the
Python tests (without SB3/PyTorch) and a replay of the recorded trace.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…re" memo

- Jobs keeps waiting jobs in a doubly linked list in (arrival, slot) order:
  oldest() is O(1) and `fifo` stops at the first job that fits instead of
  scanning the whole pool (same tie-breaking as before).
- Jobs.blocked: one bit per job meaning "fits on no machine this tick".
  Capacity only shrinks within a tick, so the bit stays valid until the
  engine clears it on tick advance. Used by rules.firstMachine, the action
  mask and the pair-scanning policies (best_fit, tetris, random).
- Results are identical (56 reference episodes across all policies and
  workloads diff clean). fifo up to 11.9x faster (300 machines, pool 1024:
  1683 → 141 ms), sjf up to 5.1x; best_fit/tetris unchanged on large
  clusters (dominated by scoring fitting pairs).
- Tests: randomized list-order test against brute force; the reference
  property test now also checks firstMachine and the memo every step.
- A per-(job, machine) fit cache was also tried and measured slower (the
  fit test costs about as much as a lookup), so it is not included;
  documented in docs/data-layout.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
PairCache keeps (job, machine) scores across decisions and recomputes only
the column of the machine that changed, using new Machines.epoch/stamp
change counters (engine calls touch() on placement). Decisions are
bit-identical to a full scan; a property test compares against a fresh
policy at every step. 300 machines: best_fit 11x, tetris 14x,
tetris_srpt 5.7x faster.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dev0Guy
dev0Guy merged commit 0d30722 into main Nov 25, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant