Skip to content

Repository files navigation

City / Car RL Prototype

alt text

Table of Contents

Overview

This repository contains prototypes for 2D autonomous driving and combat simulations. It is intended as a modular research and prototyping workspace for urban-style agent environments.

Goals

  • Provide a modular simulation baseline for research and experimentation.
  • Offer runnable demos and scaffolds for training RL agents (PPO, self-play, neuroevolution).

Design notes

  • Rendering: arcade for the city demo (sprite performance); pygame kept for smaller experiments.
  • Physics: kinematic (forward speed, heading, steering), deterministic and debuggable.
  • Sensors: LIDAR-like rays in the pygame sandbox; arcade demo prepared for sensor extensions.

Arcade demo (how it works)

  • CityMap.generate() creates a tile grid of roads and buildings and chooses spawn points.
  • Car sprites implement simple steering and shooting.
  • Arena contains the main loop: AI update, bullet spawning, and collision checks.

Extensions & Roadmap

Prioritized extension plans useful for next-stage development.

High priority

  • Core RL Environment: Gym-compatible CityDriveEnv (observations: N-ray LIDAR, speed, heading error; actions: steering, throttle).
  • PPO Trainer & Training Harness: vectorized envs, TensorBoard logging, checkpointing.
  • Performance & Headless Training: headless stepping and multiprocessing vectorized envs.

Medium priority

  • Sensors & Perception: 5–7 ray LIDAR in arcade and optional camera input.
  • Self-play & Combat: opponent pools, league training, reward shaping for combat.
  • Procedural City & Missions: spline roads, intersections, missions (capture/escort/delivery).

Low / Long term

  • Visuals & UX polish: sprites, particles, HUD, themes.
  • Research extensions: SAC/TD3, population-based training, domain randomization.

Implementation guidance & conventions

  • Decouple simulation from rendering; provide step(action) for headless training and render() for visualization.
  • Deterministic seeding, unit tests for physics and sensors, and reproducible checkpoints.

Core concepts & architecture

  • Decoupled simulation and rendering to enable headless, high-throughput training.
  • Deterministic kinematic bicycle model (bounded steering rate, friction) for repeatability.
  • Compact, structured observations (LIDAR, speed, heading) for sample-efficient learning.
  • Reward-first design: sparse checkpoint bonuses with dense heading/progress guidance; avoid loopholes.

Tools, extensions & concepts used

  • Vectorized environments for parallel experience collection.
  • Observation wrappers (running mean/std normalization, clipping, frame-stacking).
  • Curriculum learning to scale difficulty automatically as agents improve.
  • Self-play and population methods for adversarial robustness.
  • Experiment tracking (TensorBoard / W&B) and hyperparameter search (Optuna).

Deep theory (RL foundations)

This section summarizes core theoretical concepts relevant for continuous-control RL.

Policy Gradient Theorem

The policy gradient theorem gives the gradient of expected return w.r.t. policy parameters θ:

∇_θ J(θ) = E_π [∇_θ log π_θ(a|s) * A^π(s,a)]

Estimators use Monte Carlo or bootstrapped advantages; variance reduction via baselines or learned critics is essential.

Actor-Critic and Bias–Variance Tradeoff

