| Crates.io | operant-core |
| lib.rs | operant-core |
| version | 0.3.3 |
| created_at | 2025-12-02 19:11:22.626481+00 |
| updated_at | 2025-12-02 19:11:22.626481+00 |
| description | Core traits and vectorization for Operant RL library |
| homepage | https://github.com/galenoshea/operant |
| repository | https://github.com/galenoshea/operant |
| max_upload_size | |
| id | 1962437 |
| size | 12,387 |
High-performance SIMD-optimized Gymnasium-compatible reinforcement learning environments in Rust with Python bindings.
~600x faster than Gymnasium for vectorized environments.
Operant provides native Rust implementations of Gymnasium environments with:
Unlike PufferLib which wraps existing Gymnasium environments for vectorization, Operant implements environments natively in Rust for maximum performance.
| Environment | State Dim | Action Space | Physics | Reward |
|---|---|---|---|---|
| CartPole | 4 | Discrete(2) | Inverted pendulum balance | +1 per step alive |
| MountainCar | 2 | Discrete(3) | Sparse reward climbing | -1 per step |
| Pendulum | 3 | Continuous(1) | Swing-up control | Cost minimization |
All environments provide Gymnasium-compatible observation_space and action_space properties for easy integration with RL frameworks.
CartPole Benchmark (4096 envs)
============================================================
Operant... 97.54M steps/sec
Gymnasium... 0.16M steps/sec
Speedup: ~600x faster than Gymnasium
pip install operant
cargo add operant
Requires Rust nightly and Poetry:
# 1. Install Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# 2. Install Poetry
curl -sSL https://install.python-poetry.org | python3 -
# 3. Setup project
poetry install
poetry run maturin develop --release
import numpy as np
from operant.envs import CartPoleVecEnv
# Create 4096 parallel environments
num_envs = 4096
env = CartPoleVecEnv(num_envs)
obs, info = env.reset(seed=42) # Shape: (4096, 4)
for step in range(10000):
actions = np.random.randint(0, 2, size=num_envs, dtype=np.int32)
obs, rewards, terminals, truncations, info = env.step(actions)
For heavier environments or large batch sizes, enable parallel execution:
# Use 4 worker threads for parallel step execution
env = CartPoleVecEnv(num_envs=8192, workers=4)
from operant.envs import MountainCarVecEnv
num_envs = 4096
env = MountainCarVecEnv(num_envs)
obs, info = env.reset(seed=42) # Shape: (4096, 2)
for step in range(10000):
actions = np.random.randint(0, 3, size=num_envs, dtype=np.int32)
obs, rewards, terminals, truncations, info = env.step(actions)
from operant.envs import PendulumVecEnv
num_envs = 4096
env = PendulumVecEnv(num_envs)
obs, info = env.reset(seed=42) # Shape: (4096, 3) - [cos(θ), sin(θ), θ_dot]
for step in range(10000):
actions = np.random.uniform(-2.0, 2.0, size=num_envs).astype(np.float32)
obs, rewards, terminals, truncations, info = env.step(actions)
use operant::{CartPole, VecEnv};
fn main() {
// Create 1024 parallel environments
let mut env = CartPole::new(1024);
// Reset all environments
let obs = env.reset();
// Step with actions
let actions = vec![0; 1024];
let (obs, rewards, terminals, truncations) = env.step(&actions);
}
from operant.utils import Logger
# Context manager automatically handles cleanup
with Logger(csv_path="training.csv") as logger:
for step in range(1000):
# ... training loop ...
logger.log(steps=num_envs, reward=mean_reward, length=mean_length)
Old imports (deprecated):
from operant import PyCartPoleVecEnv, Logger
New imports (recommended):
from operant.envs import CartPoleVecEnv
from operant.utils import Logger
The old import style will continue to work until v0.4.0, but will emit deprecation warnings.
Compare Operant at 4096 environments:
poetry run python benches/cartpole_benchmark.py
Test across multiple environment counts (1, 16, 256, 1024, 4096):
poetry run python benches/cartpole_benchmark.py --all
Operant uses a Struct-of-Arrays (SoA) memory layout with SIMD vectorization:
# Run tests
poetry run pytest
# Build in debug mode (faster compilation)
poetry run maturin develop
# Build in release mode (faster runtime)
poetry run maturin develop --release
MIT