RACE-Net-M weights, card and code
Browse files- .gitattributes +2 -0
- README.md +103 -0
- ablation.json +129 -0
- config.json +23 -0
- model.safetensors +3 -0
- racenet_m.py +144 -0
- racenet_m_architecture.png +3 -0
- trackD.py +414 -0
- trackD_vs_liars.png +3 -0
- train_racenet_m.py +140 -0
- training_log.json +266 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
racenet_m_architecture.png filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
trackD_vs_liars.png filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: other
|
| 3 |
+
license_name: roborace-mixed
|
| 4 |
+
license_link: https://huggingface.co/datasets/Nabidnur/RoboRACE-Bench/blob/main/LICENSES.md
|
| 5 |
+
library_name: pytorch
|
| 6 |
+
pipeline_tag: other
|
| 7 |
+
tags: [robotics, multi-agent, byzantine-robustness, map-fusion, truth-inference, set-transformer, roborace]
|
| 8 |
+
datasets: [Nabidnur/RoboRACE-Bench]
|
| 9 |
+
model-index:
|
| 10 |
+
- name: RACE-Net-M
|
| 11 |
+
results:
|
| 12 |
+
- task: {type: other, name: Byzantine-robust map fusion}
|
| 13 |
+
dataset: {name: RoboRACE-Bench Track D, type: Nabidnur/RoboRACE-Bench, config: trackD_maze_results, split: train}
|
| 14 |
+
metrics:
|
| 15 |
+
- {type: spl, value: 70.2, name: mean SPL x100 over 500 cells}
|
| 16 |
+
---
|
| 17 |
+
|
| 18 |
+
# RACE-Net-M — receiver-anchored channel estimation for map fusion
|
| 19 |
+
|
| 20 |
+
**A 44,837-parameter model that fuses the map reports of a robot swarm when some robots lie.**
|
| 21 |
+
Part of [RoboRACE](https://huggingface.co/datasets/Nabidnur/RoboRACE-Bench) · sibling model for camera swarms: [RACE-Net-V](https://huggingface.co/Nabidnur/RoboRACE-Net) ·
|
| 22 |
+
[showcase Space](https://huggingface.co/spaces/Nabidnur/RoboRACE) · code: [github.com/Hisernberg/robotics](https://github.com/Hisernberg/robotics)
|
| 23 |
+
|
| 24 |
+
<p align="center"><img src="racenet_m_architecture.png" width="640"></p>
|
| 25 |
+
|
| 26 |
+
## What it does
|
| 27 |
+
|
| 28 |
+
Robots explore an unknown map and broadcast what they sensed (cell is *free* / *wall*). Some robots corrupt their broadcasts: they invert
|
| 29 |
+
everything, invent walls or openings, answer at random, turn bad later, or collude. A receiving robot has to decide which cells are walls.
|
| 30 |
+
RACE-Net-M returns the log-odds of *wall* for every reported cell. It assumes one thing only: **the receiver's own sensor is right**.
|
| 31 |
+
|
| 32 |
+
1. **Anchored counts.** Each peer's claims are compared with the cells the receiver sensed itself (claim × truth, recent and old claims apart).
|
| 33 |
+
2. **Peer transformer.** A small transformer across peers turns the counts into a channel embedding per peer.
|
| 34 |
+
3. **Signed read-out.** Each kind of claim gets a signed evidence value: a reliable peer is trusted, a consistent liar is *inverted and used*,
|
| 35 |
+
a random one is ignored. Reputation systems can only discard a liar.
|
| 36 |
+
4. **Refinement (3 rounds).** Each peer is also checked against the belief of all the *other* peers on cells the receiver has not
|
| 37 |
+
sensed (leave-one-out), so a robot can verify a peer it never overlapped with through a peer it already trusts.
|
| 38 |
+
|
| 39 |
+
Peers and cells are sets (no positions): any map size and any swarm size.
|
| 40 |
+
|
| 41 |
+
## Use
|
| 42 |
+
|
| 43 |
+
```python
|
| 44 |
+
import sys, numpy as np
|
| 45 |
+
from huggingface_hub import snapshot_download
|
| 46 |
+
d = snapshot_download("Nabidnur/RoboRACE-Net-M"); sys.path.insert(0, d)
|
| 47 |
+
import racenet_m
|
| 48 |
+
|
| 49 |
+
# own [H, W] the receiver's own observations: -1 unknown, 0 free, 1 wall
|
| 50 |
+
# claims [n, H, W] latest broadcast of every robot (same coding); stamp [n, H, W] time of that broadcast
|
| 51 |
+
log_odds = racenet_m.wall_logodds(own, claims, stamp, i=0, t=now, path=d) # i = index of the receiver
|
| 52 |
+
wall = log_odds > 2.0 # threshold chosen on development mazes
|
| 53 |
+
```
|
| 54 |
+
About 2 ms per call on one CPU core. Dependencies: `torch`, `numpy`, `safetensors`.
|
| 55 |
+
|
| 56 |
+
## Training
|
| 57 |
+
|
| 58 |
+
45,361 snapshots from simulated maze swarms (maze seeds 5000–7399) in which liars follow
|
| 59 |
+
**random two-coin channels with an optional change point** (`racenet_m.sample_channels`). The benchmark's attack names never appear in training,
|
| 60 |
+
and the colluding ring attack is outside that family. 4,000 AdamW steps on a CPU (8 min including the
|
| 61 |
+
simulation). Benchmark mazes (seeds 1000–1019) and tuning mazes (seeds 0–39) are disjoint from training and validation mazes.
|
| 62 |
+
|
| 63 |
+
## Evaluation: RoboRACE Track D
|
| 64 |
+
|
| 65 |
+
Six robots, 20 fresh mazes × 25 conditions (6 attacks × 1–4 liars, plus the clean case) = 500 cells. Score = SPL ×100 (success weighted by path
|
| 66 |
+
length) of the honest robots. Ranks are medians over 2,000 bootstrap resamples of cells.
|
| 67 |
+
|
| 68 |
+
| rank | fusion rule | mean SPL | no liars | liars < 50% | liars ≥ 50% | goal reached % |
|
| 69 |
+
|---:|---|---:|---:|---:|---:|---:|
|
| 70 |
+
| 1 | **RACE-Net-M + reachability repair (this model)** | 70.2 | 76.8 | 73.1 | 66.8 | 97.8 |
|
| 71 |
+
| 2 | **RACE-Net-M (this model)** | 69.8 | 76.8 | 72.6 | 66.4 | 97.4 |
|
| 72 |
+
| 3 | race_map (hand-written Bayesian rule, ours) | 66.2 | 76.8 | 69.7 | 61.8 | 94.2 |
|
| 73 |
+
| 4 | W-MSR-style trimming (LeBlanc et al. 2013; F known) | 64.1 | 76.8 | 65.6 | 61.5 | 96.5 |
|
| 74 |
+
| 5 | beta-reputation (Jøsang & Ismail 2002) | 63.8 | 76.8 | 67.9 | 58.5 | 91.9 |
|
| 75 |
+
| 6 | EigenTrust (Kamvar et al. 2003) | 63.0 | 75.0 | 66.8 | 58.2 | 91.8 |
|
| 76 |
+
| 7 | Dawid–Skene (1979), unanchored | 62.0 | 76.3 | 67.4 | 55.5 | 90.1 |
|
| 77 |
+
| 8 | no communication | 61.6 | 61.5 | 62.0 | 61.2 | 95.2 |
|
| 78 |
+
| 9 | naive map merging | 56.7 | 76.8 | 59.9 | 51.7 | 83.8 |
|
| 79 |
+
| 10 | per-cell majority | 55.7 | 76.8 | 61.5 | 48.1 | 82.1 |
|
| 80 |
+
| – | *oracle that knows the liars (reference)* | 71.3 | 76.8 | 74.5 | 67.5 | 98.2 |
|
| 81 |
+
|
| 82 |
+
<p align="center"><img src="trackD_vs_liars.png" width="900"></p>
|
| 83 |
+
|
| 84 |
+
Belief quality on held-out mazes (accuracy on cells a peer reported and the receiver has not sensed):
|
| 85 |
+
|
| 86 |
+
| model | all | 3 of 5 peers lie | 5 of 5 peers lie |
|
| 87 |
+
|---|---:|---:|---:|
|
| 88 |
+
| race_map (hand-written Bayesian rule) | 90.1 | 82.7 | 72.9 |
|
| 89 |
+
| rounds = 1 (no refinement) | 91.8 | 85.7 | 76.1 |
|
| 90 |
+
| rounds = 2 | 92.2 | 86.4 | 77.4 |
|
| 91 |
+
| rounds = 3 (released) | 92.3 | 86.7 | 77.6 |
|
| 92 |
+
| rounds = 3, second seed | 92.3 | 86.7 | 77.6 |
|
| 93 |
+
|
| 94 |
+
## Limitations
|
| 95 |
+
|
| 96 |
+
- **Collusion on a few cells.** A bloc that is honest everywhere except around the goal looks reliable until a robot gets there; the
|
| 97 |
+
reachability repair recovers part of the loss, the oracle is still better.
|
| 98 |
+
- One simulator (grid mazes, exact 8-neighbour sensing, six robots). Not tested on real robots or noisy sensors.
|
| 99 |
+
- Claims are assumed conditionally independent given the truth; the model has no notion of where a peer was when it made a claim.
|
| 100 |
+
|
| 101 |
+
## Files
|
| 102 |
+
`model.safetensors` · `config.json` · `training_log.json` · `ablation.json` · `racenet_m.py` (model, training distribution, inference) ·
|
| 103 |
+
`train_racenet_m.py` · `trackD.py` (the benchmark, baselines and planner)
|
ablation.json
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"metric": "accuracy on cells that a peer reported and the receiver has not sensed, 6,111 held-out snapshots (maze seeds 9000+)",
|
| 3 |
+
"rows": [
|
| 4 |
+
{
|
| 5 |
+
"model": "rounds = 1 (no refinement)",
|
| 6 |
+
"val_cell_acc": 0.9181,
|
| 7 |
+
"val_cell_acc_bma": 0.9014,
|
| 8 |
+
"by_liars": {
|
| 9 |
+
"0": [
|
| 10 |
+
0.9996,
|
| 11 |
+
1.0
|
| 12 |
+
],
|
| 13 |
+
"1": [
|
| 14 |
+
0.9492,
|
| 15 |
+
0.9398
|
| 16 |
+
],
|
| 17 |
+
"2": [
|
| 18 |
+
0.9003,
|
| 19 |
+
0.8725
|
| 20 |
+
],
|
| 21 |
+
"3": [
|
| 22 |
+
0.8566,
|
| 23 |
+
0.8269
|
| 24 |
+
],
|
| 25 |
+
"4": [
|
| 26 |
+
0.8295,
|
| 27 |
+
0.7985
|
| 28 |
+
],
|
| 29 |
+
"5": [
|
| 30 |
+
0.7606,
|
| 31 |
+
0.7293
|
| 32 |
+
]
|
| 33 |
+
}
|
| 34 |
+
},
|
| 35 |
+
{
|
| 36 |
+
"model": "rounds = 2",
|
| 37 |
+
"val_cell_acc": 0.922,
|
| 38 |
+
"val_cell_acc_bma": 0.9014,
|
| 39 |
+
"by_liars": {
|
| 40 |
+
"0": [
|
| 41 |
+
0.9997,
|
| 42 |
+
1.0
|
| 43 |
+
],
|
| 44 |
+
"1": [
|
| 45 |
+
0.9525,
|
| 46 |
+
0.9398
|
| 47 |
+
],
|
| 48 |
+
"2": [
|
| 49 |
+
0.9029,
|
| 50 |
+
0.8725
|
| 51 |
+
],
|
| 52 |
+
"3": [
|
| 53 |
+
0.8636,
|
| 54 |
+
0.8269
|
| 55 |
+
],
|
| 56 |
+
"4": [
|
| 57 |
+
0.8378,
|
| 58 |
+
0.7985
|
| 59 |
+
],
|
| 60 |
+
"5": [
|
| 61 |
+
0.7744,
|
| 62 |
+
0.7293
|
| 63 |
+
]
|
| 64 |
+
}
|
| 65 |
+
},
|
| 66 |
+
{
|
| 67 |
+
"model": "rounds = 3 (released)",
|
| 68 |
+
"val_cell_acc": 0.9233,
|
| 69 |
+
"val_cell_acc_bma": 0.9014,
|
| 70 |
+
"by_liars": {
|
| 71 |
+
"0": [
|
| 72 |
+
0.9998,
|
| 73 |
+
1.0
|
| 74 |
+
],
|
| 75 |
+
"1": [
|
| 76 |
+
0.9542,
|
| 77 |
+
0.9398
|
| 78 |
+
],
|
| 79 |
+
"2": [
|
| 80 |
+
0.9035,
|
| 81 |
+
0.8725
|
| 82 |
+
],
|
| 83 |
+
"3": [
|
| 84 |
+
0.8673,
|
| 85 |
+
0.8269
|
| 86 |
+
],
|
| 87 |
+
"4": [
|
| 88 |
+
0.839,
|
| 89 |
+
0.7985
|
| 90 |
+
],
|
| 91 |
+
"5": [
|
| 92 |
+
0.7762,
|
| 93 |
+
0.7293
|
| 94 |
+
]
|
| 95 |
+
}
|
| 96 |
+
},
|
| 97 |
+
{
|
| 98 |
+
"model": "rounds = 3, second seed",
|
| 99 |
+
"val_cell_acc": 0.9233,
|
| 100 |
+
"val_cell_acc_bma": 0.9014,
|
| 101 |
+
"by_liars": {
|
| 102 |
+
"0": [
|
| 103 |
+
0.9996,
|
| 104 |
+
1.0
|
| 105 |
+
],
|
| 106 |
+
"1": [
|
| 107 |
+
0.9539,
|
| 108 |
+
0.9398
|
| 109 |
+
],
|
| 110 |
+
"2": [
|
| 111 |
+
0.9042,
|
| 112 |
+
0.8725
|
| 113 |
+
],
|
| 114 |
+
"3": [
|
| 115 |
+
0.8665,
|
| 116 |
+
0.8269
|
| 117 |
+
],
|
| 118 |
+
"4": [
|
| 119 |
+
0.8401,
|
| 120 |
+
0.7985
|
| 121 |
+
],
|
| 122 |
+
"5": [
|
| 123 |
+
0.7764,
|
| 124 |
+
0.7293
|
| 125 |
+
]
|
| 126 |
+
}
|
| 127 |
+
}
|
| 128 |
+
]
|
| 129 |
+
}
|
config.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"d": 48,
|
| 3 |
+
"heads": 4,
|
| 4 |
+
"layers": 2,
|
| 5 |
+
"rounds": 3,
|
| 6 |
+
"n_params": 44837,
|
| 7 |
+
"architecture": "RaceNetM (anchored channel encoder + peer transformer + signed read-out, iterative)",
|
| 8 |
+
"input": "peer claims [P peers, N cells] in {none, free, wall} with a recent/old flag, and the receiver's own sensed cells",
|
| 9 |
+
"output": "log-odds of 'wall' per cell",
|
| 10 |
+
"training": {
|
| 11 |
+
"data": "maze swarms with random two-coin liars and change points (racenet_m.sample_channels)",
|
| 12 |
+
"train_seeds": [
|
| 13 |
+
5000,
|
| 14 |
+
7399
|
| 15 |
+
],
|
| 16 |
+
"val_seeds_from": 9000,
|
| 17 |
+
"snapshots": 45361,
|
| 18 |
+
"steps": 4000,
|
| 19 |
+
"batch": 48,
|
| 20 |
+
"lr": 0.002,
|
| 21 |
+
"seed": 0
|
| 22 |
+
}
|
| 23 |
+
}
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:65f379bd022bbe68a38cf38ecd049334059d8aae4d5e4334865b3ebc0a2a8fc5
|
| 3 |
+
size 182476
|
racenet_m.py
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""RACE-Net-M — receiver-anchored channel estimation for MAP fusion (this repo's second model).
|
| 2 |
+
|
| 3 |
+
Same design as RACE-Net-V (`racenet.py`), applied to a map instead of a window of episodes:
|
| 4 |
+
anchor the receiver's own sensed cells are the only trusted evidence
|
| 5 |
+
channels for every peer, its claims are compared with the anchor inside the current map
|
| 6 |
+
(in-context channel estimation: hard counts on cells the receiver sensed itself)
|
| 7 |
+
swarm view a small transformer across peers lets the estimate of one peer depend on the others
|
| 8 |
+
signed use the read-out gives each kind of claim a signed evidence value (log-likelihood ratio of
|
| 9 |
+
'wall'): a reliable peer is trusted, a consistent liar is inverted, a random one ignored
|
| 10 |
+
refinement the fused belief is fed back as soft counts on cells the receiver has NOT sensed
|
| 11 |
+
(each peer is checked against the belief of all the *other* peers), for R rounds;
|
| 12 |
+
this is what lets a robot verify a peer it has never overlapped with, through a peer it trusts
|
| 13 |
+
|
| 14 |
+
Peers and cells are sets: no positions, so the model works for any map size and swarm size.
|
| 15 |
+
It is trained on random two-coin liars with a random change point (`sample_channels`); the benchmark's
|
| 16 |
+
attack names never appear in training, and the colluding ring attack is outside that family.
|
| 17 |
+
"""
|
| 18 |
+
from __future__ import annotations
|
| 19 |
+
|
| 20 |
+
import json
|
| 21 |
+
import os
|
| 22 |
+
from pathlib import Path
|
| 23 |
+
|
| 24 |
+
import numpy as np
|
| 25 |
+
import torch
|
| 26 |
+
import torch.nn as nn
|
| 27 |
+
|
| 28 |
+
CONFIG = {"d": 48, "heads": 4, "layers": 2, "rounds": 3}
|
| 29 |
+
_HERE = Path(__file__).resolve()
|
| 30 |
+
DEFAULT_DIR = (_HERE.parents[3] / "models/racenet_m") if len(_HERE.parents) > 3 and (_HERE.parents[3] / "models/racenet_m").exists() \
|
| 31 |
+
else _HERE.parent
|
| 32 |
+
if os.environ.get("RACENET_M_DIR"):
|
| 33 |
+
DEFAULT_DIR = Path(os.environ["RACENET_M_DIR"])
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
class RaceNetM(nn.Module):
|
| 37 |
+
def __init__(self, d=48, heads=4, layers=2, rounds=3):
|
| 38 |
+
super().__init__()
|
| 39 |
+
self.rounds = rounds
|
| 40 |
+
self.embed = nn.Sequential(nn.Linear(36, d), nn.GELU(), nn.Linear(d, d))
|
| 41 |
+
self.round_emb = nn.Parameter(torch.zeros(rounds, d))
|
| 42 |
+
enc = nn.TransformerEncoderLayer(d, heads, 2 * d, dropout=0.0, batch_first=True, norm_first=True)
|
| 43 |
+
self.peers = nn.TransformerEncoder(enc, layers, enable_nested_tensor=False)
|
| 44 |
+
self.head = nn.Sequential(nn.LayerNorm(d), nn.Linear(d, d), nn.GELU(), nn.Linear(d, 4))
|
| 45 |
+
self.bias = nn.Parameter(torch.zeros(1))
|
| 46 |
+
nn.init.normal_(self.round_emb, std=0.02)
|
| 47 |
+
|
| 48 |
+
@staticmethod
|
| 49 |
+
def _feats(n):
|
| 50 |
+
"""n: [B, P, 2 (truth), 4 (epoch*2 + claim)] counts -> 16 features (log-counts and per-truth rates)."""
|
| 51 |
+
tot = n.reshape(*n.shape[:2], 2, 2, 2).sum(-1, keepdim=True) # per (truth, epoch)
|
| 52 |
+
rate = (n.reshape(*n.shape[:2], 2, 2, 2) + 0.5) / (tot + 1.0)
|
| 53 |
+
return torch.cat([torch.log1p(n).flatten(2), rate.flatten(2)], -1)
|
| 54 |
+
|
| 55 |
+
def forward(self, claim, epoch, own, return_all=False):
|
| 56 |
+
"""claim [B, P, N] in {-1 none, 0 free, 1 wall}; epoch [B, P, N] in {0 old, 1 recent}; own [B, N] in {-1, 0, 1}.
|
| 57 |
+
Returns log-odds of 'wall' per cell [B, N] (list over rounds if return_all)."""
|
| 58 |
+
has = claim >= 0
|
| 59 |
+
idx = (epoch * 2 + claim.clamp(min=0)).long() # 0..3
|
| 60 |
+
oh = torch.nn.functional.one_hot(idx, 4).float() * has[..., None] # [B, P, N, 4]
|
| 61 |
+
seen = (own >= 0)[:, None, :, None].float()
|
| 62 |
+
truth = (own == 1)[:, None, :, None].float()
|
| 63 |
+
hard = torch.stack([(oh * seen * (1 - truth)).sum(2), (oh * seen * truth).sum(2)], 2) # [B, P, 2, 4]
|
| 64 |
+
f_hard = self._feats(hard)
|
| 65 |
+
cover = torch.log1p(oh.sum(2)) # [B, P, 4] how much it reported
|
| 66 |
+
unseen = 1 - seen
|
| 67 |
+
logit, outs = None, []
|
| 68 |
+
for r in range(self.rounds):
|
| 69 |
+
if logit is None:
|
| 70 |
+
soft = torch.zeros_like(hard)
|
| 71 |
+
else:
|
| 72 |
+
ev_own = (oh * ev[:, :, None, :]).sum(-1) # this peer's own contribution
|
| 73 |
+
q = torch.sigmoid(logit[:, None, :] - ev_own)[..., None] # belief of everybody else
|
| 74 |
+
soft = torch.stack([(oh * unseen * (1 - q)).sum(2), (oh * unseen * q).sum(2)], 2)
|
| 75 |
+
x = self.embed(torch.cat([f_hard, self._feats(soft), cover], -1)) + self.round_emb[r]
|
| 76 |
+
ev = self.head(self.peers(x)) # [B, P, 4] signed evidence
|
| 77 |
+
logit = self.bias + (oh * ev[:, :, None, :]).sum((1, 3))
|
| 78 |
+
outs.append(logit)
|
| 79 |
+
return outs if return_all else logit
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
# ----------------------------------------------------------------------------- training distribution
|
| 83 |
+
def sample_channels(rng: np.random.Generator, n=6, receiver=None):
|
| 84 |
+
"""Random liars for one training episode: {robot: (t00, t11, t_change)}. Two-coin channels drawn from a broad
|
| 85 |
+
family, with an optional change point (a peer that turns bad later). No named benchmark attack is used."""
|
| 86 |
+
F = int(rng.integers(0, n))
|
| 87 |
+
liars = rng.choice([j for j in range(n) if j != receiver], min(F, n - 1 if receiver is not None else n), replace=False)
|
| 88 |
+
ch = {}
|
| 89 |
+
for j in liars:
|
| 90 |
+
kind = rng.choice(["uniform", "both_low", "wall_inventor", "free_inventor", "coin"], p=[0.3, 0.25, 0.2, 0.15, 0.1])
|
| 91 |
+
if kind == "uniform":
|
| 92 |
+
t00, t11 = rng.uniform(0, 1, 2)
|
| 93 |
+
elif kind == "both_low":
|
| 94 |
+
t00, t11 = rng.uniform(0, 0.3, 2)
|
| 95 |
+
elif kind == "wall_inventor":
|
| 96 |
+
t00, t11 = rng.uniform(0, 0.7), rng.uniform(0.9, 1)
|
| 97 |
+
elif kind == "free_inventor":
|
| 98 |
+
t00, t11 = rng.uniform(0.9, 1), rng.uniform(0, 0.7)
|
| 99 |
+
else:
|
| 100 |
+
t00, t11 = rng.uniform(0.4, 0.6, 2)
|
| 101 |
+
ch[int(j)] = (float(t00), float(t11), int(rng.integers(10, 90)) if rng.random() < 0.3 else 0)
|
| 102 |
+
return ch
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
def tensors(own, claims, stamp, i, t, window=30):
|
| 106 |
+
"""One snapshot -> (claim [P, N], epoch [P, N], own [N]) int8 arrays; peers = every robot except i."""
|
| 107 |
+
peers = [j for j in range(len(claims)) if j != i]
|
| 108 |
+
c = claims[peers].reshape(len(peers), -1)
|
| 109 |
+
e = (stamp[peers].reshape(len(peers), -1) >= t - window).astype(np.int8)
|
| 110 |
+
return c.astype(np.int8), e, own.reshape(-1).astype(np.int8)
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
# ----------------------------------------------------------------------------- inference API
|
| 114 |
+
_CACHE = {}
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
def load(path=None):
|
| 118 |
+
path = Path(path or DEFAULT_DIR)
|
| 119 |
+
if path not in _CACHE:
|
| 120 |
+
from safetensors.torch import load_file
|
| 121 |
+
cfg = json.loads((path / "config.json").read_text())
|
| 122 |
+
net = RaceNetM(**{k: cfg[k] for k in ("d", "heads", "layers", "rounds")})
|
| 123 |
+
net.load_state_dict(load_file(str(path / "model.safetensors")))
|
| 124 |
+
torch.set_num_threads(1)
|
| 125 |
+
_CACHE[path] = net.eval()
|
| 126 |
+
return _CACHE[path]
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
def available(path=None):
|
| 130 |
+
return (Path(path or DEFAULT_DIR) / "model.safetensors").exists()
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
@torch.inference_mode()
|
| 134 |
+
def wall_logodds(own, claims, stamp, i, t, window=30, path=None):
|
| 135 |
+
"""Log-odds that each cell is a wall, from the peers' broadcasts (0 where nobody reported)."""
|
| 136 |
+
c, e, o = tensors(own, claims, stamp, i, t, window)
|
| 137 |
+
cols = (c >= 0).any(0) # only cells somebody reported
|
| 138 |
+
out = np.zeros(o.shape)
|
| 139 |
+
if cols.any():
|
| 140 |
+
net = load(path)
|
| 141 |
+
lg = net(torch.from_numpy(c[:, cols])[None].long(), torch.from_numpy(e[:, cols])[None].long(),
|
| 142 |
+
torch.from_numpy(o[cols])[None].long())[0].numpy()
|
| 143 |
+
out[cols] = np.where(lg == 0, 1e-6, lg)
|
| 144 |
+
return out.reshape(own.shape)
|
racenet_m_architecture.png
ADDED
|
Git LFS Details
|
trackD.py
ADDED
|
@@ -0,0 +1,414 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Track D — Byzantine cooperative maze exploration (mobile-robot swarm, numpy).
|
| 2 |
+
|
| 3 |
+
N robots must each reach a common goal in an unknown grid maze. A robot senses its 8
|
| 4 |
+
neighbouring cells exactly and broadcasts what it sensed; everybody fuses the broadcasts into
|
| 5 |
+
a belief map and re-plans every step (shortest path, unknown cells optimistically free).
|
| 6 |
+
F robots are Byzantine: they explore like everyone else but corrupt what they broadcast.
|
| 7 |
+
|
| 8 |
+
Fusion rules (per receiver; a robot's own direct observations always override peers)
|
| 9 |
+
solo ignore peers (single-robot exploration baseline)
|
| 10 |
+
trust_all naive map merging: the latest claim about a cell wins
|
| 11 |
+
majority per-cell majority vote over peers
|
| 12 |
+
wmsr_trim W-MSR-style trimming (LeBlanc et al. 2013): drop F 'wall' and F 'free' votes per cell (F known)
|
| 13 |
+
eigentrust EigenTrust (Kamvar et al. 2003): global trust from pairwise agreement, receiver pre-trusted
|
| 14 |
+
dawid_skene unanchored two-coin Dawid-Skene EM over the cells (1979)
|
| 15 |
+
reputation beta-reputation (Josang & Ismail 2002): weight each peer by log-odds of its
|
| 16 |
+
verified accuracy, clipped at 0 -> liars are DISCARDED
|
| 17 |
+
race_map receiver-anchored channel estimation (this repo): Bayesian model averaging over
|
| 18 |
+
each peer's two-coin channel, verified on the receiver's own cells; claims enter
|
| 19 |
+
as signed log-likelihood ratios -> a consistent liar is INVERTED and used, a
|
| 20 |
+
wall-inventor keeps only the weight of its 'free' claims, a random peer
|
| 21 |
+
contributes nothing; a Bayes-factor test separates a sleeper's two epochs
|
| 22 |
+
race_net_m RACE-Net-M (this repo): the same anchored channel estimation, LEARNED from simulated swarms
|
| 23 |
+
with random two-coin liars (robotics.swarm.racenet_m)
|
| 24 |
+
... + reach-repair planner variant: when the fused map says the goal is unreachable, un-believe only the
|
| 25 |
+
claimed walls that enclose the goal instead of dropping every peer
|
| 26 |
+
REF oracle knows who lies (reference, not ranked)
|
| 27 |
+
|
| 28 |
+
"Verified" = cells the receiver sensed itself that the peer also reported: the receiver's own
|
| 29 |
+
sensor is the anchor (RACE's "I know I am honest"), so no labels and no majority are needed.
|
| 30 |
+
|
| 31 |
+
Score = SPL (Success weighted by Path Length, Anderson et al. 2018), averaged over honest robots;
|
| 32 |
+
a robot fails if it needs more than 2.5x its optimal path length + 10 steps.
|
| 33 |
+
"""
|
| 34 |
+
from __future__ import annotations
|
| 35 |
+
|
| 36 |
+
from collections import deque
|
| 37 |
+
|
| 38 |
+
import numpy as np
|
| 39 |
+
|
| 40 |
+
RACE_TAU = 1.0 # log-odds needed before race_map blocks a cell (chosen on development mazes, seeds 0-7)
|
| 41 |
+
ATTACKS_D = ("none", "false_walls", "false_free", "invert", "random", "collude", "sleeper")
|
| 42 |
+
BASELINES_D = ("solo", "trust_all", "majority", "wmsr_trim", "eigentrust", "dawid_skene", "reputation")
|
| 43 |
+
MV = " + reach-repair"
|
| 44 |
+
OURS_D = ("race_map", "race_net_m", "race_map" + MV, "race_net_m" + MV)
|
| 45 |
+
ABLATION_D = ("reputation" + MV, "eigentrust" + MV, "majority" + MV) # the same planner fallback on baselines
|
| 46 |
+
METHODS_D = BASELINES_D + OURS_D + ABLATION_D + ("REF oracle",)
|
| 47 |
+
# Reachability repair (ours): the task guarantees that the goal can be reached. When the fused map says it cannot,
|
| 48 |
+
# some believed wall around the goal must be a lie. The standard planner then drops every peer and plans on the robot's
|
| 49 |
+
# own observations; the repair instead keeps the fused map and only un-believes the claimed walls that enclose the
|
| 50 |
+
# goal's reachable region, layer by layer (at most REPAIR_LAYERS), until the robot is connected to the goal.
|
| 51 |
+
# NET_TAU was chosen on development mazes (scripts/tune_trackD.py); the benchmark seeds were not used.
|
| 52 |
+
NET_TAU, REPAIR_LAYERS = 2.0, 6
|
| 53 |
+
NB8 = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 0), (0, 1), (1, -1), (1, 0), (1, 1)]
|
| 54 |
+
NB4 = [(-1, 0), (1, 0), (0, -1), (0, 1)]
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def make_maze(w=12, h=12, loops=0.08, seed=0):
|
| 58 |
+
"""Perfect maze by randomised DFS, then `loops` of the interior walls removed (alternative routes)."""
|
| 59 |
+
rng = np.random.default_rng(seed)
|
| 60 |
+
G = np.ones((2 * h + 1, 2 * w + 1), np.int8)
|
| 61 |
+
seen = np.zeros((h, w), bool)
|
| 62 |
+
stack = [(0, 0)]
|
| 63 |
+
seen[0, 0] = True
|
| 64 |
+
G[1, 1] = 0
|
| 65 |
+
while stack:
|
| 66 |
+
r, c = stack[-1]
|
| 67 |
+
nb = [(r + dr, c + dc) for dr, dc in NB4 if 0 <= r + dr < h and 0 <= c + dc < w and not seen[r + dr, c + dc]]
|
| 68 |
+
if not nb:
|
| 69 |
+
stack.pop()
|
| 70 |
+
continue
|
| 71 |
+
nr, nc = nb[rng.integers(len(nb))]
|
| 72 |
+
G[r + nr + 1, c + nc + 1] = 0
|
| 73 |
+
G[2 * nr + 1, 2 * nc + 1] = 0
|
| 74 |
+
seen[nr, nc] = True
|
| 75 |
+
stack.append((nr, nc))
|
| 76 |
+
inner = [(r, c) for r in range(1, 2 * h) for c in range(1, 2 * w) if G[r, c] == 1 and (r % 2) != (c % 2)]
|
| 77 |
+
for k in rng.choice(len(inner), int(loops * len(inner)), replace=False):
|
| 78 |
+
G[inner[k]] = 0
|
| 79 |
+
return G
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def bfs(free, src):
|
| 83 |
+
"""Distance field from src over cells where free is True (4-connected)."""
|
| 84 |
+
H, W = free.shape
|
| 85 |
+
dist = np.full((H, W), -1, np.int32)
|
| 86 |
+
dist[src] = 0
|
| 87 |
+
q = deque([src])
|
| 88 |
+
while q:
|
| 89 |
+
r, c = q.popleft()
|
| 90 |
+
d = dist[r, c] + 1
|
| 91 |
+
for dr, dc in NB4:
|
| 92 |
+
rr, cc = r + dr, c + dc
|
| 93 |
+
if 0 <= rr < H and 0 <= cc < W and free[rr, cc] and dist[rr, cc] < 0:
|
| 94 |
+
dist[rr, cc] = d
|
| 95 |
+
q.append((rr, cc))
|
| 96 |
+
return dist
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
def _eigentrust(own, claims, i, alpha=0.15, iters=30):
|
| 100 |
+
"""EigenTrust (Kamvar, Schlosser & Garcia-Molina 2003). Local trust c[j,k] = max(sat - unsat, 0) over the cells
|
| 101 |
+
both j and k reported (the receiver uses its own sensed cells), row-normalised; the receiver is the only
|
| 102 |
+
pre-trusted peer. Returns the global trust vector (discard-only: trust is never negative)."""
|
| 103 |
+
n = len(claims)
|
| 104 |
+
V = claims.astype(np.int16).copy()
|
| 105 |
+
V[i] = own # the receiver rates others with what it sensed itself
|
| 106 |
+
C = np.zeros((n, n))
|
| 107 |
+
for j in range(n):
|
| 108 |
+
for k in range(n):
|
| 109 |
+
if j != k:
|
| 110 |
+
both = (V[j] >= 0) & (V[k] >= 0)
|
| 111 |
+
agree = int(np.sum(V[j][both] == V[k][both]))
|
| 112 |
+
C[j, k] = max(2 * agree - int(both.sum()), 0)
|
| 113 |
+
p = np.zeros(n)
|
| 114 |
+
p[i] = 1.0
|
| 115 |
+
rows = C.sum(1, keepdims=True)
|
| 116 |
+
C = np.where(rows > 0, C / np.maximum(rows, 1e-9), p[None])
|
| 117 |
+
t = p.copy()
|
| 118 |
+
for _ in range(iters):
|
| 119 |
+
t = (1 - alpha) * C.T @ t + alpha * p
|
| 120 |
+
t[i] = 0.0
|
| 121 |
+
return t / max(t.max(), 1e-9)
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
def _dawid_skene(own, claims, i, iters=25):
|
| 125 |
+
"""Unanchored two-coin Dawid-Skene EM over the map cells (Dawid & Skene 1979): every robot, the receiver
|
| 126 |
+
included, is one annotator; initialised with the majority vote. Returns per-cell log-odds of 'wall'."""
|
| 127 |
+
V = claims.astype(np.int16).copy()
|
| 128 |
+
V[i] = own
|
| 129 |
+
has = V >= 0
|
| 130 |
+
m = has.any(0)
|
| 131 |
+
if not m.any():
|
| 132 |
+
return np.zeros(own.shape)
|
| 133 |
+
X, Hm = (V[:, m] == 1).astype(float), has[:, m].astype(float) # [n, cells]
|
| 134 |
+
q = (X * Hm).sum(0) / np.maximum(Hm.sum(0), 1)
|
| 135 |
+
for _ in range(iters):
|
| 136 |
+
pi = np.clip(q.mean(), 0.05, 0.95)
|
| 137 |
+
t11 = np.clip(((X * Hm) @ q + 1) / (Hm @ q + 2), 0.02, 0.98) # P(says wall | wall)
|
| 138 |
+
t00 = np.clip((((1 - X) * Hm) @ (1 - q) + 1) / (Hm @ (1 - q) + 2), 0.02, 0.98) # P(says free | free)
|
| 139 |
+
ll = (np.log(pi / (1 - pi))
|
| 140 |
+
+ (Hm * (X * np.log(t11 / (1 - t00))[:, None] + (1 - X) * np.log((1 - t11) / t00)[:, None])).sum(0))
|
| 141 |
+
q = 1 / (1 + np.exp(-np.clip(ll, -30, 30)))
|
| 142 |
+
score = np.zeros(own.shape)
|
| 143 |
+
score[m] = np.clip(ll, -30, 30)
|
| 144 |
+
return score
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
def _acc_weights(own, claims, i, method, byz):
|
| 148 |
+
"""One weight per peer (majority / reputation / oracle)."""
|
| 149 |
+
n = len(claims)
|
| 150 |
+
lam = np.zeros(n)
|
| 151 |
+
for j in range(n):
|
| 152 |
+
if j == i:
|
| 153 |
+
continue
|
| 154 |
+
if method == "REF oracle":
|
| 155 |
+
lam[j] = 0.0 if byz[j] else 2.0
|
| 156 |
+
elif method == "majority":
|
| 157 |
+
lam[j] = 1.0
|
| 158 |
+
else: # beta reputation: verified accuracy, discard-only
|
| 159 |
+
both = (own >= 0) & (claims[j] >= 0)
|
| 160 |
+
a = int(np.sum(own[both] == claims[j][both]))
|
| 161 |
+
acc = (a + 1.2) / (int(both.sum()) + 2.0) # Beta prior: an unverified peer is mildly trusted
|
| 162 |
+
lam[j] = max(float(np.log(acc / (1 - acc))), 0.0)
|
| 163 |
+
return lam
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
# Channel prior: a peer's two-coin channel (t00 = P(says free | free), t11 = P(says wall | wall)) lies on a
|
| 167 |
+
# 7x7 grid; 60% prior mass on the honest type, the rest uniform. No attack-specific types.
|
| 168 |
+
_LEV = np.array([0.03, 0.2, 0.4, 0.5, 0.6, 0.8, 0.97])
|
| 169 |
+
_T00, _T11 = [x.ravel() for x in np.meshgrid(_LEV, _LEV, indexing="ij")]
|
| 170 |
+
_LOGPRIOR = np.log(np.where((_T00 == 0.97) & (_T11 == 0.97), 0.6, 0.4 / (len(_T00) - 1)))
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
def _posterior(n):
|
| 174 |
+
"""n[truth][claim] verified counts -> (posterior over channel types, log marginal likelihood)."""
|
| 175 |
+
ll = (_LOGPRIOR + n[0, 0] * np.log(_T00) + n[0, 1] * np.log1p(-_T00)
|
| 176 |
+
+ n[1, 1] * np.log(_T11) + n[1, 0] * np.log1p(-_T11))
|
| 177 |
+
m = ll.max()
|
| 178 |
+
w = np.exp(ll - m)
|
| 179 |
+
return w / w.sum(), m + np.log(w.sum())
|
| 180 |
+
|
| 181 |
+
|
| 182 |
+
def _llr(w):
|
| 183 |
+
"""Bayes-averaged evidence of a claim: [llr(claim free), llr(claim wall)] = log P(c | wall) / P(c | free)."""
|
| 184 |
+
p1w, p1f = float(w @ _T11), float(w @ (1 - _T00))
|
| 185 |
+
return np.array([np.log((1 - p1w) / (1 - p1f)), np.log(p1w / p1f)])
|
| 186 |
+
|
| 187 |
+
|
| 188 |
+
def _counts(own, claim, mask):
|
| 189 |
+
n = np.zeros((2, 2))
|
| 190 |
+
if mask.any():
|
| 191 |
+
np.add.at(n, (own[mask], claim[mask]), 1.0)
|
| 192 |
+
return n
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
def _race_score(own, claims, stamp, i, t, window):
|
| 196 |
+
"""Receiver-anchored channel estimation on a map. For every peer, the receiver compares the peer's
|
| 197 |
+
broadcasts with the cells it sensed ITSELF (its anchor) and infers the peer's two-coin channel by
|
| 198 |
+
Bayesian model averaging. Each claim then enters as a signed log-likelihood ratio: an inverter's
|
| 199 |
+
claims are flipped and used, a wall-inventor keeps only the weight of its 'free' claims, a random
|
| 200 |
+
peer averages out to zero evidence. Sleepers: if a Bayes-factor test says the recent `window` steps
|
| 201 |
+
follow a different channel than the past, the two epochs are scored with separate channels."""
|
| 202 |
+
score = np.zeros(own.shape)
|
| 203 |
+
seen = own >= 0
|
| 204 |
+
for j in range(len(claims)):
|
| 205 |
+
if j == i:
|
| 206 |
+
continue
|
| 207 |
+
has = claims[j] >= 0
|
| 208 |
+
rec = has & (stamp[j] >= t - window)
|
| 209 |
+
old = has & ~rec
|
| 210 |
+
n_rec, n_old = _counts(own, claims[j], rec & seen), _counts(own, claims[j], old & seen)
|
| 211 |
+
w_all, m_all = _posterior(n_rec + n_old)
|
| 212 |
+
w_rec, m_rec = _posterior(n_rec)
|
| 213 |
+
w_old, m_old = _posterior(n_old)
|
| 214 |
+
if m_rec + m_old - m_all > 3.0: # log Bayes factor for "the channel changed"
|
| 215 |
+
score[rec] += _llr(w_rec)[claims[j][rec]]
|
| 216 |
+
score[old] += _llr(w_old)[claims[j][old]]
|
| 217 |
+
else:
|
| 218 |
+
score[has] += _llr(w_all)[claims[j][has]]
|
| 219 |
+
return score
|
| 220 |
+
|
| 221 |
+
|
| 222 |
+
def wall_logodds(own, claims, stamp, i, method, byz, t=0, window=30):
|
| 223 |
+
"""Per-cell log-odds that a cell is a wall, from the peers' broadcasts (methods that produce evidence)."""
|
| 224 |
+
if method == "race_map":
|
| 225 |
+
return _race_score(own, claims, stamp, i, t, window)
|
| 226 |
+
if method == "race_net_m":
|
| 227 |
+
from robotics.swarm import racenet_m
|
| 228 |
+
return racenet_m.wall_logodds(own, claims, stamp, i, t, window)
|
| 229 |
+
raise ValueError(method)
|
| 230 |
+
|
| 231 |
+
|
| 232 |
+
def scores(own, claims, stamp, i, method, byz, t=0, window=30):
|
| 233 |
+
"""(per-cell evidence that the cell is a wall, threshold) for every fusion rule except solo / trust_all."""
|
| 234 |
+
n = len(claims)
|
| 235 |
+
vote = (claims == 1).astype(float) - (claims == 0).astype(float)
|
| 236 |
+
if method in ("race_map", "race_net_m"):
|
| 237 |
+
return wall_logodds(own, claims, stamp, i, method, byz, t, window), (RACE_TAU if method == "race_map" else NET_TAU)
|
| 238 |
+
if method == "wmsr_trim":
|
| 239 |
+
# W-MSR-style trimming (LeBlanc et al. 2013) per cell: drop up to F 'wall' and up to F 'free' votes
|
| 240 |
+
F = int(byz.sum()) # the number of Byzantine robots is assumed known
|
| 241 |
+
peers = np.arange(n) != i
|
| 242 |
+
nw, nf = (claims[peers] == 1).sum(0), (claims[peers] == 0).sum(0)
|
| 243 |
+
return np.maximum(nw - F, 0) - np.maximum(nf - F, 0).astype(float), 0.4
|
| 244 |
+
if method == "eigentrust":
|
| 245 |
+
return np.tensordot(_eigentrust(own, claims, i), vote, 1), 0.4
|
| 246 |
+
if method == "dawid_skene":
|
| 247 |
+
return _dawid_skene(own, claims, i), RACE_TAU
|
| 248 |
+
return np.tensordot(_acc_weights(own, claims, i, method, byz), vote, 1), 0.4
|
| 249 |
+
|
| 250 |
+
|
| 251 |
+
def belief(own, claims, stamp, i, method, byz, t=0, window=30, return_score=False):
|
| 252 |
+
"""Receiver i's map: -1 unknown, 0 free, 1 wall. Planning is optimistic (anything not believed 'wall' is
|
| 253 |
+
traversable), so only the wall threshold matters; race scores are log-likelihood ratios and need posterior
|
| 254 |
+
odds of about 3:1 before a cell is blocked."""
|
| 255 |
+
if method == "solo":
|
| 256 |
+
return (own, None) if return_score else own
|
| 257 |
+
n = len(claims)
|
| 258 |
+
if method == "trust_all":
|
| 259 |
+
st = np.where(np.arange(n)[:, None, None] == i, -1, stamp)
|
| 260 |
+
j = st.argmax(0)
|
| 261 |
+
B = np.take_along_axis(claims, j[None], 0)[0]
|
| 262 |
+
B = np.where(st.max(0) >= 0, B, -1)
|
| 263 |
+
score = None
|
| 264 |
+
else:
|
| 265 |
+
score, tau = scores(own, claims, stamp, i, method, byz, t, window)
|
| 266 |
+
B = np.where(score > tau, 1, np.where(score < -tau, 0, -1)).astype(np.int8)
|
| 267 |
+
B = np.where(own >= 0, own, B)
|
| 268 |
+
return (B, score) if return_score else B
|
| 269 |
+
|
| 270 |
+
|
| 271 |
+
def repair_field(own, B, goal, start, layers=None):
|
| 272 |
+
"""Distance-to-goal on the fused map after un-believing the claimed walls that enclose the goal (never walls the
|
| 273 |
+
robot sensed itself). Returns None if the robot is still disconnected after `layers` rounds."""
|
| 274 |
+
B = B.copy()
|
| 275 |
+
for _ in range(REPAIR_LAYERS if layers is None else layers):
|
| 276 |
+
d = bfs(B != 1, goal)
|
| 277 |
+
if d[start] >= 0:
|
| 278 |
+
return d
|
| 279 |
+
comp = d >= 0
|
| 280 |
+
edge = np.zeros_like(comp)
|
| 281 |
+
edge[1:, :] |= comp[:-1, :]
|
| 282 |
+
edge[:-1, :] |= comp[1:, :]
|
| 283 |
+
edge[:, 1:] |= comp[:, :-1]
|
| 284 |
+
edge[:, :-1] |= comp[:, 1:]
|
| 285 |
+
peel = edge & (B == 1) & (own != 1)
|
| 286 |
+
if not peel.any():
|
| 287 |
+
return None
|
| 288 |
+
B[peel] = -1
|
| 289 |
+
d = bfs(B != 1, goal)
|
| 290 |
+
return d if d[start] >= 0 else None
|
| 291 |
+
|
| 292 |
+
|
| 293 |
+
def episode(seed, attack="none", F=0, method="race_map", n=6, w=12, h=12, trace=None, sleeper_T=40,
|
| 294 |
+
channels=None, snaps=None, snap_p=0.0):
|
| 295 |
+
"""channels: optional {robot: (t00, t11, t_change)} parametric two-coin liars (used to train RACE-Net-M; then
|
| 296 |
+
`attack` is ignored for those robots). snaps: list that receives (own, claims, stamp, i, t) snapshots."""
|
| 297 |
+
mv = method.endswith(MV)
|
| 298 |
+
fuse = method.replace(MV, "")
|
| 299 |
+
rng = np.random.default_rng(seed)
|
| 300 |
+
srng = np.random.default_rng(seed + 7_000_003) if snaps is not None else None
|
| 301 |
+
G = make_maze(w, h, seed=seed)
|
| 302 |
+
H, W = G.shape
|
| 303 |
+
free_cells = np.argwhere(G == 0)
|
| 304 |
+
goal = tuple(free_cells[rng.integers(len(free_cells))])
|
| 305 |
+
dgoal = bfs(G == 0, goal)
|
| 306 |
+
far = free_cells[dgoal[free_cells[:, 0], free_cells[:, 1]] >= np.percentile(dgoal[G == 0], 55)]
|
| 307 |
+
pos = [tuple(far[k]) for k in rng.choice(len(far), n, replace=False)]
|
| 308 |
+
byz = np.zeros(n, bool)
|
| 309 |
+
byz[rng.choice(n, F, replace=False)] = True
|
| 310 |
+
if channels is not None:
|
| 311 |
+
byz[:] = False
|
| 312 |
+
byz[list(channels)] = True
|
| 313 |
+
opt = np.array([dgoal[p] for p in pos])
|
| 314 |
+
budget = (2.5 * opt + 10).astype(int) # per-robot deadline: 2.5x its optimal path + 10 steps
|
| 315 |
+
T = int(budget.max())
|
| 316 |
+
own = np.full((n, H, W), -1, np.int8) # each robot's direct observations
|
| 317 |
+
claims = np.full((n, H, W), -1, np.int8) # latest broadcast of each robot about each cell
|
| 318 |
+
stamp = np.full((n, H, W), -1, np.int32)
|
| 319 |
+
steps = np.zeros(n, int)
|
| 320 |
+
done = np.zeros(n, bool)
|
| 321 |
+
wander = [None] * n
|
| 322 |
+
ring = [(goal[0] + dr, goal[1] + dc) for dr, dc in NB4 if G[goal[0] + dr, goal[1] + dc] == 0]
|
| 323 |
+
for t in range(T):
|
| 324 |
+
# ---- sense + broadcast
|
| 325 |
+
for i in range(n):
|
| 326 |
+
for dr, dc in NB8:
|
| 327 |
+
r, c = pos[i][0] + dr, pos[i][1] + dc
|
| 328 |
+
v = G[r, c]
|
| 329 |
+
own[i, r, c] = v
|
| 330 |
+
say = v
|
| 331 |
+
if byz[i] and channels is not None:
|
| 332 |
+
t00, t11, t_change = channels[i]
|
| 333 |
+
if t >= t_change and rng.random() > (t11 if v == 1 else t00):
|
| 334 |
+
say = 1 - v
|
| 335 |
+
elif byz[i]:
|
| 336 |
+
a = attack
|
| 337 |
+
if a == "sleeper":
|
| 338 |
+
a = "none" if t < sleeper_T else "invert"
|
| 339 |
+
if a == "invert":
|
| 340 |
+
say = 1 - v
|
| 341 |
+
elif a == "false_walls" and v == 0 and rng.random() < 0.6:
|
| 342 |
+
say = 1
|
| 343 |
+
elif a == "false_free" and v == 1 and rng.random() < 0.6:
|
| 344 |
+
say = 0
|
| 345 |
+
elif a == "random":
|
| 346 |
+
say = int(rng.integers(2))
|
| 347 |
+
claims[i, r, c] = say
|
| 348 |
+
stamp[i, r, c] = t
|
| 349 |
+
if byz[i] and attack == "collude": # the bloc walls off the goal, consistently and every step
|
| 350 |
+
for r, c in ring:
|
| 351 |
+
claims[i, r, c] = 1
|
| 352 |
+
stamp[i, r, c] = t
|
| 353 |
+
# ---- move
|
| 354 |
+
beliefs = {}
|
| 355 |
+
for i in range(n):
|
| 356 |
+
if byz[i]: # liars keep exploring (random reachable targets)
|
| 357 |
+
if wander[i] is None or pos[i] == wander[i]:
|
| 358 |
+
wander[i] = tuple(free_cells[rng.integers(len(free_cells))])
|
| 359 |
+
d = bfs(G == 0, wander[i])
|
| 360 |
+
else:
|
| 361 |
+
if done[i] or steps[i] >= budget[i]:
|
| 362 |
+
continue
|
| 363 |
+
if snaps is not None and srng.random() < snap_p:
|
| 364 |
+
snaps.append((own[i].copy(), claims.copy(), stamp.copy(), i, t))
|
| 365 |
+
B, score = belief(own[i], claims, stamp, i, fuse, byz, t, return_score=True)
|
| 366 |
+
beliefs[i] = B
|
| 367 |
+
d = bfs(B != 1, goal)
|
| 368 |
+
if d[pos[i]] < 0: # the fused map says the goal is unreachable
|
| 369 |
+
d = repair_field(own[i], B, goal, pos[i]) if mv else None
|
| 370 |
+
if d is None: # standard: fall back to the robot's own observations
|
| 371 |
+
d = bfs(own[i] != 1, goal)
|
| 372 |
+
r, c = pos[i]
|
| 373 |
+
best = min(((d[r + dr, c + dc], (r + dr, c + dc)) for dr, dc in NB4
|
| 374 |
+
if G[r + dr, c + dc] == 0 and d[r + dr, c + dc] >= 0), default=None)
|
| 375 |
+
if best is not None and best[0] < d[r, c] or (best is not None and d[r, c] < 0):
|
| 376 |
+
pos[i] = best[1]
|
| 377 |
+
if not byz[i]:
|
| 378 |
+
steps[i] += 1
|
| 379 |
+
elif not byz[i]:
|
| 380 |
+
steps[i] += 1 # waiting also costs time
|
| 381 |
+
if not byz[i] and pos[i] == goal:
|
| 382 |
+
done[i] = True
|
| 383 |
+
if trace is not None:
|
| 384 |
+
trace.append({"pos": list(pos), "done": done.copy(), "beliefs": {k: v.copy() for k, v in beliefs.items()}})
|
| 385 |
+
if (done | (steps >= budget))[~byz].all():
|
| 386 |
+
break
|
| 387 |
+
honest = ~byz
|
| 388 |
+
spl = np.where(done, opt / np.maximum(steps, np.maximum(opt, 1)), 0.0)[honest]
|
| 389 |
+
return {"spl": float(spl.mean()), "success": float(done[honest].mean()),
|
| 390 |
+
"ratio": float(np.mean((steps / np.maximum(opt, 1))[honest & done])) if (honest & done).any() else float("nan"),
|
| 391 |
+
"G": G, "goal": goal, "byz": byz}
|
| 392 |
+
|
| 393 |
+
|
| 394 |
+
def _cell(args):
|
| 395 |
+
seed, attack, F, method = args
|
| 396 |
+
r = episode(seed, attack, F, method)
|
| 397 |
+
return {"track": "D", "attack": attack, "liars": F, "f": round(F / 6, 2), "subset": seed, "method": method,
|
| 398 |
+
"acc": r["spl"], "success": r["success"], "ratio": r["ratio"]}
|
| 399 |
+
|
| 400 |
+
|
| 401 |
+
def available_methods():
|
| 402 |
+
from robotics.swarm import racenet_m
|
| 403 |
+
return tuple(m for m in METHODS_D if "race_net_m" not in m or racenet_m.available())
|
| 404 |
+
|
| 405 |
+
|
| 406 |
+
def run(n_mazes=20, Fs=(1, 2, 3, 4), attacks=ATTACKS_D, procs=2, only=None, first_seed=1000):
|
| 407 |
+
"""only: optional set of method names (cells are deterministic, so new methods can be added to saved rows)."""
|
| 408 |
+
jobs = [(first_seed + s, a, F, m) for a in attacks for F in ((0,) if a == "none" else Fs)
|
| 409 |
+
for s in range(n_mazes) for m in available_methods() if only is None or m in only]
|
| 410 |
+
if procs > 1:
|
| 411 |
+
from multiprocessing import Pool
|
| 412 |
+
with Pool(procs) as p:
|
| 413 |
+
return p.map(_cell, jobs, chunksize=8)
|
| 414 |
+
return [_cell(j) for j in jobs]
|
trackD_vs_liars.png
ADDED
|
Git LFS Details
|
train_racenet_m.py
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Train RACE-Net-M (map fusion) on simulated maze swarms with random two-coin liars (CPU, ~20 min).
|
| 3 |
+
|
| 4 |
+
python scripts/train_racenet_m.py [--episodes 2400] [--steps 4000] [--procs 3]
|
| 5 |
+
Training mazes use seeds 5000+, validation 9000+; the benchmark uses seeds 1000-1019 and tuning uses 0-39.
|
| 6 |
+
Liars follow random two-coin channels with an optional change point (`racenet_m.sample_channels`): the
|
| 7 |
+
benchmark's attack names are never used, and the colluding ring attack is outside that family.
|
| 8 |
+
Writes models/racenet_m/{model.safetensors, config.json, training_log.json}.
|
| 9 |
+
"""
|
| 10 |
+
import argparse
|
| 11 |
+
import json
|
| 12 |
+
import sys
|
| 13 |
+
import time
|
| 14 |
+
from multiprocessing import Pool
|
| 15 |
+
from pathlib import Path
|
| 16 |
+
|
| 17 |
+
import numpy as np
|
| 18 |
+
import torch
|
| 19 |
+
|
| 20 |
+
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
|
| 21 |
+
from robotics.bench import trackD # noqa: E402
|
| 22 |
+
from robotics.swarm import racenet_m # noqa: E402
|
| 23 |
+
|
| 24 |
+
NAV = ("solo", "race_map", "trust_all") # how honest robots move while data is collected
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def _episode(seed):
|
| 28 |
+
rng = np.random.default_rng(seed)
|
| 29 |
+
snaps = []
|
| 30 |
+
ch = racenet_m.sample_channels(rng)
|
| 31 |
+
r = trackD.episode(seed, "none", 0, NAV[seed % len(NAV)], channels=ch, snaps=snaps, snap_p=0.06)
|
| 32 |
+
out = []
|
| 33 |
+
for own, claims, stamp, i, t in snaps:
|
| 34 |
+
c, e, o = racenet_m.tensors(own, claims, stamp, i, t)
|
| 35 |
+
base = trackD._race_score(own, claims, stamp, i, t, 30).reshape(-1) # the hand-written BMA, for reference
|
| 36 |
+
out.append((c, e, o, r["G"].reshape(-1).astype(np.int8), len(ch), base.astype(np.float32)))
|
| 37 |
+
return out
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def collect(seeds, procs):
|
| 41 |
+
with Pool(procs) as p:
|
| 42 |
+
eps = p.map(_episode, seeds, chunksize=8)
|
| 43 |
+
S = [s for ep in eps for s in ep]
|
| 44 |
+
return {"c": np.stack([s[0] for s in S]), "e": np.stack([s[1] for s in S]), "o": np.stack([s[2] for s in S]),
|
| 45 |
+
"y": np.stack([s[3] for s in S]), "F": np.array([s[4] for s in S]), "base": np.stack([s[5] for s in S])}
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def evaluate(net, D, bs=256):
|
| 49 |
+
"""Accuracy on cells that somebody reported and the receiver has not sensed, by number of liars."""
|
| 50 |
+
net.eval()
|
| 51 |
+
hit_n, hit_b, tot, byF = 0.0, 0.0, 0.0, {}
|
| 52 |
+
with torch.inference_mode():
|
| 53 |
+
for k in range(0, len(D["o"]), bs):
|
| 54 |
+
c, e, o, y = (torch.from_numpy(D[x][k:k + bs]).long() for x in ("c", "e", "o", "y"))
|
| 55 |
+
m = ((c >= 0).any(1) & (o < 0)).float()
|
| 56 |
+
pn = (net(c, e, o) > 0).long()
|
| 57 |
+
pb = (torch.from_numpy(D["base"][k:k + bs]) > 0).long()
|
| 58 |
+
for f in np.unique(D["F"][k:k + bs]):
|
| 59 |
+
sel = torch.from_numpy(D["F"][k:k + bs] == f)
|
| 60 |
+
a = byF.setdefault(int(f), [0.0, 0.0, 0.0])
|
| 61 |
+
a[0] += float(((pn == y).float() * m)[sel].sum()); a[1] += float(((pb == y).float() * m)[sel].sum()); a[2] += float(m[sel].sum())
|
| 62 |
+
hit_n += float(((pn == y).float() * m).sum()); hit_b += float(((pb == y).float() * m).sum()); tot += float(m.sum())
|
| 63 |
+
net.train()
|
| 64 |
+
return hit_n / tot, hit_b / tot, {f: (round(a[0] / a[2], 4), round(a[1] / a[2], 4)) for f, a in sorted(byF.items()) if a[2]}
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def main():
|
| 68 |
+
ap = argparse.ArgumentParser()
|
| 69 |
+
ap.add_argument("--episodes", type=int, default=2400)
|
| 70 |
+
ap.add_argument("--steps", type=int, default=4000)
|
| 71 |
+
ap.add_argument("--batch", type=int, default=48)
|
| 72 |
+
ap.add_argument("--lr", type=float, default=2e-3)
|
| 73 |
+
ap.add_argument("--procs", type=int, default=3)
|
| 74 |
+
ap.add_argument("--threads", type=int, default=3)
|
| 75 |
+
ap.add_argument("--seed", type=int, default=0)
|
| 76 |
+
ap.add_argument("--rounds", type=int, default=racenet_m.CONFIG["rounds"])
|
| 77 |
+
ap.add_argument("--cache", default="data/racenet_m")
|
| 78 |
+
ap.add_argument("--out", default="models/racenet_m")
|
| 79 |
+
a = ap.parse_args()
|
| 80 |
+
cache = Path(a.cache)
|
| 81 |
+
cache.mkdir(parents=True, exist_ok=True)
|
| 82 |
+
t0 = time.time()
|
| 83 |
+
data = {}
|
| 84 |
+
for name, seeds in (("train", range(5000, 5000 + a.episodes)), ("val", range(9000, 9000 + max(a.episodes // 8, 60)))):
|
| 85 |
+
f = cache / f"{name}_{len(seeds)}.npz"
|
| 86 |
+
if not f.exists():
|
| 87 |
+
np.savez_compressed(f, **collect(list(seeds), a.procs))
|
| 88 |
+
data[name] = dict(np.load(f))
|
| 89 |
+
print(name, "snapshots", len(data[name]["o"]), f"[{time.time() - t0:.0f}s]", flush=True)
|
| 90 |
+
torch.set_num_threads(a.threads)
|
| 91 |
+
torch.manual_seed(a.seed)
|
| 92 |
+
rng = np.random.default_rng(a.seed)
|
| 93 |
+
arch = {**racenet_m.CONFIG, "rounds": a.rounds}
|
| 94 |
+
net = racenet_m.RaceNetM(**arch)
|
| 95 |
+
n_params = sum(p.numel() for p in net.parameters())
|
| 96 |
+
opt = torch.optim.AdamW(net.parameters(), lr=a.lr, weight_decay=0.01)
|
| 97 |
+
sched = torch.optim.lr_scheduler.OneCycleLR(opt, max_lr=a.lr, total_steps=a.steps, pct_start=0.05)
|
| 98 |
+
D, n = data["train"], len(data["train"]["o"])
|
| 99 |
+
log, run = [], 0.0
|
| 100 |
+
for step in range(1, a.steps + 1):
|
| 101 |
+
k = rng.choice(n, a.batch, replace=False)
|
| 102 |
+
c, e, o, y = (torch.from_numpy(D[x][k]).long() for x in ("c", "e", "o", "y"))
|
| 103 |
+
P = c.shape[1]
|
| 104 |
+
perm = torch.from_numpy(rng.permutation(P))
|
| 105 |
+
c, e = c[:, perm], e[:, perm]
|
| 106 |
+
if rng.random() < 0.3: # smaller swarms: drop one or two peers
|
| 107 |
+
keep = P - int(rng.integers(1, 3))
|
| 108 |
+
c, e = c[:, :keep], e[:, :keep]
|
| 109 |
+
m = ((c >= 0).any(1) & (o < 0)).float()
|
| 110 |
+
outs = net(c, e, o, return_all=True)
|
| 111 |
+
loss = sum((torch.nn.functional.binary_cross_entropy_with_logits(lg, y.float(), reduction="none") * m).sum() / m.sum().clamp(min=1)
|
| 112 |
+
for lg in outs) / len(outs)
|
| 113 |
+
opt.zero_grad()
|
| 114 |
+
loss.backward()
|
| 115 |
+
torch.nn.utils.clip_grad_norm_(net.parameters(), 1.0)
|
| 116 |
+
opt.step()
|
| 117 |
+
sched.step()
|
| 118 |
+
run = 0.98 * run + 0.02 * loss.item() if step > 1 else loss.item()
|
| 119 |
+
if step % 500 == 0 or step == a.steps:
|
| 120 |
+
acc, base, byF = evaluate(net, data["val"])
|
| 121 |
+
log.append({"step": step, "train_bce": round(run, 4), "val_cell_acc": round(acc, 4), "val_cell_acc_bma": round(base, 4),
|
| 122 |
+
"val_by_liars(net, bma)": byF, "minutes": round((time.time() - t0) / 60, 1)})
|
| 123 |
+
print(log[-1], flush=True)
|
| 124 |
+
out = Path(a.out)
|
| 125 |
+
out.mkdir(parents=True, exist_ok=True)
|
| 126 |
+
from safetensors.torch import save_file
|
| 127 |
+
save_file(net.state_dict(), str(out / "model.safetensors"))
|
| 128 |
+
(out / "config.json").write_text(json.dumps({
|
| 129 |
+
**arch, "n_params": n_params, "architecture": "RaceNetM (anchored channel encoder + peer transformer + signed read-out, iterative)",
|
| 130 |
+
"input": "peer claims [P peers, N cells] in {none, free, wall} with a recent/old flag, and the receiver's own sensed cells",
|
| 131 |
+
"output": "log-odds of 'wall' per cell",
|
| 132 |
+
"training": {"data": "maze swarms with random two-coin liars and change points (racenet_m.sample_channels)",
|
| 133 |
+
"train_seeds": [5000, 5000 + a.episodes - 1], "val_seeds_from": 9000, "snapshots": int(n),
|
| 134 |
+
"steps": a.steps, "batch": a.batch, "lr": a.lr, "seed": a.seed}}, indent=1))
|
| 135 |
+
(out / "training_log.json").write_text(json.dumps(log, indent=1))
|
| 136 |
+
print("saved", out, "params", n_params)
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
if __name__ == "__main__":
|
| 140 |
+
main()
|
training_log.json
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[
|
| 2 |
+
{
|
| 3 |
+
"step": 500,
|
| 4 |
+
"train_bce": 0.1853,
|
| 5 |
+
"val_cell_acc": 0.9152,
|
| 6 |
+
"val_cell_acc_bma": 0.9014,
|
| 7 |
+
"val_by_liars(net, bma)": {
|
| 8 |
+
"0": [
|
| 9 |
+
0.9998,
|
| 10 |
+
1.0
|
| 11 |
+
],
|
| 12 |
+
"1": [
|
| 13 |
+
0.9479,
|
| 14 |
+
0.9398
|
| 15 |
+
],
|
| 16 |
+
"2": [
|
| 17 |
+
0.89,
|
| 18 |
+
0.8725
|
| 19 |
+
],
|
| 20 |
+
"3": [
|
| 21 |
+
0.8581,
|
| 22 |
+
0.8269
|
| 23 |
+
],
|
| 24 |
+
"4": [
|
| 25 |
+
0.8223,
|
| 26 |
+
0.7985
|
| 27 |
+
],
|
| 28 |
+
"5": [
|
| 29 |
+
0.7564,
|
| 30 |
+
0.7293
|
| 31 |
+
]
|
| 32 |
+
},
|
| 33 |
+
"minutes": 6.6
|
| 34 |
+
},
|
| 35 |
+
{
|
| 36 |
+
"step": 1000,
|
| 37 |
+
"train_bce": 0.1843,
|
| 38 |
+
"val_cell_acc": 0.9192,
|
| 39 |
+
"val_cell_acc_bma": 0.9014,
|
| 40 |
+
"val_by_liars(net, bma)": {
|
| 41 |
+
"0": [
|
| 42 |
+
0.9998,
|
| 43 |
+
1.0
|
| 44 |
+
],
|
| 45 |
+
"1": [
|
| 46 |
+
0.9514,
|
| 47 |
+
0.9398
|
| 48 |
+
],
|
| 49 |
+
"2": [
|
| 50 |
+
0.8961,
|
| 51 |
+
0.8725
|
| 52 |
+
],
|
| 53 |
+
"3": [
|
| 54 |
+
0.8624,
|
| 55 |
+
0.8269
|
| 56 |
+
],
|
| 57 |
+
"4": [
|
| 58 |
+
0.8309,
|
| 59 |
+
0.7985
|
| 60 |
+
],
|
| 61 |
+
"5": [
|
| 62 |
+
0.7679,
|
| 63 |
+
0.7293
|
| 64 |
+
]
|
| 65 |
+
},
|
| 66 |
+
"minutes": 6.8
|
| 67 |
+
},
|
| 68 |
+
{
|
| 69 |
+
"step": 1500,
|
| 70 |
+
"train_bce": 0.1734,
|
| 71 |
+
"val_cell_acc": 0.9192,
|
| 72 |
+
"val_cell_acc_bma": 0.9014,
|
| 73 |
+
"val_by_liars(net, bma)": {
|
| 74 |
+
"0": [
|
| 75 |
+
0.9999,
|
| 76 |
+
1.0
|
| 77 |
+
],
|
| 78 |
+
"1": [
|
| 79 |
+
0.9505,
|
| 80 |
+
0.9398
|
| 81 |
+
],
|
| 82 |
+
"2": [
|
| 83 |
+
0.8942,
|
| 84 |
+
0.8725
|
| 85 |
+
],
|
| 86 |
+
"3": [
|
| 87 |
+
0.8644,
|
| 88 |
+
0.8269
|
| 89 |
+
],
|
| 90 |
+
"4": [
|
| 91 |
+
0.8326,
|
| 92 |
+
0.7985
|
| 93 |
+
],
|
| 94 |
+
"5": [
|
| 95 |
+
0.7696,
|
| 96 |
+
0.7293
|
| 97 |
+
]
|
| 98 |
+
},
|
| 99 |
+
"minutes": 7.0
|
| 100 |
+
},
|
| 101 |
+
{
|
| 102 |
+
"step": 2000,
|
| 103 |
+
"train_bce": 0.1706,
|
| 104 |
+
"val_cell_acc": 0.9213,
|
| 105 |
+
"val_cell_acc_bma": 0.9014,
|
| 106 |
+
"val_by_liars(net, bma)": {
|
| 107 |
+
"0": [
|
| 108 |
+
0.9998,
|
| 109 |
+
1.0
|
| 110 |
+
],
|
| 111 |
+
"1": [
|
| 112 |
+
0.9521,
|
| 113 |
+
0.9398
|
| 114 |
+
],
|
| 115 |
+
"2": [
|
| 116 |
+
0.9002,
|
| 117 |
+
0.8725
|
| 118 |
+
],
|
| 119 |
+
"3": [
|
| 120 |
+
0.865,
|
| 121 |
+
0.8269
|
| 122 |
+
],
|
| 123 |
+
"4": [
|
| 124 |
+
0.8362,
|
| 125 |
+
0.7985
|
| 126 |
+
],
|
| 127 |
+
"5": [
|
| 128 |
+
0.772,
|
| 129 |
+
0.7293
|
| 130 |
+
]
|
| 131 |
+
},
|
| 132 |
+
"minutes": 7.2
|
| 133 |
+
},
|
| 134 |
+
{
|
| 135 |
+
"step": 2500,
|
| 136 |
+
"train_bce": 0.1735,
|
| 137 |
+
"val_cell_acc": 0.9217,
|
| 138 |
+
"val_cell_acc_bma": 0.9014,
|
| 139 |
+
"val_by_liars(net, bma)": {
|
| 140 |
+
"0": [
|
| 141 |
+
0.9996,
|
| 142 |
+
1.0
|
| 143 |
+
],
|
| 144 |
+
"1": [
|
| 145 |
+
0.953,
|
| 146 |
+
0.9398
|
| 147 |
+
],
|
| 148 |
+
"2": [
|
| 149 |
+
0.905,
|
| 150 |
+
0.8725
|
| 151 |
+
],
|
| 152 |
+
"3": [
|
| 153 |
+
0.8632,
|
| 154 |
+
0.8269
|
| 155 |
+
],
|
| 156 |
+
"4": [
|
| 157 |
+
0.835,
|
| 158 |
+
0.7985
|
| 159 |
+
],
|
| 160 |
+
"5": [
|
| 161 |
+
0.7652,
|
| 162 |
+
0.7293
|
| 163 |
+
]
|
| 164 |
+
},
|
| 165 |
+
"minutes": 7.4
|
| 166 |
+
},
|
| 167 |
+
{
|
| 168 |
+
"step": 3000,
|
| 169 |
+
"train_bce": 0.1673,
|
| 170 |
+
"val_cell_acc": 0.9223,
|
| 171 |
+
"val_cell_acc_bma": 0.9014,
|
| 172 |
+
"val_by_liars(net, bma)": {
|
| 173 |
+
"0": [
|
| 174 |
+
0.9996,
|
| 175 |
+
1.0
|
| 176 |
+
],
|
| 177 |
+
"1": [
|
| 178 |
+
0.9532,
|
| 179 |
+
0.9398
|
| 180 |
+
],
|
| 181 |
+
"2": [
|
| 182 |
+
0.9008,
|
| 183 |
+
0.8725
|
| 184 |
+
],
|
| 185 |
+
"3": [
|
| 186 |
+
0.8667,
|
| 187 |
+
0.8269
|
| 188 |
+
],
|
| 189 |
+
"4": [
|
| 190 |
+
0.8388,
|
| 191 |
+
0.7985
|
| 192 |
+
],
|
| 193 |
+
"5": [
|
| 194 |
+
0.7751,
|
| 195 |
+
0.7293
|
| 196 |
+
]
|
| 197 |
+
},
|
| 198 |
+
"minutes": 7.6
|
| 199 |
+
},
|
| 200 |
+
{
|
| 201 |
+
"step": 3500,
|
| 202 |
+
"train_bce": 0.1686,
|
| 203 |
+
"val_cell_acc": 0.9229,
|
| 204 |
+
"val_cell_acc_bma": 0.9014,
|
| 205 |
+
"val_by_liars(net, bma)": {
|
| 206 |
+
"0": [
|
| 207 |
+
0.9997,
|
| 208 |
+
1.0
|
| 209 |
+
],
|
| 210 |
+
"1": [
|
| 211 |
+
0.9539,
|
| 212 |
+
0.9398
|
| 213 |
+
],
|
| 214 |
+
"2": [
|
| 215 |
+
0.9029,
|
| 216 |
+
0.8725
|
| 217 |
+
],
|
| 218 |
+
"3": [
|
| 219 |
+
0.8668,
|
| 220 |
+
0.8269
|
| 221 |
+
],
|
| 222 |
+
"4": [
|
| 223 |
+
0.8396,
|
| 224 |
+
0.7985
|
| 225 |
+
],
|
| 226 |
+
"5": [
|
| 227 |
+
0.7743,
|
| 228 |
+
0.7293
|
| 229 |
+
]
|
| 230 |
+
},
|
| 231 |
+
"minutes": 7.8
|
| 232 |
+
},
|
| 233 |
+
{
|
| 234 |
+
"step": 4000,
|
| 235 |
+
"train_bce": 0.1662,
|
| 236 |
+
"val_cell_acc": 0.9233,
|
| 237 |
+
"val_cell_acc_bma": 0.9014,
|
| 238 |
+
"val_by_liars(net, bma)": {
|
| 239 |
+
"0": [
|
| 240 |
+
0.9998,
|
| 241 |
+
1.0
|
| 242 |
+
],
|
| 243 |
+
"1": [
|
| 244 |
+
0.9542,
|
| 245 |
+
0.9398
|
| 246 |
+
],
|
| 247 |
+
"2": [
|
| 248 |
+
0.9035,
|
| 249 |
+
0.8725
|
| 250 |
+
],
|
| 251 |
+
"3": [
|
| 252 |
+
0.8673,
|
| 253 |
+
0.8269
|
| 254 |
+
],
|
| 255 |
+
"4": [
|
| 256 |
+
0.839,
|
| 257 |
+
0.7985
|
| 258 |
+
],
|
| 259 |
+
"5": [
|
| 260 |
+
0.7762,
|
| 261 |
+
0.7293
|
| 262 |
+
]
|
| 263 |
+
},
|
| 264 |
+
"minutes": 8.0
|
| 265 |
+
}
|
| 266 |
+
]
|