Actor-critic methods learn a value function V_ϕ(s) to estimate advantages: A ≈ r + γV(s') − V(s). Bootstrapping reduces variance but introduces bias; GAE(λ) interpolates the trade-off.

Generalized Advantage Estimation (GAE)

GAE computes exponentially-weighted returns:

Â_t^{GAE(γ,λ)} = ∑_{l=0}^{∞} (γλ)^l δ_{t+l}

where δ_t = r_t + γV(s_{t+1}) − V(s_t). Typical λ in [0.90,0.99].

PPO: clipping and trust regions

PPO uses a clipped surrogate objective to limit policy updates, keeping the importance ratio within [1−ε,1+ε] for stability.

Exploration, Representation & Model-based ideas

  • Entropy regularization for exploration; anneal entropy over training.
  • Representation learning (contrastive, bisimulation) improves transfer.
  • Model-based components (short-horizon MPC, learned dynamics ensembles) can improve sample efficiency.

Evaluation & ethics

  • Report mean ± SEM across multiple seeds and perform ablation studies.
  • Use videos for qualitative analysis; log collisions, lap time, and success rates.
  • Ethics: clearly state intended uses and avoid distributing combat-capable models without caution.

If you want a condensed one-page executive summary or a PDF/diagram version of this design, tell me which format and I will generate it.


Last update: README reformatted into a professional layout. Project: City / Car RL Prototype

Overview

  • This repository contains several prototypes for 2D autonomous driving and combat simulations[in progress] Goals
  • Provide a modular simulation baseline for research and experimentation with autonomous agents in urban-style environments.
  • Offer a runnable visual demo and a scaffold for training RL agents (PPO / self-play / neuroevolution).

About this project This repository is a research and prototyping workspace for 2D autonomous agents operating in dense urban-like environments. The aim is to provide modular simulation components (map generation, physics, sensors, agents) and a flexible training pipeline so researchers or engineers can iterate rapidly between environment design and algorithmic experiments.

Core concepts and architectural choices

  • Decoupled simulation and rendering: core simulation (state updates, collision, sensors) must be independent of any rendering backend. This enables headless, high-throughput training while preserving the ability to visualize individual rollouts for debugging.
  • Deterministic, testable physics: use a kinematic bicycle model with bounded steering rate and friction for repeatability and stability during training.
  • Compact, informative observations: prefer low-dimensional, structured observations (LIDAR rays, speed, heading error) for fast learning, with optional high-dimensional visual inputs for advanced policies.
  • Reward-first design: shape rewards to convey the task while avoiding exploitative loopholes; use sparse checkpoint bonuses plus dense guidance (heading, progress).

Extensions, tools and concepts used to make this work

  • Vectorized environments: collect rollouts from many parallel instances (multiprocessing or multiprocessing spawn) to increase sample efficiency.
  • Headless stepping: simulation core exposes a step(action) API that does not rely on graphics; optionally a render() method wraps the renderer.
  • Observation wrappers: normalize observations (running mean/std), clip values, and optionally stack frames or add action-history.
  • Curriculum learning: automatically increase environment difficulty based on agent performance (e.g., increasing traffic density or obstacle count once success thresholds are met).
  • Self-play & population methods: for adversarial tasks, maintain a pool of opponents and sample them during training; combine with league training strategies to avoid regressions.
  • Checkpointing & experiment tracking: integrate with TensorBoard or Weights & Biases; save model and optimizer state regularly and archive training configs.
  • Hyperparameter search: use Optuna or simple grid/random search for LR, clip range, entropy weight, etc.

Deep theory (concise but thorough) The following concepts explain why modern RL algorithms (e.g., PPO) work well for continuous-control tasks and outline advanced ideas for improving robustness and generalization.

  1. Policy Gradient Theorem
  • The policy gradient theorem states that the gradient of the expected return with respect to policy parameters θ is an expectation of the advantage-weighted policy score: ∇_θ J(θ) = E_π [∇_θ log π_θ(a|s) * A^π(s,a)]
  • Estimators use Monte Carlo or bootstrapped advantages; variance reduction methods (baseline subtraction, critic) are essential.
  1. Actor-Critic and Variance-Bias tradeoff
  • Actor-critic methods learn a parameterized value function V_ϕ(s) to estimate advantages A ≈ r + γV(s') − V(s). Bootstrapping reduces variance but introduces bias; GAE(λ) interpolates between high-bias/low-variance (λ→0) and low-bias/high-variance (λ→1).
  1. Generalized Advantage Estimation (GAE)
  • GAE computes exponentially-weighted returns to trade off bias and variance: Â_t^{GAE(γ,λ)} = ∑_{l=0}^{∞} (γλ)^l δ_{t+l} *** End Patch
  • Tuning λ is critical: typical values 0.90–0.99.

About

an attempt to learn rl

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages