|
| 1 | +# UXsim: Network Traffic Flow Simulator |
| 2 | + |
| 3 | +UXsim is a pure Python macroscopic and mesoscopic network traffic flow simulator for large-scale (city-scale) traffic phenomena simulation. It is designed for scientific and educational purposes with simple, lightweight, and customizable features. |
| 4 | + |
| 5 | +Always reference these instructions first and fallback to search or bash commands only when you encounter unexpected information that does not match the info here. |
| 6 | + |
| 7 | +## Working Effectively |
| 8 | + |
| 9 | +### Bootstrap and Setup |
| 10 | +- Install Python 3.9 or higher (Python 3.12+ recommended) |
| 11 | +- Bootstrap the repository: |
| 12 | + ```bash |
| 13 | + cd /home/runner/work/UXsim/UXsim |
| 14 | + python -m pip install --upgrade pip |
| 15 | + pip install . |
| 16 | + ``` |
| 17 | + - **NEVER CANCEL**: Installation takes 2-4 minutes. Set timeout to 300+ seconds. |
| 18 | + |
| 19 | +### Install Test Dependencies |
| 20 | +- For running tests, install additional dependencies: |
| 21 | + ```bash |
| 22 | + pip install pytest pytest-rerunfailures pytest-xdist setuptools |
| 23 | + ``` |
| 24 | +- For advanced examples (optional): |
| 25 | + ```bash |
| 26 | + pip install osmnx torch streamlit gymnasium deap geopandas shapely |
| 27 | + ``` |
| 28 | + |
| 29 | +### Quick Validation |
| 30 | +- Verify installation works: |
| 31 | + ```bash |
| 32 | + python -c "import uxsim; print('UXsim import successful')" |
| 33 | + ``` |
| 34 | +- Run simple example to validate basic functionality (1-2 seconds): |
| 35 | + ```bash |
| 36 | + python demos_and_examples/example_00en_simple.py |
| 37 | + ``` |
| 38 | + |
| 39 | +## Testing and Validation |
| 40 | + |
| 41 | +### Example Tests |
| 42 | +- **NEVER CANCEL**: Full example test suite takes 12+ minutes. Set timeout to 20+ minutes: |
| 43 | + ```bash |
| 44 | + pytest tests/test_examples.py --durations=10 -v |
| 45 | + ``` |
| 46 | +- Test specific examples (faster, 1-60 seconds each): |
| 47 | + ```bash |
| 48 | + pytest tests/test_examples.py -v -k "example_00en_simple" |
| 49 | + pytest tests/test_examples.py -v -k "example_01en_basic" |
| 50 | + ``` |
| 51 | + |
| 52 | +### Verification Tests |
| 53 | +- **NEVER CANCEL**: Each verification test file takes 30-120 seconds. Set timeout to 300+ seconds: |
| 54 | + ```bash |
| 55 | + pytest tests/test_verification_straight_road.py -v --durations=5 |
| 56 | + pytest tests/test_verification_sioux_falls.py -v --durations=5 |
| 57 | + pytest tests/test_verification_exceptional.py -v --durations=5 |
| 58 | + ``` |
| 59 | + |
| 60 | +### Function Tests |
| 61 | +- Test core functionality (30-60 seconds): |
| 62 | + ```bash |
| 63 | + pytest tests/test_other_functions.py -v --durations=5 |
| 64 | + ``` |
| 65 | + |
| 66 | +### Parallel Testing |
| 67 | +- Use parallel testing to speed up test suites: |
| 68 | + ```bash |
| 69 | + pytest -n auto tests/test_examples.py --durations=0 -v |
| 70 | + ``` |
| 71 | + |
| 72 | +## Manual Validation Scenarios |
| 73 | + |
| 74 | +### Basic Simulation Scenario |
| 75 | +Always validate changes by running a complete simulation scenario: |
| 76 | + |
| 77 | +1. **Simple Linear Network**: |
| 78 | + ```bash |
| 79 | + python demos_and_examples/example_00en_simple.py |
| 80 | + ``` |
| 81 | + - Expected: Completes in 1-2 seconds |
| 82 | + - Validates: Basic simulation, vehicle movement, statistics output |
| 83 | + |
| 84 | +2. **Basic Network with Visualization**: |
| 85 | + ```bash |
| 86 | + python demos_and_examples/example_01en_basic.py |
| 87 | + ``` |
| 88 | + - Expected: Completes in 15-20 seconds |
| 89 | + - Validates: Network creation, demand generation, simulation, analysis, visualization |
| 90 | + |
| 91 | +3. **Large-Scale Scenario**: |
| 92 | + ```bash |
| 93 | + python demos_and_examples/example_23en_huge_scale_simlation_at_Chicago.py |
| 94 | + ``` |
| 95 | + - **NEVER CANCEL**: Takes 40-60 seconds. Set timeout to 120+ seconds. |
| 96 | + - Validates: Large network handling, performance optimization |
| 97 | + |
| 98 | +### Interactive Testing |
| 99 | +For changes to analysis or visualization: |
| 100 | +```bash |
| 101 | +python -c " |
| 102 | +from uxsim import * |
| 103 | +W = World(name='test', deltan=5, tmax=600, print_mode=1, save_mode=0, show_mode=0) |
| 104 | +W.addNode('orig', 0, 0) |
| 105 | +W.addNode('dest', 1000, 0) |
| 106 | +W.addLink('link1', 'orig', 'dest', length=1000, free_flow_speed=20) |
| 107 | +W.adddemand('orig', 'dest', 0, 300, 0.5) |
| 108 | +W.exec_simulation() |
| 109 | +W.analyzer.print_simple_stats() |
| 110 | +print('Manual validation successful') |
| 111 | +" |
| 112 | +``` |
| 113 | + |
| 114 | +## Build Timings and Expectations |
| 115 | + |
| 116 | +### Installation |
| 117 | +- `pip install .`: 2-4 minutes |
| 118 | +- `pip install pytest pytest-xdist`: 1-2 minutes |
| 119 | +- `pip install osmnx torch streamlit`: 3-5 minutes (optional advanced dependencies) |
| 120 | + |
| 121 | +### Test Suite Timings |
| 122 | +- **Simple examples** (example_00en, example_01en): 1-20 seconds each |
| 123 | +- **Medium examples** (bottleneck, signal control): 20-60 seconds each |
| 124 | +- **Complex examples** (gridlock, large networks): 60-120 seconds each |
| 125 | +- **Full example test suite**: 12+ minutes total |
| 126 | +- **Verification tests**: 30-120 seconds per test file |
| 127 | +- **Function tests**: 30-60 seconds total |
| 128 | + |
| 129 | +### Individual Test Warnings |
| 130 | +- **NEVER CANCEL** any test taking longer than expected |
| 131 | +- Tests involving large networks or complex scenarios may take several minutes |
| 132 | +- Chicago large-scale example specifically takes 40-60 seconds |
| 133 | + |
| 134 | +## Common Issues and Dependencies |
| 135 | + |
| 136 | +### Optional Dependencies |
| 137 | +Some examples require additional packages: |
| 138 | + |
| 139 | +- **OSM Import Examples**: |
| 140 | + ```bash |
| 141 | + pip install osmnx geopandas shapely |
| 142 | + ``` |
| 143 | + - `example_16en_import_from_OpenStreetMap.py` fails without osmnx |
| 144 | + |
| 145 | +- **Deep Learning Examples**: |
| 146 | + ```bash |
| 147 | + pip install torch gymnasium deap |
| 148 | + ``` |
| 149 | + - `example_12en_*_pytorch*.py` and `example_14en_*_pytorch.py` need pytorch |
| 150 | + |
| 151 | +- **Interactive Examples**: |
| 152 | + ```bash |
| 153 | + pip install streamlit |
| 154 | + ``` |
| 155 | + - `example_27en_interactive_simulation_by_streamlit.py` needs streamlit |
| 156 | + |
| 157 | +### GUI Limitations |
| 158 | +- GUI viewer examples (`example_17en_result_GUI_viewer_*.py`, `example_18en_result_GUI_viewer_*.py`) require X11 display |
| 159 | +- These examples are not testable in headless GitHub Actions environment |
| 160 | +- They are excluded from automated tests but can be run locally with display |
| 161 | + |
| 162 | +### Test Exclusions |
| 163 | +The following examples are excluded from automated testing: |
| 164 | +- `example_14en_*_Deep_Reinforcement_Learning_pytorch.py` (takes too much time) |
| 165 | +- `example_17en_result_GUI_viewer_*.py` (requires GUI) |
| 166 | +- `example_18en_result_GUI_viewer_*.py` (requires GUI) |
| 167 | + |
| 168 | +## Key Project Structure |
| 169 | + |
| 170 | +### Core Files |
| 171 | +``` |
| 172 | +uxsim/ |
| 173 | +├── __init__.py # Package initialization, version info |
| 174 | +├── uxsim.py # Main simulation engine (~2300 lines) |
| 175 | +├── analyzer.py # Results analysis and visualization |
| 176 | +├── utils.py # Utility functions |
| 177 | +├── scenario_reader_writer.py # I/O for scenarios |
| 178 | +└── [subdirectories]/ # Specialized modules (OSM, GUI, etc.) |
| 179 | +``` |
| 180 | + |
| 181 | +### Important Directories |
| 182 | +``` |
| 183 | +demos_and_examples/ # 40+ example scripts and Jupyter notebooks |
| 184 | +├── demo_notebook_*.ipynb # Interactive tutorial notebooks |
| 185 | +├── example_00en_simple.py # Simplest example (start here) |
| 186 | +├── example_01en_basic.py # Basic example with visualization |
| 187 | +├── example_*en_*.py # English examples by topic |
| 188 | +└── example_*jp_*.py # Japanese examples |
| 189 | +
|
| 190 | +tests/ # Comprehensive test suite |
| 191 | +├── test_examples.py # Tests all example scripts |
| 192 | +├── test_verification_*.py # Mathematical model verification |
| 193 | +└── test_other_functions.py # Core functionality tests |
| 194 | +``` |
| 195 | + |
| 196 | +### Configuration Files |
| 197 | +- `pyproject.toml`: Package configuration, dependencies, build settings |
| 198 | +- `.github/workflows/`: GitHub Actions CI/CD pipelines |
| 199 | +- `MANIFEST.in`: Additional files to include in package distribution |
| 200 | + |
| 201 | +## Validation Checklist |
| 202 | + |
| 203 | +When making changes to UXsim, always run: |
| 204 | + |
| 205 | +1. **Quick validation** (30 seconds): |
| 206 | + ```bash |
| 207 | + python -c "import uxsim; print('Import OK')" |
| 208 | + python demos_and_examples/example_00en_simple.py |
| 209 | + ``` |
| 210 | + |
| 211 | +2. **Core functionality** (2 minutes): |
| 212 | + ```bash |
| 213 | + pytest tests/test_other_functions.py -v |
| 214 | + ``` |
| 215 | + |
| 216 | +3. **Example validation** (12+ minutes - **NEVER CANCEL**): |
| 217 | + ```bash |
| 218 | + pytest tests/test_examples.py -v --durations=10 |
| 219 | + ``` |
| 220 | + |
| 221 | +4. **Mathematical verification** (5+ minutes): |
| 222 | + ```bash |
| 223 | + pytest tests/test_verification_straight_road.py -v |
| 224 | + pytest tests/test_verification_sioux_falls.py -v |
| 225 | + ``` |
| 226 | + |
| 227 | +## Development Notes |
| 228 | + |
| 229 | +### Code Organization |
| 230 | +- `uxsim.py` contains the core simulation engine and must only contain essential simulation code |
| 231 | +- Additional utilities should be added as submodules (like existing `Utilities/`, `OSMImporter/`, etc.) |
| 232 | +- Keep the main simulation code lightweight and focused |
| 233 | + |
| 234 | +### Performance Expectations |
| 235 | +- UXsim can simulate 60,000+ vehicles in city-scale networks in 30 seconds |
| 236 | +- Large metropolitan networks (1M+ vehicles) simulate in 40+ seconds |
| 237 | +- Performance scales roughly with vehicle count and network complexity |
| 238 | + |
| 239 | +### Memory Requirements |
| 240 | +- Typical simulations require minimal memory |
| 241 | +- Large-scale simulations (Chicago example) may use several hundred MB |
| 242 | +- Memory usage scales with vehicle count and simulation duration |
| 243 | + |
| 244 | +Always follow these instructions to ensure consistent, reliable development and testing of UXsim changes. |
0 commit comments