A generative world model for simulating and visualizing urban system dynamics
UrbanSim WM enables policy experiments by predicting city-scale energy consumption, air quality, and mobility patterns. Built with a DreamerV3-style latent world model, it simulates counterfactual scenarios like car-free days or renewable energy transitions.
UrbanSim WM combines machine learning with urban systems modeling to answer questions like:
- What if we implement car-free days twice a week?
- How does renewable energy adoption affect air quality?
- What's the impact of combined policies on traffic and emissions?
- World Model: DreamerV3-style recurrent state space model (RSSM) for temporal dynamics
- Policy Simulation: Interactive sliders to test urban interventions
- Real-Time Visualization: Charts for PM2.5, energy consumption, and traffic
- Data Integration: Connectors for OpenAQ, mobility datasets, and energy grids
- Fully Dockerized: One command to run the entire stack
βββββββββββββββββββββββββββββββββββββββ
β Next.js 14 Dashboard (Frontend) β
β - Policy sliders & visualizations β
ββββββββββββββββ¬βββββββββββββββββββββββ
β HTTP/REST
ββββββββββββββββΌβββββββββββββββββββββββ
β FastAPI Backend (Python) β
β - /api/simulate endpoint β
β - Model inference wrapper β
ββββββββββββββββ¬βββββββββββββββββββββββ
β Model Loading
ββββββββββββββββΌβββββββββββββββββββββββ
β DreamerV3 World Model (PyTorch) β
β - Encoder β RSSM β Predictor β
ββββββββββββββββ¬βββββββββββββββββββββββ
β Training Data
ββββββββββββββββΌβββββββββββββββββββββββ
β ETL Data Pipelines β
β - WAQI (air quality, primary) β
β - OpenAQ (fallback) β
β - Mobility APIs β
β - Energy grid data β
βββββββββββββββββββββββββββββββββββββββFor a detailed, step-by-step guide with tips and troubleshooting, see the Quickstart:
- docs/QUICKSTART.md
For a deep-dive into what was implemented and why, see the Implementation Summary:
- docs/IMPLEMENTATION_SUMMARY.md
- docs/CODEBASE_STATUS.md β Current component statuses and links
- docs/FUTURE_ROADMAP.md β Planned phases and milestones
- docs/MODEL_INFERENCE_FLOW.md β End-to-end data β model flow
- docs/AIR_QUALITY_DATA_SOURCES.md β Air quality sources (WAQI primary, alternatives)
- docs/ENERGY_DATA_SOURCES.md β Energy data options for Pakistan
- docs/MOBILITY_DATA_SOURCES.md β Mobility/traffic data options for Pakistan
- Docker & Docker Compose
- Make (optional, for convenience commands)
- 8GB+ RAM recommended
git clone <repository-url>
cd urbansim-wmcp .env.example .env
# Edit .env and add your API keys (optional for demo)
# Key variables (see .env.example):
# - WAQI_API_TOKEN= # optional for real air quality
# - LOGS_DIR=./logs # persisted logs (mounted in Docker)
# - PROCESSED_DATA_DIR=./etl/processed_data # ETL outputs (mounted in Docker)
# - REDIS_URL=redis://redis:6379/0 # optional caching# Using Make (recommended)
make build
make up
# Or using Docker Compose directly
docker-compose build
docker-compose up -d- Frontend Dashboard: http://localhost:3000
- API Documentation: http://localhost:8000/docs
- API Health Check: http://localhost:8000/
urbansim-wm/
βββ backend/ # FastAPI backend service
β βββ app/
β β βββ main.py # Application entry point
β β βββ api/ # API endpoints
β β β βββ routes.py # Route aggregation
β β β βββ simulate.py # Simulation endpoint
β β β βββ retrain.py # Retraining endpoint
β β βββ models/ # Model wrappers
β β βββ etl/ # ETL sources (WAQI primary, OpenAQ fallback)
β β βββ core/ # Configuration
β βββ requirements.txt
β βββ Dockerfile
β
βββ training/ # Model training service
β βββ urban_world_model.py # Training harness
β βββ modules/
β β βββ encoder.py # Observation encoder
β β βββ rssm.py # Recurrent State Space Model
β β βββ predictor.py # Observation decoder
β βββ configs/
β β βββ base.yaml # Training hyperparameters
β βββ notebooks/ # Jupyter notebooks
β βββ checkpoints/ # Model checkpoints
β
βββ frontend/ # Next.js 14 frontend
β βββ app/ # App Router (Next.js 14)
β β βββ page.tsx # Main simulator page
β β βββ layout.tsx # Root layout
β β βββ globals.css # Global styles
β βββ components/ # React components
β β βββ PolicyControls.tsx
β β βββ AirQualityChart.tsx
β β βββ EnergyChart.tsx
β β βββ TrafficChart.tsx
β βββ package.json
β βββ Dockerfile
β
βββ docker-compose.yml # Multi-service orchestration
βββ Makefile # Convenience commands
βββ .env.example # Environment template
βββ README.md # This filemake help # Show all available commands
make build # Build all Docker containers
make up # Start all services
make down # Stop all services
make restart # Restart all services
make train # Run model training
make logs # View logs from all services
make clean # Remove containers and volumes
make etl # Run data fetchers# Backend only
make backend
docker-compose logs -f backend
# Frontend only
make frontend
docker-compose logs -f frontend
# Training
make train# Install dependencies
make install
# Or manually:
cd backend && pip install -r requirements.txt
cd frontend && npm install
cd training && pip install -r requirements.txt
# Run backend
cd backend
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
# Run frontend
cd frontend
npm run dev
# Run training
cd training
python urban_world_model.pySimulate urban dynamics under a policy scenario.
Request Body:
{
"city": "Lahore",
"start_time": "2025-01-01T00:00:00Z",
"horizon_hours": 48,
"policy": {
"car_free_ratio": 0.2,
"renewable_mix": 0.35
}
}Response:
{
"city": "Lahore",
"start": "2025-01-01T00:00:00Z",
"horizon": 48,
"policy": {
"car_free_ratio": 0.2,
"renewable_mix": 0.35
},
"simulated": [
{
"hour": 0,
"pm25": 65.3,
"energy_mwh": 1150.2,
"traffic_index": 0.75
},
...
],
"meta": {
"generated_at": 1704067200,
"model_version": "v0.1.0"
}
}Health check endpoint.
Trigger background model retraining.
Request Body:
{
"config_path": "training/configs/base.yaml"
}Response (example):
{
"status": "retrain_started",
"job_id": "a1b2c3d4",
"config": "training/configs/base.yaml",
"logs": "training/logs/train_a1b2c3d4.log",
"message": "Retraining job started in background"
}Notes:
- Logs stream to
training/logs/train_<job_id>.log. - On success, latest checkpoints are copied to
training/checkpoints/latest.jsonandtraining/checkpoints/latest.pth.
# Run all tests (TODO: implement)
make test
# Or manually:
cd backend && pytest
cd frontend && npm test- Primary source: WAQI Feed API (
backend/app/etl/waqi.py).- Configure
WAQI_API_TOKENin.envfor live data. - Outputs JSON to
PROCESSED_DATA_DIR(default./etl/processed_data). - Includes real-time PM2.5, forecast stats, min/max, and timestamps.
- Configure
- Fallback: OpenAQ v3 (
backend/app/etl/openaq.py).- Robust sensor mean with outlier guards and request backoff.
Other planned integrations:
- Mobility: TomTom/HERE
- Energy: EIA/ENTSOβE or city providers
- Weather: OpenWeather or NOAA
- Encoder: Maps multi-modal observations (PM2.5, energy, traffic) to latent embeddings
- RSSM (Recurrent State Space Model): Learns temporal dynamics
- Deterministic state (GRU): Captures sequential patterns
- Stochastic state (VAE): Models uncertainty
- Predictor: Decodes latent states back to observations
π For a complete deep dive into the DreamerV3 implementation, see docs/DREAMERV3_IMPLEMENTATION.md. This document covers:
- Architecture details (Encoder, RSSM, Predictor)
- Training process and techniques (KL annealing, gradient clipping, LR scheduling)
- Inference flow (from ETL data to predictions)
- Code locations and how everything works together
# Run training container
make train
# Or with custom config
docker-compose --profile training run --rm training python urban_world_model.pyTraining hyperparameters are in training/configs/base.yaml.
Training loop includes dataset loading, validation, checkpointing, KL annealing, gradient clipping, LR scheduling, and TensorBoard logging.
- Create a new fetcher in
etl/ - Update
training/urban_world_model.pyto load the data - Modify encoder input dimensions in
training/configs/base.yaml
- Encoder:
training/modules/encoder.py - RSSM:
training/modules/rssm.py - Predictor:
training/modules/predictor.py
- Add new charts in
frontend/components/ - Modify policy controls in
frontend/components/PolicyControls.tsx - Tailwind styling is mobile-responsive. Adjust global styles in
frontend/app/globals.css. - API calls use relative paths (
/api/...) with Next.js rewrites for Docker.
docker-compose logs backend
# Check for missing environment variables or port conflicts- In Docker, frontend uses relative paths and Next.js rewrites; ensure the backend service is named
backendand healthy:curl http://backend:8000/from within the container. - On host, open
http://localhost:3000and verify network tab calls are proxied to the backend.
- Logs: check mounted directory specified by
LOGS_DIR(default./logs). - ETL outputs: check
PROCESSED_DATA_DIR(default./etl/processed_data). - Ensure volumes are mounted in
docker-compose.yml(e.g.,./logs:/logs,./etl/processed_data:/etl/processed_data).
# Clean rebuild
make clean
make buildFull roadmap is maintained in docs/FUTURE_ROADMAP.md. Highlights:
- Real mobility data integration (Google CSV β TomTom/HERE) [see docs/MOBILITY_DATA_SOURCES.md]
- Real energy data integration (NTDC/NEPRA reports β EIA proxies) [see docs/ENERGY_DATA_SOURCES.md]
- ETL caching/optimization (Redis)
- Input validation polish for
/api/simulate - UI scenario comparison and export (CSV/JSON)
- Initial test scaffolding (backend & frontend)
- Multi-source data aggregation across AQ/Mobility/Energy [see docs/AIR_QUALITY_DATA_SOURCES.md, docs/MOBILITY_DATA_SOURCES.md, docs/ENERGY_DATA_SOURCES.md]
- Advanced explainability from model internals
- CI/CD pipeline and observability (metrics, traces)
- Broader test suite (unit, integration, e2e)
- Performance optimization and caching for inference
- Optional: TorchServe/gRPC; AuthN/Z; spatial maps and district overlays
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- DreamerV3: Hafner et al. (2023) - "Mastering Diverse Domains through World Models"
- WAQI: Real-time air quality feed
- OpenAQ: Open air quality data for all (fallback)
- FastAPI: Modern, fast web framework for Python
- Next.js: React framework for production
- Recharts: Composable charting library
For questions or support, please open an issue on GitHub.
Maintainer:
- Adnan Sattar β LinkedIn
Built with β€οΈ for smarter, more sustainable cities

