RamK2006/Event-Driven-Congestion-Intelligence-System

GitHub: RamK2006/Event-Driven-Congestion-Intelligence-System

基于 LightGBM 与 HDBSCAN 的交通事件影响预测与响应平台,通过分析历史数据预测事故严重程度与清理时间并推荐分流策略。

Stars: 0 | Forks: 0

# 🛡️ 事件影响与响应智能平台 [![Python](https://img.shields.io/badge/Python-3.11+-3776AB?logo=python&logoColor=white)](https://python.org) [![Flask](https://img.shields.io/badge/Flask-3.x-000000?logo=flask&logoColor=white)](https://flask.palletsprojects.com) [![LightGBM](https://img.shields.io/badge/LightGBM-4.x-02569B?logo=microsoft&logoColor=white)](https://lightgbm.readthedocs.io) [![Leaflet](https://img.shields.io/badge/Leaflet-1.9-199900?logo=leaflet&logoColor=white)](https://leafletjs.com) [![License](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)

实时演示功能架构API 参考本地开发部署

## 📢 诚实的重构(必要披露) ## 实时演示 | 组件 | URL | |-----------|-----| | 🖥️ **前端仪表板** | [dashboard-tan-ten-51.vercel.app](https://dashboard-tan-ten-51.vercel.app) | | ⚙️ **后端 API** | [event-driven-congestion-intelligence.onrender.com](https://event-driven-congestion-intelligence.onrender.com) | | 💚 **健康检查** | [`/api/health`](https://event-driven-congestion-intelligence.onrender.com/api/health) | ## 功能 ### 🗺️ 事件地图 所有历史事件按经纬度绘制,按优先级进行颜色编码(高 = 红色,低 = 绿色),并带有 HDBSCAN 热点集群覆盖和标记聚类。 ### 🔮 新事件预测 提交位置、原因、车辆类型和日期时间 → 实时获取预测的优先级、封闭可能性、清理时间以及启发式分流建议。 ### 👮 警察局工作量 交互式条形图 + 各站点事件负载排名表,可按时间窗口过滤。识别超负荷的站点以进行资源再平衡。 ### 📊 历史趋势 随时间变化的事件计数,包含月度、小时和星期视图。可按原因、走廊和区域过滤。 ## 架构 ``` ┌─────────────────────────────────────────────────────┐ │ Data Layer │ │ Cleaned CSV → Feature Engineering → Target Derivation│ └──────────────────────┬──────────────────────────────┘ │ ┌──────────────────────┴──────────────────────────────┐ │ Modeling Layer │ │ LightGBM × 3 │ HDBSCAN Clustering │ Heuristic │ │ (Priority, │ (Hotspot Detection) │ Diversion │ │ Closure, │ │ Lookup │ │ Clearance) │ │ Table │ └──────────────────────┬──────────────────────────────┘ │ ┌──────────────────────┴──────────────────────────────┐ │ Flask REST API (Backend) │ │ /api/predict │ /api/events │ /api/workload │ ... │ └──────────────────────┬──────────────────────────────┘ │ ┌──────────────────────┴──────────────────────────────┐ │ Event Command Center Dashboard (Frontend) │ │ Leaflet Map │ Prediction Form │ Workload │ Trends │ │ │ │ Panel │ Panel │ │ Professional four-panel operations UI │ └─────────────────────────────────────────────────────┘ ``` ## 模型 | 模型 | 目标 | 算法 | 关键指标 | |-------|--------|-----------|------------| | **A** | 优先级(高/低) | LightGBM Classifier | Precision, Recall, F1, ROC-AUC | | **B** | 道路封闭(是/否) | LightGBM Classifier | Precision, Recall, F1, ROC-AUC | | **C** | 清理时间(分钟) | LightGBM Regressor | MAE, RMSE, Median AE | - **调优**:使用 Optuna 进行超参数搜索,具有可配置的试验预算(`--trials`) - **特征工程**:时间/周期性特征、位置密度、H3 单元格、历史频率信号、关键词标记、交互特征以及防泄漏的目标编码 - **数据集划分**:80/20,分类模型采用分层抽样 - **类别平衡**:在适用时使用 LightGBM 类别加权 / 正类缩放 - **产物**:`models/` 中拟合好的模型包,`data/feature_config.json` 中的特征契约,以及 `outputs/model_diagnostics/` 中的诊断信息 - **完整指标**:参见 `outputs/model_evaluation_report.json` ## API 参考 所有端点均由 Flask 后端提供。 ### 健康与状态 | 方法 | 端点 | 描述 | |--------|----------|-------------| | `GET` | `/api/health` | 健康检查 — 返回模型/数据加载状态 | | `GET` | `/api/filters` | 可用的过滤选项(原因、走廊、区域) | | `GET` | `/api/evaluation` | 模型评估指标 | ### 数据端点 | 方法 | 端点 | 描述 | |--------|----------|-------------| | `GET` | `/api/events` | 所有历史事件(用于地图显示) | | `GET` | `/api/events/summary` | 汇总摘要统计信息 | | `GET` | `/api/hotspots` | HDBSCAN 热点集群数据 | | `GET` | `/api/diversions` | 完整的分流查找表 | | `GET` | `/api/diversions/:corridor` | 特定走廊的分流建议 | ### 分析端点 | 方法 | 端点 | 查询参数 | 描述 | |--------|----------|-------------|-------------| | `GET` | `/api/workload` | `start_date`, `end_date` | 警察局工作量数据 | | `GET` | `/api/trends` | `cause`, `corridor`, `zone` | 历史趋势数据(月度、小时、每日) | ### 预测 | 方法 | 端点 | 请求体 | 描述 | |--------|----------|------|-------------| | `POST` | `/api/predict` | `{latitude, longitude, event_cause, veh_type, event_type, datetime_str}` | 返回优先级、封闭、清理时间预测 + 分流建议 |
预测请求示例 ``` curl -X POST https://your-backend-url/api/predict \ -H "Content-Type: application/json" \ -d '{ "latitude": 12.9716, "longitude": 77.5946, "event_cause": "accident", "veh_type": "heavy_vehicle", "event_type": "unplanned", "datetime_str": "2025-06-15T14:30:00" }' ```
## 本地开发 ### 前置条件 - Python 3.11+ - pip ### 设置 ``` # 1. Clone 仓库 git clone https://github.com//.git cd # 2. 创建并激活虚拟环境 python -m venv .venv # Windows: .\.venv\Scripts\activate # Linux/Mac: source .venv/bin/activate # 3. 安装依赖 pip install -r requirements.txt # 4. 运行完整的离线 pipeline (features + models + hotspots + diversions) # Cheap smoke run: python run.py --trials 10 # Final training run: python run.py --trials 100 # 或者仅使用显式 tuning budget 重新训练 models python src/train_models.py --trials 100 # 5. 启动服务器 (同时提供 API + dashboard) python src/server.py # 6. 打开浏览器 # 导航至 http://localhost:5000 ``` ## 部署 ### 后端 — Render(免费层) 1. 将此 repo 推送到 GitHub 2. 访问 [render.com](https://render.com) → **New** → **Web Service** 3. 连接您的 GitHub repository 4. 配置: - **Root Directory**:_(留空)_ - **Build Command**:`pip install -r requirements.txt` - **Start Command**:`gunicorn wsgi:app --bind 0.0.0.0:$PORT --workers 2 --timeout 120` - **Instance Type**:Free 5. 部署 → 复制生成的 URL(例如 `https://your-app.onrender.com`) 6. 验证:`curl https://your-app.onrender.com/api/health` ### 前端 — Vercel(免费层) 1. 访问 [vercel.com](https://vercel.com) → **New Project** 2. 导入相同的 GitHub repository 3. 配置: - **Root Directory**:`dashboard` - **Framework Preset**:Other 4. 部署前,在 `dashboard/index.html` 中添加后端 API 配置: 5. 部署 → 您的仪表板将通过全球 CDN 上线 ### 环境变量(可选) | 变量 | 默认值 | 描述 | |----------|---------|-------------| | `PORT` | `5000` | 服务器端口(由 Render 自动设置) | | `CORS_ORIGINS` | `*` | CORS 允许的来源,以逗号分隔 | ## 数据集 - **来源**:Astram 事件数据(匿名化) — 班加罗尔交通中断事件 - **行数**:8,173 条事件生命周期记录 - **覆盖范围**:94.3% 为计划外事件,5.7% 为计划内事件 - **未使用外部数据**(假日日历是一个静态硬编码的参考表) ## 技术栈 | 组件 | 技术 | 版本 | |-----------|-----------|---------| | Backend | Python, Flask, Flask-CORS | 3.11+, 3.x | | ML | LightGBM, scikit-learn, HDBSCAN | 4.x, 1.9+, 0.8+ | | Spatial | H3, Leaflet.js | 4.x, 1.9.4 | | Charts | Chart.js | 4.4.0 | | Explainability | SHAP | 0.52+ | | Production | Gunicorn | 21.2+ | ## 文件结构 ``` ├── dashboard/ # Frontend (static HTML/CSS/JS) │ ├── index.html # Event Command Center │ ├── style.css # Premium dark theme │ ├── app.js # Dashboard logic │ └── vercel.json # Vercel deployment config ├── src/ │ ├── __init__.py # Package init │ ├── data_pipeline.py # Data cleaning + feature engineering │ ├── train_models.py # LightGBM training + evaluation │ ├── clustering.py # HDBSCAN hotspot detection │ ├── diversion.py # Co-occurrence diversion heuristic │ └── server.py # Flask API server ├── data/ # Generated: cleaned datasets ├── models/ # Trained model files (.pkl) ├── outputs/ # Evaluation reports, clusters, diversions ├── run.py # Pipeline orchestrator ├── wsgi.py # WSGI entry point (gunicorn) ├── requirements.txt # Python dependencies ├── Procfile # Render/Heroku process file ├── render.yaml # Render Blueprint ├── runtime.txt # Python version spec ├── .gitignore # Git ignore rules └── README.md # This file ``` ## 已知限制 1. **分流建议**是启发式且基于共现的 — 它们展示的是历史上相关的替代走廊,而不是最短路径路由。未使用道路网络图。 2. **Render 免费层**可能会遇到冷启动(在不活动一段时间后,首次请求时约有 30 秒的启动时间)。 3. **清理时间预测**仅适用于有记录解决结果(封闭/已解决状态)的事件。根据设计,仍标记为“活跃”的事件被排除在此模型的训练之外,因为它们缺乏真实值清理持续时间。 ## License 本项目为 GridLock 黑客马拉松 构建。请查看 repository 了解 License 详情。
标签:AI, Flask, LightGBM, Python, Web, 交通管理, 数据, 无后门, 自动化代码审查, 逆向工具