Skip to content

Port the cart pole example to pyo3 and gymnasium - #112

Merged
TLmaK0 merged 1 commit into
developfrom
issue-95-openai-cartpole
Nov 25, 2026
Merged

TLmaK0 merged 1 commit into
developfrom
issue-95-openai-cartpole

Conversation

@TLmaK0

@TLmaK0 TLmaK0 commented Nov 25, 2026

Copy link
Copy Markdown
Owner

What changes

  • examples/openai.rs (cart pole) no longer uses cpython/python3_sys (which are not dependencies). It now uses the same pyo3 0.22 setup as the lunar lander examples and the gymnasium API: env.reset() returns (obs, info) and env.step() returns (obs, reward, terminated, truncated, info). It runs CartPole-v1 (v0 prints a gymnasium deprecation warning). The champion threshold is 475 and the telemetry max_fitness is 500.
  • The episode loop is in examples/cart_pole_common/mod.rs, which has no Python in it, and tests/cart_pole_tests.rs tests it with a fake environment. This follows the lunar_lander_common pattern. The old loop called step once before it first activated the network, and it ignored the reset observation. The new loop activates on the reset observation first.
  • The example uses one gymnasium env and sets threads() = 1, so concurrent episodes cannot interleave on that env. The champion is rendered in a separate render_mode="human" env. If rendering fails (no pygame or no display), the example reports the error and does not panic.
  • Removed the 4 pre-existing clippy warnings in examples/openai_lunar_lander.rs. create_organism now sits in a small module with #![allow(clippy::useless_conversion)], because that warning comes from the code pyo3 0.22's #[pyfunction] generates. An allow on the fn itself does not reach that code.
  • README: added how to run the cart pole example.

Verification

  • cargo check --release --features openai --examples passes with the pyo3 config stub, so CI: check the openai-feature examples #96 can enable it in CI. cargo clippy --release --features openai --examples shows 0 warnings in openai, openai_lunar_lander and test_champion.
  • Real run: in a container with Python 3.13 and gymnasium installed by pip, cargo run --release --example openai --features openai reached a 500 reward in generation 1 in each of 3 runs. CartPole is easy even for the initial direct input→output networks. Rendering failed in the container because pygame is not installed, and the example reported the error as intended.
  • Gates (no feature): cargo fmt -- --check OK; cargo clippy --release --all-targets gives the same warning count as develop, none of them in the new files; cargo build --release --examples OK; cargo test --release --no-fail-fast all green (new cart_pole_tests: 4 passed).

Closing criterion

cargo check --release --features openai --example openai succeeds. The whole --examples check succeeds too.

ref #95
Part of epic #55

🤖 Generated with Claude Code

examples/openai.rs used the cpython and python3_sys crates, which are not
dependencies, so `cargo check --features openai --examples` failed. It now
uses the pyo3 setup of the lunar lander examples and the gymnasium API
(reset() -> (obs, info), step() -> 5-tuple) on CartPole-v1.

The episode loop lives in examples/cart_pole_common (no Python) and is
tested by tests/cart_pole_tests.rs with a fake environment. It also fixes
the old loop, which stepped once before ever activating the network and
ignored the reset observation.

Also removes the clippy warnings of openai_lunar_lander.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@TLmaK0
TLmaK0 merged commit ed13c59 into develop Nov 25, 2026
7 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