所有主指标均由版本化公开数据、验证集冻结对照和SHA-256绑定代码复算;同时公开更严格基线下的边际收益与峰值权衡。 Every headline metric is recomputed from versioned public data, a validation-frozen comparator, and SHA-256-bound code—with the harder comparator and peak trade-off disclosed.
截图来自增强公开数据包的留出测试轨迹和浏览器实测按钮联动。图中数值是可复现的离线场景输出,不是实时码头遥测、生产绩效或监管核证结果。
The screenshots show held-out replay from the enhanced public package and browser-verified button linkage. Values are reproducible offline scenario outputs—not live terminal telemetry, production performance, or regulatory assurance.
This project places port energy use, shore power, equipment allocation, delay, cost, and emissions inside one constrained environment. It connects dataset validation, reinforcement-learning training, a control-theory baseline, held-out evaluation, trajectory replay, model governance, and a bilingual cockpit. Its central goal is evidence: every important number should trace back to a split, a declared physical assumption, an algorithm action, and a persisted artifact.
为什么它不是普通大屏 / Why this is more than a dashboard
维度 / Dimension
实际实现 / What is implemented
实验内核 / Experiment core
Gymnasium v1/v2/v3 分层合同:19 维能碳基准、25 维逐日船舶活动增强、35 维实港接入合同;动作始终为 4 维连续或 81 个离散组合。 / Layered 19/25/35-observation Gymnasium contracts with the same four continuous controls or 81 explicit discrete actions.
算法矩阵 / Algorithm matrix
PPO、SAC、TD3、DQN 四种 RL 算法,加四步有限时域约束 MPC。 / Four RL algorithms—PPO, SAC, TD3, DQN—plus a constrained four-step finite-horizon MPC baseline.
训练边界 / Training boundary
train 无渲染拟合、validation 选型、完成后才在 test 生成轨迹。 / Non-rendering fit on train, selection on validation, and trajectory generation only during final test evaluation.
证据链 / Evidence chain
配置、随机种子、CSV/元数据/组合包 SHA-256、回调指标、checkpoint、模型哈希、测试与验证结果。 / Config, seed, CSV/metadata/package SHA-256, callback metrics, checkpoints, model hash, evaluation, and verification evidence.
碳核算 / Carbon accounting
范围一辅助燃油与所在地法范围二分列;没有合同凭证时市场法范围二保持不可用。 / Scope 1 auxiliary fuel and location-based Scope 2 are separated; market-based Scope 2 remains unavailable without contractual instruments.
安全边界 / Safety boundary
生产调度硬编码禁用;外部连接器可选;变更接口需要角色权限和人工确认。 / Production dispatch is hard-disabled; external connectors are optional; mutation routes require role gates and explicit confirmation.
A 24-step held-out trajectory with throughput, crane and yard-vehicle actions, peak load, energy, Scope 1/2 emissions, cost, and safety violations.
Baseline and policy comparisons share the same environment, dataset package, and test partition.
Missing weather, AIS, TOS, yard occupancy, AGV battery, and renewable-mix fields remain visibly unavailable instead of being fabricated.
2. 模型与联动治理 / Model and integration governance
治理面板区分驾驶舱、真实 learner 运行时、小懿 AI、本地航行模拟器和 Godot 执行环境。首次克隆时,只有仓库内能力显示就绪;未配置的外部桌面项目会明确离线。
The governance panel separates the cockpit, real learner runtime, Xiaoyi AI, the local sailing simulator, and the Godot runtime. On a clean clone, only repository-owned capabilities report ready; unconfigured desktop integrations remain explicitly offline.
系统架构 / Architecture
flowchart LR
subgraph Data["Data and provenance / 数据与血缘"]
CSV["Canonical CSV"]
META["Metadata, units, assumptions"]
HASH["Schema + split + SHA-256 gates"]
CSV --> HASH
META --> HASH
end
subgraph Experiment["Experiment plane / 实验平面"]
TRAIN["Train split\nrender_mode=None"]
ENV["PortEnergyDispatchEnv\nv1 · v2 · v3"]
RL["PPO · SAC · TD3 · DQN"]
MPC["Constrained MPC"]
TEST["Held-out test\ntrajectory rendering"]
HASH --> TRAIN --> ENV
ENV --> RL --> TEST
ENV --> MPC --> TEST
end
subgraph Evidence["Evidence plane / 证据平面"]
RUN["Run manifest + metrics"]
ART["Checkpoints + artifact hashes"]
REG["Offline model registry"]
TEST --> RUN --> REG
RL --> ART --> REG
MPC --> ART
end
subgraph Product["Product plane / 产品平面"]
API["FastAPI + role gates + audit"]
UI["React bilingual cockpit"]
OPT["Optional Xiaoyi and Godot connectors"]
REG --> API --> UI
OPT -. "explicitly optional" .-> API
end
裁剪策略梯度,用于连续资源配置。 / Clipped policy-gradient baseline for continuous allocation.
sac
RL
continuous
熵正则离策略 actor-critic,适合岸电和设备比例。 / Entropy-regularized off-policy actor-critic for shore-power and equipment ratios.
td3
RL
continuous
双评论家与延迟策略更新,强调平滑连续控制。 / Twin critics and delayed updates for smooth continuous control.
dqn
RL
81 discrete presets
在 3×3×3×3 可审计岸电、岸桥、场内车辆与储能组合上做值学习。 / Value learning over an auditable 3×3×3×3 shore-power, crane, yard and storage grid.
mpc
control theory
constrained beam search
四步有限时域、宽度 4 的约束束搜索;默认驾驶舱的可复现对照。 / Four-step constrained beam search with width 4; the reproducible default cockpit comparator.
四种 RL 算法均通过 Stable-Baselines3 的实际 learn() 路径运行;仓库测试对每个 learner 执行最小 smoke run。smoke run 只证明管线可执行,不代表策略已收敛或优于基线。
All four RL algorithms execute the actual Stable-Baselines3 learn() path, and the test suite performs a minimal smoke run for each learner. A smoke run proves pipeline executability—not convergence or superiority.
v1 exposes 19 energy-dispatch observations; v2 adds six official vessel-activity signals; the real-port v3 contract adds ten mandatory weather, availability, shore-compatibility and renewable-power inputs. Four actions control shore power, active crane and yard fleets, and battery charge/discharge. Each step computes throughput, queue, load, peak violations, degradation, auxiliary fuel, Scope 1/2 emissions, energy and delay costs; action shields enforce grid, availability, compatibility, SOC and terminal-SOC reachability constraints.
每个新 run 写入 backend/app/data/runs/<job-id>/,但运行目录和二进制模型默认不进入 Git。源码仓库保持轻量;需要公开的 benchmark 模型与结果应作为独立 release artifact 发布。
Each new run is written to backend/app/data/runs/<job-id>/, while run outputs and binary models are ignored by Git. The source repository stays lightweight; benchmark models and results should be published as separate release artifacts.
EIA Hourly Electric Grid Monitor: LADWP hourly demand and consumed carbon intensity for 2020–2025; 51,726 of 52,608 hours are reported and 882 are quality-coded month-hour median imputations, for 98.32% source coverage.
New training defaults to port_la_2020_2024_vessel_activity_hourly, which adds 1,238 official Port of Los Angeles Wharfinger Division business-day anchor, berth, departure, and dwell observations to the versioned energy-carbon base. Its 43,848 contiguous hours use 2020–2022 for training, 2023 for validation, and 2024 for held-out testing. Non-reporting days are explicitly marked interpolations, not hourly terminal telemetry. The original package and metrics remain intact as the longer energy-carbon baseline.
月度 TEU 通过公开的确定性曲线分配到小时;LADWP 商业分时电价时段只用于形成日内形状,并归一回 EIA 月均电价。该价格仍是情景代理而非港口账单,设备容量、负荷、储能与延误成本是元数据中声明的模型参数。完整来源、单位、插补、转换与哈希见 数据卡 和 dataset metadata。
Monthly TEU is allocated to hours through a disclosed deterministic profile. LADWP commercial time-of-use periods provide only the intraday shape, rescaled to each EIA monthly mean. Prices remain scenario proxies rather than terminal bills; equipment, storage and delay parameters are declared model assumptions. See the data card and dataset metadata.
Across 48 deterministic held-out windows spanning 2025—1,152 simulated hourly steps—the four-step constrained MPC reduces energy by 8.4%, carbon by 8.7%, scenario cost by 7.9%, and peak load by 3.2% against the strong “full shore power + fixed fully staffed cargo resources” baseline. Mean equipment activation falls 28.8%, throughput retention is 99.97%, and constraint satisfaction is 100%. Three predeclared objective-weight settings yield a carbon-improvement range of 8.69%–8.74%. These are public-data offline scenario results, not measured terminal KPIs, and they do not show RL superiority over MPC. See the benchmark report for denominators, sample indices, limits, and hashes.
The report also publishes a harder comparator: select an 80%/80% crane/yard-vehicle static configuration from nine candidates using only 2024 validation data, freeze it, and test in 2025. Against that comparator, MPC still reduces carbon by 2.84%, energy by 2.52%, and cost by 2.08%, while increasing throughput by 0.85%—but peak load rises 3.38%. This result discloses marginal algorithm benefit and the multi-objective trade-off; it does not replace the full-resource redundancy scenario above.
The vessel-activity enhanced report evaluates 48×24 held-out hours from 2024. MPC reduces carbon by 8.90% and scenario cost by 8.22% versus the fixed full-resource comparator, with 100.00% throughput retention and 100% constraint satisfaction. Against the harder validation-selected 80%/80% comparator, carbon still falls 2.77% and cost 2.11%, while peak load rises 3.61%. These remain offline scenario results.
快速开始 / Quick start
本地开发 / Local development
要求:Python 3.11+、Node.js 20+、pnpm。首次安装 PyTorch 可能需要数分钟。
Requirements: Python 3.11+, Node.js 20+, and pnpm. The first PyTorch installation may take several minutes.
git clone https://github.com/wenjiayi123/port-energy-carbon-cockpit.git
cd port-energy-carbon-cockpit
make bootstrap
make demo
驾驶舱 / Cockpit: http://127.0.0.1:5173/
OpenAPI: http://127.0.0.1:8808/docs
Readiness: http://127.0.0.1:8808/api/health/ready
Metrics: http://127.0.0.1:8808/api/metrics
加固容器 / Hardened containers
export OPERATOR_API_KEY="$(openssl rand -hex 24)"
make docker-up
Compose 将前后端绑定到 loopback,后端生产模式强制 API key;容器使用非 root 用户、只读文件系统、全部 capability drop 和独立 run/audit volume。面向互联网时仍应在前面配置 TLS、企业 SSO、每用户授权和集中式审计。
Compose binds both services to loopback and enforces an API key in backend production mode. Containers use non-root users, read-only filesystems, dropped capabilities, and dedicated run/audit volumes. Internet-facing deployments still need TLS, enterprise SSO, per-user authorization, and centralized audit retention.
可复现实验 / Reproducible experiments
cd backend
# 列出四种 RL 与 MPC / list four RL algorithms plus MPC
.venv/bin/python -m app.rl.cli algorithms
# 验证数据契约和哈希 / validate dataset contract and hashes
.venv/bin/python -m app.rl.cli validate-data port_la_2020_2024_vessel_activity_hourly
# 仅在 train 上训练,不渲染 / fit on train only, without rendering
.venv/bin/python -m app.rl.cli train \
--algorithm sac \
--dataset port_la_2020_2024_vessel_activity_hourly \
--total-steps 120000 \
--seed 20260720
# 训练完成后才在 test 上评估并生成轨迹 / held-out evaluation after fitting
.venv/bin/python -m app.rl.cli evaluate --strategy auto:latest
# 仅用validation选型 / tune on validation only; short runs must be marked smoke
PYTHONPATH=. .venv/bin/python -m app.rl.tuning \
--algorithm all \
--dataset port_la_2020_2024_vessel_activity_hourly \
--steps 10000 \
--final-seeds 11,29,47 \
--output ../reports/rl_tuning_vessel_activity_10k.json
# 重算公开MPC报告 / recompute and verify the publishable MPC report
PYTHONPATH=. .venv/bin/python -m app.rl.benchmark run
PYTHONPATH=. .venv/bin/python -m app.rl.benchmark \
verify ../reports/offline_benchmark_v3.json
# 在仓库根目录重建逐日船舶活动数据并复算增强报告cd ..
make data-enhanced
make benchmark-enhanced
make verify-benchmark-enhanced
API 启动训练需要 confirm=true。训练进度来自 model.num_timesteps、callback 指标和实际耗时;ETA 使用已测 step rate 推导,不使用固定时长计时器。
API training requires confirm=true. Progress comes from model.num_timesteps, callback metrics, and measured elapsed time; ETA is derived from observed step rate, not a fixed-duration timer.
The enhanced package also includes a reproducible
10k multi-seed RL matrix: all four
learners completed real fit/validation/test execution with zero modeled safety
violations across the reported seeds. It is explicitly labelled short-budget
comparative evidence, not convergence or production performance.
A separate 100k TD3 run
is intentionally retained as rejected evidence: its split/artifact/safety
checks passed, but it underperformed both constrained-control and fixed-resource
comparators on carbon and scenario cost. It is not used as a positive metric.
替换港口数据 / Bring your own port data
算法不绑定洛杉矶港。使用稳定 canonical schema 和相邻 metadata 即可替换数据,而不改 learner 或驾驶舱。完整字段、单位与时序模式见 docs/DATASETS.md,模板见 docs/examples/port_dataset_template.csv。
Algorithms are not hard-coded to Los Angeles. Replace the data through the stable canonical schema and adjacent metadata without modifying the learner or cockpit. See docs/DATASETS.md and the CSV template.
v3 的字段映射选项见 python scripts/prepare_port_dataset.py --help;缺少天气、泊位/设备/电网可用率、岸电兼容或可再生能源字段会 fail closed。完整流程见 实港接入蓝图。 / See the mapper help and the real-port blueprint; missing v3 deployment fields fail closed.
profiled_period:适合公开月度/聚合 benchmark,按声明曲线构造 episode。 / for aggregate public benchmarks with a declared profile.
sequential_rows:适合只读 TOS/EMS 小时快照,环境按不可变行推进。 / for immutable hourly TOS/EMS snapshots advanced row by row.
CLI 可读取操作者明确提供的外部 CSV;HTTP API 只允许仓库已注册的数据集 ID,阻断任意文件路径访问。 / The CLI may read operator-supplied external CSV files; HTTP endpoints accept only registered dataset IDs and reject arbitrary filesystem paths.
API 表面 / API surface
Endpoint
方法 / Method
语义 / Semantics
/api/dashboard/snapshot
GET
当前公开 benchmark 与测试轨迹快照 / current benchmark and held-out snapshot
在接入真实港口前,必须完成 TOS/EMS 只读适配、参数校准、计量血缘、身份权限、shadow mode、回滚演练、人工验收和独立安全联锁。完整门槛见 生产就绪说明 与 威胁模型。
Before a real-port integration, provide read-only TOS/EMS adapters, parameter calibration, meter lineage, identity controls, shadow mode, rollback drills, operator acceptance, and an independent safety interlock. See production readiness and the threat model.
Production refuses to start without API_AUTH_MODE=api_key; keys require at least 24 characters and support viewer/operator/admin roles.
Mutation requests are written to a JSONL audit stream; every request receives an ID, structured access log, and baseline security headers.
HTTP dataset references are confined to the registry and strategy IDs are format-allowlisted against path traversal.
CI covers Ruff, backend/RL tests, dataset validation, frontend build, dependency audits, and container builds.
CodeQL, Dependency Review, and OpenSSF Scorecard activate after the repository becomes public; they remain skipped during private review to avoid false failures from GitHub Free private-feature limits.
Actions are pinned to full commit SHAs; Dependabot updates are grouped monthly to control noise.
make test# backend API, accounting, Gymnasium and five-baseline smoke tests
make build # TypeScript + Vite production build
make validate # repository structure and default dataset contractcd backend
.venv/bin/python -m ruff check app
.venv/bin/python -m pip_audit
cd ../frontend
pnpm audit --audit-level high
Intel macOS 仅能解析旧 PyTorch 2.2.2 wheel,当前已知漏洞使其只能作为兼容开发环境。安全发布门槛以 Linux CI/容器中解析的当前 PyTorch 版本为准,且不得加载不可信模型。
Intel macOS resolves only the legacy PyTorch 2.2.2 wheel; known vulnerabilities make it a compatibility-only development environment. The security release gate is the current Linux CI/container resolution, and untrusted model files must never be loaded.
代码采用 MIT License。数据来源与 AI 辅助原创视觉资产的说明见 THIRD_PARTY_NOTICES.md 和 ASSET_PROVENANCE.md。Port of Los Angeles 名称与标识归其权利人所有;本项目与该港口不存在隶属或背书关系。
Code is licensed under the MIT License. Data attribution and AI-assisted original artwork provenance are documented in THIRD_PARTY_NOTICES.md and ASSET_PROVENANCE.md. Port of Los Angeles names and marks remain with their owners; this project is not affiliated with or endorsed by the Port.