#!/bin/bash
# Run phase1 eval on all routes after trainings complete.
# Splits across 2 GPUs in parallel chains.

set -e
ROOT=/home/chy/mambafortrafficmodeling
UNIFIED_EVAL=${ROOT}/artifacts/verify_2026_04_24/eval_phase1_unified.py
MIXED_EVAL=${ROOT}/Mixed_CFM/eval_phase1.py

cd ${ROOT}

# GPU 0: baselines + route_a (6 models)
{
for prefix in baseline_ciciot2023 route_a_causal_ciciot2023; do
  for seed in 42 43 44; do
    name=${prefix}_seed${seed}
    md=${ROOT}/artifacts/route_comparison/${name}
    [ -f "${md}/model.pt" ] || continue
    [ -f "${md}/phase1_summary.json" ] && continue
    echo "[GPU0 eval] ${name}"
    cd ${ROOT}/Unified_CFM
    CUDA_VISIBLE_DEVICES=0 stdbuf -oL uv run --no-sync python -u ${UNIFIED_EVAL} \
      --model-dir ${md} --out-dir ${md} \
      --batch-size 256 --n-steps 16 --jacobian-n-eps 4 \
      --n-val-cap 5000 --n-atk-cap 10000 \
      > ${md}/phase1.log 2>&1
  done
done
echo "[GPU0 done]"
} &
GPU0_PID=$!

# GPU 1: route_b + route_c (6 models)
{
for seed in 42 43 44; do
  name=route_b_spectral_ciciot2023_seed${seed}
  md=${ROOT}/artifacts/route_comparison/${name}
  [ -f "${md}/model.pt" ] || continue
  [ -f "${md}/phase1_summary.json" ] && continue
  echo "[GPU1 eval] ${name}"
  cd ${ROOT}/Unified_CFM
  CUDA_VISIBLE_DEVICES=1 stdbuf -oL uv run --no-sync python -u ${UNIFIED_EVAL} \
    --model-dir ${md} --out-dir ${md} \
    --batch-size 256 --n-steps 16 --jacobian-n-eps 4 \
    --n-val-cap 5000 --n-atk-cap 10000 \
    > ${md}/phase1.log 2>&1
done
for seed in 42 43 44; do
  name=route_c_mixed_ciciot2023_seed${seed}
  md=${ROOT}/artifacts/route_comparison/${name}
  [ -f "${md}/model.pt" ] || continue
  [ -f "${md}/phase1_summary.json" ] && continue
  echo "[GPU1 eval] ${name}"
  cd ${ROOT}/Mixed_CFM
  CUDA_VISIBLE_DEVICES=1 stdbuf -oL uv run --no-sync python -u ${MIXED_EVAL} \
    --model-dir ${md} --out-dir ${md} \
    --batch-size 256 --n-steps 16 \
    --n-val-cap 5000 --n-atk-cap 10000 \
    > ${md}/phase1.log 2>&1
done
echo "[GPU1 done]"
} &
GPU1_PID=$!

wait $GPU0_PID
wait $GPU1_PID
echo "[all phase1 done]"
cd ${ROOT} && uv run --no-sync python artifacts/route_comparison/aggregate_results.py
